mirror of
https://github.com/hanzoai/chat-rag-api.git
synced 2026-10-30 18:45:41 +00:00
Extracted from the deployed chat-rag-api:0.7.8-hanzo image; adds a Qdrant vector store targeting hanzoai/vector as a drop-in for pgvector. - app/services/vector_store/qdrant_vector.py: QdrantVector(QdrantVectorStore) with the id helpers (get_all_ids/get_filtered_ids/get_documents_by_ids), delete-by-file_id, and the route's Mongo-style file_id filter translated to native Qdrant filters. file_id stored in payload metadata (unique point ids). - factory.get_vector_store: mode='qdrant' (lazy import; pg/atlas deploys unaffected). - config: VectorDBType.QDRANT + QDRANT_URL/QDRANT_API_KEY/QDRANT_DISTANCE/ EMBEDDINGS_DIM(1536) + construction branch. - health: qdrant_health_check (client.get_collections()). - requirements: langchain-qdrant==0.2.0, qdrant-client==1.12.1. Verified: pytest tests/test_qdrant_vector.py (4 tests) against in-memory Qdrant; factory builds QdrantVector with all route methods present.
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
# app/services/database.py
|
|
import asyncpg
|
|
from app.config import DSN, logger
|
|
|
|
class PSQLDatabase:
|
|
pool = None
|
|
|
|
@classmethod
|
|
async def get_pool(cls):
|
|
if cls.pool is None:
|
|
cls.pool = await asyncpg.create_pool(dsn=DSN)
|
|
return cls.pool
|
|
|
|
@classmethod
|
|
async def close_pool(cls):
|
|
if cls.pool is not None:
|
|
await cls.pool.close()
|
|
cls.pool = None
|
|
|
|
async def ensure_custom_id_index_on_embedding():
|
|
table_name = "langchain_pg_embedding"
|
|
column_name = "custom_id"
|
|
# You might want to standardize the index naming convention
|
|
index_name = f"idx_{table_name}_{column_name}"
|
|
|
|
pool = await PSQLDatabase.get_pool()
|
|
async with pool.acquire() as conn:
|
|
await conn.execute(f"""
|
|
CREATE INDEX IF NOT EXISTS {index_name} ON {table_name} ({column_name});
|
|
""")
|
|
logger.debug(f"Checking if index '{index_name}' on '{table_name}({column_name}) exists, if not found then the index is created.'")
|
|
|
|
|
|
async def pg_health_check() -> bool:
|
|
try:
|
|
pool = await PSQLDatabase.get_pool()
|
|
async with pool.acquire() as conn:
|
|
await conn.fetchval("SELECT 1")
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Health check failed: {e}")
|
|
return False |