Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -194,32 +194,39 @@ class QASystem:
|
|
194 |
logger.error(f"System initialization error: {str(e)}")
|
195 |
return False
|
196 |
|
197 |
-
def process_query(self, query: str) ->
|
|
|
198 |
try:
|
199 |
-
|
|
|
|
|
200 |
|
201 |
-
# Use
|
202 |
-
|
203 |
-
|
204 |
-
# Stream the responses
|
205 |
-
for step in self.graph.stream(
|
206 |
{"messages": [HumanMessage(content=query)]},
|
207 |
-
stream_mode="values",
|
208 |
config={"configurable": {"thread_id": thread_id}}
|
209 |
-
)
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
-
return responses
|
220 |
except Exception as e:
|
221 |
logger.error(f"Query processing error: {str(e)}")
|
222 |
-
return
|
|
|
|
|
|
|
223 |
|
224 |
qa_system = QASystem()
|
225 |
if qa_system.initialize_system():
|
@@ -229,5 +236,6 @@ else:
|
|
229 |
|
230 |
@app.post("/query")
|
231 |
async def query_api(query: str):
|
232 |
-
|
233 |
-
|
|
|
|
194 |
logger.error(f"System initialization error: {str(e)}")
|
195 |
return False
|
196 |
|
197 |
+
def process_query(self, query: str) -> Dict[str, str]:
|
198 |
+
"""Process a query and return a single final response"""
|
199 |
try:
|
200 |
+
# Generate a unique thread ID for production use
|
201 |
+
# For simplicity, using a fixed ID here
|
202 |
+
thread_id = "abc123"
|
203 |
|
204 |
+
# Use invoke instead of stream to get only the final result
|
205 |
+
final_state = self.graph.invoke(
|
|
|
|
|
|
|
206 |
{"messages": [HumanMessage(content=query)]},
|
|
|
207 |
config={"configurable": {"thread_id": thread_id}}
|
208 |
+
)
|
209 |
+
|
210 |
+
# Extract only the last AI message from the final state
|
211 |
+
ai_messages = [m for m in final_state["messages"] if m.type == "ai"]
|
212 |
+
|
213 |
+
if ai_messages:
|
214 |
+
# Return only the last AI message
|
215 |
+
return {
|
216 |
+
'content': ai_messages[-1].content,
|
217 |
+
'type': ai_messages[-1].type
|
218 |
+
}
|
219 |
+
return {
|
220 |
+
'content': "No response generated",
|
221 |
+
'type': 'error'
|
222 |
+
}
|
223 |
|
|
|
224 |
except Exception as e:
|
225 |
logger.error(f"Query processing error: {str(e)}")
|
226 |
+
return {
|
227 |
+
'content': f"Query processing error: {str(e)}",
|
228 |
+
'type': 'error'
|
229 |
+
}
|
230 |
|
231 |
qa_system = QASystem()
|
232 |
if qa_system.initialize_system():
|
|
|
236 |
|
237 |
@app.post("/query")
|
238 |
async def query_api(query: str):
|
239 |
+
"""API endpoint that returns a single response for a query"""
|
240 |
+
response = qa_system.process_query(query)
|
241 |
+
return {"response": response}
|