![]() Introduce a sample chat application using AgentChat and FastAPI, demonstrating single-agent and team chat functionalities, along with state persistence and conversation history management. Resolves #5423 --------- Co-authored-by: Victor Dibia <victor.dibia@gmail.com> Co-authored-by: Victor Dibia <victordibia@microsoft.com> |
||
---|---|---|
.. | ||
.gitignore | ||
README.md | ||
app_agent.html | ||
app_agent.py | ||
app_team.html | ||
app_team.py | ||
model_config_template.yaml |
README.md
AgentChat App with FastAPI
This sample demonstrates how to create a simple chat application using AgentChat and FastAPI.
You will be using the following features of AgentChat:
- Agent:
AssistantAgent
UserProxyAgent
with a custom websocket input function
- Team:
RoundRobinGroupChat
- State persistence:
save_state
andload_state
methods of both agent and team.
Setup
Install the required packages with OpenAI support:
pip install -U "autogen-ext[openai]" "fastapi" "uvicorn" "PyYAML"
To use models other than OpenAI, see the Models documentation.
Create a new file named model_config.yaml
in the same directory as this README file to configure your model settings.
See model_config_template.yaml
for an example.
Chat with a single agent
To start the FastAPI server for single-agent chat, run:
python app_agent.py
Visit http://localhost:8001 in your browser to start chatting.
Chat with a team of agents
To start the FastAPI server for team chat, run:
python app_team.py
Visit http://localhost:8002 in your browser to start chatting.
The team also includes a UserProxyAgent
agent with a custom websocket input function
that allows the user to send messages to the team from the browser.
The team follows a round-robin strategy so each agent will take turns to respond. When it is the user's turn, the input box will be enabled. Once the user sends a message, the input box will be disabled and the agents will take turns to respond.
State persistence
The agents and team use the load_state
and save_state
methods to load and save
their state from and to files on each turn.
For the agent, the state is saved to and loaded from agent_state.json
.
For the team, the state is saved to and loaded from team_state.json
.
You can inspect the state files to see the state of the agents and team
once you have chatted with them.
When the server restarts, the agents and team will load their state from the state files to maintain their state across restarts.
Additionally, the apps uses separate JSON files,
agent_history.json
and team_history.json
, to store the conversation history
for display in the browser.