Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -429,6 +429,33 @@ questions:
|
|
429 |
</interact>
|
430 |
"""
|
431 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
import re
|
433 |
|
434 |
def parse_followup_response(input_text):
|
@@ -614,6 +641,50 @@ async def followup_agent(query: FollowupQueryModel, background_tasks: Background
|
|
614 |
|
615 |
return StreamingResponse(process_response(), media_type="text/event-stream")
|
616 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
617 |
|
618 |
if __name__ == "__main__":
|
619 |
import uvicorn
|
|
|
429 |
</interact>
|
430 |
"""
|
431 |
|
432 |
+
FOLLOWUP_DIGIYATRA_PROMPT = """
|
433 |
+
You are a helpful, assistant tasked to assist digiyatra users, who can create interactive buttons and markdown responses. Provide youtube links to the user if relevant links are given in the context.
|
434 |
+
|
435 |
+
If the user request needs further clarification, provide clarifying questions using <interact> to assist the user.
|
436 |
+
Else respond with a helpful answer using <response>.
|
437 |
+
The options in <interact> tags will be rendered as buttons so that user can interact with it. Hence make sure to use it for engaging with the user with Next Steps, followup questions, quizzes etc. whichever appropriate
|
438 |
+
|
439 |
+
Your output format: # Use the following <response>,<interact> tags in any order as many times required.
|
440 |
+
<response>response to user request formatted using markdown</response>
|
441 |
+
<interact>
|
442 |
+
questions:
|
443 |
+
- text: [First interaction question]
|
444 |
+
options:
|
445 |
+
- [Option 1]
|
446 |
+
- [Option 2]
|
447 |
+
- [Option 3]
|
448 |
+
- [Option 4 (if needed)]
|
449 |
+
- text: [Second interaction question]
|
450 |
+
options:
|
451 |
+
- [Option 1]
|
452 |
+
- [Option 2]
|
453 |
+
- [Option 3]
|
454 |
+
# Add more questions as needed
|
455 |
+
# make sure this section is in valid YAML format
|
456 |
+
</interact>
|
457 |
+
"""
|
458 |
+
|
459 |
import re
|
460 |
|
461 |
def parse_followup_response(input_text):
|
|
|
641 |
|
642 |
return StreamingResponse(process_response(), media_type="text/event-stream")
|
643 |
|
644 |
+
## Digiyatra
|
645 |
+
|
646 |
+
@app.post("/digiyatra-followup")
|
647 |
+
async def followup_agent(query: FollowupQueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
|
648 |
+
"""
|
649 |
+
Followup agent endpoint that provides helpful responses or generates clarifying questions based on user queries.
|
650 |
+
Requires API Key authentication via X-API-Key header.
|
651 |
+
"""
|
652 |
+
logger.info(f"Received followup agent query: {query.query}")
|
653 |
+
|
654 |
+
if query.conversation_id not in conversations:
|
655 |
+
conversations[query.conversation_id] = [
|
656 |
+
{"role": "system", "content": FOLLOWUP_DIGIYATRA_PROMPT}
|
657 |
+
]
|
658 |
+
|
659 |
+
conversations[query.conversation_id].append({"role": "user", "content": query.query})
|
660 |
+
last_activity[query.conversation_id] = time.time()
|
661 |
+
|
662 |
+
# Limit tokens in the conversation history
|
663 |
+
limited_conversation = conversations[query.conversation_id]
|
664 |
+
|
665 |
+
def process_response():
|
666 |
+
full_response = ""
|
667 |
+
for content in chat_with_llama_stream(limited_conversation, model=query.model_id):
|
668 |
+
full_response += content
|
669 |
+
yield content
|
670 |
+
|
671 |
+
logger.info(f"LLM RAW response for query: {query.query}: {full_response}")
|
672 |
+
response_content, interact = parse_followup_response(full_response)
|
673 |
+
|
674 |
+
result = {
|
675 |
+
"response": response_content,
|
676 |
+
"clarification": interact
|
677 |
+
}
|
678 |
+
|
679 |
+
yield "\n\n" + json.dumps(result)
|
680 |
+
|
681 |
+
# Add the assistant's response to the conversation history
|
682 |
+
conversations[query.conversation_id].append({"role": "assistant", "content": full_response})
|
683 |
+
|
684 |
+
background_tasks.add_task(update_db, query.user_id, query.conversation_id, query.query, full_response)
|
685 |
+
logger.info(f"Completed followup agent response for query: {query.query}, send result: {result}")
|
686 |
+
|
687 |
+
return StreamingResponse(process_response(), media_type="text/event-stream")
|
688 |
|
689 |
if __name__ == "__main__":
|
690 |
import uvicorn
|