Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
|
|
1 year ago | |
|---|---|---|
| .. | ||
| docs | 1 year ago | |
| .gitignore | 1 year ago | |
| README.md | 1 year ago | |
| app.py | 1 year ago | |
| requirements.txt | 1 year ago | |
In this sample, we will build a simple chat interface that interacts with a RoundRobinGroupChat team built using the AutoGen AgentChat api.
The app.py script sets up a Chainlit chat interface that communicates with the AutoGen team. When a chat starts, it
async def get_weather(city: str) -> str:
return f"The weather in {city} is 73 degrees and Sunny."
assistant_agent = AssistantAgent(
name="assistant_agent",
tools=[get_weather],
model_client=OpenAIChatCompletionClient(
model="gpt-4o-2024-08-06"))
termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)
team = RoundRobinGroupChat(
participants=[assistant_agent], termination_condition=termination)
To get started, ensure you have setup an API Key. We will be using the OpenAI API for this example.
Ensure you have an OPENAPI API key. Set this key in your environment variables as OPENAI_API_KEY.
Install the required Python packages by running:
pip install -r requirements.txt
app.py script to start the Chainlit server.chainlit run app.py -h
http://localhost:8000 by default.start_chat: Initializes the chat sessionrun_team: Sends the user's query to the team streams the agent responses back to the chat interface.chat: Receives messages from the user and passes them to the run_team function.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.
from typing import Optional
from autogen_core import CancellationToken
from autogen_agentchat.agents import AssistantAgent, UserProxyAgent
from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient
async def chainlit_input_func(prompt: str, cancellation_token: Optional[CancellationToken] = None) -> str:
try:
response = await cl.AskUserMessage(
content=prompt,
author="System",
).send()
return response["output"]
except Exception as e:
raise RuntimeError(f"Failed to get user input: {str(e)}") from e
user_proxy_agent = UserProxyAgent(
name="user_proxy_agent",
input_func=chainlit_input_func,
)
assistant_agent = AssistantAgent(
name="assistant_agent",
model_client=OpenAIChatCompletionClient(
model="gpt-4o-2024-08-06"))
termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)
team = RoundRobinGroupChat(
participants=[user_proxy_agent, assistant_agent],
termination_condition=termination)
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:
This is a mirror of AutoGen from GitHub. AutoGen is a framework that enables the development of LLM applications using multiple agents that can converse with each other to solve tasks.
Python SVG Jupyter Notebook C# TSX other