Module: RubyLLM::Providers::Bedrock::Models

Included in:
RubyLLM::Providers::Bedrock
Defined in:
lib/ruby_llm/providers/bedrock/models.rb

Overview

Models methods for AWS Bedrock.

Constant Summary collapse

REGION_PREFIXES =
%w[us eu ap sa ca me af il].freeze

Class Method Summary collapse

Class Method Details

.create_model_info(model_data, slug, _capabilities = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 26

def create_model_info(model_data, slug, _capabilities = nil)
  model_id = model_id_with_region(model_data['modelId'], model_data)
  converse_data = model_data['converse'] || {}

  Model::Info.new(
    id: model_id,
    name: model_data['modelName'],
    provider: slug,
    family: model_data['modelFamily'] || model_data['providerName']&.downcase,
    created_at: nil,
    context_window: parse_context_window(model_data),
    max_output_tokens: converse_data['maxTokensDefault'] || converse_data['maxTokensMaximum'],
    modalities: {
      input: normalize_modalities(model_data['inputModalities']),
      output: normalize_modalities(model_data['outputModalities'])
    },
    capabilities: parse_capabilities(model_data),
    pricing: {},
    metadata: {
      provider_name: model_data['providerName'],
      model_arn: model_data['modelArn'],
      inference_types: model_data['inferenceTypesSupported'],
      converse: converse_data
    }
  )
end

.model_id_with_region(model_id, model_data) ⇒ Object



53
54
55
56
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 53

def model_id_with_region(model_id, model_data)
  inference_types = Array(model_data['inferenceTypesSupported'])
  normalize_inference_profile_id(model_id, inference_types, @config.bedrock_region)
end

.models_api_baseObject



12
13
14
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 12

def models_api_base
  "https://bedrock.#{bedrock_region}.amazonaws.com"
end

.models_urlObject



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

def models_url
  '/foundation-models'
end

.normalize_inference_profile_id(model_id, inference_types, region) ⇒ Object



58
59
60
61
62
63
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 58

def normalize_inference_profile_id(model_id, inference_types, region)
  return model_id unless inference_types.include?('INFERENCE_PROFILE')
  return model_id if inference_types.include?('ON_DEMAND')

  with_region_prefix(model_id, region)
end

.normalize_modalities(modalities) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 85

def normalize_modalities(modalities)
  Array(modalities).map do |modality|
    normalized = modality.to_s.downcase
    case normalized
    when 'embedding' then 'embeddings'
    when 'speech' then 'audio'
    else normalized
    end
  end
end

.parse_capabilities(model_data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 96

def parse_capabilities(model_data)
  capabilities = []
  capabilities << 'streaming' if model_data['responseStreamingSupported']

  converse = model_data['converse'] || {}
  capabilities << 'function_calling' if converse.is_a?(Hash)
  capabilities << 'reasoning' if converse.dig('reasoningSupported', 'embedded')

  capabilities
end

.parse_context_window(model_data) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 114

def parse_context_window(model_data)
  value = model_data.dig('description', 'maxContextWindow')
  return unless value.is_a?(String)

  if value.match?(/\A\d+[kK]\z/)
    value.to_i * 1000
  elsif value.match?(/\A\d+\z/)
    value.to_i
  end
end

.parse_list_models_response(response, slug, _capabilities) ⇒ Object



20
21
22
23
24
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 20

def parse_list_models_response(response, slug, _capabilities)
  Array(response.body['modelSummaries']).map do |model_data|
    create_model_info(model_data, slug)
  end
end

.reasoning_embedded?(model) ⇒ Boolean



107
108
109
110
111
112
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 107

def reasoning_embedded?(model)
   = RubyLLM::Utils.deep_symbolize_keys(model. || {})
  converse = [:converse] || {}
  reasoning_supported = converse[:reasoningSupported] || {}
  reasoning_supported[:embedded] || false
end

.region_prefix(region) ⇒ Object



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

def region_prefix(region)
  prefix = region.to_s.split('-').first
  prefix = '' if prefix.nil?
  prefix.empty? ? 'us' : prefix
end

.region_prefixed?(model_id) ⇒ Boolean



81
82
83
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 81

def region_prefixed?(model_id)
  model_id.match?(/\A(?:#{REGION_PREFIXES.join('|')})\./)
end

.with_region_prefix(model_id, region) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/ruby_llm/providers/bedrock/models.rb', line 65

def with_region_prefix(model_id, region)
  prefix = region_prefix(region)

  if region_prefixed?(model_id)
    model_id.sub(/\A(?:#{REGION_PREFIXES.join('|')})\./, "#{prefix}.")
  else
    "#{prefix}.#{model_id}"
  end
end