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.

agent.py 952 B

1234567891011121314151617181920212223242526
  1. import yaml
  2. from autogen_agentchat.agents import AssistantAgent
  3. from autogen_agentchat.messages import TextMessage
  4. from autogen_core import CancellationToken
  5. from autogen_core.models import ChatCompletionClient
  6. class Agent:
  7. def __init__(self) -> None:
  8. # Load the model client from config.
  9. with open("model_config.yml", "r") as f:
  10. model_config = yaml.safe_load(f)
  11. model_client = ChatCompletionClient.load_component(model_config)
  12. self.agent = AssistantAgent(
  13. name="assistant",
  14. model_client=model_client,
  15. system_message="You are a helpful AI assistant.",
  16. )
  17. async def chat(self, prompt: str) -> str:
  18. response = await self.agent.on_messages(
  19. [TextMessage(content=prompt, source="user")],
  20. CancellationToken(),
  21. )
  22. assert isinstance(response.chat_message, TextMessage)
  23. return response.chat_message.content