Lex / embed.py
taaha3244's picture
Create embed.py
0457256 verified
raw
history blame
1.45 kB
import uuid
from langchain_community.vectorstores import Qdrant
from qdrant_client import models
from utils import setup_qdrant_client,setup_openai_embeddings
def embed_documents_into_qdrant(documents, api_key, qdrant_url, qdrant_api_key, collection_name="Lex-v1"):
"""Embed documents into Qdrant."""
embeddings_model = setup_openai_embeddings(api_key)
client = setup_qdrant_client(qdrant_url, qdrant_api_key)
qdrant = Qdrant(client=client, collection_name=collection_name, embeddings=embeddings_model)
try:
qdrant.add_documents(documents)
except Exception as e:
print("Failed to embed documents:", e)
def embed_documents_with_unique_collection(documents, api_key, qdrant_url, qdrant_api_key, collection_name=None):
"""Embed documents into a unique Qdrant collection."""
if not collection_name:
collection_name = f"session-{uuid.uuid4()}"
client = setup_qdrant_client(qdrant_url, qdrant_api_key)
client.create_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(size=1536, distance=models.Distance.COSINE)
)
embeddings_model = setup_openai_embeddings(api_key)
qdrant = Qdrant(client=client, collection_name=collection_name, embeddings=embeddings_model)
try:
qdrant.add_documents(documents)
except Exception as e:
print("Failed to embed documents:", e)
return collection_name