Update app.py
Browse files
app.py
CHANGED
|
@@ -124,43 +124,35 @@ async def health_check():
|
|
| 124 |
|
| 125 |
@app.post("/query", response_model=QueryResponse, tags=["Query"])
|
| 126 |
async def query_rag(request: QueryRequest):
|
| 127 |
-
"""
|
| 128 |
-
Query the RAG system
|
| 129 |
-
"""
|
| 130 |
global last_qa_context
|
| 131 |
-
|
| 132 |
if not rag_system:
|
| 133 |
-
raise HTTPException(
|
| 134 |
-
status_code=503,
|
| 135 |
-
detail="RAG system not initialized. Check logs for errors."
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
try:
|
| 139 |
-
# Build prompt using previous Q/A if available
|
| 140 |
-
if last_qa_context:
|
| 141 |
-
prompt = f"{last_qa_context}\n\n{request.question}"
|
| 142 |
-
else:
|
| 143 |
-
prompt = request.question
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
# Save
|
| 149 |
last_qa_context = (
|
| 150 |
-
f"
|
| 151 |
-
f"
|
| 152 |
)
|
| 153 |
-
|
| 154 |
return QueryResponse(
|
| 155 |
-
answer=result[
|
| 156 |
-
images=result[
|
| 157 |
-
texts=result[
|
| 158 |
question=request.question
|
| 159 |
)
|
| 160 |
-
|
| 161 |
except Exception as e:
|
| 162 |
logger.error(f"Error processing query: {str(e)}")
|
| 163 |
-
raise HTTPException(status_code=500, detail=
|
|
|
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
import uvicorn
|
|
|
|
| 124 |
|
| 125 |
@app.post("/query", response_model=QueryResponse, tags=["Query"])
|
| 126 |
async def query_rag(request: QueryRequest):
|
|
|
|
|
|
|
|
|
|
| 127 |
global last_qa_context
|
| 128 |
+
|
| 129 |
if not rag_system:
|
| 130 |
+
raise HTTPException(status_code=503, detail="RAG system not initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
try:
|
| 133 |
+
# Pass ONLY the question to retrieval
|
| 134 |
+
result = rag_system.ask(
|
| 135 |
+
request.question,
|
| 136 |
+
context=last_qa_context
|
| 137 |
+
)
|
| 138 |
|
| 139 |
+
# Save lightweight context for next turn
|
| 140 |
last_qa_context = (
|
| 141 |
+
f"Q: {request.question}\n"
|
| 142 |
+
f"A: {result['answer'][:500]}" # truncate for safety
|
| 143 |
)
|
| 144 |
+
|
| 145 |
return QueryResponse(
|
| 146 |
+
answer=result["answer"],
|
| 147 |
+
images=result["images"],
|
| 148 |
+
texts=result["texts"],
|
| 149 |
question=request.question
|
| 150 |
)
|
| 151 |
+
|
| 152 |
except Exception as e:
|
| 153 |
logger.error(f"Error processing query: {str(e)}")
|
| 154 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 155 |
+
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
import uvicorn
|