autogen/python/packages/autogen-agentchat/tests
Eric Zhu 7e5c1154cf
Support for external agent runtime in AgentChat (#5843)
Resolves #4075

1. Introduce custom runtime parameter for all AgentChat teams
(RoundRobinGroupChat, SelectorGroupChat, etc.). This is done by making
sure each team's topics are isolated from other teams, and decoupling
state from agent identities. Also, I removed the closure agent from the
BaseGroupChat and use the group chat manager agent to relay messages to
the output message queue.
2. Added unit tests to test scenarios with custom runtimes by using
pytest fixture
3. Refactored existing unit tests to use ReplayChatCompletionClient with
a few improvements to the client.
4. Fix a one-liner bug in AssistantAgent that caused deserialized agent
to have handoffs.

How to use it? 

```python
import asyncio
from autogen_core import SingleThreadedAgentRuntime
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
from autogen_ext.models.replay import ReplayChatCompletionClient

async def main() -> None:
    # Create a runtime
    runtime = SingleThreadedAgentRuntime()
    runtime.start()

    # Create a model client.
    model_client = ReplayChatCompletionClient(
        ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
    )

    # Create agents
    agent1 = AssistantAgent("assistant1", model_client=model_client, system_message="You are a helpful assistant.")
    agent2 = AssistantAgent("assistant2", model_client=model_client, system_message="You are a helpful assistant.")

    # Create a termination condition
    termination_condition = TextMentionTermination("10", sources=["assistant1", "assistant2"])

    # Create a team
    team = RoundRobinGroupChat([agent1, agent2], runtime=runtime, termination_condition=termination_condition)

    # Run the team
    stream = team.run_stream(task="Count to 10.")
    async for message in stream:
        print(message)
    
    # Save the state.
    state = await team.save_state()

    # Load the state to an existing team.
    await team.load_state(state)

    # Run the team again
    model_client.reset()
    stream = team.run_stream(task="Count to 10.")
    async for message in stream:
        print(message)

    # Create a new team, with the same agent names.
    agent3 = AssistantAgent("assistant1", model_client=model_client, system_message="You are a helpful assistant.")
    agent4 = AssistantAgent("assistant2", model_client=model_client, system_message="You are a helpful assistant.")
    new_team = RoundRobinGroupChat([agent3, agent4], runtime=runtime, termination_condition=termination_condition)

    # Load the state to the new team.
    await new_team.load_state(state)

    # Run the new team
    model_client.reset()
    new_stream = new_team.run_stream(task="Count to 10.")
    async for message in new_stream:
        print(message)
    
    # Stop the runtime
    await runtime.stop()

asyncio.run(main())
```

TODOs as future PRs:
1. Documentation.
2. How to handle errors in custom runtime when the agent has exception?

---------

Co-authored-by: Ryan Sweet <rysweet@microsoft.com>
2025-03-06 10:32:52 -08:00
..
test_assistant_agent.py Support for external agent runtime in AgentChat (#5843) 2025-03-06 10:32:52 -08:00
test_code_executor_agent.py Make FileSurfer and CodeExecAgent Declarative (#5765) 2025-03-01 15:46:30 +00:00
test_declarative_components.py Declarative BaseChat Agents (#5055) 2025-01-16 22:29:40 -08:00
test_group_chat.py Support for external agent runtime in AgentChat (#5843) 2025-03-06 10:32:52 -08:00
test_group_chat_endpoint.py feat: enhance Gemini model support in OpenAI client and tests (#5461) 2025-02-09 10:12:59 -08:00
test_magentic_one_group_chat.py Support for external agent runtime in AgentChat (#5843) 2025-03-06 10:32:52 -08:00
test_sequential_routed_agent.py Move grpc runtimes to ext, flatten application (#4553) 2024-12-04 16:23:20 -08:00
test_society_of_mind_agent.py Support for external agent runtime in AgentChat (#5843) 2025-03-06 10:32:52 -08:00
test_termination_condition.py feat: Add FunctionCallTermination condition (#5808) 2025-03-04 03:26:47 +00:00
test_userproxy_agent.py Flatten core base and components (#4513) 2024-12-03 17:00:44 -08:00
test_utils.py Assistant agent drop images when not provided with a vision-capable model. (#5351) 2025-02-04 14:55:04 +00:00
utils.py Remove logging from autogen agentchat (#4510) 2024-12-03 14:45:10 -08:00