autogen_core.models#

pydantic model AssistantMessage[源代码]#

基类:BaseModel

助手消息是从语言模型中采样得到的。

Show JSON schema
{
   "title": "AssistantMessage",
   "description": "\u52a9\u624b\u6d88\u606f\u662f\u4ece\u8bed\u8a00\u6a21\u578b\u4e2d\u91c7\u6837\u5f97\u5230\u7684\u3002",
   "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"
   ]
}

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

  • source (str)

  • thought (str | None)

  • type (Literal['AssistantMessage'])

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

消息的内容。

field source: str [Required]#

发送此消息的代理名称。

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[源代码]#

从模型创建单个响应。

参数:
  • 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。

Returns:

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][源代码]#

从模型创建以 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。

Returns:

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[源代码]#
abstract total_usage() RequestUsage[源代码]#
pydantic model ChatCompletionTokenLogprob[源代码]#

基类:BaseModel

Show JSON schema
{
   "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"
   ]
}

Fields:
  • bytes (List[int] | None)

  • logprob (float)

  • token (str)

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

field bytes: List[int] | None = None#
field logprob: float [Required]#
field token: str [Required]#
field top_logprobs: List[TopLogprob] | None = None#
pydantic model CreateResult[源代码]#

基类:BaseModel

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

Show JSON schema
{
   "title": "CreateResult",
   "description": "\u521b\u5efa\u7ed3\u679c\u5305\u542b\u6a21\u578b\u8865\u5168\u7684\u8f93\u51fa\u3002",
   "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"
   ]
}

Fields:
  • 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 cached: bool [Required]#

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

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

模型补全的输出。

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

模型完成生成补全的原因。

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

补全中令牌的对数概率。

field thought: str | None = None#

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

field usage: RequestUsage [Required]#

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

pydantic model FunctionExecutionResult[源代码]#

基类:BaseModel

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

Show JSON schema
{
   "title": "FunctionExecutionResult",
   "description": "\u51fd\u6570\u6267\u884c\u7ed3\u679c\u5305\u542b\u51fd\u6570\u8c03\u7528\u7684\u8f93\u51fa\u3002",
   "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"
   ]
}

Fields:
  • call_id (str)

  • content (str)

  • is_error (bool | None)

  • name (str)

field call_id: str [Required]#

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

field content: str [Required]#

函数调用的输出。

field is_error: bool | None = None#

函数调用是否导致错误。

field name: str [Required]#

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

pydantic model FunctionExecutionResultMessage[源代码]#

基类:BaseModel

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

