🛠️ 命令¶
命令是 Agent 完成任何任务的方式;例如,与用户或 API 交互以及使用工具。它们由实现 CommandProvider
⚙️ 协议的组件提供。命令是可以由 Agent 调用的函数,它们可以有参数和返回值,这些将被 Agent 看到。
class CommandProvider(Protocol):
def get_commands(self) -> Iterator[Command]:
...
command
装饰器¶
提供命令最简单且推荐的方式是在组件方法上使用 command
装饰器,然后在 get_commands
中作为提供者的一部分 yield 它。每个命令都需要一个名称、描述和一个参数模式 - JSONSchema
。默认情况下,方法名用作命令名,文档字符串的第一部分(在第一个双换行符之前)用作描述,模式可以在装饰器中提供。
command
装饰器的使用示例¶
# Assuming this is inside some component class
@command(
parameters={
"a": JSONSchema(
type=JSONSchema.Type.INTEGER,
description="The first number",
required=True,
),
"b": JSONSchema(
type=JSONSchema.Type.INTEGER,
description="The second number",
required=True,
)})
def multiply(self, a: int, b: int) -> str:
"""
Multiplies two numbers.
Args:
a: First number
b: Second number
Returns:
Result of multiplication
"""
return str(a * b)
Agent 将能够调用此命令,该命令名为 multiply
,带有两个参数,并且将收到结果。该命令的描述将是:Multiplies two numbers.
我们可以在装饰器中提供 names
和 description
,上面的命令等同于
@command(
names=["multiply"],
description="Multiplies two numbers.",
parameters={
"a": JSONSchema(
type=JSONSchema.Type.INTEGER,
description="The first number",
required=True,
),
"b": JSONSchema(
type=JSONSchema.Type.INTEGER,
description="The second number",
required=True,
)})
def multiply_command(self, a: int, b: int) -> str:
return str(a * b)
要将 multiply
命令提供给 Agent,我们需要在 get_commands
中 yield 它
def get_commands(self) -> Iterator[Command]:
yield self.multiply
直接创建 Command
¶
如果您不想使用装饰器,可以直接创建一个 Command
对象。
def multiply(self, a: int, b: int) -> str:
return str(a * b)
def get_commands(self) -> Iterator[Command]:
yield Command(
names=["multiply"],
description="Multiplies two numbers.",
method=self.multiply,
parameters=[
CommandParameter(name="a", spec=JSONSchema(
type=JSONSchema.Type.INTEGER,
description="The first number",
required=True,
)),
CommandParameter(name="b", spec=JSONSchema(
type=JSONSchema.Type.INTEGER,
description="The second number",
required=True,
)),
],
)