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

Contract::ContractError

Instance Method Summary collapse

Methods included from Contract

#included

Instance Method Details

#contentString

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

Returns:

  • (String)

    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

Returns:

  • (Hash)

    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

#idString?

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.

Returns:

  • (String, nil)


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_tokensInteger

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

Returns:

  • (Integer)

    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

#messagesArray<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

Returns:

  • (Array<LLM::Messsage>)

    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 messages
  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

#modelString

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

Returns:

  • (String)

    Returns the model name



73
74
75
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 73

def model
  body.modelId
end

#output_tokensInteger

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

Returns:

  • (Integer)

    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_contentString?

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

Returns:

  • (String, nil)

    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_tokensInteger

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

Returns:

  • (Integer)


55
56
57
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 55

def reasoning_tokens
  0
end

#total_tokensInteger

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

Returns:

  • (Integer)

    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

#usageLLM::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

Returns:



67
68
69
# File 'lib/llm/providers/bedrock/response_adapter/completion.rb', line 67

def usage
  super
end