| |
| |
| |
|
|
| |
| |
|
|
| import subprocess |
| import sys |
|
|
| subprocess.check_call([ |
| sys.executable, |
| "-m", |
| "pip", |
| "install", |
| "-q", |
| "smolagents", |
| "ddgs" |
| ]) |
|
|
|
|
| |
| |
| |
|
|
| import os |
|
|
| from smolagents import ( |
| CodeAgent, |
| InferenceClientModel, |
| DuckDuckGoSearchTool, |
| PythonInterpreterTool |
| ) |
|
|
|
|
| |
| |
| |
|
|
| class BasicAgent: |
|
|
| def __init__(self): |
|
|
| print("Starting GAIA Agent...") |
|
|
| |
| hf_token = os.getenv("HF_TOKEN") |
|
|
| if not hf_token: |
| raise ValueError( |
| "HF_TOKEN is missing. " |
| "Go to Space Settings → Secrets and add HF_TOKEN." |
| ) |
|
|
| |
| |
| |
|
|
| model = InferenceClientModel( |
| model_id="Qwen/Qwen2.5-72B-Instruct", |
| token=hf_token |
| ) |
|
|
| |
| |
| |
|
|
| search = DuckDuckGoSearchTool( |
| max_results=5 |
| ) |
|
|
| |
| |
| |
|
|
| calculator = PythonInterpreterTool( |
| authorized_imports=[ |
| "math", |
| "statistics", |
| "datetime", |
| "json", |
| "re" |
| ] |
| ) |
|
|
| |
| |
| |
|
|
| self.agent = CodeAgent( |
| model=model, |
| tools=[ |
| search, |
| calculator |
| ], |
| max_steps=8 |
| ) |
|
|
| print("GAIA Agent ready!") |
|
|
|
|
| |
| |
| |
|
|
| def __call__(self, question: str) -> str: |
|
|
| print("\n" + "=" * 60) |
| print("QUESTION:") |
| print(question) |
| print("=" * 60) |
|
|
| prompt = f""" |
| You are a highly capable general-purpose AI agent. |
| |
| Solve the user's question accurately. |
| |
| IMPORTANT: |
| |
| 1. Understand exactly what the question asks. |
| |
| 2. Use web search when the question requires: |
| - current information |
| - factual information |
| - information not available from your knowledge |
| |
| 3. Use the Python tool whenever calculations are required. |
| |
| 4. Carefully check arithmetic. |
| |
| 5. For questions involving dates, numbers, percentages, |
| units, names, or lists, verify your answer carefully. |
| |
| 6. If several steps are required, solve them systematically. |
| |
| 7. Do not invent information. |
| |
| 8. Use the available tools whenever they improve accuracy. |
| |
| 9. The final response must directly answer the question. |
| |
| 10. Follow the requested answer format exactly. |
| |
| 11. Do not include unnecessary discussion in the final answer. |
| |
| USER QUESTION: |
| {question} |
| """ |
|
|
| try: |
|
|
| result = self.agent.run(prompt) |
|
|
| answer = str(result).strip() |
|
|
| print("\nANSWER:") |
| print(answer) |
|
|
| return answer |
|
|
| except Exception as e: |
|
|
| print("AGENT ERROR:") |
| print(e) |
|
|
| return f"Agent error: {e}" |