Class: Ollama::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/response.rb

Overview

Response wrapper for chat() that provides method access to response data

Example:

response = client.chat(messages: [...])
response.message&.content      # Access content
response.message&.thinking     # Access thinking output
response.message&.tool_calls   # Access tool_calls
response.done?                 # Check if generation finished
response.done_reason           # Why generation stopped
response.total_duration        # Total time in nanoseconds

Defined Under Namespace

Classes: Message

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Response

Returns a new instance of Response.



17
18
19
# File 'lib/ollama/response.rb', line 17

def initialize(data)
  @data = data || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Delegate unknown methods to underlying hash for forward compatibility. This allows accessing any raw response fields not covered by explicit accessors.



120
121
122
123
124
# File 'lib/ollama/response.rb', line 120

def method_missing(method, ...)
  return super unless @data.respond_to?(method)

  @data.public_send(method, ...)
end

Instance Method Details

#[](key) ⇒ Object

Delegate hash access to underlying data



114
115
116
# File 'lib/ollama/response.rb', line 114

def [](key)
  @data[key]
end

#contentObject

Convenient content accessor (shorthand for message&.content)



109
110
111
# File 'lib/ollama/response.rb', line 109

def content
  message&.content
end

#created_atObject

ISO 8601 timestamp of response creation



45
46
47
# File 'lib/ollama/response.rb', line 45

def created_at
  @data["created_at"] || @data[:created_at]
end

#done?Boolean

Whether generation has finished

Returns:

  • (Boolean)


30
31
32
# File 'lib/ollama/response.rb', line 30

def done?
  @data["done"] || @data[:done] || false
end

#done_reasonObject

Reason the generation stopped



35
36
37
# File 'lib/ollama/response.rb', line 35

def done_reason
  @data["done_reason"] || @data[:done_reason]
end

#eval_countObject

Number of tokens generated in the response



70
71
72
# File 'lib/ollama/response.rb', line 70

def eval_count
  @data["eval_count"] || @data[:eval_count]
end

#eval_durationObject

Time spent generating tokens in nanoseconds



75
76
77
# File 'lib/ollama/response.rb', line 75

def eval_duration
  @data["eval_duration"] || @data[:eval_duration]
end

#latency_msFloat?

Total generation latency in milliseconds (converted from nanoseconds). Returns nil when timing data is unavailable.

Returns:

  • (Float, nil)


97
98
99
100
101
# File 'lib/ollama/response.rb', line 97

def latency_ms
  return nil unless total_duration

  (total_duration / 1_000_000.0).round(2)
end

#load_durationObject

Time spent loading the model in nanoseconds



55
56
57
# File 'lib/ollama/response.rb', line 55

def load_duration
  @data["load_duration"] || @data[:load_duration]
end

#logprobsObject

Log probability information when logprobs are enabled



80
81
82
# File 'lib/ollama/response.rb', line 80

def logprobs
  @data["logprobs"] || @data[:logprobs]
end

#messageObject

Access the message object



22
23
24
25
26
27
# File 'lib/ollama/response.rb', line 22

def message
  msg = @data["message"] || @data[:message]
  return nil unless msg

  Message.new(msg)
end

#modelObject

Model name used



40
41
42
# File 'lib/ollama/response.rb', line 40

def model
  @data["model"] || @data[:model]
end

#prompt_eval_countObject

Number of tokens in the prompt



60
61
62
# File 'lib/ollama/response.rb', line 60

def prompt_eval_count
  @data["prompt_eval_count"] || @data[:prompt_eval_count]
end

#prompt_eval_durationObject

Time spent evaluating the prompt in nanoseconds



65
66
67
# File 'lib/ollama/response.rb', line 65

def prompt_eval_duration
  @data["prompt_eval_duration"] || @data[:prompt_eval_duration]
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/ollama/response.rb', line 126

def respond_to_missing?(method, include_private = false)
  @data.respond_to?(method, include_private) || super
end

#to_hObject

Access raw data as hash



104
105
106
# File 'lib/ollama/response.rb', line 104

def to_h
  @data
end

#total_durationObject

Total time spent generating in nanoseconds



50
51
52
# File 'lib/ollama/response.rb', line 50

def total_duration
  @data["total_duration"] || @data[:total_duration]
end

#usageHash

Aggregated token usage for observability and cost tracking.

Returns:

  • (Hash)

    { prompt_tokens:, completion_tokens:, total_tokens: }



86
87
88
89
90
91
92
# File 'lib/ollama/response.rb', line 86

def usage
  {
    prompt_tokens: prompt_eval_count,
    completion_tokens: eval_count,
    total_tokens: (prompt_eval_count.to_i + eval_count.to_i).then { |v| v.zero? ? nil : v }
  }.compact
end