File size: 2,791 Bytes
e1b6305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ac2739
e1b6305
 
 
 
 
 
 
 
c4ece8b
e1b6305
 
 
 
0ac2739
e1b6305
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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