autogen_ext.models.anthropic#
- 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, 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) -- 模型的能力描述。使用自定义模型时必须提供。
bedrock_info (BedrockInfo, optional) -- 模型在 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())
- classmethod _from_config(config: AnthropicBedrockClientConfigurationConfigModel) Self [源代码]#
从配置对象创建组件的新实例。
- 参数:
config (T) -- 配置对象。
- Returns:
Self -- 组件的新实例。
- _to_config() AnthropicBedrockClientConfigurationConfigModel [源代码]#
导出当前组件实例的配置,该配置可用于创建具有相同配置的新组件实例。
- Returns:
T -- 组件的配置。
- component_config_schema#
- component_provider_override: ClassVar[str | None] = 'autogen_ext.models.anthropic.AnthropicBedrockChatCompletionClient'#
覆盖组件的provider字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'model'#
组件的逻辑类型。
- class AnthropicBedrockClientConfiguration[源代码]#
基类:
AnthropicClientConfiguration
- bedrock_info: BedrockInfo#
- model_capabilities: ModelCapabilities#
- pydantic model AnthropicBedrockClientConfigurationConfigModel[源代码]#
基类:
AnthropicClientConfigurationConfigModel
Show JSON schema
{ "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/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\u662f\u4e00\u4e2a\u5305\u542b\u6a21\u578b\u5c5e\u6027\u4fe1\u606f\u7684\u5b57\u5178\u3002\n\u9884\u671f\u7528\u4e8e\u6a21\u578b\u5ba2\u6237\u7aef\u7684model_info\u5c5e\u6027\u4e2d\u3002\n\n\u968f\u7740\u6211\u4eec\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\uff0c\u9884\u8ba1\u8fd9\u4e2a\u7ed3\u6784\u4f1a\u4e0d\u65ad\u6269\u5c55\u3002", "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", "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" ], "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" ] }
- Fields:
bedrock_info (autogen_ext.models.anthropic.config.BedrockInfoConfigModel | 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", # 如果环境变量 ANTHROPIC_API_KEY 已设置则可选 ) 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) -- 配置对象。
- Returns:
Self -- 组件的新实例。
- _to_config() AnthropicClientConfigurationConfigModel [源代码]#
导出当前组件实例的配置,该配置可用于创建具有相同配置的新组件实例。
- Returns:
T -- 组件的配置。
- component_config_schema#
- component_provider_override: ClassVar[str | None] = 'autogen_ext.models.anthropic.AnthropicChatCompletionClient'#
覆盖组件的provider字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'model'#
组件的逻辑类型。
- class AnthropicClientConfiguration[源代码]#
基类:
BaseAnthropicClientConfiguration
- model_capabilities: ModelCapabilities#
- 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": [ { "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\u662f\u4e00\u4e2a\u5305\u542b\u6a21\u578b\u5c5e\u6027\u4fe1\u606f\u7684\u5b57\u5178\u3002\n\u9884\u671f\u7528\u4e8e\u6a21\u578b\u5ba2\u6237\u7aef\u7684model_info\u5c5e\u6027\u4e2d\u3002\n\n\u968f\u7740\u6211\u4eec\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\uff0c\u9884\u8ba1\u8fd9\u4e2a\u7ed3\u6784\u4f1a\u4e0d\u65ad\u6269\u5c55\u3002", "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", "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" ], "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" ] }
- Fields:
tool_choice (Literal['auto', 'any', 'none'] | Dict[str, Any] | None)
tools (List[Dict[str, Any]] | None)
- class BaseAnthropicChatCompletionClient(client: Any, *, create_args: Dict[str, Any], model_info: ModelInfo | None = None)[源代码]#
-
- actual_usage() RequestUsage [源代码]#
- property capabilities: ModelCapabilities#
- 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计数不完全匹配。
- 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 -- 模型调用的结果。
- 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] [源代码]#
创建一个 AsyncGenerator,基于提供的消息和工具生成完成结果的流。
- remaining_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage | AssistantMessage | FunctionExecutionResultMessage, FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools: Sequence[Tool | ToolSchema] = []) int [源代码]#
根据模型的token限制计算剩余的token数量。
- 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)