File size: 1,880 Bytes
467eb8d 10e9b7d 467eb8d 31243f4 467eb8d 31243f4 467eb8d 3c4371f 467eb8d 3c4371f 467eb8d e80aab9 467eb8d e80aab9 467eb8d e514fd7 467eb8d e514fd7 e80aab9 467eb8d e80aab9 467eb8d e80aab9 467eb8d | 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 | import math
from datetime import datetime
import gradio as gr
from duckduckgo_search import DDGS
from smolagents import CodeAgent, tool
from smolagents.models import LiteLLMModel
from smolagents.tools import FinalAnswerTool
# -----------------------
# TOOL 1: WEB SEARCH
# -----------------------
@tool
def web_search(query: str) -> str:
"""
Search the web for information.
Args:
query: search query
"""
results = DDGS().text(query, max_results=5)
formatted = []
for r in results:
formatted.append(f"{r['title']} - {r['body']}")
return "\n".join(formatted)
# -----------------------
# TOOL 2: CALCULATOR
# -----------------------
@tool
def calculator(expression: str) -> str:
"""
Evaluate a mathematical expression.
Args:
expression: math expression like 12*45+2
"""
try:
result = eval(expression, {"math": math})
return str(result)
except Exception as e:
return f"Error: {str(e)}"
# -----------------------
# TOOL 3: CURRENT TIME
# -----------------------
@tool
def current_datetime() -> str:
"""
Returns current date and time.
"""
return datetime.now().isoformat()
model = LiteLLMModel(
model_id="ollama/qwen2:7b",
temperature=0.2,
)
agent = CodeAgent(
model=model,
tools=[
web_search,
calculator,
current_datetime,
FinalAnswerTool()
],
max_steps=12,
verbosity_level=1
)
def run_agent(question):
try:
answer = agent.run(question)
return answer
except Exception as e:
return str(e)
demo = gr.Interface(
fn=run_agent,
inputs=gr.Textbox(label="Ask the GAIA Agent"),
outputs="text",
title="GAIA Agent",
description="Agent built with smolagents for the Hugging Face Agents Course"
)
if __name__ == "__main__":
demo.launch() |