Source code for autogen_agentchat.agents._base_chat_agent
from abc import ABC, abstractmethod
from typing import List, Sequence
from autogen_core.base import CancellationToken
from autogen_core.components import FunctionCall, Image
from autogen_core.components.models import FunctionExecutionResult
from autogen_core.components.tools import Tool
from pydantic import BaseModel
[docs]
class BaseMessage(BaseModel):
"""A base message."""
source: str
"""The name of the agent that sent this message."""
[docs]
class TextMessage(BaseMessage):
"""A text message."""
content: str
"""The content of the message."""
[docs]
class MultiModalMessage(BaseMessage):
"""A multimodal message."""
content: List[str | Image]
"""The content of the message."""
[docs]
class StopMessage(BaseMessage):
"""A message requesting stop of a conversation."""
content: str
"""The content for the stop message."""
ChatMessage = TextMessage | MultiModalMessage | StopMessage | ToolCallMessage | ToolCallResultMessage
"""A message used by agents in a team."""
[docs]
class BaseChatAgent(ABC):
"""Base class for a chat agent that can participant in a team."""
def __init__(self, name: str, description: str) -> None:
self._name = name
if self._name.isidentifier() is False:
raise ValueError("The agent name must be a valid Python identifier.")
self._description = description
@property
def name(self) -> str:
"""The name of the agent. This is used by team to uniquely identify
the agent. It should be unique within the team."""
return self._name
@property
def description(self) -> str:
"""The description of the agent. This is used by team to
make decisions about which agents to use. The description should
describe the agent's capabilities and how to interact with it."""
return self._description
[docs]
@abstractmethod
async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> ChatMessage:
"""Handle incoming messages and return a response message."""
...