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

Swarm: User-defined AfterWork string for agent selection using LLM #369

Merged
merged 13 commits into from
Feb 4, 2025

Conversation

marklysze
Copy link
Collaborator

Why are these changes needed?

In line with #26, this provides the developer with an option to specify a string prompt for an LLM to use to select the next agent in an agent's AfterWork hand off.

It extends the AfterWorkOption.SWARM_MANAGER (which, by default, uses the default auto speaker selection options) with an additional AfterWork parameter, next_agent_selection_msg, which becomes the selection prompt used by the LLM.

next_agent_selection_msg can be a string or a Callable that returns a string.

The signature for the Callable is:
def my_selection_message(agent: ConversableAgent, messages: List[Dict[str, Any]]) -> str

The string provided, or returned from the Callable, will have the following string substitutions:

  • {agentlist} will be replaced by a comma-delimited list of agents
  • context variable keys will be replaced by their values

The underlying groupchat's GroupChatManager will be used for the speaker selection and must, therefore, have an LLM Config. This can be set through the new swarm_manager_args parameter on a/initiate_swarm_chat.

Example:

context_variables = {
    "order_id": "ABC1234"
}

register_handoff(
    from=planner,
    hand_to=[
        AfterWork(agent=AfterWorkOption.SWARM_MANAGER, next_agent_selection_msg="Select the next agent from this list {agentlist}. From the list please select the order_mgmt_agent. Order Id is {order_id}"),
    ]
)

_, _, _ = initiate_swarm_chat(
    ...,
    swarm_manager_args={"llm_config": llm_config}
)

Related issue number

#26

Checks

@marklysze marklysze self-assigned this Jan 7, 2025
@marklysze marklysze added enhancement New feature or request swarm labels Jan 7, 2025
@marklysze marklysze mentioned this pull request Jan 7, 2025
3 tasks
@marklysze
Copy link
Collaborator Author

Thanks for the initial review @sonichi, if you could have another look over now that would be appreciated.

@marklysze marklysze requested a review from sonichi January 8, 2025 00:39
@marklysze
Copy link
Collaborator Author

@yiranwu0, @linmou - if you're able to test this it would be appreciated!

Copy link
Collaborator

@yiranwu0 yiranwu0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good to me overall. My only question is about the context_str class. I think this class might not be necessary at all, and also consider existing packages like dedent.

@marklysze
Copy link
Collaborator Author

Thanks all, merging so we can merge swarm to conversable.

@marklysze marklysze merged commit b390f33 into swarmagenttoconversable Feb 4, 2025
8 of 9 checks passed
@marklysze marklysze deleted the swarmafterworkmgrmsg branch February 4, 2025 19:50
marklysze added a commit that referenced this pull request Feb 5, 2025
* Moved functions and update_agent_state_before_reply parameters and associated functions

Signed-off-by: Mark Sze <[email protected]>

* Move SwarmAgent functions out or to ConversableAgent

Signed-off-by: Mark Sze <[email protected]>

* Move SwarmAgent init to function

Signed-off-by: Mark Sze <[email protected]>

* Updated agent's string representation

Signed-off-by: Mark Sze <[email protected]>

* Update swarm tests and add to ConversableAgent tests

Signed-off-by: Mark Sze <[email protected]>

* Agent string representation, documentation updates for SwarmAgent and register_hand_off

Signed-off-by: Mark Sze <[email protected]>

* Updated test for agent string representation

Signed-off-by: Mark Sze <[email protected]>

* Removed static method for Python 3.9 support

Signed-off-by: Mark Sze <[email protected]>

* Update autogen/agentchat/conversable_agent.py

Co-authored-by: Chi Wang <[email protected]>

* Summary from nested chat refactoring, comment fix

Signed-off-by: Mark Sze <[email protected]>

* UPDATE_SYSTEM_MESSAGE, AFTER_WORK, ON_CONDITION to UpdateSystemMessage / AfterWork / OnCondition

Signed-off-by: Mark Sze <[email protected]>

* Copyright updates

Signed-off-by: Mark Sze <[email protected]>

* add new class UpdateCondition, unit test passed.

* Code formatting

* Code formatting

* add document

* Added UpdateCondition to init

Signed-off-by: Mark Sze <[email protected]>

* Documentation layout

Signed-off-by: Mark Sze <[email protected]>

* SwarmAgent deprecation

Signed-off-by: Mark Sze <[email protected]>

* Updated init

Signed-off-by: Mark Sze <[email protected]>

* Updated comments and UpdateSystemMessage attribute

Signed-off-by: Mark Sze <[email protected]>

* Update autogen/agentchat/__init__.py

Co-authored-by: Chi Wang <[email protected]>

* Update test/agentchat/test_conversable_agent.py

Co-authored-by: Chi Wang <[email protected]>

* Update autogen/agentchat/__init__.py

Co-authored-by: Chi Wang <[email protected]>

* Remove files from main that have been removed

Signed-off-by: Mark Sze <[email protected]>

* Swarm: User-defined AfterWork string for agent selection using LLM (#369)

* Add next_agent_selection_msg

Signed-off-by: Mark Sze <[email protected]>

* Updated next_agent_selection_msg types, group chat constants

Signed-off-by: Mark Sze <[email protected]>

* Prepare group chat for auto speaker selection

Signed-off-by: Mark Sze <[email protected]>

* Documentation

Signed-off-by: Mark Sze <[email protected]>

* Add swarm_manager_args

Signed-off-by: Mark Sze <[email protected]>

* Updated initiate_swarm_chat in documentation

Signed-off-by: Mark Sze <[email protected]>

* Restore groupchat prompt template

Signed-off-by: Mark Sze <[email protected]>

* constant name change, callable updated to be final string, documentation

Signed-off-by: Mark Sze <[email protected]>

* Introduce ContextStr, remove UpdateCondition, add documentation

Signed-off-by: Mark Sze <[email protected]>

* Update init

Signed-off-by: Mark Sze <[email protected]>

* Documentation

Signed-off-by: Mark Sze <[email protected]>

---------

Signed-off-by: Mark Sze <[email protected]>
Co-authored-by: Yiran Wu <[email protected]>

* Test updates

Signed-off-by: Mark Sze <[email protected]>

---------

Signed-off-by: Mark Sze <[email protected]>
Co-authored-by: Chi Wang <[email protected]>
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Yiran Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request swarm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants