Creating a new tool
We’ll be getting into the AG2 code-base, it’s useful to understand how AG2 works under the hood, see this section for the rundown.
Creating a new tool in AG2 is easy and empowers any ConversableAgent-based agent in AG2.
How a tool is created
Let’s look at how the Crawl4AITool tool was implemented.
The Crawl4AITool uses a 3rd party package to crawl a website and extract information, returning the crawled data as its response.
Here’s some code from the Crawl4AITool with annotations added (current code here):
And this tool can now be registered with any ConversableAgent-based agent.
Here’s how to use the Crawl4AITool:
Protecting secrets
Secrets such as password, tokens, or personal information needs to be protected from capture. AG2 provides dependency injection as a way to secure this sensitive information while still allowing agents to perform their tasks effectively, even when working with large language models (LLMs).
See the DiscordSendTool example in the Creating an Agent documentation to see how it is implemented for a tool.
See Tools with Secrets for guidance.
Where to put your code
Decide on a folder name that matches your tool name, use underscores to separate words, e.g. deep_research
.
Create your tool code in a folder under autogen/tools/contrib/
.
Put your tool tests in a folder under test/tools/contrib
.
Documentation
As a way for other developers to learn about and understand how to use your tool, it is recommended to create a Jupyter notebook that:
- Explains what the tool is
- How to install AG2 for the tool (e.g. with extras)
- Has sample codes, simple to advanced
- Notes on capabilities and limitations
As an example, here’s the notebook for the Crawl4AI tool.
3rd party packages
If your tool requires a 3rd party package to be installed, add an extra in the pyproject.toml file, for example:
Put the current version of the packages as the minimum version and the next major version for the less than value.
Changes to pyproject.toml cover pyautogen
, autogen
, and ag2
packages because they propagate automatically to setup_ag2.py and setup_autogen.py.
Tests
It’s critical that tests are created for each piece of functionality within your tool.
See this test file for the BrowserUseTool as an example.
See this documentation for how to run tests locally and coverage.
Create a Pull Request
We’re excited to review and test your new AG2 tool! Create your Pull Request (PR) here.
Set the PR as a Draft PR if you’re not ready for it to be merged into the AG2 repository.
See our Contributor Guide for more guidance.
Encapsulating the tools in an agent
If it makes sense to have an agent pre-built with your tool(s), consider creating a tool-based agent. Now, when you need your tool you can just add the agent to your workflow.
Help me get started…
Two basic agents and a tool are available in the agents and tools contrib
namespaces that you can look at and use as a starting point for your own agents and tools.
- TimeReplyAgent (reply-based Agent) - source code, test code
- TimeToolAgent (tool-based Agent) - source code, test code
- TimeTool (Tool) - source code, test code