Spaces:
Build error
Build error
Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
2 |
+
|
3 |
+
# Initialize a model (use your preferred model_id)
|
4 |
+
model = HfApiModel(model_id="meta-llama/Llama-2-7b-chat-hf") # Or another model
|
5 |
+
|
6 |
+
# Define custom tools if needed (optional)
|
7 |
+
from smolagents.tool import tool
|
8 |
+
|
9 |
+
@tool
|
10 |
+
def add(a: int, b: int) -> int:
|
11 |
+
"""Add two numbers."""
|
12 |
+
return a + b
|
13 |
+
|
14 |
+
@tool
|
15 |
+
def multiply(a: int, b: int) -> int:
|
16 |
+
"""Multiply two numbers."""
|
17 |
+
return a * b
|
18 |
+
|
19 |
+
# Create the agent with web search and custom tools
|
20 |
+
agent = CodeAgent(
|
21 |
+
tools=[DuckDuckGoSearchTool(), add, multiply],
|
22 |
+
model=model,
|
23 |
+
)
|
24 |
+
|
25 |
+
def answer_question(question: str) -> str:
|
26 |
+
"""Run the agent on a question and return the answer."""
|
27 |
+
result = agent.run(question)
|
28 |
+
# Format as per system_prompt.txt
|
29 |
+
if "FINAL ANSWER:" not in result:
|
30 |
+
result = f"FINAL ANSWER: {result}"
|
31 |
+
return result
|