File size: 3,906 Bytes
3e00788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr
from gradio import ChatMessage
import time
import random

def calculator_tool(expression):
    """Simple calculator tool"""
    try:
        return eval(expression)
    except:
        return "Error: Could not evaluate expression"

def weather_tool(location):
    """Simulated weather tool"""
    conditions = ["sunny", "cloudy", "rainy", "snowy"]
    temp = random.randint(0, 35)
    return f"{random.choice(conditions)}, {temp}°C in {location}"

def agent_with_tools(query, history):
    
    # Initial thinking
    thinking = ChatMessage(
        role="assistant",
        content="Let me think about this query...",
        metadata={"title": "🧠 Thinking", "id": 1}
    )
    history.append(thinking)
    yield history
    
    # Decide which tool to use based on query
    if "calculate" in query.lower() or any(op in query for op in ['+', '-', '*', '/']):
        # Extract the expression (simplified for demo)
        expression = query.split("calculate")[-1].strip() if "calculate" in query.lower() else query
        
        # Show tool usage as nested thought
        tool_call = ChatMessage(
            role="assistant",
            content=f"Expression to evaluate: {expression}",
            metadata={
                "title": "🧮 Calculator Tool", 
                "parent_id": 1,
                "id": 2,
                "status": "pending"
            }
        )
        history.append(tool_call)
        yield history
        
        # Simulate calculation time
        time.sleep(1)
        
        # Get result and update tool call
        result = calculator_tool(expression)
        tool_call.content = f"Expression: {expression}\nResult: {result}"
        tool_call.metadata["status"] = "done"
        tool_call.metadata["duration"] = 0.8  # Simulated duration
        yield history
        
        # Final response
        response = ChatMessage(
            role="assistant",
            content=f"I calculated that for you. The result is {result}."
        )
        
    elif "weather" in query.lower():
        # Extract location (simplified)
        location = query.split("weather in")[-1].strip() if "weather in" in query.lower() else "your location"
        
        # Show tool usage
        tool_call = ChatMessage(
            role="assistant",
            content=f"Checking weather for: {location}",
            metadata={
                "title": "🌤️ Weather Tool", 
                "parent_id": 1,
                "id": 2,
                "status": "pending"
            }
        )
        history.append(tool_call)
        yield history
        
        # Simulate API call
        time.sleep(1.5)
        
        # Get result and update tool call
        result = weather_tool(location)
        tool_call.content = f"Location: {location}\nCurrent conditions: {result}"
        tool_call.metadata["status"] = "done"
        tool_call.metadata["duration"] = 1.2  # Simulated duration
        yield history
        
        # Final response
        response = ChatMessage(
            role="assistant",
            content=f"I checked the weather for you. It's currently {result}."
        )
    else:
        # Default response for other queries
        time.sleep(1)
        response = ChatMessage(
            role="assistant",
            content=f"I understand you're asking about '{query}', but I don't have a specific tool for that. I can help with calculations or weather."
        )
    
    # Add final response
    history.append(response)
    yield history

demo = gr.ChatInterface(
    agent_with_tools,
    title="Sample Agents with Tool Visualization using gr.ChatMessage",
    description="Ask me to calculate something or check the weather!",
    examples=[
        "Calculate 145 * 32", 
        "What's the weather in Tokyo?",
        "Tell me about quantum physics"
    ],
    type="messages"
)

demo.launch()