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): url = "https://icanhazdadjoke.com/" 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)