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_team.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from typing import List, cast
  2. import chainlit as cl
  3. import yaml
  4. from autogen_agentchat.agents import AssistantAgent
  5. from autogen_agentchat.base import TaskResult
  6. from autogen_agentchat.conditions import TextMentionTermination
  7. from autogen_agentchat.messages import ModelClientStreamingChunkEvent, TextMessage
  8. from autogen_agentchat.teams import RoundRobinGroupChat
  9. from autogen_core import CancellationToken
  10. from autogen_core.models import ChatCompletionClient
  11. @cl.on_chat_start # type: ignore
  12. async def start_chat() -> None:
  13. # Load model configuration and create the model client.
  14. with open("model_config.yaml", "r") as f:
  15. model_config = yaml.safe_load(f)
  16. model_client = ChatCompletionClient.load_component(model_config)
  17. # Create the assistant agent.
  18. assistant = AssistantAgent(
  19. name="assistant",
  20. model_client=model_client,
  21. system_message="You are a helpful assistant.",
  22. model_client_stream=True, # Enable model client streaming.
  23. )
  24. # Create the critic agent.
  25. critic = AssistantAgent(
  26. name="critic",
  27. model_client=model_client,
  28. system_message="You are a critic. Provide constructive feedback. "
  29. "Respond with 'APPROVE' if your feedback has been addressed.",
  30. model_client_stream=True, # Enable model client streaming.
  31. )
  32. # Termination condition.
  33. termination = TextMentionTermination("APPROVE", sources=["critic"])
  34. # Chain the assistant and critic agents using RoundRobinGroupChat.
  35. group_chat = RoundRobinGroupChat([assistant, critic], termination_condition=termination)
  36. # Set the assistant agent in the user session.
  37. cl.user_session.set("prompt_history", "") # type: ignore
  38. cl.user_session.set("team", group_chat) # type: ignore
  39. @cl.set_starters # type: ignore
  40. async def set_starts() -> List[cl.Starter]:
  41. return [
  42. cl.Starter(
  43. label="Poem Writing",
  44. message="Write a poem about the ocean.",
  45. ),
  46. cl.Starter(
  47. label="Story Writing",
  48. message="Write a story about a detective solving a mystery.",
  49. ),
  50. cl.Starter(
  51. label="Write Code",
  52. message="Write a function that merge two list of numbers into single sorted list.",
  53. ),
  54. ]
  55. @cl.on_message # type: ignore
  56. async def chat(message: cl.Message) -> None:
  57. # Get the team from the user session.
  58. team = cast(RoundRobinGroupChat, cl.user_session.get("team")) # type: ignore
  59. # Streaming response message.
  60. streaming_response: cl.Message | None = None
  61. # Stream the messages from the team.
  62. async for msg in team.run_stream(
  63. task=[TextMessage(content=message.content, source="user")],
  64. cancellation_token=CancellationToken(),
  65. ):
  66. if isinstance(msg, ModelClientStreamingChunkEvent):
  67. # Stream the model client response to the user.
  68. if streaming_response is None:
  69. # Start a new streaming response.
  70. streaming_response = cl.Message(content="", author=msg.source)
  71. await streaming_response.stream_token(msg.content)
  72. elif streaming_response is not None:
  73. # Done streaming the model client response.
  74. # We can skip the current message as it is just the complete message
  75. # of the streaming response.
  76. await streaming_response.send()
  77. # Reset the streaming response so we won't enter this block again
  78. # until the next streaming response is complete.
  79. streaming_response = None
  80. elif isinstance(msg, TaskResult):
  81. # Send the task termination message.
  82. final_message = "Task terminated. "
  83. if msg.stop_reason:
  84. final_message += msg.stop_reason
  85. await cl.Message(content=final_message).send()
  86. else:
  87. # Skip all other message types.
  88. pass