Skip to content
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

Merged

Conversation

bassilkhilo
Copy link
Collaborator

@bassilkhilo bassilkhilo commented Dec 2, 2024

… 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:

from file_2 import hello_world

agent_1 = SwarmAgent(
        name="agent_1",
        system_message=("Your task is to call hello_world() function."),
        llm_config=llm_config,
        functions=[
            hello_world,
        ],
    )

agent_2 = SwarmAgent(
        name="agent_2",
        system_message=("Your task is to let the user know what the previous agent said."),
        llm_config=llm_config,
        functions=[
        ],
    )

In the above code, we initialized a swarm agent and passed the hello_world function in file_1.py

file_2.py:

def hello_world(context_variables: dict) -> SwarmResult:
    """retuns hello world, then transfers to agent_2"""
    
    value_message = "Hello, World!"
        
    return SwarmResult(
        values=value_message,
        context_variables=context_variables,
        agent="agent_2",
    )

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

… via groupchat.agent_by_name. Also Modified Swarmresult to accept agent as an str or SwarmAgent obj
@bassilkhilo bassilkhilo force-pushed the allow-agent-name-as-string-in-swarm-results branch from a7ce87d to be68fe2 Compare December 2, 2024 22:22
@marklysze
Copy link
Collaborator

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.

@bassilkhilo
Copy link
Collaborator Author

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.

Thank you Mark!

Just added the example code to demonstrate this.

Copy link
Collaborator

@marklysze marklysze left a 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.

autogen/agentchat/contrib/swarm_agent.py Outdated Show resolved Hide resolved
@bassilkhilo
Copy link
Collaborator Author

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.

@bassilkhilo bassilkhilo self-assigned this Dec 3, 2024
@marklysze marklysze self-requested a review December 3, 2024 03:20
Copy link
Collaborator

@marklysze marklysze left a 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).

@qingyun-wu qingyun-wu added this pull request to the merge queue Dec 3, 2024
Merged via the queue into ag2ai:main with commit 17e4b14 Dec 3, 2024
202 of 210 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants