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 ⇒ 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_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_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
#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
79 80 81 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 79 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
94 95 96 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 94 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_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
73 74 75 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 73 def model body.modelId 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
85 86 87 88 89 90 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 85 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
61 62 63 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 61 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
67 68 69 |
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 67 def usage super end |