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.

app.py 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import chainlit as cl
  2. from autogen_agentchat.agents import AssistantAgent
  3. from autogen_agentchat.base import TaskResult
  4. from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
  5. from autogen_agentchat.teams import RoundRobinGroupChat
  6. from autogen_ext.models.openai import OpenAIChatCompletionClient
  7. async def get_weather(city: str) -> str:
  8. return f"The weather in {city} is 73 degrees and Sunny."
  9. @cl.on_chat_start # type: ignore
  10. async def start_chat():
  11. cl.user_session.set("prompt_history", "") # type: ignore
  12. async def run_team(query: str):
  13. assistant_agent = AssistantAgent(
  14. name="assistant_agent", tools=[get_weather], model_client=OpenAIChatCompletionClient(model="gpt-4o-2024-08-06")
  15. )
  16. termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)
  17. team = RoundRobinGroupChat(participants=[assistant_agent], termination_condition=termination)
  18. response_stream = team.run_stream(task=query)
  19. async for msg in response_stream:
  20. if hasattr(msg, "content"):
  21. cl_msg = cl.Message(content=msg.content, author="Agent Team") # type: ignore
  22. await cl_msg.send()
  23. if isinstance(msg, TaskResult):
  24. cl_msg = cl.Message(content="Termination condition met. Team and Agents are reset.", author="Agent Team")
  25. await cl_msg.send()
  26. @cl.on_message # type: ignore
  27. async def chat(message: cl.Message):
  28. await run_team(message.content) # type: ignore