Module: LLM::OpenAI::ResponseAdapter::Completion

Includes:
Contract::Completion
Defined in:
lib/llm/providers/openai/response_adapter/completion.rb

Constant Summary

Constants included from Contract

Contract::ContractError

Instance Method Summary collapse

Methods included from Contract

#included

Instance Method Details

#cache_read_tokensInteger

Returns the number of cached input tokens, or 0 when the provider does not report cache usage

Returns:

  • (Integer)

    Returns the number of cached input tokens, or 0 when the provider does not report cache usage



72
73
74
75
76
77
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 72

def cache_read_tokens
  body
    .usage
    &.prompt_tokens_details
    &.cached_tokens || 0
end

#cache_write_tokensInteger

Returns the number of cache creation input tokens, or 0 when the provider does not report cache creation usage

Returns:

  • (Integer)

    Returns the number of cache creation input tokens, or 0 when the provider does not report cache creation usage



81
82
83
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 81

def cache_write_tokens
  0
end

#contentString

Returns the LLM response

Returns:

  • (String)

    Returns the LLM response



105
106
107
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 105

def content
  super
end

#content!Hash

Returns the LLM response after parsing it as JSON

Returns:

  • (Hash)

    Returns the LLM response after parsing it as JSON



117
118
119
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 117

def content!
  super
end

#input_audio_tokensInteger

Returns the number of input audio tokens, or 0 when the provider does not report input audio usage

Returns:

  • (Integer)

    Returns the number of input audio tokens, or 0 when the provider does not report input audio usage



45
46
47
48
49
50
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 45

def input_audio_tokens
  body
    .usage
    &.prompt_tokens_details
    &.audio_tokens || 0
end

#input_image_tokensInteger

Returns the number of input image tokens, or 0 when the provider does not report input image usage

Returns:

  • (Integer)

    Returns the number of input image tokens, or 0 when the provider does not report input image usage



63
64
65
66
67
68
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 63

def input_image_tokens
  body
    .usage
    &.prompt_tokens_details
    &.image_tokens || 0
end

#input_tokensInteger

Returns the number of input tokens

Returns:

  • (Integer)

    Returns the number of input tokens



24
25
26
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 24

def input_tokens
  body.usage&.prompt_tokens || 0
end

#messagesArray<LLM::Messsage> Also known as: choices

Returns one or more messages

Returns:

  • (Array<LLM::Messsage>)

    Returns one or more messages



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 7

def messages
  body.choices.map.with_index do |choice, index|
    message = choice.message
    extra = {
      index:, response: self,
      logprobs: choice.logprobs,
      reasoning_content: message.reasoning_content,
      tool_calls: adapt_tool_calls(message.tool_calls),
      original_tool_calls: message.tool_calls
    }
    LLM::Message.new(message.role, message.content, extra)
  end
end

#modelString

Returns the model name

Returns:

  • (String)

    Returns the model name



99
100
101
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 99

def model
  body.model
end

#output_audio_tokensInteger

Returns the number of output audio tokens, or 0 when the provider does not report output audio usage

Returns:

  • (Integer)

    Returns the number of output audio tokens, or 0 when the provider does not report output audio usage



54
55
56
57
58
59
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 54

def output_audio_tokens
  body
    .usage
    &.completion_tokens_details
    &.audio_tokens || 0
end

#output_tokensInteger

Returns the number of output tokens

Returns:

  • (Integer)

    Returns the number of output tokens



30
31
32
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 30

def output_tokens
  body.usage&.completion_tokens || 0
end

#reasoning_contentString?

Returns the reasoning content when the provider exposes it

Returns:

  • (String, nil)

    Returns the reasoning content when the provider exposes it



111
112
113
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 111

def reasoning_content
  super
end

#reasoning_tokensInteger

Returns the number of reasoning tokens

Returns:

  • (Integer)


36
37
38
39
40
41
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 36

def reasoning_tokens
  body
    .usage
    &.completion_tokens_details
    &.reasoning_tokens || 0
end

#total_tokensInteger

Returns the total number of tokens

Returns:

  • (Integer)

    Returns the total number of tokens



87
88
89
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 87

def total_tokens
  body.usage&.total_tokens || 0
end

#usageLLM::Usage

Returns usage information

Returns:



93
94
95
# File 'lib/llm/providers/openai/response_adapter/completion.rb', line 93

def usage
  super
end