Spaces:
Running
Running
Fix query JSON body and improve error handling
Browse files- backend/api.py +19 -12
backend/api.py
CHANGED
|
@@ -136,17 +136,24 @@ class QueryRequest(BaseModel):
|
|
| 136 |
# This Endpoint Load the VectorDataBase and answer the User question
|
| 137 |
@app.post("/query")
|
| 138 |
async def get_response(req: QueryRequest):
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# This Endpoint Load the VectorDataBase and answer the User question
|
| 137 |
@app.post("/query")
|
| 138 |
async def get_response(req: QueryRequest):
|
| 139 |
+
try:
|
| 140 |
+
vectorstore = get_vectorstore()
|
| 141 |
+
retriever = vectorstore.as_retriever(
|
| 142 |
+
search_type="mmr",
|
| 143 |
+
search_kwargs={"k": 3}
|
| 144 |
+
)
|
| 145 |
+
chain = get_rag_chain(retriever)
|
| 146 |
+
response = chain.invoke(req.input)
|
| 147 |
|
| 148 |
+
system_stats["total_queries"] += 1
|
| 149 |
|
| 150 |
+
return {
|
| 151 |
+
"question": req.input,
|
| 152 |
+
"response": response.content
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
raise HTTPException(
|
| 157 |
+
status_code=500,
|
| 158 |
+
detail=f"Query processing failed: {str(e)}"
|
| 159 |
+
)
|