Update app.py
Browse files
app.py
CHANGED
|
@@ -27,8 +27,9 @@ logger = logging.getLogger(__name__)
|
|
| 27 |
# Global variables
|
| 28 |
rag_system: Optional[MultimodalRAGSystem] = None
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
# Lifecycle management
|
| 34 |
@asynccontextmanager
|
|
@@ -124,23 +125,24 @@ async def health_check():
|
|
| 124 |
|
| 125 |
@app.post("/query", response_model=QueryResponse, tags=["Query"])
|
| 126 |
async def query_rag(request: QueryRequest):
|
| 127 |
-
global
|
| 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 |
-
|
| 137 |
)
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
|
| 145 |
return QueryResponse(
|
| 146 |
answer=result["answer"],
|
|
@@ -154,6 +156,7 @@ async def query_rag(request: QueryRequest):
|
|
| 154 |
raise HTTPException(status_code=500, detail=str(e))
|
| 155 |
|
| 156 |
|
|
|
|
| 157 |
if __name__ == "__main__":
|
| 158 |
import uvicorn
|
| 159 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 27 |
# Global variables
|
| 28 |
rag_system: Optional[MultimodalRAGSystem] = None
|
| 29 |
|
| 30 |
+
# Keep short conversation history
|
| 31 |
+
chat_history: List[Dict[str, str]] = []
|
| 32 |
+
MAX_HISTORY_TURNS = 3
|
| 33 |
|
| 34 |
# Lifecycle management
|
| 35 |
@asynccontextmanager
|
|
|
|
| 125 |
|
| 126 |
@app.post("/query", response_model=QueryResponse, tags=["Query"])
|
| 127 |
async def query_rag(request: QueryRequest):
|
| 128 |
+
global chat_history
|
| 129 |
|
| 130 |
if not rag_system:
|
| 131 |
raise HTTPException(status_code=503, detail="RAG system not initialized.")
|
| 132 |
|
| 133 |
try:
|
|
|
|
| 134 |
result = rag_system.ask(
|
| 135 |
+
question=request.question,
|
| 136 |
+
chat_history=chat_history
|
| 137 |
)
|
| 138 |
|
| 139 |
+
# Update history
|
| 140 |
+
chat_history.append({"role": "user", "content": request.question})
|
| 141 |
+
chat_history.append({"role": "assistant", "content": result["answer"]})
|
| 142 |
+
|
| 143 |
+
# Trim history safely
|
| 144 |
+
if len(chat_history) > MAX_HISTORY_TURNS * 2:
|
| 145 |
+
chat_history = chat_history[-MAX_HISTORY_TURNS * 2:]
|
| 146 |
|
| 147 |
return QueryResponse(
|
| 148 |
answer=result["answer"],
|
|
|
|
| 156 |
raise HTTPException(status_code=500, detail=str(e))
|
| 157 |
|
| 158 |
|
| 159 |
+
|
| 160 |
if __name__ == "__main__":
|
| 161 |
import uvicorn
|
| 162 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|