Amazon Bedrock Provider

The Amazon Bedrock provider connects to AWS Bedrock for Claude and other foundation models.

Installation

Add the AWS SDK gem to your Gemfile:

gem 'aws-sdk-bedrockruntime'

Configuration

Configure your AWS credentials using standard AWS methods (environment variables, IAM roles, etc.):

Riffer.configure do |config|
  config.amazon_bedrock.region = 'us-east-1'
end

Bearer Token Authentication

For API token authentication:

Riffer.configure do |config|
  config.amazon_bedrock.region = 'us-east-1'
  config.amazon_bedrock.api_token = ENV['BEDROCK_API_TOKEN']
end

Or per-agent:

class MyAgent < Riffer::Agent
  model 'amazon_bedrock/anthropic.claude-3-sonnet-20240229-v1:0'
  provider_options region: 'us-west-2', api_token: ENV['BEDROCK_API_TOKEN']
end

Supported Models

Use Bedrock model IDs in the amazon_bedrock/model format:

# Claude models
model 'amazon_bedrock/anthropic.claude-3-opus-20240229-v1:0'
model 'amazon_bedrock/anthropic.claude-3-sonnet-20240229-v1:0'
model 'amazon_bedrock/anthropic.claude-3-haiku-20240307-v1:0'

# Other foundation models available in Bedrock
model 'amazon_bedrock/amazon.titan-text-express-v1'

Model Options

Options are passed through to the Bedrock Converse API. Use the nested structures the API expects.

inference_config

Controls generation parameters:

model_options inference_config: {
  max_tokens: 4096,
  temperature: 0.7,
  top_p: 0.95,
  stop_sequences: ["\n\nHuman:"]
}

additional_model_request_fields

Model-specific parameters (e.g., top_k for Claude):

model_options additional_model_request_fields: {
  top_k: 250
}

Example

Riffer.configure do |config|
  config.amazon_bedrock.region = 'us-east-1'
end

class AssistantAgent < Riffer::Agent
  model 'amazon_bedrock/anthropic.claude-3-sonnet-20240229-v1:0'
  instructions 'You are a helpful assistant.'
  model_options inference_config: {temperature: 0.7, max_tokens: 4096}
end

agent = AssistantAgent.new
puts agent.generate("Explain cloud computing")

Streaming

agent.stream("Tell me about AWS services").each do |event|
  case event
  when Riffer::StreamEvents::TextDelta
    print event.content
  when Riffer::StreamEvents::TextDone
    puts "\n[Complete]"
  when Riffer::StreamEvents::ToolCallDone
    puts "[Tool: #{event.name}]"
  end
end

Tool Calling

Bedrock provider converts tools to the Bedrock tool_config format:

class S3ListTool < Riffer::Tool
  description "Lists objects in an S3 bucket"

  params do
    required :bucket, String, description: "The S3 bucket name"
    optional :prefix, String, description: "Object prefix filter"
  end

  def call(context:, bucket:, prefix: nil)
    # Implementation
    "Found 10 objects in #{bucket}"
  end
end

class AWSAgent < Riffer::Agent
  model 'amazon_bedrock/anthropic.claude-3-sonnet-20240229-v1:0'
  uses_tools [S3ListTool]
end

Message Format

The provider converts Riffer messages to Bedrock format:

Riffer Message Bedrock Format
System Added to system array as {text: ...}
User {role: "user", content: [{text: ...}]}
Assistant {role: "assistant", content: [...]}
Tool {role: "user", content: [{tool_result: ...}]}

Direct Provider Usage

provider = Riffer::Providers::AmazonBedrock.new(
  region: 'us-east-1',
  api_token: ENV['BEDROCK_API_TOKEN']  # Optional
)

response = provider.generate_text(
  prompt: "Hello!",
  model: "anthropic.claude-3-sonnet-20240229-v1:0",
  temperature: 0.7
)

puts response.content

AWS IAM Permissions

Ensure your IAM role/user has the following permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "bedrock:InvokeModel",
        "bedrock:InvokeModelWithResponseStream"
      ],
      "Resource": "arn:aws:bedrock:*::foundation-model/*"
    }
  ]
}