You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Distributed Group Chat
  2. from autogen_core.application import WorkerAgentRuntimeHost
  3. This example runs a gRPC server using [WorkerAgentRuntimeHost](../../src/autogen_core/application/_worker_runtime_host.py) and instantiates three distributed runtimes using [WorkerAgentRuntime](../../src/autogen_core/application/_worker_runtime.py). These runtimes connect to the gRPC server as hosts and facilitate a round-robin distributed group chat. This example leverages the [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/ai-services/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.
  4. ## Setup
  5. ### Setup Python Environment
  6. 1. Create a virtual environment as instructed in [README](../../../../../../../../README.md).
  7. 2. Run `uv pip install chainlit` in the same virtual environment
  8. ### General Configuration
  9. In the `config.yaml` file, you can configure the `client_config` section to connect the code to the Azure OpenAI Service.
  10. ### Authentication
  11. The recommended method for authentication is through Azure Active Directory (AAD), as explained in [Model Clients - Azure AI](https://microsoft.github.io/autogen/dev/user-guide/core-user-guide/framework/model-clients.html#azure-openai). This example works with both the AAD approach (recommended) and by providing the `api_key` in the `config.yaml` file.
  12. ## Run
  13. ### Run Through Scripts
  14. The [run.sh](./run.sh) file provides commands to run the host and agents using [tmux](https://github.com/tmux/tmux/wiki). The steps for this approach are:
  15. 1. Install tmux.
  16. 2. Activate the Python environment: `source .venv/bin/activate`.
  17. 3. Run the bash script: `./run.sh`.
  18. Here is a screen recording of the execution:
  19. [![Distributed Group Chat Demo with Simple UI Integration](https://img.youtube.com/vi/503QJ1onV8I/0.jpg)](https://youtu.be/503QJ1onV8I?feature=shared)
  20. **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.
  21. ### Run Individual Files
  22. 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`.
  23. 1. `python run_host.py`: Starts the host and listens for agent connections.
  24. 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)
  25. 3. `python run_editor.py`: Starts the <img src="./public/avatars/editor.png" width="20" height="20" style="vertical-align:middle"> editor agent and connects it to the host.
  26. 4. `python run_writer.py`: Starts the <img src="./public/avatars/writer.png" width="20" height="20" style="vertical-align:middle"> writer agent and connects it to the host.
  27. 5. `python run_group_chat_manager.py`: Run chainlit app which starts <img src="./public/avatars/group_chat_manager.png" width="20" height="20" style="vertical-align:middle"> group chat manager agent and sends the initial message to start the conversation.
  28. ## What's Going On?
  29. The general flow of this example is as follows:
  30. 0. The UI Agent runs starts the UI App, listens for stream of messages in the UI topic and displays them in the UI.
  31. 1. The <img src="./public/avatars/group_chat_manager.png" width="20" height="20" style="vertical-align:middle"> Group Chat Manager, on behalf of <img src="./public/avatars/user.png" width="20" height="20" style="vertical-align:middle"> `User`, sends a `RequestToSpeak` request to the <img src="./public/avatars/writer.png" width="20" height="20" style="vertical-align:middle"> `writer_agent`.
  32. 2. The <img src="./public/avatars/writer.png" width="20" height="20" style="vertical-align:middle"> `writer_agent` writes a short sentence into the group chat topic.
  33. 3. The <img src="./public/avatars/editor.png" width="20" height="20" style="vertical-align:middle"> `editor_agent` receives the message in the group chat topic and updates its memory.
  34. 4. The <img src="./public/avatars/group_chat_manager.png" width="20" height="20" style="vertical-align:middle"> Group Chat Manager receives the message sent by the writer into the group chat simultaneously and sends the next participant, the <img src="./public/avatars/editor.png" width="20" height="20" style="vertical-align:middle"> `editor_agent`, a `RequestToSpeak` message.
  35. 5. The <img src="./public/avatars/editor.png" width="20" height="20" style="vertical-align:middle"> `editor_agent` sends its feedback to the group chat topic.
  36. 6. The <img src="./public/avatars/writer.png" width="20" height="20" style="vertical-align:middle"> `writer_agent` receives the feedback and updates its memory.
  37. 7. The <img src="./public/avatars/group_chat_manager.png" width="20" height="20" style="vertical-align:middle"> Group Chat Manager receives the message simultaneously and repeats the loop from step 1.
  38. Here is an illustration of the system developed in this example:
  39. ```mermaid
  40. graph TD;
  41. subgraph Host
  42. A1[GRPC Server]
  43. wt[Writer Topic]
  44. et[Editor Topic]
  45. ut[UI Topic]
  46. gct[Group Chat Topic]
  47. end
  48. all_agents[All Agents - Simplified Arrows!] --> A1
  49. subgraph Distributed Writer Runtime
  50. wt -.->|2 - Subscription| writer_agent
  51. gct -.->|4 - Subscription| writer_agent
  52. writer_agent -.->|3.1 - Publish: UI Message| ut
  53. writer_agent -.->|3.2 - Publish: Group Chat Message| gct
  54. end
  55. subgraph Distributed Editor Runtime
  56. et -.->|6 - Subscription| editor_agent
  57. gct -.->|4 - Subscription| editor_agent
  58. editor_agent -.->|7.1 - Publish: UI Message| ut
  59. editor_agent -.->|7.2 - Publish: Group Chat Message| gct
  60. end
  61. subgraph Distributed Group Chat Manager Runtime
  62. gct -.->|4 - Subscription| group_chat_manager
  63. group_chat_manager -.->|1 - Request To Speak| wt
  64. group_chat_manager -.->|5 - Request To Speak| et
  65. group_chat_manager -.->|\* - Publish Some of to UI Message| ut
  66. end
  67. subgraph Distributed UI Runtime
  68. ut -.->|\* - Subscription| ui_agent
  69. end
  70. style wt fill:#beb2c3,color:#000
  71. style et fill:#beb2c3,color:#000
  72. style gct fill:#beb2c3,color:#000
  73. style ut fill:#beb2c3,color:#000
  74. style writer_agent fill:#b7c4d7,color:#000
  75. style editor_agent fill:#b7c4d7,color:#000
  76. style group_chat_manager fill:#b7c4d7,color:#000
  77. style ui_agent fill:#b7c4d7,color:#000
  78. ```
  79. ## TODO:
  80. - [ ] Properly handle chat restarts. It complains about group chat manager being already registered
  81. - [ ] Add streaming to the UI like [this example](https://docs.chainlit.io/advanced-features/streaming) when [this bug](https://github.com/microsoft/autogen/issues/4213) is resolved