bedrock

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 13 Imported by: 0

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:

Basic usage:

provider, _ := bedrock.New("us-east-1", "AKIA...", "secret...", "")
proxy := llmproxy.NewProxy(provider)

Index

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

type CachePoint struct {
	Type string `json:"type"`
	TTL  string `json:"ttl,omitempty"`
}

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

func NewEnricher(region, accessKeyID, secretAccessKey, sessionToken string) *Enricher

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.

func (*Enricher) Enrich

func (e *Enricher) Enrich(req *http.Request, meta llmproxy.BodyMetadata, rawBody []byte) error

Enrich adds AWS Signature V4 headers to the request.

type Extractor

type Extractor struct{}

Extractor implements llmproxy.ResponseExtractor for Bedrock responses.

func NewExtractor

func NewExtractor() *Extractor

NewExtractor creates a new Bedrock response extractor.

func (*Extractor) Extract

func (e *Extractor) Extract(resp *http.Response) (llmproxy.ResponseMetadata, []byte, error)

Extract parses a Bedrock Converse response and returns unified metadata.

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

func New(region, accessKeyID, secretAccessKey, sessionToken string) (*Provider, error)

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...")

func NewWithConfig

func NewWithConfig(region, accessKeyID, secretAccessKey, sessionToken string, useConverseAPI bool) (*Provider, error)

NewWithConfig creates a Bedrock provider with full configuration.

type Resolver

type Resolver struct {
	Region      string
	UseConverse bool
}

Resolver implements llmproxy.URLResolver for AWS Bedrock.

func NewInvokeResolver

func NewInvokeResolver(region string) *Resolver

NewInvokeResolver creates a new Bedrock resolver for the Invoke API.

func NewResolver

func NewResolver(region string) *Resolver

NewResolver creates a new Bedrock resolver for the Converse API.

func (*Resolver) Resolve

func (r *Resolver) Resolve(meta llmproxy.BodyMetadata) (*url.URL, error)

Resolve returns the Bedrock endpoint URL for the given model. The URL format depends on whether we use the Converse or Invoke 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

type ToolConfig struct {
	Tools      []Tool `json:"tools"`
	ToolChoice any    `json:"toolChoice,omitempty"`
}

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.

type ToolSpec

type ToolSpec struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	InputSchema any    `json:"inputSchema"`
}

ToolSpec contains tool specification.

type ToolUse

type ToolUse struct {
	ToolUseID string `json:"toolUseId"`
	Name      string `json:"name"`
	Input     any    `json:"input"`
}

ToolUse represents a tool use request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL