pvanand commited on
Commit
f6c67ca
·
verified ·
1 Parent(s): d7f34ff

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -0
main.py CHANGED
@@ -572,6 +572,49 @@ async def followup_agent(query: FollowupQueryModel, background_tasks: Background
572
 
573
  return StreamingResponse(process_response(), media_type="text/event-stream")
574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
  if __name__ == "__main__":
576
  import uvicorn
577
  logger.info("Starting the application")
 
572
 
573
  return StreamingResponse(process_response(), media_type="text/event-stream")
574
 
575
+ @app.post("/v3/followup-agent")
576
+ async def followup_agent(query: FollowupQueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
577
+ """
578
+ Followup agent endpoint that provides helpful responses or generates clarifying questions based on user queries.
579
+ Requires API Key authentication via X-API-Key header.
580
+ """
581
+ logger.info(f"Received followup agent query: {query.query}")
582
+
583
+ if query.conversation_id not in conversations:
584
+ conversations[query.conversation_id] = [
585
+ {"role": "system", "content": FOLLOWUP_AGENT_PROMPT}
586
+ ]
587
+
588
+ conversations[query.conversation_id].append({"role": "user", "content": query.query})
589
+ last_activity[query.conversation_id] = time.time()
590
+
591
+ # Limit tokens in the conversation history
592
+ limited_conversation = conversations[query.conversation_id]
593
+
594
+ def process_response():
595
+ full_response = ""
596
+ for content in chat_with_llama_stream(limited_conversation, model=query.model_id):
597
+ full_response += content
598
+ yield content
599
+
600
+ logger.info(f"LLM RAW response for query: {query.query}: {full_response}")
601
+ response_content, interact = parse_followup_response(full_response)
602
+
603
+ result = {
604
+ "clarification": interact
605
+ }
606
+
607
+ yield "<json>" +"[[["+ json.dumps(result)+"]]]"+"</json>"
608
+
609
+ # Add the assistant's response to the conversation history
610
+ conversations[query.conversation_id].append({"role": "assistant", "content": full_response})
611
+
612
+ background_tasks.add_task(update_db, query.user_id, query.conversation_id, query.query, full_response)
613
+ logger.info(f"Completed followup agent response for query: {query.query}, send result: {result}")
614
+
615
+ return StreamingResponse(process_response(), media_type="text/event-stream")
616
+
617
+
618
  if __name__ == "__main__":
619
  import uvicorn
620
  logger.info("Starting the application")