Documentation
¶
Overview ¶
Package bedrock provides a provider implementation for AWS Bedrock.
AWS Bedrock provides access to foundation models from various providers (Anthropic, AI21, Cohere, Amazon, Meta, etc.) through a unified API.
Key differences from OpenAI:
- Endpoint: https://bedrock-runtime.{region}.amazonaws.com/model/{modelId}/invoke
- Auth: AWS Signature V4 (requires AWS credentials)
- Uses Converse API for unified request format across models
- Model IDs include provider prefix (e.g., "anthropic.claude-3-sonnet-20240229-v1:0")
Basic usage:
provider, _ := bedrock.New("us-east-1", "AKIA...", "secret...", "")
proxy := llmproxy.NewProxy(provider)
Index ¶
- func ParseConverseRequest(data []byte) (llmproxy.BodyMetadata, error)
- type CacheDetail
- type CachePoint
- type ContentBlock
- type ConverseRequest
- type ConverseResponse
- type Enricher
- type Extractor
- type ImageSource
- type ImageSourceData
- type InferenceConfig
- type Message
- type ModelIDExtractor
- type Output
- type OutputMessage
- type Parser
- type Provider
- type Resolver
- type ResponseMetrics
- type ResponseUsage
- type S3Location
- type SystemBlock
- type Tool
- type ToolConfig
- type ToolResult
- type ToolSpec
- type ToolUse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseConverseRequest ¶
func ParseConverseRequest(data []byte) (llmproxy.BodyMetadata, error)
ParseConverseRequest parses raw bytes as a Converse request.
Types ¶
type CacheDetail ¶ added in v0.0.3
type CacheDetail struct {
TTL string `json:"ttl"`
CacheWriteInputTokens int `json:"cacheWriteInputTokens"`
}
CacheDetail contains cache details for a checkpoint.
type CachePoint ¶ added in v0.0.3
CachePoint represents a cache checkpoint for prompt caching.
type ContentBlock ¶
type ContentBlock struct {
Text string `json:"text,omitempty"`
Image *ImageSource `json:"image,omitempty"`
ToolUse *ToolUse `json:"toolUse,omitempty"`
ToolResult *ToolResult `json:"toolResult,omitempty"`
CachePoint *CachePoint `json:"cachePoint,omitempty"`
}
ContentBlock represents a content block (text, image, tool use, etc.).
type ConverseRequest ¶
type ConverseRequest struct {
ModelID string `json:"modelId,omitempty"`
Messages []Message `json:"messages"`
System []SystemBlock `json:"system,omitempty"`
InferenceConfig InferenceConfig `json:"inferenceConfig,omitempty"`
ToolConfig *ToolConfig `json:"toolConfig,omitempty"`
Custom map[string]any `json:"-"`
}
ConverseRequest represents a Bedrock Converse API request.
func (*ConverseRequest) UnmarshalJSON ¶
func (r *ConverseRequest) UnmarshalJSON(data []byte) error
UnmarshalJSON captures unknown fields into Custom.
type ConverseResponse ¶
type ConverseResponse struct {
RequestID string `json:"requestId,omitempty"`
ModelID string `json:"modelId,omitempty"`
Output *Output `json:"output,omitempty"`
Usage ResponseUsage `json:"usage"`
StopReason string `json:"stopReason,omitempty"`
Metrics *ResponseMetrics `json:"metrics,omitempty"`
}
ConverseResponse represents a Bedrock Converse API response.
type Enricher ¶
type Enricher struct {
AccessKeyID string
SecretAccessKey string
SessionToken string
Region string
Service string
}
Enricher implements llmproxy.RequestEnricher for AWS Bedrock. It handles AWS Signature V4 authentication.
func NewEnricher ¶
NewEnricher creates a new Bedrock enricher with AWS credentials.
Parameters:
- region: AWS region (e.g., "us-east-1", "us-west-2")
- accessKeyID: AWS Access Key ID
- secretAccessKey: AWS Secret Access Key
- sessionToken: AWS Session Token (optional, for temporary credentials)
func NewEnricherWithService ¶
func NewEnricherWithService(region, accessKeyID, secretAccessKey, sessionToken, service string) *Enricher
NewEnricherWithService creates an enricher with a custom service name. Use "bedrock-runtime" for the runtime API.
type Extractor ¶
type Extractor struct{}
Extractor implements llmproxy.ResponseExtractor for Bedrock responses.
func NewExtractor ¶
func NewExtractor() *Extractor
NewExtractor creates a new Bedrock response extractor.
type ImageSource ¶
type ImageSource struct {
Format string `json:"format"`
Source ImageSourceData `json:"source"`
}
ImageSource represents an image in a content block.
type ImageSourceData ¶
type ImageSourceData struct {
Bytes []byte `json:"bytes,omitempty"`
S3Location *S3Location `json:"s3Location,omitempty"`
}
ImageSourceData contains the image data.
type InferenceConfig ¶
type InferenceConfig struct {
MaxTokens int `json:"maxTokens,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"topP,omitempty"`
StopSequences []string `json:"stopSequences,omitempty"`
}
InferenceConfig contains inference parameters.
type Message ¶
type Message struct {
Role string `json:"role"`
Content []ContentBlock `json:"content"`
}
Message represents a message in a Converse request.
type ModelIDExtractor ¶
type ModelIDExtractor struct{}
ModelIDExtractor extracts the model ID from a request body. This is useful when the model ID is in the body and needs to be used in the URL path.
func (*ModelIDExtractor) Extract ¶
func (e *ModelIDExtractor) Extract(body io.ReadCloser) (string, []byte, error)
Extract reads the body and returns the model ID.
type Output ¶
type Output struct {
Message *OutputMessage `json:"message,omitempty"`
}
Output contains the model's response.
type OutputMessage ¶
type OutputMessage struct {
Role string `json:"role"`
Content []ContentBlock `json:"content"`
}
OutputMessage represents the assistant's response message.
type Parser ¶
type Parser struct{}
Parser implements llmproxy.BodyParser for Bedrock's Converse API format.
func (*Parser) Parse ¶
func (p *Parser) Parse(body io.ReadCloser) (llmproxy.BodyMetadata, []byte, error)
Parse reads a Bedrock Converse API request and extracts metadata. Bedrock uses a unified format that works across different model providers.
type Provider ¶
type Provider struct {
*llmproxy.BaseProvider
}
Provider is an AWS Bedrock provider implementation.
func New ¶
New creates a new Bedrock provider with AWS credentials. Uses the Converse API which provides a unified format across models.
Parameters:
- region: AWS region (e.g., "us-east-1", "us-west-2", "eu-west-1")
- accessKeyID: AWS Access Key ID
- secretAccessKey: AWS Secret Access Key
- sessionToken: AWS Session Token (optional, pass "" for long-term credentials)
Example:
// Long-term credentials
provider, _ := bedrock.New("us-east-1", "AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "")
// Temporary credentials (from AssumeRole, etc.)
provider, _ := bedrock.New("us-east-1", "AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "AQoDYXdzEJr...")
type Resolver ¶
Resolver implements llmproxy.URLResolver for AWS Bedrock.
func NewInvokeResolver ¶
NewInvokeResolver creates a new Bedrock resolver for the Invoke API.
func NewResolver ¶
NewResolver creates a new Bedrock resolver for the Converse API.
type ResponseMetrics ¶
type ResponseMetrics struct {
LatencyMs int64 `json:"latencyMs"`
}
ResponseMetrics contains performance metrics.
type ResponseUsage ¶
type ResponseUsage struct {
InputTokens int `json:"inputTokens"`
OutputTokens int `json:"outputTokens"`
TotalTokens int `json:"totalTokens"`
CacheReadInputTokens int `json:"cacheReadInputTokens,omitempty"`
CacheWriteInputTokens int `json:"cacheWriteInputTokens,omitempty"`
CacheDetails []CacheDetail `json:"cacheDetails,omitempty"`
}
ResponseUsage contains token usage information.
type S3Location ¶
type S3Location struct {
URI string `json:"uri"`
}
S3Location represents an S3 location for image data.
type SystemBlock ¶
type SystemBlock struct {
Text string `json:"text"`
CachePoint *CachePoint `json:"cachePoint,omitempty"`
}
SystemBlock represents a system message block.
type Tool ¶
type Tool struct {
ToolSpec *ToolSpec `json:"toolSpec,omitempty"`
CachePoint *CachePoint `json:"cachePoint,omitempty"`
}
Tool represents a tool definition.
type ToolConfig ¶
ToolConfig contains tool configuration.
type ToolResult ¶
type ToolResult struct {
ToolUseID string `json:"toolUseId"`
Content []ContentBlock `json:"content"`
Status string `json:"status,omitempty"`
}
ToolResult represents a tool execution result.