Spaces:
Runtime error
Runtime error
Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
from smolagents import (
|
4 |
+
ToolCallingAgent,
|
5 |
+
CodeAgent,
|
6 |
+
MCPClient,
|
7 |
+
WikipediaSearchTool,
|
8 |
+
InferenceClientModel,
|
9 |
+
)
|
10 |
+
|
11 |
+
max_steps_internet_researcher = 10
|
12 |
+
max_steps_manager = 10
|
13 |
+
max_retries = 3
|
14 |
+
cooldown_on_error = 10
|
15 |
+
|
16 |
+
|
17 |
+
qwuen_72b = "Qwen/Qwen2.5-72B-Instruct"
|
18 |
+
model_235b_thinking = "qwen/qwen3-235b-a22b-thinking-2507"
|
19 |
+
model_235b_instruct = "qwen/Qwen3-235B-A22B-Instruct-2507"
|
20 |
+
model_235b = "qwen/qwen3-235b-a22b-fp8"
|
21 |
+
model_480b = "qwen/qwen3-coder-480b-a35b-instruct"
|
22 |
+
|
23 |
+
|
24 |
+
def get_hf_model():
|
25 |
+
hf_token = os.getenv("HF_TOKEN")
|
26 |
+
return InferenceClientModel(model_235b_instruct, token=hf_token, timeout=300)
|
27 |
+
|
28 |
+
|
29 |
+
def get_tavily_mcp_client():
|
30 |
+
tavily_token = os.getenv("TAVILY_TOKEN")
|
31 |
+
|
32 |
+
# context manager + Streamable HTTP transport:
|
33 |
+
return MCPClient(
|
34 |
+
{
|
35 |
+
"url": f"https://mcp.tavily.com/mcp/?tavilyApiKey={tavily_token}",
|
36 |
+
"transport": "streamable-http",
|
37 |
+
}
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
class Agent:
|
42 |
+
def __init__(self):
|
43 |
+
|
44 |
+
model = get_hf_model()
|
45 |
+
mcpClient = get_tavily_mcp_client()
|
46 |
+
|
47 |
+
tools = mcpClient.get_tools()
|
48 |
+
tools.append(
|
49 |
+
WikipediaSearchTool(
|
50 |
+
user_agent="Research ([email protected])",
|
51 |
+
language="en",
|
52 |
+
content_type="summary",
|
53 |
+
extract_format="WIKI",
|
54 |
+
)
|
55 |
+
)
|
56 |
+
|
57 |
+
internet_reseacher = ToolCallingAgent(
|
58 |
+
tools=tools,
|
59 |
+
model=model,
|
60 |
+
max_steps=max_steps_internet_researcher,
|
61 |
+
name="web_search_agent",
|
62 |
+
description="Runs web searches for you.",
|
63 |
+
)
|
64 |
+
|
65 |
+
agent = CodeAgent(
|
66 |
+
tools=[],
|
67 |
+
managed_agents=[internet_reseacher],
|
68 |
+
max_steps=max_steps_manager,
|
69 |
+
model=model,
|
70 |
+
)
|
71 |
+
|
72 |
+
self.agent = agent
|
73 |
+
|
74 |
+
print("BasicAgent initialized.")
|
75 |
+
|
76 |
+
def __call__(self, question: str) -> str:
|
77 |
+
prompt = f"You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with ONLY the final answer. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. the question is: {question}"
|
78 |
+
print(f"Agent received question: {prompt}...")
|
79 |
+
|
80 |
+
for _ in range(0, max_retries):
|
81 |
+
try:
|
82 |
+
answer = self.agent.run(prompt)
|
83 |
+
|
84 |
+
if isinstance(answer, str):
|
85 |
+
if "AGENT ERROR" in answer:
|
86 |
+
print("agent error: ", answer)
|
87 |
+
time.sleep(cooldown_on_error)
|
88 |
+
continue
|
89 |
+
print(f"Agent returning fixed answer: {answer}")
|
90 |
+
|
91 |
+
return answer
|
92 |
+
except Exception as e:
|
93 |
+
print("Other error: ", e)
|
94 |
+
time.sleep(cooldown_on_error)
|
95 |
+
|
96 |
+
return "ERROR: no more retries"
|