跳到主要内容

oai.anthropic

为Anthropic API创建一个与OpenAI兼容的客户端。

示例用法: 通过运行pip install --upgrade anthropic安装anthropic包。

导入 autogen

config_list = [ { "model": "claude-3-sonnet-20240229", "api_key": os.getenv("ANTHROPIC_API_KEY"), "api_type": "anthropic", } ]

assistant = autogen.AssistantAgent("assistant", llm_config={"config_list": config_list})

使用Anthropic Bedrock的示例:

通过运行 pip install --upgrade anthropic 来安装 anthropic 包。

导入 autogen

config_list = [ { "model": "anthropic.claude-3-5-sonnet-20240620-v1:0", "aws_access_key":, "aws_secret_key":, "aws_session_token":, "aws_region":"us-east-1", "api_type": "anthropic", } ]

assistant = autogen.AssistantAgent("assistant", llm_config={"config_list": config_list})

AnthropicClient

class AnthropicClient()

__init__

def __init__(**kwargs: Any)

初始化Anthropic API客户端。

参数:

  • api_key str - Anthropic API的API密钥或设置ANTHROPIC_API_KEY环境变量。

加载配置

def load_config(params: Dict[str, Any])

加载Anthropic API客户端的配置。

成本

def cost(response) -> float

使用Anthropic定价计算完成成本。

消息检索

def message_retrieval(response) -> List

从响应中检索并返回一个字符串列表或一个Choice.Message列表。

注意:如果返回的是一个Choice.Message列表,它目前需要包含OpenAI的ChatCompletion Message对象的字段,因为在当前代码库中,除非使用自定义代理,否则这是函数或工具调用的预期。

get_usage

@staticmethod
def get_usage(response: ChatCompletion) -> Dict

获取tokens的使用情况及其成本信息。

oai_messages_to_anthropic_messages

def oai_messages_to_anthropic_messages(
params: Dict[str, Any]) -> list[dict[str, Any]]

将消息从OAI格式转换为Anthropic格式。我们修正了任何特定的角色顺序和类型等。