autogen_ext.models.anthropic#

class AnthropicChatCompletionClient(**kwargs: Unpack)[源代码]#

基础:BaseAnthropicChatCompletionClient, Component[AnthropicClientConfigurationConfigModel]

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

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

  • api_key (str, optional) – Anthropic API 密钥。如果环境变量中没有,则必须提供。

  • base_url (str, 可选) – 覆盖默认的API端点。

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

  • temperature (float, 可选) – 控制随机性。较低的值更确定性。默认值为1.0。

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

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

  • model_info (ModelInfo, 可选) – 模型的能力。如果使用自定义模型,则需要此参数。

要使用此客户端,您必须安装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) 自我[源代码]#

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

Parameters:

config (T) – 配置对象。

Returns:

Self – 组件的新实例。

_to_config() AnthropicClientConfigurationConfigModel[源代码]#

导出配置,该配置将用于创建一个与此实例配置相匹配的组件新实例。

Returns:

T – 组件的配置。

component_config_schema#

AnthropicClientConfigurationConfigModel的别名

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

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

component_type: ClassVar[ComponentType] = 'model'#

组件的逻辑类型。

class AnthropicClientConfiguration[源代码]#

基础类:BaseAnthropicClientConfiguration

api_key: str#
base_url: str | #
default_headers: 字典[str, str] | #
max_retries: int | #
max_tokens: int | #
metadata: 字典[str, str] | #
model: str#
model_capabilities: ModelCapabilities#
model_info: ModelInfo#
response_format: ResponseFormat | #
stop_sequences: 列表[str] | #
temperature: float | #
timeout: float | #
tool_choice: 字面量['auto', 'any', 'none'] | 字典[str, 任何] | #
tools: 列表[字典[str, 任何]] | #
top_k: int | #
top_p: float | #
pydantic model AnthropicClientConfigurationConfigModel[源代码]#

基础:BaseAnthropicClientConfigurationConfigModel

Show JSON schema
{
   "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": [
            {
               "type": "string"
            },
            {
               "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-4o",
                        "o1",
                        "o3",
                        "gpt-4",
                        "gpt-35",
                        "r1",
                        "gemini-1.5-flash",
                        "gemini-1.5-pro",
                        "gemini-2.0-flash",
                        "claude-3-haiku",
                        "claude-3-sonnet",
                        "claude-3-opus",
                        "claude-3.5-haiku",
                        "claude-3.5-sonnet",
                        "unknown"
                     ],
                     "type": "string"
                  },
                  {
                     "type": "string"
                  }
               ],
               "title": "Family"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output",
            "family"
         ],
         "title": "ModelInfo",
         "type": "object"
      },
      "ResponseFormat": {
         "properties": {
            "type": {
               "enum": [
                  "text",
                  "json_object"
               ],
               "title": "Type",
               "type": "string"
            }
         },
         "required": [
            "type"
         ],
         "title": "ResponseFormat",
         "type": "object"
      }
   },
   "required": [
      "model"
   ]
}

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

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

field tool_choice: 字面量['auto', 'any', 'none'] | 字典[str, 任何] | = None#
field tools: 列表[字典[str, 任何]] | = None#
class BaseAnthropicChatCompletionClient(client: AsyncAnthropic, *, create_args: 字典[str, 任何], model_info: ModelInfo | = None)[源代码]#

基础类:ChatCompletionClient

actual_usage() RequestUsage[源代码]#
property capabilities: ModelCapabilities#
async close() [源代码]#
count_tokens(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = []) int[源代码]#

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

注意:这是基于常见的标记化模式的估计,可能无法完全匹配Anthropic对Claude模型的精确标记计数。

async create(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = [], json_output: bool | = None, extra_create_args: 映射[str, 任何] = {}, cancellation_token: CancellationToken | = None) CreateResult[源代码]#
async create_stream(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = [], json_output: bool | = None, extra_create_args: 映射[str, 任何] = {}, cancellation_token: CancellationToken | = None, max_consecutive_empty_chunk_tolerance: int = 0) AsyncGenerator[str | CreateResult, ][源代码]#

创建一个AsyncGenerator,根据提供的消息和工具生成一系列补全内容。

property model_info: ModelInfo#
remaining_tokens(messages: Sequence[已注解[系统消息 | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[工具 | 工具模式] = []) int[源代码]#

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

total_usage() RequestUsage[源代码]#
pydantic model CreateArgumentsConfigModel[源代码]#

基础:BaseModel

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

Fields:
  • 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 | = 4096#
field metadata: 字典[str, str] | = None#
field model: str [Required]#
field response_format: ResponseFormat | = None#
field stop_sequences: 列表[str] | = None#
field temperature: float | = 1.0#
field top_k: int | = None#
field top_p: float | = None#