Spaces:
Running
Running
SUBHRAJIT MOHANTY
commited on
Commit
·
09225f8
1
Parent(s):
574641e
Chore: Bux fixes on accessing collection
Browse files
app.py
CHANGED
|
@@ -195,6 +195,11 @@ class RAGService:
|
|
| 195 |
async def retrieve_relevant_chunks(query: str, top_k: int = Config.TOP_K) -> List[str]:
|
| 196 |
"""Retrieve relevant document chunks from Qdrant"""
|
| 197 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
# Get query embedding - all-MiniLM works well without special prefixes
|
| 199 |
query_embedding = await embedding_service.get_query_embedding(query)
|
| 200 |
|
|
@@ -255,10 +260,16 @@ async def health_check():
|
|
| 255 |
qdrant_status = f"error: {str(e)}"
|
| 256 |
|
| 257 |
# Test embedding service
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
return {
|
| 261 |
-
"status": "healthy",
|
| 262 |
"groq": "connected" if groq_client else "not configured",
|
| 263 |
"qdrant": qdrant_status,
|
| 264 |
"embedding_service": embedding_health,
|
|
@@ -402,11 +413,14 @@ async def stream_chat_completion(messages: List[Dict], request: ChatCompletionRe
|
|
| 402 |
}
|
| 403 |
yield f"data: {json.dumps(error_chunk)}\n\n"
|
| 404 |
|
| 405 |
-
# Additional endpoints for managing the vector database
|
| 406 |
@app.post("/v1/embeddings/add")
|
| 407 |
async def add_document(content: str, metadata: Optional[Dict] = None):
|
| 408 |
"""Add a document to the vector database"""
|
| 409 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
# Generate embedding for document
|
| 411 |
embedding = await embedding_service.get_document_embedding(content)
|
| 412 |
|
|
@@ -436,6 +450,10 @@ async def add_document(content: str, metadata: Optional[Dict] = None):
|
|
| 436 |
async def batch_add_documents(documents: List[Dict[str, Any]]):
|
| 437 |
"""Add multiple documents to the vector database"""
|
| 438 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
# Extract texts and metadata
|
| 440 |
texts = [doc.get("content", "") for doc in documents]
|
| 441 |
metadatas = [doc.get("metadata", {}) for doc in documents]
|
|
@@ -475,6 +493,10 @@ async def batch_add_documents(documents: List[Dict[str, Any]]):
|
|
| 475 |
async def create_collection():
|
| 476 |
"""Create a new collection in Qdrant with the correct vector size"""
|
| 477 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
from qdrant_client.models import VectorParams, Distance
|
| 479 |
|
| 480 |
await qdrant_client.create_collection(
|
|
|
|
| 195 |
async def retrieve_relevant_chunks(query: str, top_k: int = Config.TOP_K) -> List[str]:
|
| 196 |
"""Retrieve relevant document chunks from Qdrant"""
|
| 197 |
try:
|
| 198 |
+
# Check if embedding service is initialized
|
| 199 |
+
if embedding_service is None:
|
| 200 |
+
print("Error: Embedding service is not initialized")
|
| 201 |
+
return []
|
| 202 |
+
|
| 203 |
# Get query embedding - all-MiniLM works well without special prefixes
|
| 204 |
query_embedding = await embedding_service.get_query_embedding(query)
|
| 205 |
|
|
|
|
| 260 |
qdrant_status = f"error: {str(e)}"
|
| 261 |
|
| 262 |
# Test embedding service
|
| 263 |
+
if embedding_service is None:
|
| 264 |
+
embedding_health = {"status": "not_initialized", "error": "EmbeddingService is None"}
|
| 265 |
+
else:
|
| 266 |
+
try:
|
| 267 |
+
embedding_health = embedding_service.health_check()
|
| 268 |
+
except Exception as e:
|
| 269 |
+
embedding_health = {"status": "error", "error": str(e)}
|
| 270 |
|
| 271 |
return {
|
| 272 |
+
"status": "healthy" if embedding_service is not None else "unhealthy",
|
| 273 |
"groq": "connected" if groq_client else "not configured",
|
| 274 |
"qdrant": qdrant_status,
|
| 275 |
"embedding_service": embedding_health,
|
|
|
|
| 413 |
}
|
| 414 |
yield f"data: {json.dumps(error_chunk)}\n\n"
|
| 415 |
|
|
|
|
| 416 |
@app.post("/v1/embeddings/add")
|
| 417 |
async def add_document(content: str, metadata: Optional[Dict] = None):
|
| 418 |
"""Add a document to the vector database"""
|
| 419 |
try:
|
| 420 |
+
# Check if embedding service is initialized
|
| 421 |
+
if embedding_service is None:
|
| 422 |
+
raise HTTPException(status_code=500, detail="Embedding service is not initialized")
|
| 423 |
+
|
| 424 |
# Generate embedding for document
|
| 425 |
embedding = await embedding_service.get_document_embedding(content)
|
| 426 |
|
|
|
|
| 450 |
async def batch_add_documents(documents: List[Dict[str, Any]]):
|
| 451 |
"""Add multiple documents to the vector database"""
|
| 452 |
try:
|
| 453 |
+
# Check if embedding service is initialized
|
| 454 |
+
if embedding_service is None:
|
| 455 |
+
raise HTTPException(status_code=500, detail="Embedding service is not initialized")
|
| 456 |
+
|
| 457 |
# Extract texts and metadata
|
| 458 |
texts = [doc.get("content", "") for doc in documents]
|
| 459 |
metadatas = [doc.get("metadata", {}) for doc in documents]
|
|
|
|
| 493 |
async def create_collection():
|
| 494 |
"""Create a new collection in Qdrant with the correct vector size"""
|
| 495 |
try:
|
| 496 |
+
# Check if embedding service is initialized
|
| 497 |
+
if embedding_service is None:
|
| 498 |
+
raise HTTPException(status_code=500, detail="Embedding service is not initialized")
|
| 499 |
+
|
| 500 |
from qdrant_client.models import VectorParams, Distance
|
| 501 |
|
| 502 |
await qdrant_client.create_collection(
|