First_agent_template / agentic_ui.py
cmgramse's picture
Update agentic_ui.py
c4ece8b verified
import gradio as gr
from tools.perplexity_tools import (
get_ai_research_papers,
summarize_paper,
get_citation,
explain_concept,
)
def chat_ui_interaction(query: str, chat_history: list, api_key: str, model: str):
"""Handles user queries in the chat UI, showing the agent's thought process."""
# Add user message to chat history
chat_history.append(("user", query))
# Let the agent decide which tool to invoke
thought_process = []
try:
# Simulate the agent's thought process
thought_process.append("🤔 Analyzing your query...")
# Example: Decide which tool to use based on the query
if "explain" in query.lower():
thought_process.append("🔍 Using the 'explain_concept' tool...")
result = explain_concept(query, api_key, model)
elif "summarize" in query.lower():
thought_process.append("📄 Using the 'summarize_paper' tool...")
result = summarize_paper(query, api_key, model)
elif "citation" in query.lower():
thought_process.append("📚 Using the 'get_citation' tool...")
result = get_citation(query, api_key, model)
else:
thought_process.append("🔎 Using the 'get_ai_research_papers' tool...")
result = get_ai_research_papers(query, api_key, model)
# Add thought process to chat history
for step in thought_process:
chat_history.append(("assistant", step))
# Add final result to chat history
chat_history.append(("assistant", result))
except Exception as e:
chat_history.append(("assistant", f"Error: {str(e)}"))
return chat_history
def create_agentic_ui(input_api_key, model_dropdown):
"""Creates the Agentic UI (Chat UI) components."""
with gr.Column() as agentic_ui:
gr.Markdown("# AI Research Assistant (Agentic Mode)")
# Chat interface
with gr.Row():
# Left column for threads (optional)
with gr.Column(scale=1):
gr.Markdown("### Threadss")
threads = gr.Textbox(label="Threads", interactive=False)
# Center column for chat messages
with gr.Column(scale=3):
chatbot = gr.Chatbot(label="Chat History", type="messages") # Use type="messages"
chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
chat_button = gr.Button("Send")
# Handle chat interactions
chat_button.click(
fn=chat_ui_interaction,
inputs=[chat_input, chatbot, input_api_key, model_dropdown],
outputs=chatbot
)
return agentic_ui