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.4 kB

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