Skip to main content

oai.anthropic

Create an OpenAI-compatible client for the Anthropic API.

Example usage: Install the anthropic package by running pip install --upgrade anthropic.

import 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})

Example usage for Anthropic Bedrock:

Install the anthropic package by running pip install --upgrade anthropic.

import 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)

Initialize the Anthropic API client.

Arguments:

  • api_key str - The API key for the Anthropic API or set the ANTHROPIC_API_KEY environment variable.

load_config

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

Load the configuration for the Anthropic API client.

cost

def cost(response) -> float

Calculate the cost of the completion using the Anthropic pricing.

message_retrieval

def message_retrieval(response) -> List

Retrieve and return a list of strings or a list of Choice.Message from the response.

NOTE: if a list of Choice.Message is returned, it currently needs to contain the fields of OpenAI's ChatCompletion Message object, since that is expected for function or tool calling in the rest of the codebase at the moment, unless a custom agent is being used.

get_usage

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

Get the usage of tokens and their cost information.

oai_messages_to_anthropic_messages

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

Convert messages from OAI format to Anthropic format. We correct for any specific role orders and types, etc.