Show JSON schema
{
   "title": "FunctionExecutionResultMessage",
   "description": "\u51fd\u6570\u6267\u884c\u7ed3\u679c\u6d88\u606f\u5305\u542b\u591a\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u8f93\u51fa\u3002",
   "type": "object",
   "properties": {
      "content": {
         "items": {
            "$ref": "#/$defs/FunctionExecutionResult"
         },
         "title": "Content",
         "type": "array"
      },
      "type": {
         "const": "FunctionExecutionResultMessage",
         "default": "FunctionExecutionResultMessage",
         "title": "Type",
         "type": "string"
      }
   },
   "$defs": {
      "FunctionExecutionResult": {
         "description": "\u51fd\u6570\u6267\u884c\u7ed3\u679c\u5305\u542b\u51fd\u6570\u8c03\u7528\u7684\u8f93\u51fa\u3002",
         "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"
   ]
}

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

  • type (Literal['FunctionExecutionResultMessage'])

field content: List[FunctionExecutionResult] [Required]#
field type: Literal['FunctionExecutionResultMessage'] = 'FunctionExecutionResultMessage'#
class ModelCapabilities(**kwargs)[源代码]#

基类:TypedDict

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

基类:object

模型家族(model family)是指从能力角度具有相似特征的一组模型。这与具体的支持特性(如视觉识别、函数调用和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', 'gemini-2.5-flashclaude-3-haiku', 'claude-3-sonnet', 'claude-3-opus', 'claude-3-5-haiku', 'claude-3-5-sonnet', 'claude-3-7-sonnet', 'llama-3.3-8b', 'llama-3.3-70b', 'llama-4-scout', 'llama-4-maverick', 'codestral', 'open-codestral-mamba', 'mistral', 'ministral', 'pixtral', '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'#
CODESRAL = 'codestral'#
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_FLASH = 'gemini-2.5-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'#
LLAMA_3_3_70B = 'llama-3.3-70b'#
LLAMA_3_3_8B = 'llama-3.3-8b'#
LLAMA_4_MAVERICK = 'llama-4-maverick'#
LLAMA_4_SCOUT = 'llama-4-scout'#
MINISTRAL = 'ministral'#
MISTRAL = 'mistral'#
O1 = 'o1'#
O3 = 'o3'#
O4 = 'o4'#
OPEN_CODESRAL_MAMBA = 'open-codestral-mamba'#
PIXTRAL = 'pixtral'#
R1 = 'r1'#
UNKNOWN = 'unknown'#
static is_claude(family: str) bool[源代码]#
static is_gemini(family: str) bool[源代码]#
static is_llama(family: str) bool[源代码]#
static is_mistral(family: str) bool[源代码]#
static is_openai(family: str) bool[源代码]#
class ModelInfo[源代码]#

基类: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', 'gemini-2.5-flashclaude-3-haiku', 'claude-3-sonnet', 'claude-3-opus', 'claude-3-5-haiku', 'claude-3-5-sonnet', 'claude-3-7-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 中的常量之一,或表示未知模型家族的字符串。

function_calling: Required[bool]#

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

json_output: Required[bool]#

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

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)[源代码]#

基类:object

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

基类:BaseModel

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

备注

OpenAI 正逐步弃用 'system' 角色,转而使用 'developer' 角色。 详情请参阅 Model Spec。 不过,其 API 仍允许使用 'system' 角色,服务器端会自动将其转换为 'developer' 角色 因此,开发者消息可以使用 SystemMessage

Show JSON schema
{
   "title": "SystemMessage",
   "description": "\u7cfb\u7edf\u6d88\u606f\u5305\u542b\u6765\u81ea\u5f00\u53d1\u8005\u7684\u6a21\u578b\u6307\u4ee4\u3002\n\n.. note::\n\n    OpenAI \u6b63\u9010\u6b65\u5f03\u7528 'system' \u89d2\u8272\uff0c\u8f6c\u800c\u4f7f\u7528 'developer' \u89d2\u8272\u3002\n    \u8be6\u60c5\u8bf7\u53c2\u9605 `Model Spec <https://cdn.openai.com/spec/model-spec-2024-05-08.html#definitions>`_\u3002\n    \u4e0d\u8fc7\uff0c\u5176 API \u4ecd\u5141\u8bb8\u4f7f\u7528 'system' \u89d2\u8272\uff0c\u670d\u52a1\u5668\u7aef\u4f1a\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a 'developer' \u89d2\u8272\n    \u56e0\u6b64\uff0c\u5f00\u53d1\u8005\u6d88\u606f\u53ef\u4ee5\u4f7f\u7528 `SystemMessage`\u3002",
   "type": "object",
   "properties": {
      "content": {
         "title": "Content",
         "type": "string"
      },
      "type": {
         "const": "SystemMessage",
         "default": "SystemMessage",
         "title": "Type",
         "type": "string"
      }
   },
   "required": [
      "content"
   ]
}

Fields:
  • content (str)

  • type (Literal['SystemMessage'])

field content: str [Required]#

消息的内容。

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

基类:object

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

基类:BaseModel

用户消息包含终端用户的输入,或作为提供给模型数据的通用容器。

Show JSON schema
{
   "title": "UserMessage",
   "description": "\u7528\u6237\u6d88\u606f\u5305\u542b\u7ec8\u7aef\u7528\u6237\u7684\u8f93\u5165\uff0c\u6216\u4f5c\u4e3a\u63d0\u4f9b\u7ed9\u6a21\u578b\u6570\u636e\u7684\u901a\u7528\u5bb9\u5668\u3002",
   "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"
   ]
}

Fields:
  • 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'#
validate_model_info(model_info: ModelInfo) None[源代码]#

验证模型信息字典。

抛出:

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