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
-
#content! ⇒ LLM::Object
private
Returns the LLM response after parsing it as JSON.
-
#id ⇒ String?
private
Returns the Bedrock request id when present.
-
#input_tokens ⇒ Integer
private
Returns the number of input tokens.
-
#output_tokens ⇒ Integer
private
Returns the number of output tokens.
-
#reasoning_tokens ⇒ Integer
private
Returns the number of reasoning tokens.
-
#input_audio_tokens ⇒ Integer
private
Returns the number of input audio tokens, or 0 when the provider does not report input audio usage.
-
#output_audio_tokens ⇒ Integer
private
Returns the number of output audio tokens, or 0 when the provider does not report output 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.
-
#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.
-
#total_tokens ⇒ Integer
private
Returns the total number of tokens.
-
#usage ⇒ LLM::Usage
private
Returns usage information.
-
#model ⇒ String
private
Returns the model name.
-
#content ⇒ String
private
Returns the LLM response.
-
#reasoning_content ⇒ String?
private
Returns the reasoning content when the provider exposes it.
-
#messages ⇒ Array<LLM::Messsage>
(also: #choices)
private
Returns one or more messages.
Methods included from Contract
Instance Method Details
#content! ⇒ LLM::Object
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
123 124 125 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 123 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.
36 37 38 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 36 def id res["x-amzn-requestid"] || res["x-amzn-request-id"] 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
42 43 44 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 42 def input_tokens body.usage&.inputTokens || 0 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
48 49 50 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 48 def output_tokens body.usage&.outputTokens || 0 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
54 55 56 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 54 def reasoning_tokens 0 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
60 61 62 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 60 def input_audio_tokens super 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
66 67 68 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 66 def output_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
72 73 74 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 72 def input_image_tokens super end |
#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
78 79 80 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 78 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
84 85 86 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 84 def cache_write_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
90 91 92 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 90 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
96 97 98 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 96 def usage super 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
102 103 104 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 102 def model body.modelId 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
108 109 110 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 108 def content super 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
114 115 116 117 118 119 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 114 def reasoning_content @reasoning_content ||= begin text = parts.filter_map { _1.dig("reasoningContent", "text") }.join text.empty? ? nil : text end 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 |
# 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) } LLM::Message.new(role, choice["text"], extra) end end |