oai.bedrock
Create a compatible client for the Amazon Bedrock Converse API.
Example usage:
Install the boto3
package by running pip install --upgrade boto3
.
BedrockClient
Client for Amazon’s Bedrock Converse API.
__init__
Initialises BedrockClient for Amazon’s Bedrock Converse API
message_retrieval
Retrieve the messages from the response.
parse_custom_params
Parses custom parameters for logic in this client class
parse_params
Loads the valid parameters required to invoke Bedrock Converse Returns a tuple of (base_params, additional_params)
create
Run Amazon Bedrock inference and return AutoGen response
cost
Calculate the cost of the response.
get_usage
Get the usage of tokens and their cost information.
extract_system_messages
Extract the system messages from the list of messages.
Arguments:
messages
list[dict] - List of messages.
Returns:
List[SystemMessage]
- List of System messages.
oai_messages_to_bedrock_messages
Convert messages from OAI format to Bedrock format. We correct for any specific role orders and types, etc. AWS Bedrock requires messages to alternate between user and assistant roles. This function ensures that the messages are in the correct order and format for Bedrock by inserting “Please continue” messages as needed. This is the same method as the one in the Autogen Anthropic client
parse_image
Try to get the raw data from an image url.
Ref: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ImageSource.html returns a tuple of (Image Data, Content Type)
format_tool_calls
Converts Converse API response tool calls to AutoGen format
convert_stop_reason_to_finish_reason
Converts Bedrock finish reasons to our finish reasons, according to OpenAI:
- stop: if the model hit a natural stop point or a provided stop sequence,
- length: if the maximum number of tokens specified in the request was reached,
- content_filter: if content was omitted due to a flag from our content filters,
- tool_calls: if the model called a tool
calculate_cost
Calculate the cost of the completion using the Bedrock pricing.