autogen_core.models#

class ModelCapabilities(**kwargs)[source]#

基类:TypedDict

vision: Required[bool]#
function_calling: Required[bool]#
json_output: Required[bool]#
class ChatCompletionClient[source]#

基类:ComponentBase[BaseModel], ABC

abstract async create(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], tool_choice: Tool | Literal['auto', 'required', 'none'] = 'auto', 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], 可选) – 与模型一起使用的工具。默认为 []。

  • tool_choice (Tool | Literal["auto", "required", "none"], 可选) – 单个 Tool 对象,用于强制模型使用,"auto" 允许模型选择任何可用工具,"required" 强制使用工具,"none" 禁用工具使用。默认为 "auto"。

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

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

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

返回:

CreateResult – 模型调用的结果。

abstract create_stream(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = [], tool_choice: Tool | Literal['auto', 'required', 'none'] = 'auto', 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], 可选) – 与模型一起使用的工具。默认为 []。

  • tool_choice (Tool | Literal["auto", "required", "none"], 可选) – 单个 Tool 对象,用于强制模型使用,"auto" 允许模型选择任何可用工具,"required" 强制使用工具,"none" 禁用工具使用。默认为 "auto"。

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

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

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

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

返回:

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

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

基类: BaseModel

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

注意

OpenAI 正在放弃使用“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)

  • type (Literal['SystemMessage'])

field content: str [Required]#

消息内容。

field type: Literal['SystemMessage'] = 'SystemMessage'#
pydantic model UserMessage[source]#

基类: 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)

  • type (Literal['UserMessage'])

field content: str | List[str | Image] [Required]#

消息内容。

field source: str [Required]#

发送此消息的代理名称。

field type: Literal['UserMessage'] = 'UserMessage'#
pydantic model AssistantMessage[source]#

基类: 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] [Required]#

消息内容。

field thought: str | None = None#

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

field source: str [Required]#

发送此消息的代理名称。

field type: Literal['AssistantMessage'] = 'AssistantMessage'#
pydantic model 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)

field content: str [Required]#

函数调用的输出。

field name: str [Required]#

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

field call_id: str [Required]#

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

field is_error: bool | None = None#

函数调用是否导致错误。

pydantic model 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'])

field content: List[FunctionExecutionResult] [Required]#
field type: Literal['FunctionExecutionResultMessage'] = 'FunctionExecutionResultMessage'#
class RequestUsage(prompt_tokens: int, completion_tokens: int)[source]#

基类: object

prompt_tokens: int#
completion_tokens: int#
pydantic model 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)

field finish_reason: Literal['stop', 'length','function_calls', 'content_filter', 'unknown'] [Required]#

模型完成生成的原因。

field content: str | List[FunctionCall] [Required]#

模型完成的输出。

field usage: RequestUsage [Required]#

提示和完成中使用的标记。

field cached: bool [Required]#

完成是否从缓存响应生成。

field logprobs: List[ChatCompletionTokenLogprob] | None = None#

完成中标记的对数概率。

field thought: str | None = None#

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

class TopLogprob(logprob: float, bytes: List[int] | None = None)[source]#

基类: object

logprob: float#
bytes: List[int] | None = None#
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)

field token: str [Required]#
field logprob: float [Required]#
field top_logprobs: List[TopLogprob] | None = None#
field bytes: List[int] | None = None#
class ModelFamily(*args: Any, **kwargs: Any)[source]#

基类: object

模型家族是一组在功能方面具有相似特征的模型。这与视觉、函数调用和 JSON 输出等离散支持的功能不同。

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

GPT_5 = 'gpt-5'#
GPT_41 = 'gpt-41'#
GPT_45 = 'gpt-45'#
GPT_4O = 'gpt-4o'#
O1 = 'o1'#
O3 = 'o3'#
O4 = 'o4'#
GPT_4 = 'gpt-4'#
GPT_35 = 'gpt-35'#
R1 = 'r1'#
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'#
GEMINI_2_5_FLASH = 'gemini-2.5-flash'#
CLAUDE_3_HAIKU = 'claude-3-haiku'#
CLAUDE_3_SONNET = 'claude-3-sonnet'#
CLAUDE_3_OPUS = 'claude-3-opus'#
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_4_OPUS = 'claude-4-opus'#
CLAUDE_4_SONNET = 'claude-4-sonnet'#
LLAMA_3_3_8B = 'llama-3.3-8b'#
LLAMA_3_3_70B = 'llama-3.3-70b'#
LLAMA_4_SCOUT = 'llama-4-scout'#
LLAMA_4_MAVERICK = 'llama-4-maverick'#
CODESRAL = 'codestral'#
OPEN_CODESRAL_MAMBA = 'open-codestral-mamba'#
MISTRAL = 'mistral'#
MINISTRAL = 'ministral'#
PIXTRAL = 'pixtral'#
UNKNOWN = 'unknown'#
ANY#

别名 Literal['gpt-5', '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', 'gemini-2.5-flash', 'claude-3-haiku', 'claude-3-sonnet', 'claude-3-opus', 'claude-3-5-haiku', 'claude-3-5-sonnet', 'claude-3-7-sonnet', 'claude-4-opus', 'claude-4-sonnet', 'llama-3.3-8b', 'llama-3.3-70b', 'llama-4-scout', 'llama-4-maverick', 'codestral', 'open-codestral-mamba', 'mistral', 'ministral', 'pixtral', 'unknown']

static is_claude(family: str) bool[source]#
static is_gemini(family: str) bool[source]#
static is_openai(family: str) bool[source]#
static is_llama(family: str) bool[source]#
static is_mistral(family: str) bool#
class ModelInfo[source]#

基类:TypedDict

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

我们期望随着我们添加更多功能,它会不断增长。

vision: Required[bool]#

如果模型支持视觉(即图像输入),则为 True,否则为 False。

function_calling: Required[bool]#

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

json_output: Required[bool]#

这与结构化 JSON 不同。

类型:

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

family: Required[Literal['gpt-5', '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', 'gemini-2.5-flash', 'claude-3-haiku', 'claude-3-sonnet', 'claude-3-opus', 'claude-3-5-haiku', 'claude-3-5-sonnet', 'claude-3-7-sonnet', 'claude-4-opus', 'claude-4-sonnet', 'llama-3.3-8b', 'llama-3.3-70b', 'llama-4-scout', 'llama-4-maverick', 'codestral', 'open-codestral-mamba', 'mistral', 'ministral', 'pixtral', 'unknown'] | str]#

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

structured_output: Required[bool]#

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

multiple_system_messages: bool | None#

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

validate_model_info(model_info: ModelInfo) None[source]#

验证模型信息字典。

抛出:

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