Module: Legion::Extensions::Llm::Bedrock::Provider::DispatchHelpers

Included in:
Legion::Extensions::Llm::Bedrock::Provider
Defined in:
lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb

Overview

Public dispatch surface: chat, stream, count_tokens, embed, complete. These methods form the contract between the framework executor and the Bedrock provider instance.

Instance Method Summary collapse

Instance Method Details

#chat(messages:, model:, temperature: nil, max_tokens: nil, tools: {}, tool_prefs: nil, params: {}, thinking: nil, **opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 12

def chat(
  messages:,
  model:,
  temperature: nil,
  max_tokens: nil,
  tools: {},
  tool_prefs: nil,
  params: {},
  thinking: nil,
  **opts
)
  # Passthrough request params that do not map to an explicit
  # keyword reach the Converse payload, never silently dropped.
  params = params.merge(opts)
  enforce_model_allowed!(model_id(model))
  log.info { "bedrock.provider.chat: model=#{model_id(model)} messages=#{messages.size}" }

  if anthropic_model?(model_id(model)) && (thinking || (tools && !tools.empty?))
    return invoke_model_chat(messages:, model:, temperature:, max_tokens:, tools:, tool_prefs:,
                             thinking:, params:)
  end

  request = Utils.deep_merge(
    converse_request(messages, model:, temperature:, max_tokens:, tools:, tool_prefs:, thinking:),
    params
  )
  log_chat_request(request, model, tools, params, tool_prefs)

  start_time = Time.now
  response = converse_with_error_log(request, model, start_time)
  elapsed = ((Time.now - start_time) * 1000).round
  log_chat_response(response, model, elapsed)
  parse_converse_response(response, model_id(model))
end

#complete(messages, tools:, temperature:, model:, params: {}, schema: nil, thinking: nil, tool_prefs: nil) ⇒ Object

The nameless ** accepts and ignores HTTP-style kwargs the base contract carries (headers:) — Bedrock transport is the AWS SDK, which owns its own request signing.



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 123

def complete(messages, tools:, temperature:, model:, params: {}, schema: nil,
             thinking: nil, tool_prefs: nil, **, &)
  payload = params.dup
  payload[:additional_model_request_fields] ||= {}
  payload[:additional_model_request_fields][:response_format] = schema if schema

  if block_given?
    stream(messages:, model:, temperature:, tools:, tool_prefs:, params: payload, thinking:, &)
  else
    chat(messages:, model:, temperature:, tools:, tool_prefs:, params: payload, thinking:)
  end
end

#count_tokens(messages:, model:, system: nil, params: {}, **opts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 80

def count_tokens(messages:, model:, system: nil, params: {}, **opts)
  # Passthrough request params that do not map to an explicit
  # keyword reach the CountTokens payload, never silently dropped.
  params = params.merge(opts)
  log.debug { "bedrock.provider.count_tokens: model=#{model_id(model)}" }
  request = Utils.deep_merge(
    {
      model_id: self.class.inference_profile_id(model_id(model), geo_prefix: geo_prefix),
      input: {
        converse: { messages: format_messages(messages), system: system_blocks(system) }.compact
      }
    },
    params
  )
  response = runtime_client.count_tokens(**request)
  { input_tokens: value(response, :input_tokens), raw: normalize_response(response) }
end

#embed(text:, model:, dimensions: nil, params: {}, **opts) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 98

def embed(text:, model:, dimensions: nil, params: {}, **opts)
  # Passthrough request params that do not map to an explicit
  # keyword reach the InvokeModel body, never silently dropped.
  params = params.merge(opts)
  mid = model_id(model)
  enforce_model_allowed!(mid)
  unless titan_embed?(mid)
    raise NotImplementedError,
          "Bedrock embedding payload for #{mid} is not standardized"
  end

  log.info { "bedrock.provider.embed: model=#{mid}" }
  body = Utils.deep_merge({ inputText: text, dimensions: dimensions }.compact, params)
  response = runtime_client.invoke_model(
    model_id: mid,
    content_type: 'application/json',
    accept: 'application/json',
    body: Legion::JSON.generate(body)
  )
  parse_embedding_response(response, model: mid)
end

#stream(messages:, model:, temperature: nil, max_tokens: nil, tools: {}, tool_prefs: nil, params: {}, thinking: nil, **opts) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 47

def stream(messages:, model:, temperature: nil, max_tokens: nil, tools: {}, tool_prefs: nil, params: {},
           thinking: nil, **opts, &)
  # Passthrough request params that do not map to an explicit
  # keyword reach the Converse payload, never silently dropped.
  params = params.merge(opts)
  enforce_model_allowed!(model_id(model))
  log.info do
    "bedrock.provider.stream: model=#{model_id(model)} messages=#{messages.size} tools=#{tools.size}"
  end

  if anthropic_model?(model_id(model)) && (thinking || (tools && !tools.empty?))
    return invoke_model_stream(messages:, model:, temperature:, max_tokens:, tools:, tool_prefs:,
                               thinking:, params:, &)
  end

  request = Utils.deep_merge(
    converse_request(messages, model:, temperature:, max_tokens:, tools:, tool_prefs:, thinking:),
    params
  )
  log.debug do
    "bedrock.provider.stream: request prepared model=#{model_id(model)} tools=#{tools.size} " \
      "tool_choice=#{tool_choice_label(tool_prefs)} param_keys=#{params.keys.map(&:to_s).sort.join(',')}"
  end
  thinking_config = request.dig(:additional_model_request_fields, :thinking)
  log.debug { "bedrock.provider.stream: thinking_config=#{thinking_config.inspect}" } if thinking_config

  start_time = Time.now
  result = stream_converse(request, model_id(model), &)
  elapsed = ((Time.now - start_time) * 1000).round
  log.debug { "bedrock.provider.stream: completed model=#{model_id(model)} elapsed_ms=#{elapsed}" }
  result
end