| 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 |
| 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 |
| 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 |
| 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() |