agent_course_open / agent.py
chezhian's picture
Create agent.py
f10d5f8 verified
raw
history blame
870 Bytes
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
# Initialize a model (use your preferred model_id)
model = HfApiModel(model_id="meta-llama/Llama-2-7b-chat-hf") # Or another model
# Define custom tools if needed (optional)
from smolagents.tool import tool
@tool
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
# Create the agent with web search and custom tools
agent = CodeAgent(
tools=[DuckDuckGoSearchTool(), add, multiply],
model=model,
)
def answer_question(question: str) -> str:
"""Run the agent on a question and return the answer."""
result = agent.run(question)
# Format as per system_prompt.txt
if "FINAL ANSWER:" not in result:
result = f"FINAL ANSWER: {result}"
return result