Spaces:
Build error
Build error
Refactor agent structure by modularizing agent implementations into separate directories for web, data analysis, and media agents. Remove legacy code from agents.py, prompts.py, and tools.py, enhancing maintainability. Update main_v2.py to reflect new import paths and agent initialization. Add new tools for enhanced functionality, including web searching and data extraction. Update requirements.txt to include necessary dependencies for new tools.
Browse files- agents.py +0 -111
- agents/__init__.py +9 -0
- agents/data_agent/__init__.py +3 -0
- agents/data_agent/agent.py +33 -0
- agents/media_agent/__init__.py +3 -0
- agents/media_agent/agent.py +33 -0
- agents/web_agent/__init__.py +3 -0
- agents/web_agent/agent.py +33 -0
- main_v2.py +5 -5
- prompts.py +0 -52
- prompts/code_agent.yaml +0 -325
- prompts/toolcalling_agent.yaml +0 -239
- requirements.txt +3 -0
- tools.py +0 -254
- tools/__init__.py +21 -0
- tools/analyze_image/__init__.py +3 -0
- tools/analyze_image/tool.py +39 -0
- tools/browse_webpage/__init__.py +3 -0
- tools/browse_webpage/tool.py +43 -0
- tools/extract_dates/__init__.py +3 -0
- tools/extract_dates/tool.py +30 -0
- tools/find_in_page/__init__.py +3 -0
- tools/find_in_page/tool.py +28 -0
- tools/parse_csv/__init__.py +3 -0
- tools/parse_csv/tool.py +38 -0
- tools/perform_calculation/__init__.py +3 -0
- tools/perform_calculation/tool.py +38 -0
- tools/read_pdf/__init__.py +3 -0
- tools/read_pdf/tool.py +24 -0
- tools/web_search/__init__.py +3 -0
- tools/web_search/tool.py +17 -0
- tools/wikipedia_rag/README.md +52 -0
- tools/wikipedia_rag/__init__.py +3 -0
- tools/wikipedia_rag/run.py +28 -0
- tools/wikipedia_rag/tool.py +81 -0
agents.py
DELETED
@@ -1,111 +0,0 @@
|
|
1 |
-
import importlib
|
2 |
-
|
3 |
-
import yaml
|
4 |
-
from smolagents import CodeAgent
|
5 |
-
|
6 |
-
from prompts import (
|
7 |
-
DATA_AGENT_SYSTEM_PROMPT,
|
8 |
-
MEDIA_AGENT_SYSTEM_PROMPT,
|
9 |
-
WEB_AGENT_SYSTEM_PROMPT,
|
10 |
-
)
|
11 |
-
from tools import (
|
12 |
-
analyze_image,
|
13 |
-
browse_webpage,
|
14 |
-
extract_dates,
|
15 |
-
find_in_page,
|
16 |
-
parse_csv,
|
17 |
-
perform_calculation,
|
18 |
-
read_pdf,
|
19 |
-
web_search,
|
20 |
-
)
|
21 |
-
|
22 |
-
|
23 |
-
def create_web_agent(model):
|
24 |
-
"""
|
25 |
-
Create a specialized agent for web browsing tasks.
|
26 |
-
|
27 |
-
Args:
|
28 |
-
model: The model to use for the agent
|
29 |
-
|
30 |
-
Returns:
|
31 |
-
Configured CodeAgent for web browsing
|
32 |
-
"""
|
33 |
-
|
34 |
-
prompt_templates = yaml.safe_load(
|
35 |
-
importlib.resources.files("smolagents.prompts")
|
36 |
-
.joinpath("code_agent.yaml")
|
37 |
-
.read_text()
|
38 |
-
)
|
39 |
-
# prompt_templates["system_prompt"] = WEB_AGENT_SYSTEM_PROMPT
|
40 |
-
|
41 |
-
web_agent = CodeAgent(
|
42 |
-
tools=[web_search, browse_webpage, find_in_page, extract_dates],
|
43 |
-
model=model,
|
44 |
-
name="web_agent",
|
45 |
-
description="Specialized agent for web browsing and searching. Use this agent to find information online, browse websites, and extract information from web pages.",
|
46 |
-
add_base_tools=True,
|
47 |
-
additional_authorized_imports=["requests", "bs4", "re", "json"],
|
48 |
-
prompt_templates=prompt_templates,
|
49 |
-
)
|
50 |
-
|
51 |
-
return web_agent
|
52 |
-
|
53 |
-
|
54 |
-
def create_data_analysis_agent(model):
|
55 |
-
"""
|
56 |
-
Create a specialized agent for data analysis tasks.
|
57 |
-
|
58 |
-
Args:
|
59 |
-
model: The model to use for the agent
|
60 |
-
|
61 |
-
Returns:
|
62 |
-
Configured CodeAgent for data analysis
|
63 |
-
"""
|
64 |
-
prompt_templates = yaml.safe_load(
|
65 |
-
importlib.resources.files("smolagents.prompts")
|
66 |
-
.joinpath("code_agent.yaml")
|
67 |
-
.read_text()
|
68 |
-
)
|
69 |
-
# prompt_templates["system_prompt"] = DATA_AGENT_SYSTEM_PROMPT
|
70 |
-
|
71 |
-
data_agent = CodeAgent(
|
72 |
-
tools=[parse_csv, perform_calculation],
|
73 |
-
model=model,
|
74 |
-
name="data_agent",
|
75 |
-
description="Specialized agent for data analysis. Use this agent to analyze data, perform calculations, and extract insights from structured data.",
|
76 |
-
add_base_tools=True,
|
77 |
-
additional_authorized_imports=["pandas", "numpy", "math", "csv", "io"],
|
78 |
-
prompt_templates=prompt_templates,
|
79 |
-
)
|
80 |
-
|
81 |
-
return data_agent
|
82 |
-
|
83 |
-
|
84 |
-
def create_media_agent(model):
|
85 |
-
"""
|
86 |
-
Create a specialized agent for handling media (images, PDFs).
|
87 |
-
|
88 |
-
Args:
|
89 |
-
model: The model to use for the agent
|
90 |
-
|
91 |
-
Returns:
|
92 |
-
Configured CodeAgent for media handling
|
93 |
-
"""
|
94 |
-
prompt_templates = yaml.safe_load(
|
95 |
-
importlib.resources.files("smolagents.prompts")
|
96 |
-
.joinpath("code_agent.yaml")
|
97 |
-
.read_text()
|
98 |
-
)
|
99 |
-
# prompt_templates["system_prompt"] = MEDIA_AGENT_SYSTEM_PROMPT
|
100 |
-
|
101 |
-
media_agent = CodeAgent(
|
102 |
-
tools=[analyze_image, read_pdf],
|
103 |
-
model=model,
|
104 |
-
name="media_agent",
|
105 |
-
description="Specialized agent for handling media files like images and PDFs. Use this agent to analyze images and extract text from PDF documents.",
|
106 |
-
add_base_tools=True,
|
107 |
-
additional_authorized_imports=["PIL", "io", "requests"],
|
108 |
-
prompt_templates=prompt_templates,
|
109 |
-
)
|
110 |
-
|
111 |
-
return media_agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
agents/__init__.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .web_agent import create_web_agent
|
2 |
+
from .data_agent import create_data_agent
|
3 |
+
from .media_agent import create_media_agent
|
4 |
+
|
5 |
+
__all__ = [
|
6 |
+
'create_web_agent',
|
7 |
+
'create_data_agent',
|
8 |
+
'create_media_agent'
|
9 |
+
]
|
agents/data_agent/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .agent import create_data_agent
|
2 |
+
|
3 |
+
__all__ = ['create_data_agent']
|
agents/data_agent/agent.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import importlib
|
2 |
+
import yaml
|
3 |
+
from smolagents import CodeAgent
|
4 |
+
from tools import parse_csv, perform_calculation
|
5 |
+
|
6 |
+
def create_data_agent(model):
|
7 |
+
"""
|
8 |
+
Create a specialized agent for data analysis tasks.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
model: The model to use for the agent
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
Configured CodeAgent for data analysis
|
15 |
+
"""
|
16 |
+
# Load default prompts
|
17 |
+
prompt_templates = yaml.safe_load(
|
18 |
+
importlib.resources.files("smolagents.prompts")
|
19 |
+
.joinpath("code_agent.yaml")
|
20 |
+
.read_text()
|
21 |
+
)
|
22 |
+
|
23 |
+
data_agent = CodeAgent(
|
24 |
+
tools=[parse_csv, perform_calculation],
|
25 |
+
model=model,
|
26 |
+
name="data_agent",
|
27 |
+
description="Specialized agent for data analysis. Use this agent to analyze data, perform calculations, and extract insights from structured data.",
|
28 |
+
add_base_tools=True,
|
29 |
+
additional_authorized_imports=["pandas", "numpy", "math", "csv", "io"],
|
30 |
+
prompt_templates=prompt_templates,
|
31 |
+
)
|
32 |
+
|
33 |
+
return data_agent
|
agents/media_agent/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .agent import create_media_agent
|
2 |
+
|
3 |
+
__all__ = ['create_media_agent']
|
agents/media_agent/agent.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import importlib
|
2 |
+
import yaml
|
3 |
+
from smolagents import CodeAgent
|
4 |
+
from tools import analyze_image, read_pdf
|
5 |
+
|
6 |
+
def create_media_agent(model):
|
7 |
+
"""
|
8 |
+
Create a specialized agent for handling media (images, PDFs).
|
9 |
+
|
10 |
+
Args:
|
11 |
+
model: The model to use for the agent
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
Configured CodeAgent for media handling
|
15 |
+
"""
|
16 |
+
# Load default prompts
|
17 |
+
prompt_templates = yaml.safe_load(
|
18 |
+
importlib.resources.files("smolagents.prompts")
|
19 |
+
.joinpath("code_agent.yaml")
|
20 |
+
.read_text()
|
21 |
+
)
|
22 |
+
|
23 |
+
media_agent = CodeAgent(
|
24 |
+
tools=[analyze_image, read_pdf],
|
25 |
+
model=model,
|
26 |
+
name="media_agent",
|
27 |
+
description="Specialized agent for handling media files like images and PDFs. Use this agent to analyze images and extract text from PDF documents.",
|
28 |
+
add_base_tools=True,
|
29 |
+
additional_authorized_imports=["PIL", "io", "requests"],
|
30 |
+
prompt_templates=prompt_templates,
|
31 |
+
)
|
32 |
+
|
33 |
+
return media_agent
|
agents/web_agent/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .agent import create_web_agent
|
2 |
+
|
3 |
+
__all__ = ['create_web_agent']
|
agents/web_agent/agent.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import importlib
|
2 |
+
import yaml
|
3 |
+
from smolagents import CodeAgent
|
4 |
+
from tools import web_search, browse_webpage, find_in_page, extract_dates
|
5 |
+
|
6 |
+
def create_web_agent(model):
|
7 |
+
"""
|
8 |
+
Create a specialized agent for web browsing tasks.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
model: The model to use for the agent
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
Configured CodeAgent for web browsing
|
15 |
+
"""
|
16 |
+
# Load default prompts
|
17 |
+
prompt_templates = yaml.safe_load(
|
18 |
+
importlib.resources.files("smolagents.prompts")
|
19 |
+
.joinpath("code_agent.yaml")
|
20 |
+
.read_text()
|
21 |
+
)
|
22 |
+
|
23 |
+
web_agent = CodeAgent(
|
24 |
+
tools=[web_search, browse_webpage, find_in_page, extract_dates],
|
25 |
+
model=model,
|
26 |
+
name="web_agent",
|
27 |
+
description="Specialized agent for web browsing and searching. Use this agent to find information online, browse websites, and extract information from web pages.",
|
28 |
+
add_base_tools=True,
|
29 |
+
additional_authorized_imports=["requests", "bs4", "re", "json"],
|
30 |
+
prompt_templates=prompt_templates,
|
31 |
+
)
|
32 |
+
|
33 |
+
return web_agent
|
main_v2.py
CHANGED
@@ -14,7 +14,9 @@ from smolagents import CodeAgent, LiteLLMModel
|
|
14 |
from smolagents.default_tools import DuckDuckGoSearchTool, VisitWebpageTool
|
15 |
from smolagents.monitoring import LogLevel
|
16 |
|
17 |
-
from agents import
|
|
|
|
|
18 |
from utils import extract_final_answer
|
19 |
|
20 |
_disable_debugging()
|
@@ -40,9 +42,9 @@ model = LiteLLMModel(
|
|
40 |
model_id=MODEL_ID,
|
41 |
)
|
42 |
|
43 |
-
|
44 |
-
data_agent = create_data_analysis_agent(model)
|
45 |
media_agent = create_media_agent(model)
|
|
|
46 |
|
47 |
prompt_templates = yaml.safe_load(
|
48 |
importlib.resources.files("smolagents.prompts")
|
@@ -65,8 +67,6 @@ agent = CodeAgent(
|
|
65 |
model=model,
|
66 |
prompt_templates=prompt_templates,
|
67 |
tools=[
|
68 |
-
# web_search,
|
69 |
-
# perform_calculation,
|
70 |
DuckDuckGoSearchTool(max_results=1),
|
71 |
VisitWebpageTool(max_output_length=256),
|
72 |
],
|
|
|
14 |
from smolagents.default_tools import DuckDuckGoSearchTool, VisitWebpageTool
|
15 |
from smolagents.monitoring import LogLevel
|
16 |
|
17 |
+
from agents.data_agent.agent import create_data_agent
|
18 |
+
from agents.media_agent.agent import create_media_agent
|
19 |
+
from agents.web_agent.agent import create_web_agent
|
20 |
from utils import extract_final_answer
|
21 |
|
22 |
_disable_debugging()
|
|
|
42 |
model_id=MODEL_ID,
|
43 |
)
|
44 |
|
45 |
+
data_agent = create_data_agent(model)
|
|
|
46 |
media_agent = create_media_agent(model)
|
47 |
+
web_agent = create_web_agent(model)
|
48 |
|
49 |
prompt_templates = yaml.safe_load(
|
50 |
importlib.resources.files("smolagents.prompts")
|
|
|
67 |
model=model,
|
68 |
prompt_templates=prompt_templates,
|
69 |
tools=[
|
|
|
|
|
70 |
DuckDuckGoSearchTool(max_results=1),
|
71 |
VisitWebpageTool(max_output_length=256),
|
72 |
],
|
prompts.py
DELETED
@@ -1,52 +0,0 @@
|
|
1 |
-
# Enhanced system prompts for GAIA benchmark
|
2 |
-
MANAGER_SYSTEM_PROMPT = """
|
3 |
-
You are a manager agent for the GAIA benchmark. Your job is to:
|
4 |
-
1. Break down complex questions into logical steps
|
5 |
-
2. Delegate tasks to specialized agents when appropriate
|
6 |
-
3. Synthesize information from different sources
|
7 |
-
4. Track progress and ensure all parts of the question are addressed
|
8 |
-
5. Formulate a precise final answer in the exact format requested
|
9 |
-
|
10 |
-
You have these specialized agents available:
|
11 |
-
- web_agent: For web browsing, searching, and extracting information from websites
|
12 |
-
- data_agent: For data analysis, calculations, and working with structured data
|
13 |
-
- media_agent: For analyzing images and extracting content from PDFs
|
14 |
-
|
15 |
-
Focus on delivering accurate, precise answers rather than explanations.
|
16 |
-
"""
|
17 |
-
|
18 |
-
WEB_AGENT_SYSTEM_PROMPT = """
|
19 |
-
You are a web agent specialized in finding and extracting information from the internet.
|
20 |
-
Your primary functions are:
|
21 |
-
1. Performing targeted web searches
|
22 |
-
2. Browsing webpages to extract specific information
|
23 |
-
3. Finding relevant content within pages
|
24 |
-
4. Extracting dates and temporal information
|
25 |
-
|
26 |
-
Be thorough and precise in your search strategies. Try multiple search queries if needed.
|
27 |
-
Return only the specific information requested, formatted clearly.
|
28 |
-
"""
|
29 |
-
|
30 |
-
DATA_AGENT_SYSTEM_PROMPT = """
|
31 |
-
You are a data analysis agent specialized in working with structured data.
|
32 |
-
Your primary functions are:
|
33 |
-
1. Analyzing CSV and tabular data
|
34 |
-
2. Performing calculations and statistical analysis
|
35 |
-
3. Extracting insights from numerical data
|
36 |
-
4. Formatting results according to specifications
|
37 |
-
|
38 |
-
Be precise in your calculations and data handling. Check your work for accuracy.
|
39 |
-
Return only the specific information requested, formatted clearly.
|
40 |
-
"""
|
41 |
-
|
42 |
-
MEDIA_AGENT_SYSTEM_PROMPT = """
|
43 |
-
You are a media analysis agent specialized in working with images and documents.
|
44 |
-
Your primary functions are:
|
45 |
-
1. Analyzing images to identify objects, text, and relationships
|
46 |
-
2. Extracting text content from PDF documents
|
47 |
-
3. Describing visual elements in detail
|
48 |
-
4. Identifying patterns in visual data
|
49 |
-
|
50 |
-
Be thorough in your analysis and precise in your descriptions.
|
51 |
-
Return only the specific information requested, formatted clearly.
|
52 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prompts/code_agent.yaml
DELETED
@@ -1,325 +0,0 @@
|
|
1 |
-
system_prompt: |-
|
2 |
-
You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
|
3 |
-
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
|
4 |
-
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
|
5 |
-
|
6 |
-
At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
|
7 |
-
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_code>' sequence.
|
8 |
-
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
9 |
-
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
10 |
-
In the end you have to return a final answer using the `final_answer` tool.
|
11 |
-
|
12 |
-
Here are a few examples using notional tools:
|
13 |
-
---
|
14 |
-
Task: "Generate an image of the oldest person in this document."
|
15 |
-
|
16 |
-
Thought: I will proceed step by step and use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer.
|
17 |
-
Code:
|
18 |
-
```py
|
19 |
-
answer = document_qa(document=document, question="Who is the oldest person mentioned?")
|
20 |
-
print(answer)
|
21 |
-
```<end_code>
|
22 |
-
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
|
23 |
-
|
24 |
-
Thought: I will now generate an image showcasing the oldest person.
|
25 |
-
Code:
|
26 |
-
```py
|
27 |
-
image = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
|
28 |
-
final_answer(image)
|
29 |
-
```<end_code>
|
30 |
-
|
31 |
-
---
|
32 |
-
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
|
33 |
-
|
34 |
-
Thought: I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool
|
35 |
-
Code:
|
36 |
-
```py
|
37 |
-
result = 5 + 3 + 1294.678
|
38 |
-
final_answer(result)
|
39 |
-
```<end_code>
|
40 |
-
|
41 |
-
---
|
42 |
-
Task:
|
43 |
-
"Answer the question in the variable `question` about the image stored in the variable `image`. The question is in French.
|
44 |
-
You have been provided with these additional arguments, that you can access using the keys as variables in your python code:
|
45 |
-
{'question': 'Quel est l'animal sur l'image?', 'image': 'path/to/image.jpg'}"
|
46 |
-
|
47 |
-
Thought: I will use the following tools: `translator` to translate the question into English and then `image_qa` to answer the question on the input image.
|
48 |
-
Code:
|
49 |
-
```py
|
50 |
-
translated_question = translator(question=question, src_lang="French", tgt_lang="English")
|
51 |
-
print(f"The translated question is {translated_question}.")
|
52 |
-
answer = image_qa(image=image, question=translated_question)
|
53 |
-
final_answer(f"The answer is {answer}")
|
54 |
-
```<end_code>
|
55 |
-
|
56 |
-
---
|
57 |
-
Task:
|
58 |
-
In a 1979 interview, Stanislaus Ulam discusses with Martin Sherwin about other great physicists of his time, including Oppenheimer.
|
59 |
-
What does he say was the consequence of Einstein learning too much math on his creativity, in one word?
|
60 |
-
|
61 |
-
Thought: I need to find and read the 1979 interview of Stanislaus Ulam with Martin Sherwin.
|
62 |
-
Code:
|
63 |
-
```py
|
64 |
-
pages = search(query="1979 interview Stanislaus Ulam Martin Sherwin physicists Einstein")
|
65 |
-
print(pages)
|
66 |
-
```<end_code>
|
67 |
-
Observation:
|
68 |
-
No result found for query "1979 interview Stanislaus Ulam Martin Sherwin physicists Einstein".
|
69 |
-
|
70 |
-
Thought: The query was maybe too restrictive and did not find any results. Let's try again with a broader query.
|
71 |
-
Code:
|
72 |
-
```py
|
73 |
-
pages = search(query="1979 interview Stanislaus Ulam")
|
74 |
-
print(pages)
|
75 |
-
```<end_code>
|
76 |
-
Observation:
|
77 |
-
Found 6 pages:
|
78 |
-
[Stanislaus Ulam 1979 interview](https://ahf.nuclearmuseum.org/voices/oral-histories/stanislaus-ulams-interview-1979/)
|
79 |
-
|
80 |
-
[Ulam discusses Manhattan Project](https://ahf.nuclearmuseum.org/manhattan-project/ulam-manhattan-project/)
|
81 |
-
|
82 |
-
(truncated)
|
83 |
-
|
84 |
-
Thought: I will read the first 2 pages to know more.
|
85 |
-
Code:
|
86 |
-
```py
|
87 |
-
for url in ["https://ahf.nuclearmuseum.org/voices/oral-histories/stanislaus-ulams-interview-1979/", "https://ahf.nuclearmuseum.org/manhattan-project/ulam-manhattan-project/"]:
|
88 |
-
whole_page = visit_webpage(url)
|
89 |
-
print(whole_page)
|
90 |
-
print("\n" + "="*80 + "\n") # Print separator between pages
|
91 |
-
```<end_code>
|
92 |
-
Observation:
|
93 |
-
Manhattan Project Locations:
|
94 |
-
Los Alamos, NM
|
95 |
-
Stanislaus Ulam was a Polish-American mathematician. He worked on the Manhattan Project at Los Alamos and later helped design the hydrogen bomb. In this interview, he discusses his work at
|
96 |
-
(truncated)
|
97 |
-
|
98 |
-
Thought: I now have the final answer: from the webpages visited, Stanislaus Ulam says of Einstein: "He learned too much mathematics and sort of diminished, it seems to me personally, it seems to me his purely physics creativity." Let's answer in one word.
|
99 |
-
Code:
|
100 |
-
```py
|
101 |
-
final_answer("diminished")
|
102 |
-
```<end_code>
|
103 |
-
|
104 |
-
---
|
105 |
-
Task: "Which city has the highest population: Guangzhou or Shanghai?"
|
106 |
-
|
107 |
-
Thought: I need to get the populations for both cities and compare them: I will use the tool `search` to get the population of both cities.
|
108 |
-
Code:
|
109 |
-
```py
|
110 |
-
for city in ["Guangzhou", "Shanghai"]:
|
111 |
-
print(f"Population {city}:", search(f"{city} population")
|
112 |
-
```<end_code>
|
113 |
-
Observation:
|
114 |
-
Population Guangzhou: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
|
115 |
-
Population Shanghai: '26 million (2019)'
|
116 |
-
|
117 |
-
Thought: Now I know that Shanghai has the highest population.
|
118 |
-
Code:
|
119 |
-
```py
|
120 |
-
final_answer("Shanghai")
|
121 |
-
```<end_code>
|
122 |
-
|
123 |
-
---
|
124 |
-
Task: "What is the current age of the pope, raised to the power 0.36?"
|
125 |
-
|
126 |
-
Thought: I will use the tool `wiki` to get the age of the pope, and confirm that with a web search.
|
127 |
-
Code:
|
128 |
-
```py
|
129 |
-
pope_age_wiki = wiki(query="current pope age")
|
130 |
-
print("Pope age as per wikipedia:", pope_age_wiki)
|
131 |
-
pope_age_search = web_search(query="current pope age")
|
132 |
-
print("Pope age as per google search:", pope_age_search)
|
133 |
-
```<end_code>
|
134 |
-
Observation:
|
135 |
-
Pope age: "The pope Francis is currently 88 years old."
|
136 |
-
|
137 |
-
Thought: I know that the pope is 88 years old. Let's compute the result using python code.
|
138 |
-
Code:
|
139 |
-
```py
|
140 |
-
pope_current_age = 88 ** 0.36
|
141 |
-
final_answer(pope_current_age)
|
142 |
-
```<end_code>
|
143 |
-
|
144 |
-
Above example were using notional tools that might not exist for you. On top of performing computations in the Python code snippets that you create, you only have access to these tools, behaving like regular python functions:
|
145 |
-
```python
|
146 |
-
{%- for tool in tools.values() %}
|
147 |
-
def {{ tool.name }}({% for arg_name, arg_info in tool.inputs.items() %}{{ arg_name }}: {{ arg_info.type }}{% if not loop.last %}, {% endif %}{% endfor %}) -> {{tool.output_type}}:
|
148 |
-
"""{{ tool.description }}
|
149 |
-
|
150 |
-
Args:
|
151 |
-
{%- for arg_name, arg_info in tool.inputs.items() %}
|
152 |
-
{{ arg_name }}: {{ arg_info.description }}
|
153 |
-
{%- endfor %}
|
154 |
-
"""
|
155 |
-
{% endfor %}
|
156 |
-
```
|
157 |
-
|
158 |
-
{%- if managed_agents and managed_agents.values() | list %}
|
159 |
-
You can also give tasks to team members.
|
160 |
-
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task'.
|
161 |
-
Given that this team member is a real human, you should be very verbose in your task, it should be a long string providing informations as detailed as necessary.
|
162 |
-
Here is a list of the team members that you can call:
|
163 |
-
```python
|
164 |
-
{%- for agent in managed_agents.values() %}
|
165 |
-
def {{ agent.name }}("Your query goes here.") -> str:
|
166 |
-
"""{{ agent.description }}"""
|
167 |
-
{% endfor %}
|
168 |
-
```
|
169 |
-
{%- endif %}
|
170 |
-
|
171 |
-
Here are the rules you should always follow to solve your task:
|
172 |
-
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_code>' sequence, else you will fail.
|
173 |
-
2. Use only variables that you have defined!
|
174 |
-
3. Always use the right arguments for the tools. DO NOT pass the arguments as a dict as in 'answer = wiki({'query': "What is the place where James Bond lives?"})', but use the arguments directly as in 'answer = wiki(query="What is the place where James Bond lives?")'.
|
175 |
-
4. Take care to not chain too many sequential tool calls in the same code block, especially when the output format is unpredictable. For instance, a call to search has an unpredictable return format, so do not have another tool call that depends on its output in the same block: rather output results with print() to use them in the next block.
|
176 |
-
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
|
177 |
-
6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.
|
178 |
-
7. Never create any notional variables in our code, as having these in your logs will derail you from the true variables.
|
179 |
-
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
|
180 |
-
9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
|
181 |
-
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
|
182 |
-
|
183 |
-
Now Begin!
|
184 |
-
planning:
|
185 |
-
initial_plan : |-
|
186 |
-
You are a world expert at analyzing a situation to derive facts, and plan accordingly towards solving a task.
|
187 |
-
Below I will present you a task. You will need to 1. build a survey of facts known or needed to solve the task, then 2. make a plan of action to solve the task.
|
188 |
-
|
189 |
-
## 1. Facts survey
|
190 |
-
You will build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.
|
191 |
-
These "facts" will typically be specific names, dates, values, etc. Your answer should use the below headings:
|
192 |
-
### 1.1. Facts given in the task
|
193 |
-
List here the specific facts given in the task that could help you (there might be nothing here).
|
194 |
-
|
195 |
-
### 1.2. Facts to look up
|
196 |
-
List here any facts that we may need to look up.
|
197 |
-
Also list where to find each of these, for instance a website, a file... - maybe the task contains some sources that you should re-use here.
|
198 |
-
|
199 |
-
### 1.3. Facts to derive
|
200 |
-
List here anything that we want to derive from the above by logical reasoning, for instance computation or simulation.
|
201 |
-
|
202 |
-
Don't make any assumptions. For each item, provide a thorough reasoning. Do not add anything else on top of three headings above.
|
203 |
-
|
204 |
-
## 2. Plan
|
205 |
-
Then for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
|
206 |
-
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
|
207 |
-
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
208 |
-
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
209 |
-
|
210 |
-
You can leverage these tools, behaving like regular python functions:
|
211 |
-
```python
|
212 |
-
{%- for tool in tools.values() %}
|
213 |
-
def {{ tool.name }}({% for arg_name, arg_info in tool.inputs.items() %}{{ arg_name }}: {{ arg_info.type }}{% if not loop.last %}, {% endif %}{% endfor %}) -> {{tool.output_type}}:
|
214 |
-
"""{{ tool.description }}
|
215 |
-
|
216 |
-
Args:
|
217 |
-
{%- for arg_name, arg_info in tool.inputs.items() %}
|
218 |
-
{{ arg_name }}: {{ arg_info.description }}
|
219 |
-
{%- endfor %}
|
220 |
-
"""
|
221 |
-
{% endfor %}
|
222 |
-
```
|
223 |
-
|
224 |
-
{%- if managed_agents and managed_agents.values() | list %}
|
225 |
-
You can also give tasks to team members.
|
226 |
-
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task'.
|
227 |
-
Given that this team member is a real human, you should be very verbose in your task, it should be a long string providing informations as detailed as necessary.
|
228 |
-
Here is a list of the team members that you can call:
|
229 |
-
```python
|
230 |
-
{%- for agent in managed_agents.values() %}
|
231 |
-
def {{ agent.name }}("Your query goes here.") -> str:
|
232 |
-
"""{{ agent.description }}"""
|
233 |
-
{% endfor %}
|
234 |
-
```
|
235 |
-
{%- endif %}
|
236 |
-
|
237 |
-
---
|
238 |
-
Now begin! Here is your task:
|
239 |
-
```
|
240 |
-
{{task}}
|
241 |
-
```
|
242 |
-
First in part 1, write the facts survey, then in part 2, write your plan.
|
243 |
-
update_plan_pre_messages: |-
|
244 |
-
You are a world expert at analyzing a situation, and plan accordingly towards solving a task.
|
245 |
-
You have been given the following task:
|
246 |
-
```
|
247 |
-
{{task}}
|
248 |
-
```
|
249 |
-
|
250 |
-
Below you will find a history of attempts made to solve this task.
|
251 |
-
You will first have to produce a survey of known and unknown facts, then propose a step-by-step high-level plan to solve the task.
|
252 |
-
If the previous tries so far have met some success, your updated plan can build on these results.
|
253 |
-
If you are stalled, you can make a completely new plan starting from scratch.
|
254 |
-
|
255 |
-
Find the task and history below:
|
256 |
-
update_plan_post_messages: |-
|
257 |
-
Now write your updated facts below, taking into account the above history:
|
258 |
-
## 1. Updated facts survey
|
259 |
-
### 1.1. Facts given in the task
|
260 |
-
### 1.2. Facts that we have learned
|
261 |
-
### 1.3. Facts still to look up
|
262 |
-
### 1.4. Facts still to derive
|
263 |
-
|
264 |
-
Then write a step-by-step high-level plan to solve the task above.
|
265 |
-
## 2. Plan
|
266 |
-
### 2. 1. ...
|
267 |
-
Etc.
|
268 |
-
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
|
269 |
-
Beware that you have {remaining_steps} steps remaining.
|
270 |
-
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
271 |
-
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
272 |
-
|
273 |
-
You can leverage these tools, behaving like regular python functions:
|
274 |
-
```python
|
275 |
-
{%- for tool in tools.values() %}
|
276 |
-
def {{ tool.name }}({% for arg_name, arg_info in tool.inputs.items() %}{{ arg_name }}: {{ arg_info.type }}{% if not loop.last %}, {% endif %}{% endfor %}) -> {{tool.output_type}}:
|
277 |
-
"""{{ tool.description }}
|
278 |
-
|
279 |
-
Args:
|
280 |
-
{%- for arg_name, arg_info in tool.inputs.items() %}
|
281 |
-
{{ arg_name }}: {{ arg_info.description }}
|
282 |
-
{%- endfor %}"""
|
283 |
-
{% endfor %}
|
284 |
-
```
|
285 |
-
|
286 |
-
{%- if managed_agents and managed_agents.values() | list %}
|
287 |
-
You can also give tasks to team members.
|
288 |
-
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task'.
|
289 |
-
Given that this team member is a real human, you should be very verbose in your task, it should be a long string providing informations as detailed as necessary.
|
290 |
-
Here is a list of the team members that you can call:
|
291 |
-
```python
|
292 |
-
{%- for agent in managed_agents.values() %}
|
293 |
-
def {{ agent.name }}("Your query goes here.") -> str:
|
294 |
-
"""{{ agent.description }}"""
|
295 |
-
{% endfor %}
|
296 |
-
```
|
297 |
-
{%- endif %}
|
298 |
-
|
299 |
-
Now write your updated facts survey below, then your new plan.
|
300 |
-
managed_agent:
|
301 |
-
task: |-
|
302 |
-
You're a helpful agent named '{{name}}'.
|
303 |
-
You have been submitted this task by your manager.
|
304 |
-
---
|
305 |
-
Task:
|
306 |
-
{{task}}
|
307 |
-
---
|
308 |
-
You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear understanding of the answer.
|
309 |
-
|
310 |
-
Your final_answer WILL HAVE to contain these parts:
|
311 |
-
### 1. Task outcome (short version):
|
312 |
-
### 2. Task outcome (extremely detailed version):
|
313 |
-
### 3. Additional context (if relevant):
|
314 |
-
|
315 |
-
Put all these in your final_answer tool, everything that you do not pass as an argument to final_answer will be lost.
|
316 |
-
And even if your task resolution is not successful, please return as much context as possible, so that your manager can act upon this feedback.
|
317 |
-
report: |-
|
318 |
-
Here is the final answer from your managed agent '{{name}}':
|
319 |
-
{{final_answer}}
|
320 |
-
final_answer:
|
321 |
-
pre_messages: |-
|
322 |
-
An agent tried to answer a user query but it got stuck and failed to do so. You are tasked with providing an answer instead. Here is the agent's memory:
|
323 |
-
post_messages: |-
|
324 |
-
Based on the above, please provide an answer to the following user task:
|
325 |
-
{{task}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prompts/toolcalling_agent.yaml
DELETED
@@ -1,239 +0,0 @@
|
|
1 |
-
system_prompt: |-
|
2 |
-
You are an expert assistant who can solve any task using tool calls. You will be given a task to solve as best you can.
|
3 |
-
To do so, you have been given access to some tools.
|
4 |
-
|
5 |
-
The tool call you write is an action: after the tool is executed, you will get the result of the tool call as an "observation".
|
6 |
-
This Action/Observation can repeat N times, you should take several steps when needed.
|
7 |
-
|
8 |
-
You can use the result of the previous action as input for the next action.
|
9 |
-
The observation will always be a string: it can represent a file, like "image_1.jpg".
|
10 |
-
Then you can use it as input for the next action. You can do it for instance as follows:
|
11 |
-
|
12 |
-
Observation: "image_1.jpg"
|
13 |
-
|
14 |
-
Action:
|
15 |
-
{
|
16 |
-
"name": "image_transformer",
|
17 |
-
"arguments": {"image": "image_1.jpg"}
|
18 |
-
}
|
19 |
-
|
20 |
-
To provide the final answer to the task, use an action blob with "name": "final_answer" tool. It is the only way to complete the task, else you will be stuck on a loop. So your final output should look like this:
|
21 |
-
Action:
|
22 |
-
{
|
23 |
-
"name": "final_answer",
|
24 |
-
"arguments": {"answer": "insert your final answer here"}
|
25 |
-
}
|
26 |
-
|
27 |
-
|
28 |
-
Here are a few examples using notional tools:
|
29 |
-
---
|
30 |
-
Task: "Generate an image of the oldest person in this document."
|
31 |
-
|
32 |
-
Action:
|
33 |
-
{
|
34 |
-
"name": "document_qa",
|
35 |
-
"arguments": {"document": "document.pdf", "question": "Who is the oldest person mentioned?"}
|
36 |
-
}
|
37 |
-
Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
|
38 |
-
|
39 |
-
Action:
|
40 |
-
{
|
41 |
-
"name": "image_generator",
|
42 |
-
"arguments": {"prompt": "A portrait of John Doe, a 55-year-old man living in Canada."}
|
43 |
-
}
|
44 |
-
Observation: "image.png"
|
45 |
-
|
46 |
-
Action:
|
47 |
-
{
|
48 |
-
"name": "final_answer",
|
49 |
-
"arguments": "image.png"
|
50 |
-
}
|
51 |
-
|
52 |
-
---
|
53 |
-
Task: "What is the result of the following operation: 5 + 3 + 1294.678?"
|
54 |
-
|
55 |
-
Action:
|
56 |
-
{
|
57 |
-
"name": "python_interpreter",
|
58 |
-
"arguments": {"code": "5 + 3 + 1294.678"}
|
59 |
-
}
|
60 |
-
Observation: 1302.678
|
61 |
-
|
62 |
-
Action:
|
63 |
-
{
|
64 |
-
"name": "final_answer",
|
65 |
-
"arguments": "1302.678"
|
66 |
-
}
|
67 |
-
|
68 |
-
---
|
69 |
-
Task: "Which city has the highest population , Guangzhou or Shanghai?"
|
70 |
-
|
71 |
-
Action:
|
72 |
-
{
|
73 |
-
"name": "search",
|
74 |
-
"arguments": "Population Guangzhou"
|
75 |
-
}
|
76 |
-
Observation: ['Guangzhou has a population of 15 million inhabitants as of 2021.']
|
77 |
-
|
78 |
-
|
79 |
-
Action:
|
80 |
-
{
|
81 |
-
"name": "search",
|
82 |
-
"arguments": "Population Shanghai"
|
83 |
-
}
|
84 |
-
Observation: '26 million (2019)'
|
85 |
-
|
86 |
-
Action:
|
87 |
-
{
|
88 |
-
"name": "final_answer",
|
89 |
-
"arguments": "Shanghai"
|
90 |
-
}
|
91 |
-
|
92 |
-
Above example were using notional tools that might not exist for you. You only have access to these tools:
|
93 |
-
{%- for tool in tools.values() %}
|
94 |
-
- {{ tool.name }}: {{ tool.description }}
|
95 |
-
Takes inputs: {{tool.inputs}}
|
96 |
-
Returns an output of type: {{tool.output_type}}
|
97 |
-
{%- endfor %}
|
98 |
-
|
99 |
-
{%- if managed_agents and managed_agents.values() | list %}
|
100 |
-
You can also give tasks to team members.
|
101 |
-
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task', a long string explaining your task.
|
102 |
-
Given that this team member is a real human, you should be very verbose in your task.
|
103 |
-
Here is a list of the team members that you can call:
|
104 |
-
{%- for agent in managed_agents.values() %}
|
105 |
-
- {{ agent.name }}: {{ agent.description }}
|
106 |
-
{%- endfor %}
|
107 |
-
{%- endif %}
|
108 |
-
|
109 |
-
Here are the rules you should always follow to solve your task:
|
110 |
-
1. ALWAYS provide a tool call, else you will fail.
|
111 |
-
2. Always use the right arguments for the tools. Never use variable names as the action arguments, use the value instead.
|
112 |
-
3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself.
|
113 |
-
If no tool call is needed, use final_answer tool to return your answer.
|
114 |
-
4. Never re-do a tool call that you previously did with the exact same parameters.
|
115 |
-
|
116 |
-
Now Begin!
|
117 |
-
planning:
|
118 |
-
initial_plan : |-
|
119 |
-
You are a world expert at analyzing a situation to derive facts, and plan accordingly towards solving a task.
|
120 |
-
Below I will present you a task. You will need to 1. build a survey of facts known or needed to solve the task, then 2. make a plan of action to solve the task.
|
121 |
-
|
122 |
-
## 1. Facts survey
|
123 |
-
You will build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.
|
124 |
-
These "facts" will typically be specific names, dates, values, etc. Your answer should use the below headings:
|
125 |
-
### 1.1. Facts given in the task
|
126 |
-
List here the specific facts given in the task that could help you (there might be nothing here).
|
127 |
-
|
128 |
-
### 1.2. Facts to look up
|
129 |
-
List here any facts that we may need to look up.
|
130 |
-
Also list where to find each of these, for instance a website, a file... - maybe the task contains some sources that you should re-use here.
|
131 |
-
|
132 |
-
### 1.3. Facts to derive
|
133 |
-
List here anything that we want to derive from the above by logical reasoning, for instance computation or simulation.
|
134 |
-
|
135 |
-
Don't make any assumptions. For each item, provide a thorough reasoning. Do not add anything else on top of three headings above.
|
136 |
-
|
137 |
-
## 2. Plan
|
138 |
-
Then for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
|
139 |
-
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
|
140 |
-
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
141 |
-
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
142 |
-
|
143 |
-
You can leverage these tools:
|
144 |
-
{%- for tool in tools.values() %}
|
145 |
-
- {{ tool.name }}: {{ tool.description }}
|
146 |
-
Takes inputs: {{tool.inputs}}
|
147 |
-
Returns an output of type: {{tool.output_type}}
|
148 |
-
{%- endfor %}
|
149 |
-
|
150 |
-
{%- if managed_agents and managed_agents.values() | list %}
|
151 |
-
You can also give tasks to team members.
|
152 |
-
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task', a long string explaining your task.
|
153 |
-
Given that this team member is a real human, you should be very verbose in your task.
|
154 |
-
Here is a list of the team members that you can call:
|
155 |
-
{%- for agent in managed_agents.values() %}
|
156 |
-
- {{ agent.name }}: {{ agent.description }}
|
157 |
-
{%- endfor %}
|
158 |
-
{%- endif %}
|
159 |
-
|
160 |
-
---
|
161 |
-
Now begin! Here is your task:
|
162 |
-
```
|
163 |
-
{{task}}
|
164 |
-
```
|
165 |
-
First in part 1, write the facts survey, then in part 2, write your plan.
|
166 |
-
update_plan_pre_messages: |-
|
167 |
-
You are a world expert at analyzing a situation, and plan accordingly towards solving a task.
|
168 |
-
You have been given the following task:
|
169 |
-
```
|
170 |
-
{{task}}
|
171 |
-
```
|
172 |
-
|
173 |
-
Below you will find a history of attempts made to solve this task.
|
174 |
-
You will first have to produce a survey of known and unknown facts, then propose a step-by-step high-level plan to solve the task.
|
175 |
-
If the previous tries so far have met some success, your updated plan can build on these results.
|
176 |
-
If you are stalled, you can make a completely new plan starting from scratch.
|
177 |
-
|
178 |
-
Find the task and history below:
|
179 |
-
update_plan_post_messages: |-
|
180 |
-
Now write your updated facts below, taking into account the above history:
|
181 |
-
## 1. Updated facts survey
|
182 |
-
### 1.1. Facts given in the task
|
183 |
-
### 1.2. Facts that we have learned
|
184 |
-
### 1.3. Facts still to look up
|
185 |
-
### 1.4. Facts still to derive
|
186 |
-
|
187 |
-
Then write a step-by-step high-level plan to solve the task above.
|
188 |
-
## 2. Plan
|
189 |
-
### 2. 1. ...
|
190 |
-
Etc.
|
191 |
-
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
|
192 |
-
Beware that you have {remaining_steps} steps remaining.
|
193 |
-
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
194 |
-
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
195 |
-
|
196 |
-
You can leverage these tools:
|
197 |
-
{%- for tool in tools.values() %}
|
198 |
-
- {{ tool.name }}: {{ tool.description }}
|
199 |
-
Takes inputs: {{tool.inputs}}
|
200 |
-
Returns an output of type: {{tool.output_type}}
|
201 |
-
{%- endfor %}
|
202 |
-
|
203 |
-
{%- if managed_agents and managed_agents.values() | list %}
|
204 |
-
You can also give tasks to team members.
|
205 |
-
Calling a team member works the same as for calling a tool: simply, the only argument you can give in the call is 'task'.
|
206 |
-
Given that this team member is a real human, you should be very verbose in your task, it should be a long string providing informations as detailed as necessary.
|
207 |
-
Here is a list of the team members that you can call:
|
208 |
-
{%- for agent in managed_agents.values() %}
|
209 |
-
- {{ agent.name }}: {{ agent.description }}
|
210 |
-
{%- endfor %}
|
211 |
-
{%- endif %}
|
212 |
-
|
213 |
-
Now write your new plan below.
|
214 |
-
managed_agent:
|
215 |
-
task: |-
|
216 |
-
You're a helpful agent named '{{name}}'.
|
217 |
-
You have been submitted this task by your manager.
|
218 |
-
---
|
219 |
-
Task:
|
220 |
-
{{task}}
|
221 |
-
---
|
222 |
-
You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear understanding of the answer.
|
223 |
-
|
224 |
-
Your final_answer WILL HAVE to contain these parts:
|
225 |
-
### 1. Task outcome (short version):
|
226 |
-
### 2. Task outcome (extremely detailed version):
|
227 |
-
### 3. Additional context (if relevant):
|
228 |
-
|
229 |
-
Put all these in your final_answer tool, everything that you do not pass as an argument to final_answer will be lost.
|
230 |
-
And even if your task resolution is not successful, please return as much context as possible, so that your manager can act upon this feedback.
|
231 |
-
report: |-
|
232 |
-
Here is the final answer from your managed agent '{{name}}':
|
233 |
-
{{final_answer}}
|
234 |
-
final_answer:
|
235 |
-
pre_messages: |-
|
236 |
-
An agent tried to answer a user query but it got stuck and failed to do so. You are tasked with providing an answer instead. Here is the agent's memory:
|
237 |
-
post_messages: |-
|
238 |
-
Based on the above, please provide an answer to the following user task:
|
239 |
-
{{task}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -11,3 +11,6 @@ requests>=2.32.3
|
|
11 |
smolagents[litellm,telemetry]>=1.14.0
|
12 |
typing-extensions>=4.5.0
|
13 |
wikipedia-api>=0.8.1
|
|
|
|
|
|
|
|
11 |
smolagents[litellm,telemetry]>=1.14.0
|
12 |
typing-extensions>=4.5.0
|
13 |
wikipedia-api>=0.8.1
|
14 |
+
langchain>=0.1.0
|
15 |
+
langchain-community>=0.0.10
|
16 |
+
pandas>=2.0.0
|
tools.py
DELETED
@@ -1,254 +0,0 @@
|
|
1 |
-
import io
|
2 |
-
import re
|
3 |
-
from typing import Any, Dict, List
|
4 |
-
|
5 |
-
import pandas as pd
|
6 |
-
import requests
|
7 |
-
from bs4 import BeautifulSoup
|
8 |
-
from PIL import Image
|
9 |
-
from smolagents import tool
|
10 |
-
from smolagents.default_tools import DuckDuckGoSearchTool, VisitWebpageTool
|
11 |
-
|
12 |
-
|
13 |
-
@tool
|
14 |
-
def web_search(query: str) -> str:
|
15 |
-
"""
|
16 |
-
Search the web for information.
|
17 |
-
|
18 |
-
Args:
|
19 |
-
query: Search query to find information
|
20 |
-
|
21 |
-
Returns:
|
22 |
-
Search results as text
|
23 |
-
"""
|
24 |
-
# Using the built-in DuckDuckGo search tool from smolagents
|
25 |
-
# search_tool = DuckDuckGoSearchTool()
|
26 |
-
search_tool = DuckDuckGoSearchTool(max_results=3)
|
27 |
-
results = search_tool.execute(query)
|
28 |
-
return results
|
29 |
-
|
30 |
-
|
31 |
-
@tool
|
32 |
-
def browse_webpage(url: str) -> Dict[str, Any]:
|
33 |
-
"""
|
34 |
-
Browse a webpage and extract its content.
|
35 |
-
|
36 |
-
Args:
|
37 |
-
url: URL of the webpage to browse
|
38 |
-
|
39 |
-
Returns:
|
40 |
-
Dictionary containing title, text content, and links from the webpage
|
41 |
-
"""
|
42 |
-
try:
|
43 |
-
headers = {
|
44 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
45 |
-
}
|
46 |
-
response = requests.get(url, headers=headers)
|
47 |
-
response.raise_for_status()
|
48 |
-
|
49 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
50 |
-
|
51 |
-
# Extract title
|
52 |
-
title = soup.title.string if soup.title else "No title found"
|
53 |
-
|
54 |
-
# Extract main text content
|
55 |
-
paragraphs = soup.find_all("p")
|
56 |
-
text_content = "\n".join([p.get_text().strip() for p in paragraphs])
|
57 |
-
|
58 |
-
# Extract links
|
59 |
-
links = []
|
60 |
-
for link in soup.find_all("a", href=True):
|
61 |
-
href = link["href"]
|
62 |
-
text = link.get_text().strip()
|
63 |
-
if href.startswith("http"):
|
64 |
-
links.append({"text": text, "href": href})
|
65 |
-
|
66 |
-
return {"title": title, "content": text_content, "links": links}
|
67 |
-
except Exception as e:
|
68 |
-
return {"error": str(e)}
|
69 |
-
|
70 |
-
|
71 |
-
@tool
|
72 |
-
def analyze_image(image_url: str) -> Dict[str, Any]:
|
73 |
-
"""
|
74 |
-
Analyze an image and extract information from it.
|
75 |
-
|
76 |
-
Args:
|
77 |
-
image_url: URL of the image to analyze
|
78 |
-
|
79 |
-
Returns:
|
80 |
-
Dictionary containing information about the image
|
81 |
-
"""
|
82 |
-
try:
|
83 |
-
# Download the image
|
84 |
-
response = requests.get(image_url)
|
85 |
-
response.raise_for_status()
|
86 |
-
|
87 |
-
# Open the image
|
88 |
-
img = Image.open(io.BytesIO(response.content))
|
89 |
-
|
90 |
-
# Extract basic image information
|
91 |
-
width, height = img.size
|
92 |
-
format_type = img.format
|
93 |
-
mode = img.mode
|
94 |
-
|
95 |
-
return {
|
96 |
-
"width": width,
|
97 |
-
"height": height,
|
98 |
-
"format": format_type,
|
99 |
-
"mode": mode,
|
100 |
-
"aspect_ratio": width / height,
|
101 |
-
}
|
102 |
-
except Exception as e:
|
103 |
-
return {"error": str(e)}
|
104 |
-
|
105 |
-
|
106 |
-
@tool
|
107 |
-
def read_pdf(pdf_url: str) -> str:
|
108 |
-
"""
|
109 |
-
Extract text content from a PDF document.
|
110 |
-
|
111 |
-
Args:
|
112 |
-
pdf_url: URL of the PDF to read
|
113 |
-
|
114 |
-
Returns:
|
115 |
-
Text content extracted from the PDF
|
116 |
-
"""
|
117 |
-
try:
|
118 |
-
# Download the PDF
|
119 |
-
response = requests.get(pdf_url)
|
120 |
-
response.raise_for_status()
|
121 |
-
|
122 |
-
# This is a placeholder - in a real implementation, you would use a PDF parsing library
|
123 |
-
# such as PyPDF2, pdfplumber, or pdf2text
|
124 |
-
return "PDF content extraction would happen here in a real implementation"
|
125 |
-
except Exception as e:
|
126 |
-
return f"Error: {str(e)}"
|
127 |
-
|
128 |
-
|
129 |
-
@tool
|
130 |
-
def parse_csv(csv_url: str) -> Dict[str, Any]:
|
131 |
-
"""
|
132 |
-
Parse a CSV file and return its content as structured data.
|
133 |
-
|
134 |
-
Args:
|
135 |
-
csv_url: URL of the CSV file to parse
|
136 |
-
|
137 |
-
Returns:
|
138 |
-
Dictionary containing parsed CSV data
|
139 |
-
"""
|
140 |
-
try:
|
141 |
-
# Download the CSV
|
142 |
-
response = requests.get(csv_url)
|
143 |
-
response.raise_for_status()
|
144 |
-
|
145 |
-
# Parse the CSV
|
146 |
-
df = pd.read_csv(io.StringIO(response.text))
|
147 |
-
|
148 |
-
# Convert to dictionary format
|
149 |
-
columns = df.columns.tolist()
|
150 |
-
data = df.to_dict(orient="records")
|
151 |
-
|
152 |
-
# Return basic statistics and preview
|
153 |
-
return {
|
154 |
-
"columns": columns,
|
155 |
-
"row_count": len(data),
|
156 |
-
"preview": data[:5] if len(data) > 5 else data,
|
157 |
-
"column_dtypes": {col: str(df[col].dtype) for col in columns},
|
158 |
-
}
|
159 |
-
except Exception as e:
|
160 |
-
return {"error": str(e)}
|
161 |
-
|
162 |
-
|
163 |
-
@tool
|
164 |
-
def find_in_page(page_content: Dict[str, Any], query: str) -> List[str]:
|
165 |
-
"""
|
166 |
-
Find occurrences of a query string in page content.
|
167 |
-
|
168 |
-
Args:
|
169 |
-
page_content: Page content returned by browse_webpage
|
170 |
-
query: String to search for in the page
|
171 |
-
|
172 |
-
Returns:
|
173 |
-
List of sentences or sections containing the query
|
174 |
-
"""
|
175 |
-
results = []
|
176 |
-
if "content" in page_content:
|
177 |
-
content = page_content["content"]
|
178 |
-
# Split content into sentences
|
179 |
-
sentences = re.split(r"(?<=[.!?])\s+", content)
|
180 |
-
|
181 |
-
# Find sentences containing the query
|
182 |
-
for sentence in sentences:
|
183 |
-
if query.lower() in sentence.lower():
|
184 |
-
results.append(sentence)
|
185 |
-
|
186 |
-
return results
|
187 |
-
|
188 |
-
|
189 |
-
@tool
|
190 |
-
def extract_dates(text: str) -> List[str]:
|
191 |
-
"""
|
192 |
-
Extract dates from text content.
|
193 |
-
|
194 |
-
Args:
|
195 |
-
text: Text content to extract dates from
|
196 |
-
|
197 |
-
Returns:
|
198 |
-
List of date strings found in the text
|
199 |
-
"""
|
200 |
-
# Simple regex patterns for date extraction
|
201 |
-
# These patterns can be expanded for better coverage
|
202 |
-
date_patterns = [
|
203 |
-
r"\d{1,2}/\d{1,2}/\d{2,4}", # MM/DD/YYYY or DD/MM/YYYY
|
204 |
-
r"\d{1,2}-\d{1,2}-\d{2,4}", # MM-DD-YYYY or DD-MM-YYYY
|
205 |
-
r"\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* \d{1,2},? \d{4}\b", # Month DD, YYYY
|
206 |
-
r"\b\d{1,2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* \d{4}\b", # DD Month YYYY
|
207 |
-
]
|
208 |
-
|
209 |
-
results = []
|
210 |
-
for pattern in date_patterns:
|
211 |
-
matches = re.findall(pattern, text, re.IGNORECASE)
|
212 |
-
results.extend(matches)
|
213 |
-
|
214 |
-
return results
|
215 |
-
|
216 |
-
|
217 |
-
@tool
|
218 |
-
def perform_calculation(expression: str) -> Dict[str, Any]:
|
219 |
-
"""
|
220 |
-
Safely evaluate a mathematical expression.
|
221 |
-
|
222 |
-
Args:
|
223 |
-
expression: Mathematical expression to evaluate
|
224 |
-
|
225 |
-
Returns:
|
226 |
-
Dictionary containing the result or error message
|
227 |
-
"""
|
228 |
-
try:
|
229 |
-
# Using a safer approach than eval()
|
230 |
-
# This is very limited but safer
|
231 |
-
import math
|
232 |
-
|
233 |
-
# Define allowed names
|
234 |
-
allowed_names = {
|
235 |
-
"abs": abs,
|
236 |
-
"round": round,
|
237 |
-
"min": min,
|
238 |
-
"max": max,
|
239 |
-
"sum": sum,
|
240 |
-
"len": len,
|
241 |
-
"pow": pow,
|
242 |
-
"math": math,
|
243 |
-
}
|
244 |
-
|
245 |
-
# Clean the expression
|
246 |
-
cleaned_expr = expression.strip()
|
247 |
-
|
248 |
-
# Evaluate using safer methods (this is still a simplified example)
|
249 |
-
# In a real implementation, use a proper math expression parser
|
250 |
-
result = eval(cleaned_expr, {"__builtins__": {}}, allowed_names)
|
251 |
-
|
252 |
-
return {"result": result}
|
253 |
-
except Exception as e:
|
254 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/__init__.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .wikipedia_rag import WikipediaRAGTool
|
2 |
+
from .web_search import web_search
|
3 |
+
from .browse_webpage import browse_webpage
|
4 |
+
from .analyze_image import analyze_image
|
5 |
+
from .read_pdf import read_pdf
|
6 |
+
from .parse_csv import parse_csv
|
7 |
+
from .find_in_page import find_in_page
|
8 |
+
from .extract_dates import extract_dates
|
9 |
+
from .perform_calculation import perform_calculation
|
10 |
+
|
11 |
+
__all__ = [
|
12 |
+
'WikipediaRAGTool',
|
13 |
+
'web_search',
|
14 |
+
'browse_webpage',
|
15 |
+
'analyze_image',
|
16 |
+
'read_pdf',
|
17 |
+
'parse_csv',
|
18 |
+
'find_in_page',
|
19 |
+
'extract_dates',
|
20 |
+
'perform_calculation'
|
21 |
+
]
|
tools/analyze_image/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import analyze_image
|
2 |
+
|
3 |
+
__all__ = ['analyze_image']
|
tools/analyze_image/tool.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
from typing import Dict, Any
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from smolagents import tool
|
6 |
+
|
7 |
+
@tool
|
8 |
+
def analyze_image(image_url: str) -> Dict[str, Any]:
|
9 |
+
"""
|
10 |
+
Analyze an image and extract information from it.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
image_url: URL of the image to analyze
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
Dictionary containing information about the image
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
# Download the image
|
20 |
+
response = requests.get(image_url)
|
21 |
+
response.raise_for_status()
|
22 |
+
|
23 |
+
# Open the image
|
24 |
+
img = Image.open(io.BytesIO(response.content))
|
25 |
+
|
26 |
+
# Extract basic image information
|
27 |
+
width, height = img.size
|
28 |
+
format_type = img.format
|
29 |
+
mode = img.mode
|
30 |
+
|
31 |
+
return {
|
32 |
+
"width": width,
|
33 |
+
"height": height,
|
34 |
+
"format": format_type,
|
35 |
+
"mode": mode,
|
36 |
+
"aspect_ratio": width / height,
|
37 |
+
}
|
38 |
+
except Exception as e:
|
39 |
+
return {"error": str(e)}
|
tools/browse_webpage/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import browse_webpage
|
2 |
+
|
3 |
+
__all__ = ['browse_webpage']
|
tools/browse_webpage/tool.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from smolagents import tool
|
5 |
+
|
6 |
+
@tool
|
7 |
+
def browse_webpage(url: str) -> Dict[str, Any]:
|
8 |
+
"""
|
9 |
+
Browse a webpage and extract its content.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
url: URL of the webpage to browse
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
Dictionary containing title, text content, and links from the webpage
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
headers = {
|
19 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
20 |
+
}
|
21 |
+
response = requests.get(url, headers=headers)
|
22 |
+
response.raise_for_status()
|
23 |
+
|
24 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
25 |
+
|
26 |
+
# Extract title
|
27 |
+
title = soup.title.string if soup.title else "No title found"
|
28 |
+
|
29 |
+
# Extract main text content
|
30 |
+
paragraphs = soup.find_all("p")
|
31 |
+
text_content = "\n".join([p.get_text().strip() for p in paragraphs])
|
32 |
+
|
33 |
+
# Extract links
|
34 |
+
links = []
|
35 |
+
for link in soup.find_all("a", href=True):
|
36 |
+
href = link["href"]
|
37 |
+
text = link.get_text().strip()
|
38 |
+
if href.startswith("http"):
|
39 |
+
links.append({"text": text, "href": href})
|
40 |
+
|
41 |
+
return {"title": title, "content": text_content, "links": links}
|
42 |
+
except Exception as e:
|
43 |
+
return {"error": str(e)}
|
tools/extract_dates/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import extract_dates
|
2 |
+
|
3 |
+
__all__ = ['extract_dates']
|
tools/extract_dates/tool.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
import re
|
3 |
+
from smolagents import tool
|
4 |
+
|
5 |
+
@tool
|
6 |
+
def extract_dates(text: str) -> List[str]:
|
7 |
+
"""
|
8 |
+
Extract dates from text content.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
text: Text content to extract dates from
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
List of date strings found in the text
|
15 |
+
"""
|
16 |
+
# Simple regex patterns for date extraction
|
17 |
+
# These patterns can be expanded for better coverage
|
18 |
+
date_patterns = [
|
19 |
+
r"\d{1,2}/\d{1,2}/\d{2,4}", # MM/DD/YYYY or DD/MM/YYYY
|
20 |
+
r"\d{1,2}-\d{1,2}-\d{2,4}", # MM-DD-YYYY or DD-MM-YYYY
|
21 |
+
r"\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* \d{1,2},? \d{4}\b", # Month DD, YYYY
|
22 |
+
r"\b\d{1,2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* \d{4}\b", # DD Month YYYY
|
23 |
+
]
|
24 |
+
|
25 |
+
results = []
|
26 |
+
for pattern in date_patterns:
|
27 |
+
matches = re.findall(pattern, text, re.IGNORECASE)
|
28 |
+
results.extend(matches)
|
29 |
+
|
30 |
+
return results
|
tools/find_in_page/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import find_in_page
|
2 |
+
|
3 |
+
__all__ = ['find_in_page']
|
tools/find_in_page/tool.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Dict, Any
|
2 |
+
import re
|
3 |
+
from smolagents import tool
|
4 |
+
|
5 |
+
@tool
|
6 |
+
def find_in_page(page_content: Dict[str, Any], query: str) -> List[str]:
|
7 |
+
"""
|
8 |
+
Find occurrences of a query string in page content.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
page_content: Page content returned by browse_webpage
|
12 |
+
query: String to search for in the page
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
List of sentences or sections containing the query
|
16 |
+
"""
|
17 |
+
results = []
|
18 |
+
if "content" in page_content:
|
19 |
+
content = page_content["content"]
|
20 |
+
# Split content into sentences
|
21 |
+
sentences = re.split(r"(?<=[.!?])\s+", content)
|
22 |
+
|
23 |
+
# Find sentences containing the query
|
24 |
+
for sentence in sentences:
|
25 |
+
if query.lower() in sentence.lower():
|
26 |
+
results.append(sentence)
|
27 |
+
|
28 |
+
return results
|
tools/parse_csv/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import parse_csv
|
2 |
+
|
3 |
+
__all__ = ['parse_csv']
|
tools/parse_csv/tool.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
from typing import Dict, Any
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
from smolagents import tool
|
6 |
+
|
7 |
+
@tool
|
8 |
+
def parse_csv(csv_url: str) -> Dict[str, Any]:
|
9 |
+
"""
|
10 |
+
Parse a CSV file and return its content as structured data.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
csv_url: URL of the CSV file to parse
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
Dictionary containing parsed CSV data
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
# Download the CSV
|
20 |
+
response = requests.get(csv_url)
|
21 |
+
response.raise_for_status()
|
22 |
+
|
23 |
+
# Parse the CSV
|
24 |
+
df = pd.read_csv(io.StringIO(response.text))
|
25 |
+
|
26 |
+
# Convert to dictionary format
|
27 |
+
columns = df.columns.tolist()
|
28 |
+
data = df.to_dict(orient="records")
|
29 |
+
|
30 |
+
# Return basic statistics and preview
|
31 |
+
return {
|
32 |
+
"columns": columns,
|
33 |
+
"row_count": len(data),
|
34 |
+
"preview": data[:5] if len(data) > 5 else data,
|
35 |
+
"column_dtypes": {col: str(df[col].dtype) for col in columns},
|
36 |
+
}
|
37 |
+
except Exception as e:
|
38 |
+
return {"error": str(e)}
|
tools/perform_calculation/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import perform_calculation
|
2 |
+
|
3 |
+
__all__ = ['perform_calculation']
|
tools/perform_calculation/tool.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
import math
|
3 |
+
from smolagents import tool
|
4 |
+
|
5 |
+
@tool
|
6 |
+
def perform_calculation(expression: str) -> Dict[str, Any]:
|
7 |
+
"""
|
8 |
+
Safely evaluate a mathematical expression.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
expression: Mathematical expression to evaluate
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
Dictionary containing the result or error message
|
15 |
+
"""
|
16 |
+
try:
|
17 |
+
# Define allowed names
|
18 |
+
allowed_names = {
|
19 |
+
"abs": abs,
|
20 |
+
"round": round,
|
21 |
+
"min": min,
|
22 |
+
"max": max,
|
23 |
+
"sum": sum,
|
24 |
+
"len": len,
|
25 |
+
"pow": pow,
|
26 |
+
"math": math,
|
27 |
+
}
|
28 |
+
|
29 |
+
# Clean the expression
|
30 |
+
cleaned_expr = expression.strip()
|
31 |
+
|
32 |
+
# Evaluate using safer methods (this is still a simplified example)
|
33 |
+
# In a real implementation, use a proper math expression parser
|
34 |
+
result = eval(cleaned_expr, {"__builtins__": {}}, allowed_names)
|
35 |
+
|
36 |
+
return {"result": result}
|
37 |
+
except Exception as e:
|
38 |
+
return {"error": str(e)}
|
tools/read_pdf/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import read_pdf
|
2 |
+
|
3 |
+
__all__ = ['read_pdf']
|
tools/read_pdf/tool.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from smolagents import tool
|
3 |
+
|
4 |
+
@tool
|
5 |
+
def read_pdf(pdf_url: str) -> str:
|
6 |
+
"""
|
7 |
+
Extract text content from a PDF document.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
pdf_url: URL of the PDF to read
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
Text content extracted from the PDF
|
14 |
+
"""
|
15 |
+
try:
|
16 |
+
# Download the PDF
|
17 |
+
response = requests.get(pdf_url)
|
18 |
+
response.raise_for_status()
|
19 |
+
|
20 |
+
# This is a placeholder - in a real implementation, you would use a PDF parsing library
|
21 |
+
# such as PyPDF2, pdfplumber, or pdf2text
|
22 |
+
return "PDF content extraction would happen here in a real implementation"
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error: {str(e)}"
|
tools/web_search/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import web_search
|
2 |
+
|
3 |
+
__all__ = ['web_search']
|
tools/web_search/tool.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import tool
|
2 |
+
from smolagents.default_tools import DuckDuckGoSearchTool
|
3 |
+
|
4 |
+
@tool
|
5 |
+
def web_search(query: str) -> str:
|
6 |
+
"""
|
7 |
+
Search the web for information.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
query: Search query to find information
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
Search results as text
|
14 |
+
"""
|
15 |
+
search_tool = DuckDuckGoSearchTool(max_results=3)
|
16 |
+
results = search_tool.execute(query)
|
17 |
+
return results
|
tools/wikipedia_rag/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Wikipedia RAG Tool
|
2 |
+
|
3 |
+
A Retrieval-Augmented Generation (RAG) tool for searching and retrieving information from Wikipedia articles.
|
4 |
+
|
5 |
+
## Features
|
6 |
+
|
7 |
+
- Loads and processes Wikipedia articles from a structured dataset
|
8 |
+
- Uses BM25 retrieval for finding relevant articles
|
9 |
+
- Returns formatted results with article metadata
|
10 |
+
- Can be used standalone or integrated with an agent
|
11 |
+
|
12 |
+
## Usage
|
13 |
+
|
14 |
+
### Standalone Usage
|
15 |
+
|
16 |
+
```bash
|
17 |
+
python run.py --query "Your search query here" --dataset-path path/to/dataset
|
18 |
+
```
|
19 |
+
|
20 |
+
### Integration with Agent
|
21 |
+
|
22 |
+
```python
|
23 |
+
from tools.wikipedia_rag import WikipediaRAGTool
|
24 |
+
|
25 |
+
# Initialize the tool
|
26 |
+
wikipedia_tool = WikipediaRAGTool(dataset_path="path/to/dataset")
|
27 |
+
|
28 |
+
# Use with an agent
|
29 |
+
agent = CodeAgent(
|
30 |
+
model=model,
|
31 |
+
tools=[wikipedia_tool],
|
32 |
+
)
|
33 |
+
```
|
34 |
+
|
35 |
+
## Requirements
|
36 |
+
|
37 |
+
- pandas
|
38 |
+
- langchain
|
39 |
+
- langchain-community
|
40 |
+
- smolagents
|
41 |
+
|
42 |
+
## Dataset Format
|
43 |
+
|
44 |
+
The tool expects a CSV file with the following columns:
|
45 |
+
- title: Article title
|
46 |
+
- content: Article content
|
47 |
+
- url: Article URL
|
48 |
+
- category: Article category (optional)
|
49 |
+
|
50 |
+
## License
|
51 |
+
|
52 |
+
MIT
|
tools/wikipedia_rag/__init__.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from .tool import WikipediaRAGTool
|
2 |
+
|
3 |
+
__all__ = ['WikipediaRAGTool']
|
tools/wikipedia_rag/run.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import argparse
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from tool import WikipediaRAGTool
|
5 |
+
|
6 |
+
def main():
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Set up argument parser
|
11 |
+
parser = argparse.ArgumentParser(description='Run Wikipedia RAG Tool')
|
12 |
+
parser.add_argument('--query', type=str, required=True, help='Search query for Wikipedia articles')
|
13 |
+
parser.add_argument('--dataset-path', type=str, default='wikipedia-structured-contents',
|
14 |
+
help='Path to the Wikipedia dataset')
|
15 |
+
args = parser.parse_args()
|
16 |
+
|
17 |
+
# Initialize the tool
|
18 |
+
tool = WikipediaRAGTool(dataset_path=args.dataset_path)
|
19 |
+
|
20 |
+
# Run the query
|
21 |
+
print(f"\nQuery: {args.query}")
|
22 |
+
print("-" * 50)
|
23 |
+
result = tool.forward(args.query)
|
24 |
+
print(f"Result: {result}")
|
25 |
+
print("-" * 50)
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
main()
|
tools/wikipedia_rag/tool.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
from typing import List, Optional
|
4 |
+
from langchain.docstore.document import Document
|
5 |
+
from langchain_community.retrievers import BM25Retriever
|
6 |
+
from smolagents import Tool
|
7 |
+
|
8 |
+
class WikipediaRAGTool(Tool):
|
9 |
+
name = "wikipedia_rag"
|
10 |
+
description = "Retrieves relevant information from Wikipedia articles using RAG."
|
11 |
+
inputs = {
|
12 |
+
"query": {
|
13 |
+
"type": "string",
|
14 |
+
"description": "The search query to find relevant Wikipedia content."
|
15 |
+
}
|
16 |
+
}
|
17 |
+
output_type = "string"
|
18 |
+
|
19 |
+
def __init__(self, dataset_path: str = "wikipedia-structured-contents"):
|
20 |
+
self.is_initialized = False
|
21 |
+
self.dataset_path = dataset_path
|
22 |
+
self.docs: List[Document] = []
|
23 |
+
self.retriever: Optional[BM25Retriever] = None
|
24 |
+
|
25 |
+
def _load_documents(self) -> None:
|
26 |
+
"""Load and process the Wikipedia dataset into Document objects."""
|
27 |
+
try:
|
28 |
+
# Load the dataset
|
29 |
+
df = pd.read_csv(os.path.join(self.dataset_path, "wikipedia_articles.csv"))
|
30 |
+
|
31 |
+
# Convert each article into a Document
|
32 |
+
self.docs = [
|
33 |
+
Document(
|
34 |
+
page_content=f"Title: {row['title']}\n\nContent: {row['content']}",
|
35 |
+
metadata={
|
36 |
+
"title": row['title'],
|
37 |
+
"url": row['url'],
|
38 |
+
"category": row.get('category', '')
|
39 |
+
}
|
40 |
+
)
|
41 |
+
for _, row in df.iterrows()
|
42 |
+
]
|
43 |
+
|
44 |
+
# Initialize the retriever
|
45 |
+
self.retriever = BM25Retriever.from_documents(self.docs)
|
46 |
+
self.is_initialized = True
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Error loading documents: {e}")
|
50 |
+
raise
|
51 |
+
|
52 |
+
def forward(self, query: str) -> str:
|
53 |
+
"""Process the query and return relevant Wikipedia content."""
|
54 |
+
if not self.is_initialized:
|
55 |
+
self._load_documents()
|
56 |
+
|
57 |
+
if not self.retriever:
|
58 |
+
return "Error: Retriever not initialized properly."
|
59 |
+
|
60 |
+
try:
|
61 |
+
# Get relevant documents
|
62 |
+
results = self.retriever.get_relevant_documents(query)
|
63 |
+
|
64 |
+
if not results:
|
65 |
+
return "No relevant Wikipedia articles found."
|
66 |
+
|
67 |
+
# Format the results
|
68 |
+
formatted_results = []
|
69 |
+
for doc in results[:3]: # Return top 3 most relevant results
|
70 |
+
metadata = doc.metadata
|
71 |
+
formatted_results.append(
|
72 |
+
f"Title: {metadata['title']}\n"
|
73 |
+
f"URL: {metadata['url']}\n"
|
74 |
+
f"Category: {metadata['category']}\n"
|
75 |
+
f"Content: {doc.page_content[:500]}...\n"
|
76 |
+
)
|
77 |
+
|
78 |
+
return "\n\n".join(formatted_results)
|
79 |
+
|
80 |
+
except Exception as e:
|
81 |
+
return f"Error retrieving information: {str(e)}"
|