oai.anthropic
Create an OpenAI-compatible client for the Anthropic API.
Example usage:
Install the anthropic
package by running pip install --upgrade anthropic
.
Example usage for Anthropic Bedrock:
Install the anthropic
package by running pip install --upgrade anthropic
.
AnthropicClient
__init__
Initialize the Anthropic API client.
Arguments:
api_key
str - The API key for the Anthropic API or set theANTHROPIC_API_KEY
environment variable.
load_config
Load the configuration for the Anthropic API client.
cost
Calculate the cost of the completion using the Anthropic pricing.
message_retrieval
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
Get the usage of tokens and their cost information.
oai_messages_to_anthropic_messages
Convert messages from OAI format to Anthropic format. We correct for any specific role orders and types, etc.