Fix Phase 1 server validation defects

This commit is contained in:
Jino Jose committed 2026-07-13 05:30:39 +05:30
1 parent 55a2a41089
commit a2ee7247d5
10 files changed
+114 -18

No files matched your search

@@ -9,6 +9,7 @@ User=cezen
WorkingDirectory=/opt/cezen/backend
Environment="CEZEN_DATA=/opt/cezen/data"
Environment="OLLAMA_URL=http://localhost:11434"
Environment="CHROMA_URL=http://localhost:8100"
Environment="PATH=/opt/cezen/backend/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/opt/cezen/backend/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8080 --workers 2
Restart=always
+9 -2
View File
@@ -2412,6 +2412,11 @@ def _ollama_embed(texts: list[str], model: str = OLLAMA_EMBED) -> list[list[floa
log_context=f"_ollama_embed(model={model})", log_exc=e)
return embeddings
def _chroma_collection_id(name: str) -> str:
"""Resolve a stable Chroma collection name to the UUID required by data routes."""
collection = _chroma_req("GET", f"/api/v1/collections/{name}")
return collection["id"]
# Collections
@app.get("/api/rag/collections")
async def list_collections(admin: dict = Depends(admin_only)):
@@ -2554,7 +2559,8 @@ async def delete_document(cid: int, doc_id: int, admin: dict = Depends(admin_onl
# Remove chunks from ChromaDB
if col:
try:
_chroma_req("POST", f"/api/v1/collections/{col['chroma_name']}/delete",
chroma_id = _chroma_collection_id(col["chroma_name"])
_chroma_req("POST", f"/api/v1/collections/{chroma_id}/delete",
body={"where": {"doc_id": {"$eq": doc_id}}})
except Exception:
pass
@@ -2598,7 +2604,8 @@ async def rag_query(body: dict, user: dict = Depends(current_user)):
raise HTTPException(status_code=503, detail="Failed to embed query")
# Query ChromaDB
result = _chroma_req("POST", f"/api/v1/collections/{col['chroma_name']}/query", body={
chroma_id = _chroma_collection_id(col["chroma_name"])
result = _chroma_req("POST", f"/api/v1/collections/{chroma_id}/query", body={
"query_embeddings": embed,
"n_results": n_results,
"include": ["documents", "metadatas", "distances"]
@@ -162,9 +162,9 @@ def chunk_text(text: str, chunk_size: int = CHUNK_SIZE, overlap: int = CHUNK_OVE
chunk = chunk[:idx + len(sep)]
break
chunks.append(chunk.strip())
start += len(chunk) - overlap
if start >= len(text):
if end >= len(text):
break
start += max(1, len(chunk) - overlap)
return [c for c in chunks if c]
# ── Embedding ─────────────────────────────────────────────────────────────────
@@ -197,8 +197,10 @@ def chroma_upsert(ids, embeddings, documents, metadatas):
"documents": documents,
"metadatas": metadatas,
}).encode()
with urllib.request.urlopen(f"{args.chroma_url}/api/v1/collections/{args.collection}", timeout=30) as response:
collection_id = json.loads(response.read().decode())["id"]
req = urllib.request.Request(
f"{args.chroma_url}/api/v1/collections/{args.collection}/upsert",
f"{args.chroma_url}/api/v1/collections/{collection_id}/upsert",
data=body, method="POST"
)
req.add_header("Content-Type", "application/json")