|
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): |
|
|
|
|
|
thinking = ChatMessage( |
|
role="assistant", |
|
content="Let me think about this query...", |
|
metadata={"title": "🧠 Thinking", "id": 1} |
|
) |
|
history.append(thinking) |
|
yield history |
|
|
|
|
|
if "calculate" in query.lower() or any(op in query for op in ['+', '-', '*', '/']): |
|
|
|
expression = query.split("calculate")[-1].strip() if "calculate" in query.lower() else query |
|
|
|
|
|
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 |
|
|
|
|
|
time.sleep(1) |
|
|
|
|
|
result = calculator_tool(expression) |
|
tool_call.content = f"Expression: {expression}\nResult: {result}" |
|
tool_call.metadata["status"] = "done" |
|
tool_call.metadata["duration"] = 0.8 |
|
yield history |
|
|
|
|
|
response = ChatMessage( |
|
role="assistant", |
|
content=f"I calculated that for you. The result is {result}." |
|
) |
|
|
|
elif "weather" in query.lower(): |
|
|
|
location = query.split("weather in")[-1].strip() if "weather in" in query.lower() else "your location" |
|
|
|
|
|
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 |
|
|
|
|
|
time.sleep(1.5) |
|
|
|
|
|
result = weather_tool(location) |
|
tool_call.content = f"Location: {location}\nCurrent conditions: {result}" |
|
tool_call.metadata["status"] = "done" |
|
tool_call.metadata["duration"] = 1.2 |
|
yield history |
|
|
|
|
|
response = ChatMessage( |
|
role="assistant", |
|
content=f"I checked the weather for you. It's currently {result}." |
|
) |
|
else: |
|
|
|
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." |
|
) |
|
|
|
|
|
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() |