Module: LLM::Bedrock::ResponseAdapter::Completion Private
- Includes:
- Contract::Completion
- Defined in:
- lib/llm/providers/bedrock/response_adapter/completion.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Adapts Bedrock Converse API completion responses.
The Bedrock Converse response body looks like:
{
"output" => {"message" => {
"role" => "assistant",
"content" => [{"text" => "..."}, {"toolUse" => {...}}]
}},
"usage" => {"inputTokens" => N, "outputTokens" => N},
"modelId" => "anthropic.claude-sonnet-4-20250514-v1:0",
"stopReason" => "end_turn"
}
Constant Summary
Constants included from Contract
Instance Method Summary collapse
-
#cache_read_tokens ⇒ Integer
private
Returns the number of cached input tokens, or 0 when the provider does not report cache usage.
-
#cache_write_tokens ⇒ Integer
private
Returns the number of cache creation input tokens, or 0 when the provider does not report cache creation usage.
-
#content ⇒ String
private
Returns the LLM response.
-
#content! ⇒ Hash
private
Returns the LLM response after parsing it as JSON.
-
#id ⇒ String?
private
Returns the Bedrock request id when present.
-
#input_audio_tokens ⇒ Integer
private
Returns the number of input audio tokens, or 0 when the provider does not report input audio usage.
-
#input_image_tokens ⇒ Integer
private
Returns the number of input image tokens, or 0 when the provider does not report input image usage.
-
#input_tokens ⇒ Integer
private
Returns the number of input tokens.
-
#messages ⇒ Array<LLM::Messsage>
(also: #choices)
private
Returns one or more messages.
-
#model ⇒ String
private
Returns the model name.
-
#output_audio_tokens ⇒ Integer
private
Returns the number of output audio tokens, or 0 when the provider does not report output audio usage.
-
#output_tokens ⇒ Integer
private
Returns the number of output tokens.
-
#reasoning_content ⇒ String?
private
Returns the reasoning content when the provider exposes it.
-
#reasoning_tokens ⇒ Integer
private
Returns the number of reasoning tokens.
-
#total_tokens ⇒ Integer
private
Returns the total number of tokens.
-
#usage ⇒ LLM::Usage
private
Returns usage information.
Methods included from Contract
Instance Method Details
#cache_read_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of cached input tokens, or 0 when the provider does not report cache usage
79 80 81 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 79 def cache_read_tokens 0 end |
#cache_write_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of cache creation input tokens, or 0 when the provider does not report cache creation usage
85 86 87 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 85 def cache_write_tokens 0 end |
#content ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the LLM response
109 110 111 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 109 def content super end |
#content! ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the LLM response after parsing it as JSON
124 125 126 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 124 def content! super end |
#id ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the Bedrock request id when present.
37 38 39 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 37 def id res["x-amzn-requestid"] || res["x-amzn-request-id"] end |
#input_audio_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of input audio tokens, or 0 when the provider does not report input audio usage
61 62 63 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 61 def input_audio_tokens super end |
#input_image_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of input image tokens, or 0 when the provider does not report input image usage
73 74 75 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 73 def input_image_tokens super end |
#input_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of input tokens
43 44 45 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 43 def input_tokens body.usage&.inputTokens || 0 end |
#messages ⇒ Array<LLM::Messsage> Also known as: choices
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns one or more messages
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 20 def source = texts.empty? && tools.any? ? [{"text" => ""}] : texts source.map.with_index do |choice, index| extra = { index:, response: self, reasoning_content:, tool_calls: adapt_tool_calls(tools), original_tool_calls: tools } LLM::Message.new(role, choice["text"], extra) end end |
#model ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the model name
103 104 105 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 103 def model body.modelId end |
#output_audio_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of output audio tokens, or 0 when the provider does not report output audio usage
67 68 69 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 67 def output_audio_tokens super end |
#output_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of output tokens
49 50 51 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 49 def output_tokens body.usage&.outputTokens || 0 end |
#reasoning_content ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the reasoning content when the provider exposes it
115 116 117 118 119 120 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 115 def reasoning_content @reasoning_content ||= begin text = parts.filter_map { _1.dig("reasoningContent", "text") }.join text.empty? ? nil : text end end |
#reasoning_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of reasoning tokens
55 56 57 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 55 def reasoning_tokens 0 end |
#total_tokens ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the total number of tokens
91 92 93 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 91 def total_tokens input_tokens + output_tokens end |
#usage ⇒ LLM::Usage
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns usage information
97 98 99 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 97 def usage super end |