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.

run_ui.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import asyncio
  2. import logging
  3. import warnings
  4. import chainlit as cl # type: ignore [reportUnknownMemberType] # This dependency is installed through instructions
  5. from _agents import MessageChunk, UIAgent
  6. from _types import AppConfig, GroupChatMessage, RequestToSpeak
  7. from _utils import get_serializers, load_config, set_all_log_levels
  8. from autogen_core import (
  9. TypeSubscription,
  10. )
  11. from autogen_ext.runtimes.grpc import GrpcWorkerAgentRuntime
  12. from chainlit import Message # type: ignore [reportAttributeAccessIssue]
  13. from rich.console import Console
  14. from rich.markdown import Markdown
  15. set_all_log_levels(logging.ERROR)
  16. message_chunks: dict[str, Message] = {} # type: ignore [reportUnknownVariableType]
  17. async def send_cl_stream(msg: MessageChunk) -> None:
  18. if msg.message_id not in message_chunks:
  19. message_chunks[msg.message_id] = Message(content="", author=msg.author)
  20. if not msg.finished:
  21. await message_chunks[msg.message_id].stream_token(msg.text) # type: ignore [reportUnknownVariableType]
  22. else:
  23. await message_chunks[msg.message_id].stream_token(msg.text) # type: ignore [reportUnknownVariableType]
  24. await message_chunks[msg.message_id].update() # type: ignore [reportUnknownMemberType]
  25. await asyncio.sleep(3)
  26. cl_msg = message_chunks[msg.message_id] # type: ignore [reportUnknownVariableType]
  27. await cl_msg.send() # type: ignore [reportUnknownMemberType]
  28. async def main(config: AppConfig):
  29. set_all_log_levels(logging.ERROR)
  30. ui_agent_runtime = GrpcWorkerAgentRuntime(host_address=config.host.address)
  31. ui_agent_runtime.add_message_serializer(get_serializers([RequestToSpeak, GroupChatMessage, MessageChunk])) # type: ignore[arg-type]
  32. Console().print(Markdown("Starting **`UI Agent`**"))
  33. ui_agent_runtime.start()
  34. set_all_log_levels(logging.ERROR)
  35. ui_agent_type = await UIAgent.register(
  36. ui_agent_runtime,
  37. "ui_agent",
  38. lambda: UIAgent(
  39. on_message_chunk_func=send_cl_stream,
  40. ),
  41. )
  42. await ui_agent_runtime.add_subscription(
  43. TypeSubscription(topic_type=config.ui_agent.topic_type, agent_type=ui_agent_type.type)
  44. ) # TODO: This could be a great example of using agent_id to route to sepecific element in the ui. Can replace MessageChunk.message_id
  45. await ui_agent_runtime.stop_when_signal()
  46. Console().print("UI Agent left the chat!")
  47. @cl.on_chat_start # type: ignore
  48. async def start_chat():
  49. set_all_log_levels(logging.ERROR)
  50. warnings.filterwarnings("ignore", category=UserWarning, message="Resolved model mismatch.*")
  51. asyncio.run(main(load_config()))