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 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Building a Multi-Agent Application with AutoGen and Chainlit
  2. In this sample, we will build a simple chat interface that interacts with a `RoundRobinGroupChat` team built using the [AutoGen AgentChat](https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/index.html) api.
  3. ![AgentChat](docs/chainlit_autogen.png).
  4. ## High-Level Description
  5. The `app.py` script sets up a Chainlit chat interface that communicates with the AutoGen team. When a chat starts, it
  6. - Initializes an AgentChat team.
  7. ```python
  8. async def get_weather(city: str) -> str:
  9. return f"The weather in {city} is 73 degrees and Sunny."
  10. assistant_agent = AssistantAgent(
  11. name="assistant_agent",
  12. tools=[get_weather],
  13. model_client=OpenAIChatCompletionClient(
  14. model="gpt-4o-2024-08-06"))
  15. termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)
  16. team = RoundRobinGroupChat(
  17. participants=[assistant_agent], termination_condition=termination)
  18. ```
  19. - As users interact with the chat, their queries are sent to the team which responds.
  20. - As agents respond/act, their responses are streamed back to the chat interface.
  21. ## Quickstart
  22. To get started, ensure you have setup an API Key. We will be using the OpenAI API for this example.
  23. 1. Ensure you have an OPENAPI API key. Set this key in your environment variables as `OPENAI_API_KEY`.
  24. 2. Install the required Python packages by running:
  25. ```shell
  26. pip install -r requirements.txt
  27. ```
  28. 3. Run the `app.py` script to start the Chainlit server.
  29. ```shell
  30. chainlit run app.py -h
  31. ```
  32. 4. Interact with the Agent Team Chainlit interface. The chat interface will be available at `http://localhost:8000` by default.
  33. ### Function Definitions
  34. - `start_chat`: Initializes the chat session
  35. - `run_team`: Sends the user's query to the team streams the agent responses back to the chat interface.
  36. - `chat`: Receives messages from the user and passes them to the `run_team` function.
  37. ## Adding a UserProxyAgent
  38. We can add a `UserProxyAgent` to the team so that the user can interact with the team directly with the input box in the chat interface. This requires defining a function for input that uses the Chainlit input box instead of the terminal.
  39. ```python
  40. from typing import Optional
  41. from autogen_core import CancellationToken
  42. from autogen_agentchat.agents import AssistantAgent, UserProxyAgent
  43. from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination
  44. from autogen_agentchat.teams import RoundRobinGroupChat
  45. from autogen_ext.models.openai import OpenAIChatCompletionClient
  46. async def chainlit_input_func(prompt: str, cancellation_token: Optional[CancellationToken] = None) -> str:
  47. try:
  48. response = await cl.AskUserMessage(
  49. content=prompt,
  50. author="System",
  51. ).send()
  52. return response["output"]
  53. except Exception as e:
  54. raise RuntimeError(f"Failed to get user input: {str(e)}") from e
  55. user_proxy_agent = UserProxyAgent(
  56. name="user_proxy_agent",
  57. input_func=chainlit_input_func,
  58. )
  59. assistant_agent = AssistantAgent(
  60. name="assistant_agent",
  61. model_client=OpenAIChatCompletionClient(
  62. model="gpt-4o-2024-08-06"))
  63. termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)
  64. team = RoundRobinGroupChat(
  65. participants=[user_proxy_agent, assistant_agent],
  66. termination_condition=termination)
  67. ```
  68. ## Next Steps (Extra Credit)
  69. In this example, we created a basic AutoGen team with a single agent in a RoundRobinGroupChat team. There are a few ways you can extend this example:
  70. - Add more [agents](https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/agents.html) to the team.
  71. - Explor custom agents that sent multimodal messages
  72. - Explore more [team](https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/teams.html) types beyond the `RoundRobinGroupChat`.