-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed_docs.py
More file actions
43 lines (30 loc) · 1.14 KB
/
Copy pathembed_docs.py
File metadata and controls
43 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
from pathlib import Path
from client import client
FOLDER_PATH = "rag_docs/"
OUTPUT_FILE = "embedded_docs.json"
embedded_docs = []
def embed_text(client, text: str, model: str = "text-embedding-3-small") -> list[float]:
"""
Returns an embedding vector for the input text using OpenAI embeddings.
Args:
client: OpenAI client object.
text: The text to embed.
model: Embedding model to use. Default is "text-embedding-3-small".
Returns:
A list of floats representing the embedding vector.
"""
response = client.embeddings.create(model=model, input=text)
return response.data[0].embedding
for file_path in Path(FOLDER_PATH).rglob("*.json"):
with open(file_path, "r", encoding="utf-8") as f:
doc = json.load(f)
description = doc.get("description")
if isinstance(description, list):
description = " ".join(description)
if description:
embedding = embed_text(client, description)
doc["description_embedding"] = embedding
embedded_docs.append(doc)
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
json.dump(embedded_docs, f, indent=2)