Class: RubyLLM::Providers::Bedrock

Inherits:
RubyLLM::Provider show all
Includes:
Anthropic::Tools, Chat, Media, Models, Signing, Streaming
Defined in:
lib/ruby_llm/providers/bedrock.rb,
lib/ruby_llm/providers/bedrock/chat.rb,
lib/ruby_llm/providers/bedrock/media.rb,
lib/ruby_llm/providers/bedrock/models.rb,
lib/ruby_llm/providers/bedrock/signing.rb,
lib/ruby_llm/providers/bedrock/streaming.rb,
lib/ruby_llm/providers/bedrock/capabilities.rb,
lib/ruby_llm/providers/bedrock/streaming/base.rb,
lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb,
lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb,
lib/ruby_llm/providers/bedrock/streaming/message_processing.rb,
lib/ruby_llm/providers/bedrock/streaming/payload_processing.rb

Overview

AWS Bedrock API integration.

Defined Under Namespace

Modules: Capabilities, Chat, Media, Models, Signing, Streaming

Instance Attribute Summary

Attributes inherited from RubyLLM::Provider

#config, #connection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Anthropic::Tools

default_input_schema, extract_tool_calls, find_tool_uses, format_tool_call, format_tool_result, format_tool_result_block, format_tool_use_block, function_for, parse_tool_calls

Methods included from Media

format_content, format_image, format_pdf

Methods included from Anthropic::Media

format_content, format_image, format_pdf, format_text, format_text_file

Methods included from Models

create_model_info, inference_profile_region_prefix, #list_models, model_id_with_region, models_url, parse_list_models_response

Methods included from Streaming::Base

#handle_stream, included, #stream_response, #stream_url

Methods included from Chat

format_basic_message, format_message, sync_response

Methods inherited from RubyLLM::Provider

#capabilities, #complete, #configuration_requirements, configured?, #configured?, configured_providers, configured_remote_providers, #embed, for, #format_messages, #format_tool_calls, #headers, #initialize, #list_models, #local?, local?, local_providers, #moderate, #name, name, #paint, #parse_tool_calls, providers, register, remote?, #remote?, remote_providers, resolve, slug, #slug, #transcribe

Methods included from Streaming

handle_stream, stream_response

Constructor Details

This class inherits a constructor from RubyLLM::Provider

Class Method Details

.capabilitiesObject



72
73
74
# File 'lib/ruby_llm/providers/bedrock.rb', line 72

def capabilities
  Bedrock::Capabilities
end

.configuration_requirementsObject



76
77
78
# File 'lib/ruby_llm/providers/bedrock.rb', line 76

def configuration_requirements
  %i[bedrock_api_key bedrock_secret_key bedrock_region]
end

Instance Method Details

#api_baseObject



17
18
19
# File 'lib/ruby_llm/providers/bedrock.rb', line 17

def api_base
  "https://bedrock-runtime.#{@config.bedrock_region}.amazonaws.com"
end

#build_headers(signature_headers, streaming: false) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/ruby_llm/providers/bedrock.rb', line 62

def build_headers(signature_headers, streaming: false)
  accept_header = streaming ? 'application/vnd.amazon.eventstream' : 'application/json'

  signature_headers.merge(
    'Content-Type' => 'application/json',
    'Accept' => accept_header
  )
end

#build_request(url, method: :post, payload: nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/ruby_llm/providers/bedrock.rb', line 53

def build_request(url, method: :post, payload: nil)
  {
    connection: @connection,
    http_method: method,
    url: url || completion_url,
    body: payload ? JSON.generate(payload, ascii_only: false) : nil
  }
end

#create_signerObject



43
44
45
46
47
48
49
50
51
# File 'lib/ruby_llm/providers/bedrock.rb', line 43

def create_signer
  Signing::Signer.new({
                        access_key_id: @config.bedrock_api_key,
                        secret_access_key: @config.bedrock_secret_key,
                        session_token: @config.bedrock_session_token,
                        region: @config.bedrock_region,
                        service: 'bedrock'
                      })
end

#parse_error(response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/providers/bedrock.rb', line 21

def parse_error(response)
  return if response.body.empty?

  body = try_parse_json(response.body)
  case body
  when Hash
    body['message']
  when Array
    body.map do |part|
      part['message']
    end.join('. ')
  else
    body
  end
end

#sign_request(url, method: :post, payload: nil) ⇒ Object



37
38
39
40
41
# File 'lib/ruby_llm/providers/bedrock.rb', line 37

def sign_request(url, method: :post, payload: nil)
  signer = create_signer
  request = build_request(url, method:, payload:)
  signer.sign_request(request)
end