VishnuRamDebyez commited on
Commit
fe9fc71
·
verified ·
1 Parent(s): d05ce95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
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) -> List[Dict[str, str]]:
 
198
  try:
199
- responses = []
 
 
200
 
201
- # Use a unique thread_id for each conversation
202
- thread_id = "abc123" # In production, generate a unique ID for each conversation
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
- if step["messages"]:
211
- # Only include AI messages in the response
212
- ai_messages = [m for m in step["messages"] if m.type == "ai"]
213
- if ai_messages:
214
- responses.append({
215
- 'content': ai_messages[-1].content,
216
- 'type': ai_messages[-1].type
217
- })
 
 
 
 
 
 
218
 
219
- return responses
220
  except Exception as e:
221
  logger.error(f"Query processing error: {str(e)}")
222
- return [{'content': f"Query processing error: {str(e)}", 'type': 'error'}]
 
 
 
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
- responses = qa_system.process_query(query)
233
- return {"responses": responses}
 
 
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}