Class: RubyLLM::Providers::BedrockInvoke

Inherits:
Provider
  • Object
show all
Includes:
BedrockInvoke::EventStream, BedrockInvoke::Signing
Defined in:
lib/ruby_llm/bedrock_invoke/provider_v1.rb,
lib/ruby_llm/bedrock_invoke/provider_v2.rb

Overview

Anthropic Claude on AWS Bedrock via the InvokeModel API (RubyLLM 2.0).

Where/auth only — the wire format lives in RubyLLM::BedrockInvoke::Protocol. Credentials come from the standard AWS chain (env vars, shared config, IRSA/Pod Identity, IMDS) unless a credential provider is configured explicitly.

Constant Summary collapse

ANTHROPIC_VERSION =
'bedrock-2023-05-31'

Constants included from BedrockInvoke::EventStream

BedrockInvoke::EventStream::EXCEPTION_STATUS

Constants included from BedrockInvoke::Signing

BedrockInvoke::Signing::DEFAULT_CHAIN_MUTEX, BedrockInvoke::Signing::SERVICE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BedrockInvoke::EventStream

#each_event_stream_event, #event_stream_decoder

Methods included from BedrockInvoke::Signing

#bedrock_invoke_credentials, #bedrock_invoke_region, cached_default_chain_credentials, reset_default_chain_cache!, #sign_headers, #sigv4_signer

Class Method Details

.assume_models_exist?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 112

def assume_models_exist?
  true
end

.capabilitiesObject



95
96
97
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 95

def capabilities
  RubyLLM::BedrockInvoke::Capabilities
end

.configuration_optionsObject



103
104
105
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 103

def configuration_options
  RubyLLM::BedrockInvoke::CONFIGURATION_OPTIONS
end

.configuration_requirementsObject



99
100
101
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 99

def configuration_requirements
  []
end

.configured?(config) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 107

def configured?(config)
  region = (config.bedrock_invoke_region if config.respond_to?(:bedrock_invoke_region))
  !(region || ENV['AWS_REGION'] || ENV.fetch('AWS_DEFAULT_REGION', nil)).nil?
end

.slugObject



91
92
93
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 91

def slug
  'bedrock_invoke'
end

Instance Method Details

#api_baseObject



18
19
20
21
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 18

def api_base
  config_value(:bedrock_invoke_api_base) ||
    "https://bedrock-runtime.#{bedrock_invoke_region}.amazonaws.com"
end

#build_thinking_payload(thinking, model = nil) ⇒ Object

1.16 gates thinking on registry reasoning_options, which assumed models never carry; Bedrock Claude supports budget thinking, so emit it directly. Called as (thinking) on 1.13-1.15 (bare payload spliced into payload) and as (thinking, model) on 1.16 (wrapped shape unwrapped by add_thinking_fields).



83
84
85
86
87
88
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 83

def build_thinking_payload(thinking, model = nil)
  inner = RubyLLM::BedrockInvoke.budget_thinking_payload(thinking)
  return nil if inner.nil?

  model.nil? ? inner : { thinking: inner }
end

#completion_urlObject



27
28
29
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 27

def completion_url
  "/model/#{RubyLLM::BedrockInvoke.escape_model_id(@model_id)}/invoke"
end

#ensure_configured!Object

Raises:

  • (ConfigurationError)


25
26
27
28
29
30
31
# File 'lib/ruby_llm/bedrock_invoke/provider_v2.rb', line 25

def ensure_configured!
  return if self.class.configured?(@config)

  raise ConfigurationError,
        'No AWS region configured for bedrock_invoke. Set config.bedrock_invoke_region ' \
        'or the AWS_REGION environment variable.'
end

#format_message(msg) ⇒ Object

Messages preserving server tool-search blocks replay verbatim. Bypassing the stock formatter also avoids its thinking-block prepend, which would both duplicate thinking blocks (they are already in the raw array) and permanently mutate the preserved array via unshift.



71
72
73
74
75
76
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 71

def format_message(msg, **)
  blocks = RubyLLM::BedrockInvoke.raw_blocks(msg)
  return { role: 'assistant', content: blocks } if blocks

  super
end

#headersObject



23
24
25
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 23

def headers
  {}
end

#list_modelsObject



63
64
65
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 63

def list_models
  []
end

#parse_completion_response(response) ⇒ Object

rubocop:enable Metrics/ParameterLists



48
49
50
51
52
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 48

def parse_completion_response(response)
  message = super
  preserve_server_blocks(message, response)
  message
end

#parse_error(response) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 54

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

  body = try_parse_json(response.body)
  return body if body.is_a?(String)

  body['message'] || body['Message'] || body['error'] || body['__type'] || super
end

#render_payload(messages, tools:, temperature:, model:, stream: false, schema: nil, thinking: nil, tool_prefs: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 36

def render_payload(messages, tools:, temperature:, model:, stream: false,
                   schema: nil, thinking: nil, tool_prefs: nil)
  @model_id = model.id
  payload = super
  payload.delete(:model)
  payload.delete(:stream)
  payload[:anthropic_version] = ANTHROPIC_VERSION
  RubyLLM::BedrockInvoke::ToolSearch.apply!(payload, tools)
  payload
end

#stream_urlObject



31
32
33
# File 'lib/ruby_llm/bedrock_invoke/provider_v1.rb', line 31

def stream_url
  "/model/#{RubyLLM::BedrockInvoke.escape_model_id(@model_id)}/invoke-with-response-stream"
end