File size: 870 Bytes
f10d5f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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