Creating a new agent
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 agent in AG2 is easy and there are two main approaches you can take.
- Create and/or use tools as the agent’s functionality, keeping the core ConversableAgent functionality pretty much unchanged.
- Create a reply function as the method for the agent to carry out its internal workflow.
Tools-based agents
Pros:
- Easier to implement
- Can work well with a small distinct set of tools
- Easy to add another tool
- If you create tools, they can be used by other agents as well
Cons:
- Too many tools may confuse the LLM
- Multiple tool calls can be triggered from one LLM response, this may produce undesired results if the sequence isn’t right
- Your agent must have an associated LLM as it needs to recommend tool calls
Reply-based agents
Pros:
- Most flexibility over how the agent works
Cons:
- More code required, more error handling, more tests
- If using a tool or two is possible, this may be more complexity than necessary
- Functionality encapsulated within the agent, can’t be easily used by other agents
How a tool-based agent is created
Let’s take a look at a tool-based agent, the DiscordAgent
. You can read more about how it’s used here.
All agents should be based on ConversableAgent, this makes them usable in all orchestrations.
The DiscordAgent uses two tools to send and retrieve messages for a Discord channel. In addition to ConversableAgent’s parameters, it also takes in the authentication and channel details, as well as a boolean to indicate whether writing instructions should be appended to the agent’s system message.
Let’s look at the code for DiscordAgent with annotations added (current code here):
Agent’s Tool
Let’s look at one of the tools that the agent is using, DiscordSendTool, to see how a Tool is created (annotations added):
(See the Creating a Tool page for a more detailed guide on creating tools)
An important aspect of this tool is the use of AG2’s dependency injection functionality. This protects the bot_token, guild_name, and channel_name, from the tool definition for the LLMs, so the LLM will not see the attributes or values. See Tools with Secrets for guidance.
And, that’s all that’s needed to create a brand new agent powered by tools.
How a reply-based agent is created
If tools aren’t a viable option for your agent consider a reply-based agent.
It’s important to understand how ConversableAgent generates a reply, see this page for more details.
Let’s take a look at a basic agent that tells the time utilising a reply function.
When we use this TimeAgent in an AG2 workflow it will always return the date and time:
Where to put your code
Decide on a folder name that matches your agent name, use underscores to separate words, e.g. deep_research
.
Create your agent code in a folder under autogen/agents/contrib/
.
Put the tests for the agent in a folder under test/agents/contrib/
.
If you are creating tools, put them in a folder under autogen/tools/contrib/
.
For tools tests, put them in a folder under test/tools/contrib
.
Documentation
As a way for other developers to learn about and understand how to use your agent, it is recommended to create a Jupyter notebook that:
- Explains what the agent is
- How to install AG2 for the agent (e.g. with extras)
- Has sample codes, simple to advanced
- Notes on capabilities and limitations
As an example, here’s the notebook for the Discord, Slack, and Telegram tools.
3rd party packages
If your agent, or their tools, require 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 agent or tool.
See this test file for the WebSurferAgent 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 agent and 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.
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