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:, tools: {}, tool_prefs: nil, params: nil, thinking: nil) ⇒ Object

08 F3: the completion funnel receives canonical values only — params is a Canonical::Params (the callable folds the wire params at the boundary); temperature/max_tokens are params members (05 O4), never named completion keys.

B1: the dialect fork has one owner — ThinkingModes, shared with the Translator (the old object-truthiness thinking predicate is deleted). B3: tools/thinking are enforced canonical at this seam, once, before any rendering (the H3 funnel the custom funnel had skipped). B5: system-role messages are folded into the single system source at the edge; the dialect renderers read the folded value only.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 24

def chat(messages:, model:, tools: {}, tool_prefs: nil, params: nil, thinking: nil)
  enforce_canonical_messages!(messages)
  # H3: tools is Hash<name, ToolDefinition> or nil — nil
  # canonicalizes to the empty set (no tools).
  tools = enforce_canonical_tools!(tools) || {}
  enforce_canonical_thinking!(thinking)
  enforce_model_allowed!(model_id(model))
  system, messages = split_system_messages(messages)
  log.info { "bedrock.provider.chat: model=#{model_id(model)} messages=#{messages.size}" }

  if ThinkingModes.invoke_model_target?(model_id: model_id(model), thinking: thinking, tools: tools)
    return invoke_model_chat(messages:, model:, tools:, tool_prefs:, system:, thinking:, params:)
  end

  request = converse_request(messages, model:, params:, tools:, tool_prefs:, system:, thinking:)
  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:, model:, params: nil, 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
135
136
137
138
139
140
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 123

def complete(messages, tools:, model:, params: nil, schema: nil,
             thinking: nil, tool_prefs: nil, **, &)
  canonical = params
  if schema
    source = params ? params.to_h : {}
    canonical = Legion::Extensions::Llm::Canonical::Params.from_hash(
      source.merge(response_format: schema)
    )
  end

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

#count_tokens(messages:, model:, system: nil) ⇒ Object

B19: the exact execution binding carries no unowned payload — the CountTokens wire is model_id + input.converse only. The old params-merge (arbitrary fleet kwargs into the AWS SDK request) is deleted; the nameless ** absorbs and ignores unowned keys (like complete does).



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

def count_tokens(messages:, model:, system: nil, **)
  enforce_canonical_messages!(messages)
  log.debug { "bedrock.provider.count_tokens: model=#{model_id(model)}" }
  request = {
    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
    }
  }
  response = runtime_client.count_tokens(**request)
  { input_tokens: value(response, :input_tokens), raw: normalize_response(response) }
end

#embed(text:, model:, dimensions: nil) ⇒ Object

B19: the Titan InvokeModel body is the documented vocabulary (inputText, dimensions) — the old params deep_merge (arbitrary fleet kwargs silently into the wire body) is deleted; the nameless ** absorbs and ignores unowned keys.



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 101

def embed(text:, model:, dimensions: nil, **)
  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 = { inputText: text, dimensions: dimensions }.compact
  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, text: text)
end

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



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
# File 'lib/legion/extensions/llm/bedrock/provider/dispatch_helpers.rb', line 48

def stream(messages:, model:, tools: {}, tool_prefs: nil, params: nil, thinking: nil, &)
  enforce_canonical_messages!(messages)
  # H3: tools is Hash<name, ToolDefinition> or nil — nil
  # canonicalizes to the empty set (no tools).
  tools = enforce_canonical_tools!(tools) || {}
  enforce_canonical_thinking!(thinking)
  enforce_model_allowed!(model_id(model))
  system, messages = split_system_messages(messages)
  log.info do
    "bedrock.provider.stream: model=#{model_id(model)} messages=#{messages.size} tools=#{tools.size}"
  end

  if ThinkingModes.invoke_model_target?(model_id: model_id(model), thinking: thinking, tools: tools)
    return invoke_model_stream(messages:, model:, tools:, tool_prefs:, system:, thinking:, params:, &)
  end

  request = converse_request(messages, model:, params:, tools:, tool_prefs:, system:, thinking:)
  log.debug do
    "bedrock.provider.stream: request prepared model=#{model_id(model)} tools=#{tools.size} " \
      "tool_choice=#{tool_choice_label(tool_prefs)} param_keys=#{param_keys(params)}"
  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), request_id: request_id_from_params(params), &)
  elapsed = ((Time.now - start_time) * 1000).round
  log.debug { "bedrock.provider.stream: completed model=#{model_id(model)} elapsed_ms=#{elapsed}" }
  result
end