-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified swarm_translation to check if agent is str, if so, get agent… #138
Modified swarm_translation to check if agent is str, if so, get agent… #138
Conversation
… via groupchat.agent_by_name. Also Modified Swarmresult to accept agent as an str or SwarmAgent obj
a7ce87d
to
be68fe2
Compare
Nice one @bassilkhilo! Any chance you could put some sample code in the description above to show when it would be used? We'll need to add to the tests, (test/agentchat/contrib/test_swarm.py) to test for the use of strings as parameters. |
… via groupchat.agent_by_name. Also Modified Swarmresult to accept agent as an str or SwarmAgent obj
…/github.com/bassilkhilo/ag2 into allow-agent-name-as-string-in-swarm-results
Thank you Mark! Just added the example code to demonstrate this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to get this one in, is very useful. Can you also add some tests to:
/test/agentchat/contrib/test_swarm.py
It would be good to test for if it's a valid agent name and if it's an invalid agent name.
Hi Mark, thank you for the review! I just pushed the unit test in test_swarm.py (test_string_agent_params_for_transfer) and added a check to the swarm_transition function to check if next_agent is None and raise an error accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @bassilkhilo, looks good, appreciate the added feature and tests (and the correction to documentation).
… via groupchat.agent_by_name. Also Modified Swarmresult to accept agent as an str or SwarmAgent obj
Why are these changes needed?
This gives the flexibility to pass an agent by its string name in SwarmResult within a registered tool.
Why?
Previously, you can only pass an agent instance. The issue with this is you may want to specify agent tools in one file, and the actual agent initializations and initiate chat in another file. If you import the agent tools into the first file, you can't import the agent instances to the second file, it will give you a circular import error.
This solution allows us to have a modular agent tool library, and pass string names of agents you'd like to transfer to, allowing for more flexible workflows.
@yiranwu0 and I hopped on a call and came up with a solution, the power of teamwork!
Here is an example of how the code can be used:
As you know, we can pass an agent in SwarmResult to transfer to another agent.
Let's say we have an the following code:
file_1.py:
In the above code, we initialized a swarm agent and passed the hello_world function in file_1.py
file_2.py:
As you see above, we have 2 files. One initializing the agents, the second with the agent tools.
If you try to import the agent instances in file_2, you will get a circular import error. The solution of passing a string as the agent name in SwarmResult solves this issue, allowing us to have a more modular, organized workflow.
We changed the agent arg in SwarmResult to accept either a SwarmAgent obj or a string.
We also modified swarm_transition in initiate_swarm_chat to check if the next_agent is a str, if so, we retrieve the instance via groupchat.agent_by_name
Related issue number
Checks