|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
from smolagents import ( |
|
|
CodeAgent, |
|
|
DuckDuckGoSearchTool, |
|
|
PythonInterpreterTool, |
|
|
VisitWebpageTool, |
|
|
LiteLLMModel, |
|
|
) |
|
|
from tools import ( |
|
|
multiply, |
|
|
add, |
|
|
subtract, |
|
|
divide, |
|
|
modulus, |
|
|
get_wikipedia_summary |
|
|
) |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
def create_gaia_agent(): |
|
|
""" |
|
|
Create a GAIA question-answering agent using Gemini API and smolagents tools |
|
|
""" |
|
|
|
|
|
|
|
|
model = LiteLLMModel( |
|
|
model_id="gemini/gemini-2.0-flash", |
|
|
api_key=os.getenv("GEMINI_API_KEY") |
|
|
) |
|
|
|
|
|
|
|
|
tools = [ |
|
|
DuckDuckGoSearchTool(), |
|
|
PythonInterpreterTool(), |
|
|
VisitWebpageTool(), |
|
|
get_wikipedia_summary, |
|
|
multiply, |
|
|
add, |
|
|
subtract, |
|
|
divide, |
|
|
modulus |
|
|
] |
|
|
|
|
|
|
|
|
agent = CodeAgent( |
|
|
tools=tools, |
|
|
model=model |
|
|
) |
|
|
|
|
|
return agent |
|
|
|
|
|
|
|
|
def answer_gaia_question(agent: CodeAgent, question: str): |
|
|
""" |
|
|
Answer a GAIA question using the agent |
|
|
|
|
|
Args: |
|
|
question (str): The GAIA question to answer |
|
|
|
|
|
Returns: |
|
|
str: The agent's answer |
|
|
""" |
|
|
|
|
|
|
|
|
enhanced_prompt = f""" |
|
|
You are an expert AI assistant designed to answer complex GAIA (General AI Assistant) questions. |
|
|
These questions often require: |
|
|
- Multi-step reasoning |
|
|
- Web searches for current information |
|
|
- Mathematical calculations |
|
|
- Data analysis |
|
|
- Combining information from multiple sources |
|
|
|
|
|
Question: {question} |
|
|
|
|
|
Please approach this systematically: |
|
|
1. Break down the question into components |
|
|
2. Identify what information you need |
|
|
3. Use appropriate tools to gather information |
|
|
4. Perform any necessary calculations |
|
|
5. Synthesize your findings into a clear answer |
|
|
|
|
|
Make sure to: |
|
|
- Use web search when you need current or specific information |
|
|
- Use Python for any calculations or data processing |
|
|
- Visit specific webpages if you need detailed information from particular sources |
|
|
- Think step by step and show your reasoning |
|
|
|
|
|
Provide a final, clear answer at the end. |
|
|
""" |
|
|
|
|
|
try: |
|
|
|
|
|
result = agent.run(enhanced_prompt) |
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error occurred while processing the question: {str(e)}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def example_usage(): |
|
|
""" |
|
|
Example of how to use the agent with specific GAIA questions |
|
|
""" |
|
|
|
|
|
|
|
|
example_questions = [ |
|
|
"What is the current population of Tokyo and how does it compare to New York City?", |
|
|
] |
|
|
|
|
|
print("Running example GAIA questions:") |
|
|
print("=" * 40) |
|
|
|
|
|
for i, question in enumerate(example_questions, 1): |
|
|
print(f"\nExample {i}: {question}") |
|
|
answer = answer_gaia_question(question) |
|
|
print(f"Answer: {answer}") |
|
|
print("-" * 40) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
example_usage() |