Spaces:
Runtime error
Runtime error
Create embed.py
Browse files
embed.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uuid
|
2 |
+
from langchain_community.vectorstores import Qdrant
|
3 |
+
from qdrant_client import models
|
4 |
+
from utils import setup_qdrant_client,setup_openai_embeddings
|
5 |
+
|
6 |
+
|
7 |
+
def embed_documents_into_qdrant(documents, api_key, qdrant_url, qdrant_api_key, collection_name="Lex-v1"):
|
8 |
+
"""Embed documents into Qdrant."""
|
9 |
+
embeddings_model = setup_openai_embeddings(api_key)
|
10 |
+
client = setup_qdrant_client(qdrant_url, qdrant_api_key)
|
11 |
+
qdrant = Qdrant(client=client, collection_name=collection_name, embeddings=embeddings_model)
|
12 |
+
try:
|
13 |
+
qdrant.add_documents(documents)
|
14 |
+
except Exception as e:
|
15 |
+
print("Failed to embed documents:", e)
|
16 |
+
|
17 |
+
def embed_documents_with_unique_collection(documents, api_key, qdrant_url, qdrant_api_key, collection_name=None):
|
18 |
+
"""Embed documents into a unique Qdrant collection."""
|
19 |
+
if not collection_name:
|
20 |
+
collection_name = f"session-{uuid.uuid4()}"
|
21 |
+
client = setup_qdrant_client(qdrant_url, qdrant_api_key)
|
22 |
+
client.create_collection(
|
23 |
+
collection_name=collection_name,
|
24 |
+
vectors_config=models.VectorParams(size=1536, distance=models.Distance.COSINE)
|
25 |
+
)
|
26 |
+
embeddings_model = setup_openai_embeddings(api_key)
|
27 |
+
qdrant = Qdrant(client=client, collection_name=collection_name, embeddings=embeddings_model)
|
28 |
+
try:
|
29 |
+
qdrant.add_documents(documents)
|
30 |
+
except Exception as e:
|
31 |
+
print("Failed to embed documents:", e)
|
32 |
+
return collection_name
|