Skip to main content

agentchat.agent

Agent(代理)

@runtime_checkable
class Agent(Protocol)

(预览版)Agent 的协议。

代理可以与其他代理进行通信并执行操作。 不同的代理在 receive 方法中执行的操作可能不同。

name(名称)

@property
def name() -> str

代理的名称。

description(描述)

@property
def description() -> str

代理的描述。用于在群聊设置中介绍代理。

send(发送)

def send(message: Union[Dict[str, Any], str],
recipient: "Agent",
request_reply: Optional[bool] = None) -> None

向另一个代理发送消息。

参数

  • message dict 或 str - 要发送的消息。如果是字典,则应为可序列化为 JSON 并遵循 OpenAI 的 ChatCompletion 模式。
  • recipient Agent - 消息的接收者。
  • request_reply bool - 是否请求接收者回复。

a_send(异步发送)

async def a_send(message: Union[Dict[str, Any], str],
recipient: "Agent",
request_reply: Optional[bool] = None) -> None

(异步)向另一个代理发送消息。

参数

  • message dict 或 str - 要发送的消息。如果是字典,则应为可序列化为 JSON 并遵循 OpenAI 的 ChatCompletion 模式。
  • recipient Agent - 消息的接收者。
  • request_reply bool - 是否请求接收者回复。

receive(接收)

def receive(message: Union[Dict[str, Any], str],
sender: "Agent",
request_reply: Optional[bool] = None) -> None

从另一个代理接收消息。

参数

  • message dict 或 str - 接收到的消息。如果是字典,则应为可序列化为 JSON 并遵循 OpenAI 的 ChatCompletion 模式。
  • sender Agent - 消息的发送者。
  • request_reply bool - 发送者是否请求回复。

a_receive(异步接收)

async def a_receive(message: Union[Dict[str, Any], str],
sender: "Agent",
request_reply: Optional[bool] = None) -> None

(异步)从另一个代理接收消息。

参数

  • message dict 或 str - 接收到的消息。如果是字典,则应为可序列化为 JSON 并遵循 OpenAI 的 ChatCompletion 模式。
  • sender Agent - 消息的发送者。
  • request_reply bool - 发送者是否请求回复。

generate_reply(生成回复)

def generate_reply(messages: Optional[List[Dict[str, Any]]] = None,
sender: Optional["Agent"] = None,
**kwargs: Any) -> Union[str, Dict[str, Any], None]

根据接收到的消息生成回复。

参数

  • messages list[dict] - 从其他代理接收到的消息列表。 这些消息是可序列化为 JSON 并遵循 OpenAI 的 ChatCompletion 模式的字典。
  • sender - Agent 实例的发送者。

返回值

str 或 dict 或 None:生成的回复。如果为 None,则不生成回复。

a_generate_reply

async def a_generate_reply(messages: Optional[List[Dict[str, Any]]] = None,
sender: Optional["Agent"] = None,
**kwargs: Any) -> Union[str, Dict[str, Any], None]

(异步)根据接收到的消息生成回复。

参数

  • messages list[dict] - 从其他 Agent 接收到的消息列表。 消息是可 JSON 序列化的字典,遵循 OpenAI 的 ChatCompletion 模式。
  • sender - Agent 实例的发送者。

返回值

str 或 dict 或 None:生成的回复。如果为 None,则不生成回复。

LLMAgent

@runtime_checkable
class LLMAgent(Agent, Protocol)

(预览版)LLM Agent 的协议。

system_message

@property
def system_message() -> str

该 Agent 的系统消息。

update_system_message

def update_system_message(system_message: str) -> None

更新该 Agent 的系统消息。

参数

  • system_message str - 推理的系统消息。