AWS Bedrock
Large Language Model service implementation using Amazon Bedrock API
Overview
AWSBedrockLLMService
provides integration with Amazon Bedrock models, including Anthropic Claude and Amazon Titan models. It supports streaming responses, function calling, and multimodal inputs with specialized context handling for AWS Bedrock’s message format while maintaining compatibility with OpenAI-style contexts.
Installation
To use AWSBedrockLLMService
, install the required dependencies:
You’ll also need to set up your AWS credentials as environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
(if using temporary credentials)AWS_REGION
(defaults to “us-east-1”)
You can obtain AWS credentials by setting up an IAM user with access to Amazon Bedrock in your AWS account.
Configuration
Constructor Parameters
Your AWS access key ID (can also use environment variable)
Your AWS secret access key (can also use environment variable)
Your AWS session token for temporary credentials (can also use environment variable)
AWS region to use for Bedrock service
Bedrock model identifier (e.g., “us.anthropic.claude-3-5-haiku-20241022-v1:0”)
Model configuration parameters
AWS Boto3 client configuration
Input Parameters
Maximum number of tokens to generate. Must be greater than or equal to 1.
Controls randomness in the output. Range: [0.0, 1.0].
Controls diversity via nucleus sampling. Range: [0.0, 1.0].
Sequences where the model will stop generating further tokens.
Response latency preference. Options: “standard” or “optimized”.
Additional parameters to pass to the model.
Input Frames
Contains conversation context
Contains conversation messages
Contains image for vision processing
Updates model settings
Output Frames
Contains generated text
Signals start of response
Signals end of response
Indicates function call initiation
Contains function call results
Indicates function call cancellation
Context Management
The AWS Bedrock service uses specialized context management to handle conversations and message formatting. This includes managing the conversation history, system prompts, function calls, and converting between OpenAI and AWS Bedrock message formats.
AWSBedrockLLMContext
The base context manager for AWS Bedrock conversations:
Context Aggregators
Context aggregators handle message format conversion and management. The service provides a method to create paired aggregators:
Creates user and assistant aggregators for handling message formatting.
Parameters
The context object containing conversation history and settings
Parameters for user message handling
Parameters for assistant message handling
Usage Example
Message Format Conversion
AWSBedrockLLMContext automatically handles the conversion between OpenAI-style messages and AWS Bedrock’s format:
- System messages are handled separately in AWS Bedrock
- User and assistant messages are converted to AWS Bedrock’s format
- Function calls are mapped to AWS Bedrock’s “toolUse” format
- Function results are mapped to AWS Bedrock’s “toolResult” format
Methods
See the LLM base class methods for additional functionality.
Function Calling
AWS Bedrock LLM service supports function calling (tool calling) which allows the LLM to request information from external services and APIs. The service automatically handles the conversion between OpenAI-style function calls and AWS Bedrock’s tool format.
Function Calling Guide
Learn how to implement function calling with standardized schemas, register handlers, manage context properly, and control execution flow in your conversational AI applications.
Function Call Example
Multimodal Support
AWSBedrockLLMService supports processing images with compatible models (such as Claude 3):
The service automatically handles the base64 encoding and proper formatting required by AWS Bedrock for image processing.
Usage Examples
Basic Usage
With Function Calling
Frame Flow
Metrics Support
The service collects various metrics:
- Token usage (prompt and completion)
- Processing time
- Time to first byte (TTFB)
- Cache utilization (if available)
Notes
- Supports streaming responses
- Handles function calling with AWS Bedrock’s “toolUse” format
- Provides OpenAI compatibility layer
- Manages conversation context with proper format conversion
- Supports vision inputs for compatible models
- Includes metrics collection
- Thread-safe processing
- Handles reconnection and error recovery