|
|
|
|
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool |
|
from tools.final_answer import FinalAnswerTool |
|
import datetime |
|
import pytz |
|
import yaml |
|
import gradio as gr |
|
|
|
|
|
@tool |
|
def get_current_time_in_timezone(timezone: str) -> str: |
|
""" |
|
A tool that fetches the current local time in a specified timezone. |
|
Args: |
|
timezone: A string representing a valid timezone (e.g., 'Europe/London'). |
|
""" |
|
try: |
|
tz = pytz.timezone(timezone) |
|
local_time = datetime.datetime.now(tz).strftime("%A, %B %d, %Y %I:%M %p") |
|
return f"The current local time in {timezone} is {local_time}." |
|
except Exception as e: |
|
return f"Sorry, I couldn’t find the time for {timezone}: {str(e)}" |
|
|
|
|
|
final_answer = FinalAnswerTool() |
|
|
|
|
|
model = HfApiModel( |
|
max_tokens=2048, |
|
temperature=0.5, |
|
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', |
|
) |
|
|
|
|
|
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
|
|
|
|
|
prompt_templates = { |
|
"system": """You are a helpful agent that thinks step by step. |
|
You must always return your final answer as a clear, complete sentence in natural language. |
|
If you call tools, use their output exactly and do not strip details. |
|
When you have the final information, pass it using the FinalAnswerTool. |
|
""" |
|
} |
|
|
|
|
|
agent = CodeAgent( |
|
model=model, |
|
tools=[ |
|
get_current_time_in_timezone, |
|
DuckDuckGoSearchTool(), |
|
image_generation_tool, |
|
final_answer |
|
], |
|
max_steps=6, |
|
verbosity_level=1, |
|
prompt_templates=prompt_templates |
|
) |
|
|
|
|
|
def agentic_chat(user_input): |
|
result = agent.run(user_input) |
|
if hasattr(result, 'final_answer'): |
|
return result.final_answer |
|
else: |
|
return str(result) |
|
|
|
iface = gr.Interface( |
|
fn=agentic_chat, |
|
inputs="text", |
|
outputs="text", |
|
title="Agentic AI Example", |
|
description="Ask me anything! I will reason step-by-step and provide clear final answers." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |