autogen_core.models#

pydantic 模型 AssistantMessage[源代码]#

基类: BaseModel

助手消息从语言模型中采样。

显示 JSON 模式
{
   "title": "AssistantMessage",
   "description": "Assistant message are sampled from the language model.",
   "type": "object",
   "properties": {
      "content": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "items": {
                  "$ref": "#/$defs/FunctionCall"
               },
               "type": "array"
            }
         ],
         "title": "Content"
      },
      "thought": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Thought"
      },
      "source": {
         "title": "Source",
         "type": "string"
      },
      "type": {
         "const": "AssistantMessage",
         "default": "AssistantMessage",
         "title": "Type",
         "type": "string"
      }
   },
   "$defs": {
      "FunctionCall": {
         "properties": {
            "id": {
               "title": "Id",
               "type": "string"
            },
            "arguments": {
               "title": "Arguments",
               "type": "string"
            },
            "name": {
               "title": "Name",
               "type": "string"
            }
         },
         "required": [
            "id",
            "arguments",
            "name"
         ],
         "title": "FunctionCall",
         "type": "object"
      }
   },
   "required": [
      "content",
      "source"
   ]
}

字段:
  • content (str | List[autogen_core._types.FunctionCall])

  • source (str)

  • thought (str | None)

  • type (Literal['AssistantMessage'])

field content: str | List[FunctionCall] [必需]#

消息的内容。

field source: str [必需]#

发送此消息的代理的名称。

field thought: str | None = None#

如果可用,则为完成的推理文本。 用于推理模型以及函数调用之外的其他文本内容。

field type: Literal['AssistantMessage'] = 'AssistantMessage'#
class ChatCompletionClient[源代码]#

基类: ComponentBase[BaseModel], ABC

abstract actual_usage() RequestUsage[源代码]#
abstract property capabilities: ModelCapabilities#
abstract async close() None[源代码]#
abstract count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[源代码]#
abstract async create(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], json_output: bool | type[BaseModel] | None = None, extra_create_args: Mapping[str, Any] = {}, cancellation_token: CancellationToken | None = None) CreateResult[source]#

从模型创建一个单一的响应。

参数:
  • messages (Sequence[LLMMessage]) – 发送给模型的消息。

  • tools (Sequence[Tool | ToolSchema], optional) – 与模型一起使用的工具。默认为 []。

  • json_output (Optional[bool | type[BaseModel]], optional) – 是否使用 JSON 模式、结构化输出或两者都不使用。 默认为 None。 如果设置为 Pydantic BaseModel 类型,它将用作结构化输出的输出类型。如果设置为布尔值,它将用于确定是否使用 JSON 模式。 如果设置为 True,请确保指示模型在指令或提示中生成 JSON 输出。

  • extra_create_args (Mapping[str, Any], optional) – 传递给底层客户端的额外参数。默认为 {}。

  • cancellation_token (Optional[CancellationToken], optional) – 用于取消的令牌。默认为 None。

返回值:

CreateResult – 模型调用的结果。

abstract create_stream(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], json_output: bool | type[BaseModel] | None = None, extra_create_args: Mapping[str, Any] = {}, cancellation_token: CancellationToken | None = None) AsyncGenerator[str | CreateResult, None][source]#

创建一个来自模型的字符串块的流,以 CreateResult 结尾。

参数:
  • messages (Sequence[LLMMessage]) – 发送给模型的消息。

  • tools (Sequence[Tool | ToolSchema], optional) – 与模型一起使用的工具。默认为 []。

  • json_output (Optional[bool | type[BaseModel]], optional) –

    是否使用 JSON 模式、结构化输出或两者都不使用。默认为 None。 如果设置为 Pydantic BaseModel 类型,它将用作结构化输出的输出类型。如果设置为布尔值,它将用于确定是否使用 JSON 模式。如果设置为 True,请确保指示模型在指令或提示中生成 JSON 输出。

  • extra_create_args (Mapping[str, Any], optional) – 传递给底层客户端的额外参数。默认为 {}。

  • cancellation_token (Optional[CancellationToken], optional) – 用于取消的令牌。默认为 None。

返回值:

AsyncGenerator[Union[str, CreateResult], None] – 一个生成字符串块并以 CreateResult 结尾的生成器。

abstract property model_info: ModelInfo#
abstract remaining_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[source]#
abstract total_usage() RequestUsage[source]#
pydantic model ChatCompletionTokenLogprob[source]#

基类: BaseModel

