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.
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
# main.py
|
|
import uvicorn
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
|
|
from starlette.responses import JSONResponse
|
|
|
|
from app.config import VectorDBType, debug_mode, RAG_HOST, RAG_PORT, CHUNK_SIZE, CHUNK_OVERLAP, PDF_EXTRACT_IMAGES, VECTOR_DB_TYPE, \
|
|
LogMiddleware, logger
|
|
from app.middleware import security_middleware
|
|
from app.routes import document_routes, pgvector_routes
|
|
from app.services.database import PSQLDatabase, ensure_custom_id_index_on_embedding
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup logic goes here
|
|
if VECTOR_DB_TYPE == VectorDBType.PGVECTOR:
|
|
await PSQLDatabase.get_pool() # Initialize the pool
|
|
await ensure_custom_id_index_on_embedding()
|
|
|
|
yield
|
|
|
|
app = FastAPI(lifespan=lifespan, debug=debug_mode)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.add_middleware(LogMiddleware)
|
|
|
|
app.middleware("http")(security_middleware)
|
|
|
|
# Set state variables for use in routes
|
|
app.state.CHUNK_SIZE = CHUNK_SIZE
|
|
app.state.CHUNK_OVERLAP = CHUNK_OVERLAP
|
|
app.state.PDF_EXTRACT_IMAGES = PDF_EXTRACT_IMAGES
|
|
|
|
# Include routers
|
|
app.include_router(document_routes.router)
|
|
if debug_mode:
|
|
app.include_router(router=pgvector_routes.router)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
body = await request.body()
|
|
logger.debug(f"Validation error occurred")
|
|
logger.debug(f"Raw request body: {body.decode()}")
|
|
logger.debug(f"Validation errors: {exc.errors()}")
|
|
return JSONResponse(
|
|
status_code=422,
|
|
content={
|
|
"detail": exc.errors(),
|
|
"body": body.decode(),
|
|
"message": "Request validation failed",
|
|
},
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host=RAG_HOST, port=RAG_PORT, log_config=None)
|