autogen_ext.models.anthropic#

class AnthropicBedrockClientConfiguration[源代码]#

基类: AnthropicClientConfiguration

api_key: str#
base_url: str | None#
bedrock_info: BedrockInfo#
default_headers: Dict[str, str] | None#
max_retries: int | None#
max_tokens: int | None#
metadata: Dict[str, str] | None#
model: str#
model_capabilities: ModelCapabilities#
model_info: ModelInfo#
response_format: ResponseFormat | None#
stop_sequences: List[str] | None#
temperature: float | None#
timeout: float | None#
tool_choice: Literal['auto', 'any', 'none'] | Dict[str, Any] | None#
tools: List[Dict[str, Any]] | None#
top_k: int | None#
top_p: float | None#
pydantic 模型 AnthropicBedrockClientConfigurationConfigModel[源代码]#

基类: AnthropicClientConfigurationConfigModel

显示 JSON 模式
{
   "title": "AnthropicBedrockClientConfigurationConfigModel",
   "type": "object",
   "properties": {
      "model": {
         "title": "Model",
         "type": "string"
      },
      "max_tokens": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": 4096,
         "title": "Max Tokens"
      },
      "temperature": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": 1.0,
         "title": "Temperature"
      },
      "top_p": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top P"
      },
      "top_k": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top K"
      },
      "stop_sequences": {
         "anyOf": [
            {
               "items": {
                  "type": "string"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Stop Sequences"
      },
      "response_format": {
         "anyOf": [
            {
               "$ref": "#/$defs/ResponseFormat"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "metadata": {
         "anyOf": [
            {
               "additionalProperties": {
                  "type": "string"
               },
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Metadata"
      },
      "api_key": {
         "anyOf": [
            {
               "format": "password",
               "type": "string",
               "writeOnly": true
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Api Key"
      },
      "base_url": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Base Url"
      },
      "model_capabilities": {
         "anyOf": [
            {
               "$ref": "#/$defs/ModelCapabilities"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "model_info": {
         "anyOf": [
            {
               "$ref": "#/$defs/ModelInfo"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "timeout": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Timeout"
      },
      "max_retries": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Max Retries"
      },
      "default_headers": {
         "anyOf": [
            {
               "additionalProperties": {
                  "type": "string"
               },
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Default Headers"
      },
      "tools": {
         "anyOf": [
            {
               "items": {
                  "type": "object"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Tools"
      },
      "tool_choice": {
         "anyOf": [
            {
               "enum": [
                  "auto",
                  "any",
                  "none"
               ],
               "type": "string"
            },
            {
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Tool Choice"
      },
      "bedrock_info": {
         "anyOf": [
            {
               "$ref": "#/$defs/BedrockInfo"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      }
   },
   "$defs": {
      "BedrockInfo": {
         "description": "BedrockInfo is a dictionary that contains information about a bedrock's properties.\nIt is expected to be used in the bedrock_info property of a model client.",
         "properties": {
            "aws_access_key": {
               "format": "password",
               "title": "Aws Access Key",
               "type": "string",
               "writeOnly": true
            },
            "aws_secret_key": {
               "format": "password",
               "title": "Aws Secret Key",
               "type": "string",
               "writeOnly": true
            },
            "aws_session_token": {
               "format": "password",
               "title": "Aws Session Token",
               "type": "string",
               "writeOnly": true
            },
            "aws_region": {
               "title": "Aws Region",
               "type": "string"
            }
         },
         "required": [
            "aws_access_key",
            "aws_secret_key",
            "aws_session_token",
            "aws_region"
         ],
         "title": "BedrockInfo",
         "type": "object"
      },
      "ModelCapabilities": {
         "deprecated": true,
         "properties": {
            "vision": {
               "title": "Vision",
               "type": "boolean"
            },
            "function_calling": {
               "title": "Function Calling",
               "type": "boolean"
            },
            "json_output": {
               "title": "Json Output",
               "type": "boolean"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output"
         ],
         "title": "ModelCapabilities",
         "type": "object"
      },
      "ModelInfo": {
         "description": "ModelInfo is a dictionary that contains information about a model's properties.\nIt is expected to be used in the model_info property of a model client.\n\nWe are expecting this to grow over time as we add more features.",
         "properties": {
            "vision": {
               "title": "Vision",
               "type": "boolean"
            },
            "function_calling": {
               "title": "Function Calling",
               "type": "boolean"
            },
            "json_output": {
               "title": "Json Output",
               "type": "boolean"
            },
            "family": {
               "anyOf": [
                  {
                     "enum": [
                        "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"
                     ],
                     "type": "string"
                  },
                  {
                     "type": "string"
                  }
               ],
               "title": "Family"
            },
            "structured_output": {
               "title": "Structured Output",
               "type": "boolean"
            },
            "multiple_system_messages": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "title": "Multiple System Messages"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output",
            "family",
            "structured_output"
         ],
         "title": "ModelInfo",
         "type": "object"
      },
      "ResponseFormat": {
         "properties": {
            "type": {
               "enum": [
                  "text",
                  "json_object"
               ],
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "type"
         ],
         "title": "ResponseFormat",
         "type": "object"
      }
   },
   "required": [
      "model"
   ]
}

字段:
  • bedrock_info (autogen_ext.models.anthropic.config.BedrockInfo | None)

field bedrock_info: BedrockInfo | None = None#
class AnthropicChatCompletionClient(**kwargs: Unpack)[源代码]#

基类: BaseAnthropicChatCompletionClient, Component[AnthropicClientConfigurationConfigModel]

用于 Anthropic 的 Claude 模型的聊天完成客户端。

参数:
  • model (str) – 要使用的 Claude 模型 (例如, “claude-3-sonnet-20240229”, “claude-3-opus-20240229”)

  • api_key (str, optional) – Anthropic API 密钥。如果不在环境变量中,则需要。

  • base_url (str, optional) – 覆盖默认 API 端点。

  • max_tokens (int, optional) – 响应中的最大 token 数。默认为 4096。

  • temperature (float, optional) – 控制随机性。越低越确定。默认为 1.0。

  • top_p (float, optional) – 通过核心抽样控制多样性。默认为 1.0。

  • top_k (int, optional) – 通过 top-k 抽样控制多样性。默认为 -1(禁用)。

  • model_info (ModelInfo, optional) – 模型的能力。如果使用自定义模型,则需要。

要使用此客户端,您必须安装 Anthropic 扩展

pip install "autogen-ext[anthropic]"

示例

import asyncio
from autogen_ext.models.anthropic import AnthropicChatCompletionClient
from autogen_core.models import UserMessage


async def main():
    anthropic_client = AnthropicChatCompletionClient(
        model="claude-3-sonnet-20240229",
        api_key="your-api-key",  # Optional if ANTHROPIC_API_KEY is set in environment
    )

    result = await anthropic_client.create([UserMessage(content="What is the capital of France?", source="user")])  # type: ignore
    print(result)


if __name__ == "__main__":
    asyncio.run(main())

要从配置加载客户端

from autogen_core.models import ChatCompletionClient

config = {
    "provider": "AnthropicChatCompletionClient",
    "config": {"model": "claude-3-sonnet-20240229"},
}

client = ChatCompletionClient.load_component(config)
classmethod _from_config(config: AnthropicClientConfigurationConfigModel) Self[源代码]#

从配置对象创建一个组件的新实例。

参数:

config (T) – 配置对象。

返回值:

Self – 组件的新实例。

_to_config() AnthropicClientConfigurationConfigModel[源代码]#

转储创建一个与此实例的配置匹配的组件新实例所需的配置。

返回值:

T – 组件的配置。

component_config_schema#

别名为 AnthropicClientConfigurationConfigModel

component_provider_override: ClassVar[str | None] = 'autogen_ext.models.anthropic.AnthropicChatCompletionClient'#

覆盖组件的提供程序字符串。这应该用于防止内部模块名称成为模块名称的一部分。

component_type: ClassVar[ComponentType] = 'model'#

组件的逻辑类型。

class AnthropicClientConfiguration[源代码]#

基类: BaseAnthropicClientConfiguration

api_key: str#
base_url: str | None#
default_headers: Dict[str, str] | None#
max_retries: int | None#
max_tokens: int | None#
metadata: Dict[str, str] | None#
model: str#
model_capabilities: ModelCapabilities#
model_info: ModelInfo#
response_format: ResponseFormat | None#
stop_sequences: List[str] | None#
temperature: float | None#
timeout: float | None#
tool_choice: Literal['auto', 'any', 'none'] | Dict[str, Any] | None#
tools: List[Dict[str, Any]] | None#
top_k: int | None#
top_p: float | None#
pydantic 模型 AnthropicClientConfigurationConfigModel[源代码]#

基类: BaseAnthropicClientConfigurationConfigModel

显示 JSON 模式
{
   "title": "AnthropicClientConfigurationConfigModel",
   "type": "object",
   "properties": {
      "model": {
         "title": "Model",
         "type": "string"
      },
      "max_tokens": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": 4096,
         "title": "Max Tokens"
      },
      "temperature": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": 1.0,
         "title": "Temperature"
      },
      "top_p": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top P"
      },
      "top_k": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top K"
      },
      "stop_sequences": {
         "anyOf": [
            {
               "items": {
                  "type": "string"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Stop Sequences"
      },
      "response_format": {
         "anyOf": [
            {
               "$ref": "#/$defs/ResponseFormat"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "metadata": {
         "anyOf": [
            {
               "additionalProperties": {
                  "type": "string"
               },
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Metadata"
      },
      "api_key": {
         "anyOf": [
            {
               "format": "password",
               "type": "string",
               "writeOnly": true
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Api Key"
      },
      "base_url": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Base Url"
      },
      "model_capabilities": {
         "anyOf": [
            {
               "$ref": "#/$defs/ModelCapabilities"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "model_info": {
         "anyOf": [
            {
               "$ref": "#/$defs/ModelInfo"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "timeout": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Timeout"
      },
      "max_retries": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Max Retries"
      },
      "default_headers": {
         "anyOf": [
            {
               "additionalProperties": {
                  "type": "string"
               },
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Default Headers"
      },
      "tools": {
         "anyOf": [
            {
               "items": {
                  "type": "object"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Tools"
      },
      "tool_choice": {
         "anyOf": [
            {
               "enum": [
                  "auto",
                  "any",
                  "none"
               ],
               "type": "string"
            },
            {
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Tool Choice"
      }
   },
   "$defs": {
      "ModelCapabilities": {
         "deprecated": true,
         "properties": {
            "vision": {
               "title": "Vision",
               "type": "boolean"
            },
            "function_calling": {
               "title": "Function Calling",
               "type": "boolean"
            },
            "json_output": {
               "title": "Json Output",
               "type": "boolean"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output"
         ],
         "title": "ModelCapabilities",
         "type": "object"
      },
      "ModelInfo": {
         "description": "ModelInfo is a dictionary that contains information about a model's properties.\nIt is expected to be used in the model_info property of a model client.\n\nWe are expecting this to grow over time as we add more features.",
         "properties": {
            "vision": {
               "title": "Vision",
               "type": "boolean"
            },
            "function_calling": {
               "title": "Function Calling",
               "type": "boolean"
            },
            "json_output": {
               "title": "Json Output",
               "type": "boolean"
            },
            "family": {
               "anyOf": [
                  {
                     "enum": [
                        "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"
                     ],
                     "type": "string"
                  },
                  {
                     "type": "string"
                  }
               ],
               "title": "Family"
            },
            "structured_output": {
               "title": "Structured Output",
               "type": "boolean"
            },
            "multiple_system_messages": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "title": "Multiple System Messages"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output",
            "family",
            "structured_output"
         ],
         "title": "ModelInfo",
         "type": "object"
      },
      "ResponseFormat": {
         "properties": {
            "type": {
               "enum": [
                  "text",
                  "json_object"
               ],
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "type"
         ],
         "title": "ResponseFormat",
         "type": "object"
      }
   },
   "required": [
      "model"
   ]
}

字段:
  • tool_choice (Literal['auto', 'any', 'none'] | Dict[str, Any] | None)

  • tools (List[Dict[str, Any]] | None)

字段 tool_choice: Literal['auto', 'any', 'none'] | Dict[str, Any] | None = None#
字段 tools: List[Dict[str, Any]] | None = None#
BaseAnthropicChatCompletionClient(client: Any, *, create_args: Dict[str, Any], model_info: ModelInfo | None = None)[source]#

基类: ChatCompletionClient

actual_usage() RequestUsage[source]#
属性 capabilities: ModelCapabilities#
异步 close() None[source]#
count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[source]#

估计消息和工具使用的 token 数量。

注意:此估计基于常见的 token 化模式,可能与 Anthropic 的 Claude 模型的确切 token 计数不完全匹配。

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 – 模型调用的结果。

async 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, max_consecutive_empty_chunk_tolerance: int = 0) AsyncGenerator[str | CreateResult, None][source]#

基于提供的消息和工具,创建一个生成补全流的 AsyncGenerator。

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

根据模型的令牌限制计算剩余令牌。

total_usage() RequestUsage[source]#
class BedrockInfo[source]#

基类:TypedDict

BedrockInfo 是一个包含有关 Bedrock 属性信息的字典。它应该在模型客户端的 bedrock_info 属性中使用。

aws_access_key: Required[SecretStr]#

用于获得 Bedrock 模型访问权限的 AWS 账户的访问密钥

aws_region: Required[str]#

用于访问 Bedrock 模型的 AWS 账户的 AWS 区域。

aws_secret_key: Required[SecretStr]#

用于访问 Bedrock 模型的 AWS 账户的访问密钥。

aws_session_token: Required[SecretStr]#

用于访问 Bedrock 模型的 AWS 账户的 AWS 会话令牌。

pydantic model CreateArgumentsConfigModel[source]#

基类: BaseModel

显示 JSON 模式
{
   "title": "CreateArgumentsConfigModel",
   "type": "object",
   "properties": {
      "model": {
         "title": "Model",
         "type": "string"
      },
      "max_tokens": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": 4096,
         "title": "Max Tokens"
      },
      "temperature": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": 1.0,
         "title": "Temperature"
      },
      "top_p": {
         "anyOf": [
            {
               "type": "number"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top P"
      },
      "top_k": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Top K"
      },
      "stop_sequences": {
         "anyOf": [
            {
               "items": {
                  "type": "string"
               },
               "type": "array"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Stop Sequences"
      },
      "response_format": {
         "anyOf": [
            {
               "$ref": "#/$defs/ResponseFormat"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "metadata": {
         "anyOf": [
            {
               "additionalProperties": {
                  "type": "string"
               },
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Metadata"
      }
   },
   "$defs": {
      "ResponseFormat": {
         "properties": {
            "type": {
               "enum": [
                  "text",
                  "json_object"
               ],
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "type"
         ],
         "title": "ResponseFormat",
         "type": "object"
      }
   },
   "required": [
      "model"
   ]
}

字段:
  • max_tokens (int | None)

  • metadata (Dict[str, str] | None)

  • model (str)

  • response_format (autogen_ext.models.anthropic.config.ResponseFormat | None)

  • stop_sequences (List[str] | None)

  • temperature (float | None)

  • top_k (int | None)

  • top_p (float | None)

field max_tokens: int | None = 4096#
field metadata: Dict[str, str] | None = None#
field model: str [Required]#
field response_format: ResponseFormat | None = None#
field stop_sequences: List[str] | None = None#
field temperature: float | None = 1.0#
field top_k: int | None = None#
field top_p: float | None = None#