autogen/python/samples/core_distributed-group-chat
湛露先生 687946258f
Clean chess examples. (#6203)
Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
2025-04-04 10:57:40 -07:00
..
public Move core samples to /python/samples (#4911) 2025-01-07 23:31:29 +00:00
.gitignore Add dependencies to distributed group chat example (#5175) 2025-01-24 08:49:53 -05:00
README.md docs(core_distributed-group-chat): fix the typos in the docs in the README.md (#5347) 2025-02-04 07:13:03 +00:00
_agents.py Move core samples to /python/samples (#4911) 2025-01-07 23:31:29 +00:00
_types.py Improve docs for model clients, add missing docs (#4930) 2025-01-08 09:12:48 -05:00
_utils.py Improve docs for model clients, add missing docs (#4930) 2025-01-08 09:12:48 -05:00
config.yaml Move core samples to /python/samples (#4911) 2025-01-07 23:31:29 +00:00
run.sh Move core samples to /python/samples (#4911) 2025-01-07 23:31:29 +00:00
run_editor_agent.py Properly close model clients in documentation and samples (#5898) 2025-03-20 07:50:14 +00:00
run_group_chat_manager.py Properly close model clients in documentation and samples (#5898) 2025-03-20 07:50:14 +00:00
run_host.py Move core samples to /python/samples (#4911) 2025-01-07 23:31:29 +00:00
run_ui.py fix: Make race condition between channel open and RPC less likely to occur (#5514) 2025-02-12 16:40:52 -05:00
run_writer_agent.py Clean chess examples. (#6203) 2025-04-04 10:57:40 -07:00

README.md

Distributed Group Chat

This example runs a gRPC server using GrpcWorkerAgentRuntimeHost and instantiates three distributed runtimes using GrpcWorkerAgentRuntime. These runtimes connect to the gRPC server as hosts and facilitate a round-robin distributed group chat. This example leverages the Azure OpenAI Service to implement writer and editor LLM agents. Agents are instructed to provide concise answers, as the primary goal of this example is to showcase the distributed runtime rather than the quality of agent responses.

Setup

Setup Python Environment

  1. Create a virtual environment and activate it. (e.g. python3.12 -m venv .venv && source .venv/bin/activate)
  2. Install dependencies.
pip install "autogen-ext[openai,azure,chainlit,rich,pyyaml]"

General Configuration

In the config.yaml file, you can configure the client_config section to connect the code to the Azure OpenAI Service.

Authentication

The recommended method for authentication is through Azure Active Directory (AAD), as explained in Model Clients - Azure AI. This example works with both the AAD approach (recommended) and by providing the api_key in the config.yaml file.

Run

Run Through Scripts

The run.sh file provides commands to run the host and agents using tmux. The steps for this approach are:

  1. Install tmux.
  2. Activate the Python environment: source .venv/bin/activate.
  3. Run the bash script: ./run.sh.

Here is a screen recording of the execution:

Distributed Group Chat Demo with Simple UI Integration

Note: Some asyncio.sleep commands have been added to the example code to make the ./run.sh execution look sequential and visually easy to follow. In practice, these lines are not necessary.

Run Individual Files

If you prefer to run Python files individually, follow these steps. Note that each step must be run in a different terminal process, and the virtual environment should be activated using source .venv/bin/activate.

  1. python run_host.py: Starts the host and listens for agent connections.
  2. chainlit run run_ui.py --port 8001: Starts the Chainlit app and UI agent and listens on UI topic to display messages. We're using port 8001 as the default port 8000 is used to run host (assuming using same machine to run all of the agents)
  3. python run_editor_agent.py: Starts the editor agent and connects it to the host.
  4. python run_writer_agent.py: Starts the writer agent and connects it to the host.
  5. python run_group_chat_manager.py: Run chainlit app which starts group chat manager agent and sends the initial message to start the conversation.

What's Going On?

The general flow of this example is as follows:

  1. The UI Agent runs starts the UI App, listens for stream of messages in the UI topic and displays them in the UI.
  2. The Group Chat Manager, on behalf of User, sends a RequestToSpeak request to the writer_agent.
  3. The writer_agent writes a short sentence into the group chat topic.
  4. The editor_agent receives the message in the group chat topic and updates its memory.
  5. The Group Chat Manager receives the message sent by the writer into the group chat simultaneously and sends the next participant, the editor_agent, a RequestToSpeak message.
  6. The editor_agent sends its feedback to the group chat topic.
  7. The writer_agent receives the feedback and updates its memory.
  8. The Group Chat Manager receives the message simultaneously and repeats the loop from step 1.

Here is an illustration of the system developed in this example:

graph TD;
    subgraph Host
        A1[GRPC Server]
        wt[Writer Topic]
        et[Editor Topic]
        ut[UI Topic]
        gct[Group Chat Topic]
    end
    all_agents[All Agents -  Simplified Arrows!] --> A1

    subgraph Distributed Writer Runtime
        wt -.->|2 - Subscription| writer_agent
        gct -.->|4 - Subscription| writer_agent
        writer_agent -.->|3.1 - Publish: UI Message| ut
        writer_agent -.->|3.2 - Publish: Group Chat Message| gct
    end

    subgraph Distributed Editor Runtime
        et -.->|6 - Subscription| editor_agent
        gct -.->|4 - Subscription| editor_agent
        editor_agent -.->|7.1 - Publish: UI Message| ut
        editor_agent -.->|7.2 - Publish: Group Chat Message| gct
    end

    subgraph Distributed Group Chat Manager Runtime
        gct -.->|4 - Subscription| group_chat_manager
        group_chat_manager -.->|1 - Request To Speak| wt
        group_chat_manager -.->|5 - Request To Speak| et
        group_chat_manager -.->|\* - Publish Some of to UI Message| ut
    end

    subgraph Distributed UI Runtime
        ut -.->|\* - Subscription| ui_agent
    end


    style wt fill:#beb2c3,color:#000
    style et fill:#beb2c3,color:#000
    style gct fill:#beb2c3,color:#000
    style ut fill:#beb2c3,color:#000
    style writer_agent fill:#b7c4d7,color:#000
    style editor_agent fill:#b7c4d7,color:#000
    style group_chat_manager fill:#b7c4d7,color:#000
    style ui_agent fill:#b7c4d7,color:#000

TODO:

  • Properly handle chat restarts. It complains about group chat manager being already registered
  • Add streaming to the UI like this example when this bug is resolved