Spaces:
No application file
No application file
| import os | |
| from qdrant_client import QdrantClient | |
| from qdrant_client.models import Distance, VectorParams | |
| from app.config import settings | |
| # Initialize Qdrant client | |
| qdrant_client = QdrantClient( | |
| url=settings.QDRANT_URL, | |
| api_key=settings.QDRANT_API_KEY, | |
| ) | |
| COLLECTION_NAME = "book_embeddings" | |
| def init_qdrant_collection(): | |
| """Initialize Qdrant collection if it doesn't exist""" | |
| try: | |
| # Check if collection exists | |
| collections = qdrant_client.get_collections().collections | |
| collection_names = [col.name for col in collections] | |
| if COLLECTION_NAME not in collection_names: | |
| # Create collection with vector configuration | |
| qdrant_client.create_collection( | |
| collection_name=COLLECTION_NAME, | |
| vectors_config=VectorParams( | |
| size=1536, # OpenAI text-embedding-3-small dimension | |
| distance=Distance.COSINE | |
| ) | |
| ) | |
| print(f"✅ Created Qdrant collection: {COLLECTION_NAME}") | |
| else: | |
| print(f"✅ Qdrant collection already exists: {COLLECTION_NAME}") | |
| except Exception as e: | |
| print(f"⚠️ Warning: Could not initialize Qdrant collection: {e}") | |
| def get_qdrant_client(): | |
| """Dependency to get Qdrant client""" | |
| return qdrant_client |