Module: LLM::Contract::Completion Abstract

Overview

This module is abstract.

Defines the interface all completion responses must implement

Constant Summary

Constants included from LLM::Contract

ContractError

Instance Method Summary collapse

Methods included from LLM::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



67
68
69
# File 'lib/llm/contract/completion.rb', line 67

def cache_read_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



75
76
77
# File 'lib/llm/contract/completion.rb', line 75

def cache_write_tokens
  0
end

#contentString

Returns the LLM response

Returns:

  • (String)

    Returns the LLM response



89
90
91
# File 'lib/llm/contract/completion.rb', line 89

def content
  messages.find(&:assistant?).content
end

#content!Hash

Returns the LLM response after parsing it as JSON

Returns:

  • (Hash)

    Returns the LLM response after parsing it as JSON



103
104
105
# File 'lib/llm/contract/completion.rb', line 103

def content!
  LLM.json.load(content)
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



43
44
45
# File 'lib/llm/contract/completion.rb', line 43

def input_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



59
60
61
# File 'lib/llm/contract/completion.rb', line 59

def input_image_tokens
  0
end

#input_tokensInteger

Returns the number of input tokens

Returns:

  • (Integer)

    Returns the number of input tokens

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/llm/contract/completion.rb', line 21

def input_tokens
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

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

Returns one or more messages

Returns:

  • (Array<LLM::Messsage>)

    Returns one or more messages

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/llm/contract/completion.rb', line 13

def messages
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#modelString

Returns the model name

Returns:

  • (String)

    Returns the model name

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/llm/contract/completion.rb', line 127

def model
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
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



51
52
53
# File 'lib/llm/contract/completion.rb', line 51

def output_audio_tokens
  0
end

#output_tokensInteger

Returns the number of output tokens

Returns:

  • (Integer)

    Returns the number of output tokens

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/llm/contract/completion.rb', line 28

def output_tokens
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#reasoning_contentString?

Returns the reasoning content when the provider exposes it

Returns:

  • (String, nil)

    Returns the reasoning content when the provider exposes it



96
97
98
# File 'lib/llm/contract/completion.rb', line 96

def reasoning_content
  messages.find(&:assistant?)&.reasoning_content
end

#reasoning_tokensInteger

Returns the number of reasoning tokens

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/llm/contract/completion.rb', line 35

def reasoning_tokens
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#total_tokensInteger

Returns the total number of tokens

Returns:

  • (Integer)

    Returns the total number of tokens

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/llm/contract/completion.rb', line 82

def total_tokens
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#usageLLM::Usage

Returns usage information

Returns:



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/llm/contract/completion.rb', line 110

def usage
  LLM::Usage.new(
    input_tokens:,
    output_tokens:,
    reasoning_tokens:,
    input_audio_tokens:,
    output_audio_tokens:,
    input_image_tokens:,
    cache_read_tokens:,
    cache_write_tokens:,
    total_tokens:
  )
end