autogen_ext.models.anthropic#
- class AnthropicChatCompletionClient(**kwargs: Unpack)[源代码]#
基类:
BaseAnthropicChatCompletionClient、Component[AnthropicClientConfigurationConfigModel]Anthropic Claude 模型的聊天补全客户端。
- 参数:
model (str) – 要使用的 Claude 模型(例如,“claude-3-sonnet-20240229”、“claude-3-opus-20240229”)
api_key (str, 可选) – Anthropic API 密钥。如果不在环境变量中则为必填项。
base_url (str, 可选) – 覆盖默认 API 端点。
max_tokens (int, 可选) – 响应中的最大 token 数。默认值为 4096。
temperature (float, 可选) – 控制随机性。值越低越确定。默认值为 1.0。
top_p (float, 可选) – 通过核采样控制多样性。默认值为 1.0。
top_k (int, 可选) – 通过 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)
- component_type: ClassVar[ComponentType] = 'model'#
组件的逻辑类型。
- component_config_schema#
- component_provider_override: ClassVar[str | None] = 'autogen_ext.models.anthropic.AnthropicChatCompletionClient'#
覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- _to_config() AnthropicClientConfigurationConfigModel[源代码]#
转储创建与此实例配置匹配的组件新实例所需的配置。
- 返回:
T – 组件的配置。
- classmethod _from_config(config: AnthropicClientConfigurationConfigModel) Self[源代码]#
从配置对象创建组件的新实例。
- 参数:
config (T) – 配置对象。
- 返回:
Self – 组件的新实例。
- class AnthropicBedrockChatCompletionClient(**kwargs: Unpack)[源代码]#
基类:
BaseAnthropicChatCompletionClient、Component[AnthropicBedrockClientConfigurationConfigModel]AWS Bedrock 上 Anthropic Claude 模型的聊天补全客户端。
- 参数:
model (str) – 要使用的 Claude 模型(例如,“claude-3-sonnet-20240229”、“claude-3-opus-20240229”)
api_key (str, 可选) – Anthropic API 密钥。如果不在环境变量中则为必填项。
base_url (str, 可选) – 覆盖默认 API 端点。
max_tokens (int, 可选) – 响应中的最大 token 数。默认值为 4096。
temperature (float, 可选) – 控制随机性。值越低越确定。默认值为 1.0。
top_p (float, 可选) – 通过核采样控制多样性。默认值为 1.0。
top_k (int, 可选) – 通过 top-k 采样控制多样性。默认值为 -1(禁用)。
model_info (ModelInfo, 可选) – 模型的功能。如果使用自定义模型则为必填项。
bedrock_info (BedrockInfo, 可选) – Bedrock 中模型的功能。如果使用 AWS bedrock 中的模型则为必填项。
要使用此客户端,您必须安装 Anthropic 扩展
pip install "autogen-ext[anthropic]"
示例
import asyncio from autogen_ext.models.anthropic import AnthropicBedrockChatCompletionClient, BedrockInfo from autogen_core.models import UserMessage, ModelInfo async def main(): anthropic_client = AnthropicBedrockChatCompletionClient( model="anthropic.claude-3-5-sonnet-20240620-v1:0", temperature=0.1, model_info=ModelInfo( vision=False, function_calling=True, json_output=False, family="unknown", structured_output=True ), bedrock_info=BedrockInfo( aws_access_key="<aws_access_key>", aws_secret_key="<aws_secret_key>", aws_session_token="<aws_session_token>", aws_region="<aws_region>", ), ) 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())
- component_type: ClassVar[ComponentType] = 'model'#
组件的逻辑类型。
- component_config_schema#
- component_provider_override: ClassVar[str | None] = 'autogen_ext.models.anthropic.AnthropicBedrockChatCompletionClient'#
覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- _to_config() AnthropicBedrockClientConfigurationConfigModel[源代码]#
转储创建与此实例配置匹配的组件新实例所需的配置。
- 返回:
T – 组件的配置。
- classmethod _from_config(config: AnthropicBedrockClientConfigurationConfigModel) Self[源代码]#
从配置对象创建组件的新实例。
- 参数:
config (T) – 配置对象。
- 返回:
Self – 组件的新实例。
- class BaseAnthropicChatCompletionClient(client: Any, *, create_args: Dict[str, Any], model_info: ModelInfo | None = None)[源代码]#
-
- 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[源代码]#
从模型创建单个响应。
- 参数:
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 – 模型调用的结果。
- async 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, max_consecutive_empty_chunk_tolerance: int = 0) AsyncGenerator[str | CreateResult, None][源代码]#
创建一个 AsyncGenerator,它根据提供的消息和工具生成补全流。
- count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[源代码]#
估计消息和工具使用的 token 数量。
注意:这是一个基于常见 token 化模式的估计,可能与 Anthropic Claude 模型的精确 token 计数不完全匹配。
- remaining_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int[源代码]#
根据模型的 token 限制计算剩余 token。
- actual_usage() RequestUsage[源代码]#
- total_usage() RequestUsage[源代码]#
- property capabilities: ModelCapabilities#
- class AnthropicClientConfiguration[源代码]#
基类:
BaseAnthropicClientConfiguration- response_format: ResponseFormat | None#
- thinking: ThinkingConfig | None#
- model_capabilities: ModelCapabilities#
- class AnthropicBedrockClientConfiguration[源代码]#
基类:
AnthropicClientConfiguration- bedrock_info: BedrockInfo#
- response_format: ResponseFormat | None#
- thinking: ThinkingConfig | None#
- model_capabilities: ModelCapabilities#
- pydantic model 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" }, "thinking": { "anyOf": [ { "$ref": "#/$defs/ThinkingConfigModel" }, { "type": "null" } ], "default": null }, "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-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" ], "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" }, "ThinkingConfigModel": { "description": "Configuration for thinking mode.", "properties": { "type": { "enum": [ "enabled", "disabled" ], "title": "Type", "type": "string" }, "budget_tokens": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "default": null, "title": "Budget Tokens" } }, "required": [ "type" ], "title": "ThinkingConfigModel", "type": "object" } }, "required": [ "model" ] }
- 字段:
- pydantic model 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" }, "thinking": { "anyOf": [ { "$ref": "#/$defs/ThinkingConfigModel" }, { "type": "null" } ], "default": null }, "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/BedrockInfoConfigModel" }, { "type": "null" } ], "default": null } }, "$defs": { "BedrockInfoConfigModel": { "properties": { "aws_access_key": { "format": "password", "title": "Aws Access 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" }, "aws_secret_key": { "format": "password", "title": "Aws Secret Key", "type": "string", "writeOnly": true } }, "required": [ "aws_access_key", "aws_session_token", "aws_region", "aws_secret_key" ], "title": "BedrockInfoConfigModel", "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-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" ], "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" }, "ThinkingConfigModel": { "description": "Configuration for thinking mode.", "properties": { "type": { "enum": [ "enabled", "disabled" ], "title": "Type", "type": "string" }, "budget_tokens": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "default": null, "title": "Budget Tokens" } }, "required": [ "type" ], "title": "ThinkingConfigModel", "type": "object" } }, "required": [ "model" ] }
- field bedrock_info: BedrockInfoConfigModel | None = None#
- pydantic model CreateArgumentsConfigModel[源代码]#
基类:
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" }, "thinking": { "anyOf": [ { "$ref": "#/$defs/ThinkingConfigModel" }, { "type": "null" } ], "default": null } }, "$defs": { "ResponseFormat": { "properties": { "type": { "enum": [ "text", "json_object" ], "title": "Type", "type": "string" } }, "required": [ "type" ], "title": "ResponseFormat", "type": "object" }, "ThinkingConfigModel": { "description": "Configuration for thinking mode.", "properties": { "type": { "enum": [ "enabled", "disabled" ], "title": "Type", "type": "string" }, "budget_tokens": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "default": null, "title": "Budget Tokens" } }, "required": [ "type" ], "title": "ThinkingConfigModel", "type": "object" } }, "required": [ "model" ] }
- 字段:
- 字段 response_format: ResponseFormat | 无 = 无#
- 字段 thinking: ThinkingConfigModel | 无 = 无#