Tutorial: How to create a new Tool
This is a quick example of how to add a new tool to agent zero, there may be other ways to accomplish this, but since i couldn't find a good example i will share my code here.
This uses a free joke api just for an example proof of concept , most llms already contain many jokes by default, so its not very practical, but a current weather api implementation would be coded similar.
To add a new tool to Agent Zero we need to create two new files and modify a third file.
The steps to add a basic tool that requests a joke from an api and returns it to agent zero are:
(note that this tool does not take any arguments, look at the existing 'knowledge_tool.py' on how to add arguments, such as 'question')
  1. Add the new tool code by adding file: query_joke_api_tool.py
  2. Create a markdown file that agent zero will include in the prompt so that the LLM knows what the tools capabilities are: agent.system.tool.joke.md
  3. Modify the agent.system.tools.md file so that our new tool is included in the LLM prompt, practically registering our new tool.
Agent zero automatically knows where to look to find and execute this tool. Explicitly ask the LLM to use this tool and it should use it.
Step 1
Create file:
# /python/tools/query_joke_api_tool.py
import requests
from urllib.parse import urlparse
from python.helpers.tool import Tool, Response
from python.helpers.errors import handle_error
class QueryJokeApiTool(Tool):
async def execute(self, **kwargs):
headers = { "Accept": "text/plain"}
print("*** Agent Zero has called the QueryJokeApiTool")
try:
# Attempt to make the API request
response = requests.get(url, headers=headers, timeout=5)
# Check if the request was successful (status code 200)
if response.status_code == 200:
myjoke = response.text.strip()
return Response(message=f"Joke content:\n\n{myjoke}", break_loop=False)
else:
return Response(message=f"Error fetching joke:", break_loop=False)
except Exception as e:
handle_error(e)
return Response(message=f"An error occurred: {str(e)}", break_loop=False)
Step 2
Create file:
#/prompts/default/agent.system.tool.joke.md
### query_joke_api_tool:
Retrieves a funny joke from the internet.
This tool is useful for finding a funny.
This joke tool does not need any arguments.
**Example usage**:
```json
{
"thoughts": [
"I need to find a joke...",
"I will use the query_joke_api_tool to fetch a joke...",
],
"tool_name": "query_joke_api_tool",
"tool_args": {
}
}
```
Step 3
Modify this file by adding in an include link to our new markdown file:
#/prompts/default/agent.system.tools.md
## Tools available:
{{ include './agent.system.tool.call_sub.md' }}
{{ include './agent.system.tool.memory.md' }}
{{ include './agent.system.tool.code_exe.md' }}
{{ include './agent.system.tool.web.md' }}
# Add in our new tool
{{ include './agent.system.tool.joke.md' }}
8
5 comments
Matthew Kelly
2
Tutorial: How to create a new Tool
Agent Zero
skool.com/agent-zero
Agent Zero AI framework
Leaderboard (30-day)
Powered by