显示 JSON 模式
{
   "title": "ChatCompletionTokenLogprob",
   "type": "object",
   "properties": {
      "token": {
         "title": "Token",
         "type": "string"
      },
      "logprob": {
         "title": "Logprob",
         "type": "number"
      },
      "top_logprobs": {
         "anyOf": [
            {
               "items": {
                  "$ref": "#/$defs/TopLogprob"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top Logprobs"
      },
      "bytes": {
         "anyOf": [
            {
               "items": {
                  "type": "integer"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Bytes"
      }
   },
   "$defs": {
      "TopLogprob": {
         "properties": {
            "logprob": {
               "title": "Logprob",
               "type": "number"
            },
            "bytes": {
               "anyOf": [
                  {
                     "items": {
                        "type": "integer"
                     },
                     "type": "array"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Bytes"
            }
         },
         "required": [
            "logprob"
         ],
         "title": "TopLogprob",
         "type": "object"
      }
   },
   "required": [
      "token",
      "logprob"
   ]
}

字段:
  • bytes (List[int] | None)

  • logprob (float)

  • token (str)

  • top_logprobs (List[autogen_core.models._types.TopLogprob] | None)

字段 bytes: List[int] | None = None#
字段 logprob: float [必需]#
字段 token: str [必需]#
字段 top_logprobs: List[TopLogprob] | None = None#
pydantic 模型 CreateResult[source]#

基类: BaseModel

创建结果包含模型补全的输出。

显示 JSON 模式
{
   "title": "CreateResult",
   "description": "Create result contains the output of a model completion.",
   "type": "object",
   "properties": {
      "finish_reason": {
         "enum": [
            "stop",
            "length",
            "function_calls",
            "content_filter",
            "unknown"
         ],
         "title": "Finish Reason",
         "type": "string"
      },
      "content": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "items": {
                  "$ref": "#/$defs/FunctionCall"
               },
               "type": "array"
            }
         ],
         "title": "Content"
      },
      "usage": {
         "$ref": "#/$defs/RequestUsage"
      },
      "cached": {
         "title": "Cached",
         "type": "boolean"
      },
      "logprobs": {
         "anyOf": [
            {
               "items": {
                  "$ref": "#/$defs/ChatCompletionTokenLogprob"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Logprobs"
      },
      "thought": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Thought"
      }
   },
   "$defs": {
      "ChatCompletionTokenLogprob": {
         "properties": {
            "token": {
               "title": "Token",
               "type": "string"
            },
            "logprob": {
               "title": "Logprob",
               "type": "number"
            },
            "top_logprobs": {
               "anyOf": [
                  {
                     "items": {
                        "$ref": "#/$defs/TopLogprob"
                     },
                     "type": "array"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Top Logprobs"
            },
            "bytes": {
               "anyOf": [
                  {
                     "items": {
                        "type": "integer"
                     },
                     "type": "array"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Bytes"
            }
         },
         "required": [
            "token",
            "logprob"
         ],
         "title": "ChatCompletionTokenLogprob",
         "type": "object"
      },
      "FunctionCall": {
         "properties": {
            "id": {
               "title": "Id",
               "type": "string"
            },
            "arguments": {
               "title": "Arguments",
               "type": "string"
            },
            "name": {
               "title": "Name",
               "type": "string"
            }
         },
         "required": [
            "id",
            "arguments",
            "name"
         ],
         "title": "FunctionCall",
         "type": "object"
      },
      "RequestUsage": {
         "properties": {
            "prompt_tokens": {
               "title": "Prompt Tokens",
               "type": "integer"
            },
            "completion_tokens": {
               "title": "Completion Tokens",
               "type": "integer"
            }
         },
         "required": [
            "prompt_tokens",
            "completion_tokens"
         ],
         "title": "RequestUsage",
         "type": "object"
      },
      "TopLogprob": {
         "properties": {
            "logprob": {
               "title": "Logprob",
               "type": "number"
            },
            "bytes": {
               "anyOf": [
                  {
                     "items": {
                        "type": "integer"
                     },
                     "type": "array"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Bytes"
            }
         },
         "required": [
            "logprob"
         ],
         "title": "TopLogprob",
         "type": "object"
      }
   },
   "required": [
      "finish_reason",
      "content",
      "usage",
      "cached"
   ]
}

字段:
  • cached (bool)

  • content (str | List[autogen_core._types.FunctionCall])

  • finish_reason (Literal['stop', 'length', 'function_calls', 'content_filter', 'unknown'])

  • logprobs (List[autogen_core.models._types.ChatCompletionTokenLogprob] | None)

  • thought (str | None)

  • usage (autogen_core.models._types.RequestUsage)

字段 cached: bool [必需]#

补全是否从缓存响应生成。

字段 content: str | List[FunctionCall] [必需]#

模型补全的输出。

字段 finish_reason: Literal['stop', 'length', 'function_calls', 'content_filter', 'unknown'] [必需]#

模型完成生成的原因。

字段 logprobs: List[ChatCompletionTokenLogprob] | None = None#

补全中 token 的 logprobs。

字段 thought: str | None = None#

如果可用,则为补全的推理文本。 用于推理模型和函数调用之外的额外文本内容。

字段 usage: RequestUsage [必需]#

提示和补全中 token 的使用情况。

pydantic 模型 FunctionExecutionResult[source]#

基类: BaseModel

函数执行结果包含函数调用的输出。

显示 JSON 模式
{
   "title": "FunctionExecutionResult",
   "description": "Function execution result contains the output of a function call.",
   "type": "object",
   "properties": {
      "content": {
         "title": "Content",
         "type": "string"
      },
      "name": {
         "title": "Name",
         "type": "string"
      },
      "call_id": {
         "title": "Call Id",
         "type": "string"
      },
      "is_error": {
         "anyOf": [
            {
               "type": "boolean"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Is Error"
      }
   },
   "required": [
      "content",
      "name",
      "call_id"
   ]
}

字段:
  • call_id (str)

  • content (str)

  • is_error (bool | None)

  • name (str)

字段 call_id: str [必需]#

函数调用的 ID。 请注意,对于某些模型,此 ID 可能为空。

字段 content: str [必需]#

函数调用的输出。

字段 is_error: bool | None = None#

函数调用是否导致错误。

字段 name: str [必需]#

(v0.4.8 中的新增功能)被调用的函数的名称。

pydantic 模型 FunctionExecutionResultMessage[source]#

基类: BaseModel

函数执行结果消息包含多个函数调用的输出。

显示 JSON 模式
{
   "title": "FunctionExecutionResultMessage",
   "description": "Function execution result message contains the output of multiple function calls.",
   "type": "object",
   "properties": {
      "content": {
         "items": {
            "$ref": "#/$defs/FunctionExecutionResult"
         },
         "title": "Content",
         "type": "array"
      },
      "type": {
         "const": "FunctionExecutionResultMessage",
         "default": "FunctionExecutionResultMessage",
         "title": "Type",
         "type": "string"
      }
   },
   "$defs": {
      "FunctionExecutionResult": {
         "description": "Function execution result contains the output of a function call.",
         "properties": {
            "content": {
               "title": "Content",
               "type": "string"
            },
            "name": {
               "title": "Name",
               "type": "string"
            },
            "call_id": {
               "title": "Call Id",
               "type": "string"
            },
            "is_error": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Is Error"
            }
         },
         "required": [
            "content",
            "name",
            "call_id"
         ],
         "title": "FunctionExecutionResult",
         "type": "object"
      }
   },
   "required": [
      "content"
   ]
}

字段:
  • content (List[autogen_core.models._types.FunctionExecutionResult])

  • type (Literal['FunctionExecutionResultMessage'])

字段 content: List[FunctionExecutionResult] [必需]#
字段 type: Literal['FunctionExecutionResultMessage'] = 'FunctionExecutionResultMessage'#
ModelCapabilities(**kwargs)[源代码]#

基类: TypedDict

function_calling: Required[bool]#
json_output: Required[bool]#
vision: Required[bool]#
ModelFamily(*args: Any, **kwargs: Any)[源代码]#

基类: object

模型族是一组从能力角度来看具有相似特征的模型。 这与离散支持的功能(如视觉、函数调用和 JSON 输出)不同。

此命名空间类保存 AutoGen 了解的模型族的常量。 其他族肯定存在,可以用字符串表示,但是,AutoGen 会将它们视为未知。

ANY#

别名: Literal[‘gpt-41’, ‘gpt-45’, ‘gpt-4o’, ‘o1’, ‘o3’, ‘o4’, ‘gpt-4’, ‘gpt-35’, ‘r1’, ‘gemini-1.5-flash’, ‘gemini-1.5-pro’, ‘gemini-2.0-flash’, ‘gemini-2.5-pro’, ‘claude-3-haiku’, ‘claude-3-sonnet’, ‘claude-3-opus’, ‘claude-3-5-haiku’, ‘claude-3-5-sonnet’, ‘claude-3-7-sonnet’, ‘unknown’]

CLAUDE_3_5_HAIKU = 'claude-3-5-haiku'#
CLAUDE_3_5_SONNET = 'claude-3-5-sonnet'#
CLAUDE_3_7_SONNET = 'claude-3-7-sonnet'#
CLAUDE_3_HAIKU = 'claude-3-haiku'#
CLAUDE_3_OPUS = 'claude-3-opus'#
CLAUDE_3_SONNET = 'claude-3-sonnet'#
GEMINI_1_5_FLASH = 'gemini-1.5-flash'#
GEMINI_1_5_PRO = 'gemini-1.5-pro'#
GEMINI_2_0_FLASH = 'gemini-2.0-flash'#
GEMINI_2_5_PRO = 'gemini-2.5-pro'#
GPT_35 = 'gpt-35'#
GPT_4 = 'gpt-4'#
GPT_41 = 'gpt-41'#
GPT_45 = 'gpt-45'#
GPT_4O = 'gpt-4o'#
O1 = 'o1'#
O3 = 'o3'#
O4 = 'o4'#
R1 = 'r1'#
UNKNOWN = 'unknown'#
静态 is_claude(family: str) bool[源代码]#
静态 is_gemini(family: str) bool[source]#
静态 is_openai(family: str) bool[source]#
ModelInfo[source]#

基类: TypedDict

ModelInfo 是一个字典,包含有关模型属性的信息。它预计在模型客户端的 model_info 属性中使用。

随着我们添加更多功能,我们预计它会随着时间的推移而增长。

family: Required[Literal['gpt-41', 'gpt-45', 'gpt-4o', 'o1', 'o3', 'o4', 'gpt-4', 'gpt-35', 'r1', 'gemini-1.5-flash', 'gemini-1.5-pro', 'gemini-2.0-flash', 'gemini-2.5-pro', 'claude-3-haiku', 'claude-3-sonnet', 'claude-3-opus', 'claude-3-5-haiku', 'claude-3-5-sonnet', 'claude-3-7-sonnet', 'unknown'] | str]#

模型系列应该是 ModelFamily 中的常量之一,或者表示未知模型系列的字符串。

function_calling: Required[bool]#

如果模型支持函数调用,则为 True,否则为 False。

json_output: Required[bool]#

这与结构化 JSON 不同。

类型:

如果模型支持 JSON 输出,则为 True,否则为 False。注意

multiple_system_messages: bool | None#

如果模型支持多个非连续的系统消息,则为 True,否则为 False。

structured_output: Required[bool]#

如果模型支持结构化输出,则为 True,否则为 False。这与 json_output 不同。

vision: Required[bool]#

如果模型支持视觉,也称为图像输入,则为 True,否则为 False。

class RequestUsage(prompt_tokens: int, completion_tokens: int)[source]#

基类: object

completion_tokens: int#
prompt_tokens: int#
pydantic 模型 SystemMessage[源代码]#

基类: BaseModel

系统消息包含来自开发者的模型指令。

注意

Open AI 正在放弃使用“system”角色,而倾向于使用“developer”角色。有关更多详细信息,请参阅模型规范。 但是,他们的 API 中仍然允许使用“system”角色,并且会在服务器端自动转换为“developer”角色。 因此,您可以将 SystemMessage 用于开发者消息。

显示 JSON 模式
{
   "title": "SystemMessage",
   "description": "System message contains instructions for the model coming from the developer.\n\n.. note::\n\n    Open AI is moving away from using 'system' role in favor of 'developer' role.\n    See `Model Spec <https://cdn.openai.com/spec/model-spec-2024-05-08.html#definitions>`_ for more details.\n    However, the 'system' role is still allowed in their API and will be automatically converted to 'developer' role\n    on the server side.\n    So, you can use `SystemMessage` for developer messages.",
   "type": "object",
   "properties": {
      "content": {
         "title": "Content",
         "type": "string"
      },
      "type": {
         "const": "SystemMessage",
         "default": "SystemMessage",
         "title": "Type",
         "type": "string"
      }
   },
   "required": [
      "content"
   ]
}

字段:
  • content (str)

  • 类型 (Literal['SystemMessage'])

字段 content: str [必需]#

消息的内容。

字段 type: Literal['SystemMessage'] = 'SystemMessage'#
TopLogprob(logprob: float, bytes: List[int] | None = None)[源代码]#

基类: object

bytes: List[int] | None = None#
logprob: float#
pydantic 模型 UserMessage[源代码]#

基类: BaseModel

用户消息包含来自最终用户的输入,或是提供给模型的通用数据。

显示 JSON 模式
{
   "title": "UserMessage",
   "description": "User message contains input from end users, or a catch-all for data provided to the model.",
   "type": "object",
   "properties": {
      "content": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "items": {
                  "anyOf": [
                     {
                        "type": "string"
                     },
                     {}
                  ]
               },
               "type": "array"
            }
         ],
         "title": "Content"
      },
      "source": {
         "title": "Source",
         "type": "string"
      },
      "type": {
         "const": "UserMessage",
         "default": "UserMessage",
         "title": "Type",
         "type": "string"
      }
   },
   "required": [
      "content",
      "source"
   ]
}

字段:
  • content (str | List[str | autogen_core._image.Image])

  • source (str)

  • 类型 (Literal['UserMessage'])

字段 content: str | List[str | Image] [必需]#

消息的内容。

字段 source: str [必需]#

发送此消息的代理的名称。

字段 type: Literal['UserMessage'] = 'UserMessage'#
validate_model_info(model_info: ModelInfo) None[源代码]#

验证模型信息字典。

引发:

ValueError – 如果模型信息字典缺少必需的字段。