Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import (InferenceClientModel, CodeAgent, ToolCallingAgent,
|
2 |
+
DuckDuckGoSearchTool, VisitWebpageTool, DuckDuckGoSearchTool, FinalAnswerTool,
|
3 |
+
WikipediaSearchTool, PythonInterpreterTool,
|
4 |
+
)
|
5 |
+
|
6 |
+
model_id = "Qwen/Qwen2.5-72B-Instruct"
|
7 |
+
model = InferenceClientModel(model_id)
|
8 |
+
|
9 |
+
web_agent = ToolCallingAgent(
|
10 |
+
tools=[
|
11 |
+
DuckDuckGoSearchTool(),
|
12 |
+
VisitWebpageTool(),
|
13 |
+
WikipediaSearchTool()
|
14 |
+
],
|
15 |
+
model=model,
|
16 |
+
name="search_agent",
|
17 |
+
description="Runs web searches for you. Give it your query as an argument.",
|
18 |
+
)
|
19 |
+
python_agent = CodeAgent(
|
20 |
+
tools=[
|
21 |
+
PythonInterpreterTool()
|
22 |
+
],
|
23 |
+
model=model,
|
24 |
+
name='python_agent',
|
25 |
+
description='Use additional_authorized_imports for you. You need to do actions and help to answer the questions with python code',
|
26 |
+
additional_authorized_imports=[
|
27 |
+
"json",
|
28 |
+
"pandas",
|
29 |
+
"numpy",
|
30 |
+
"requests",
|
31 |
+
"time",
|
32 |
+
"datetime",
|
33 |
+
],
|
34 |
+
planning_interval=5,
|
35 |
+
verbosity_level=2,
|
36 |
+
final_answer_checks=[check_reasoning_and_plot],
|
37 |
+
max_steps=15,
|
38 |
+
)
|
39 |
+
|
40 |
+
class BasicAgent:
|
41 |
+
"""An agent who is able to answer questions."""
|
42 |
+
def __init__(self):
|
43 |
+
|
44 |
+
# Instantiate Agent
|
45 |
+
self.agent = CodeAgent(tools=[
|
46 |
+
FinalAnswerTool()
|
47 |
+
],
|
48 |
+
model=model,
|
49 |
+
managed_agents=[
|
50 |
+
web_agent,
|
51 |
+
python_agent
|
52 |
+
],
|
53 |
+
additional_authorized_imports=["requests"],
|
54 |
+
)
|
55 |
+
|
56 |
+
print("BasicAgent initialized.")
|
57 |
+
|
58 |
+
def __call__(self, question: str) -> str:
|
59 |
+
print(f"Agent received question: {question}...")
|
60 |
+
answer = self.agent.run(question)
|
61 |
+
print(f"Agent returning answer: {answer}")
|
62 |
+
|
63 |
+
return answer
|