Class: Ollama::Response
- Inherits:
-
Object
- Object
- Ollama::Response
- 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
Direct Known Subclasses
Defined Under Namespace
Classes: Message
Instance Method Summary collapse
-
#[](key) ⇒ Object
Delegate hash access to underlying data.
-
#content ⇒ Object
Convenient content accessor (shorthand for message&.content).
-
#created_at ⇒ Object
ISO 8601 timestamp of response creation.
-
#done? ⇒ Boolean
Whether generation has finished.
-
#done_reason ⇒ Object
Reason the generation stopped.
-
#eval_count ⇒ Object
Number of tokens generated in the response.
-
#eval_duration ⇒ Object
Time spent generating tokens in nanoseconds.
-
#initialize(data) ⇒ Response
constructor
A new instance of Response.
-
#latency_ms ⇒ Float?
Total generation latency in milliseconds (converted from nanoseconds).
-
#load_duration ⇒ Object
Time spent loading the model in nanoseconds.
-
#logprobs ⇒ Object
Log probability information when logprobs are enabled.
-
#message ⇒ Object
Access the message object.
-
#method_missing(method) ⇒ Object
Delegate unknown methods to underlying hash for forward compatibility.
-
#model ⇒ Object
Model name used.
-
#prompt_eval_count ⇒ Object
Number of tokens in the prompt.
-
#prompt_eval_duration ⇒ Object
Time spent evaluating the prompt in nanoseconds.
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
-
#to_h ⇒ Object
Access raw data as hash.
-
#total_duration ⇒ Object
Total time spent generating in nanoseconds.
-
#usage ⇒ Hash
Aggregated token usage for observability and cost tracking.
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.
121 122 123 124 125 |
# File 'lib/ollama/response.rb', line 121 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
115 116 117 |
# File 'lib/ollama/response.rb', line 115 def [](key) @data[key] end |
#content ⇒ Object
Convenient content accessor (shorthand for message&.content)
110 111 112 |
# File 'lib/ollama/response.rb', line 110 def content &.content end |
#created_at ⇒ Object
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
30 31 32 |
# File 'lib/ollama/response.rb', line 30 def done? @data["done"] || @data[:done] || false end |
#done_reason ⇒ Object
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_count ⇒ Object
Number of tokens generated in the response
71 72 73 |
# File 'lib/ollama/response.rb', line 71 def eval_count @data["eval_count"] || @data[:eval_count] || @data.dig("usage", "completion_tokens") || @data.dig(:usage, :completion_tokens) end |
#eval_duration ⇒ Object
Time spent generating tokens in nanoseconds
76 77 78 |
# File 'lib/ollama/response.rb', line 76 def eval_duration @data["eval_duration"] || @data[:eval_duration] end |
#latency_ms ⇒ Float?
Total generation latency in milliseconds (converted from nanoseconds). Returns nil when timing data is unavailable.
98 99 100 101 102 |
# File 'lib/ollama/response.rb', line 98 def latency_ms return nil unless total_duration (total_duration / 1_000_000.0).round(2) end |
#load_duration ⇒ Object
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 |
#logprobs ⇒ Object
Log probability information when logprobs are enabled
81 82 83 |
# File 'lib/ollama/response.rb', line 81 def logprobs @data["logprobs"] || @data[:logprobs] end |
#message ⇒ Object
Access the message object
22 23 24 25 26 27 |
# File 'lib/ollama/response.rb', line 22 def msg = @data["message"] || @data[:message] return nil unless msg Message.new(msg) end |
#model ⇒ Object
Model name used
40 41 42 |
# File 'lib/ollama/response.rb', line 40 def model @data["model"] || @data[:model] end |
#prompt_eval_count ⇒ Object
Number of tokens in the prompt
60 61 62 63 |
# File 'lib/ollama/response.rb', line 60 def prompt_eval_count @data["prompt_eval_count"] || @data[:prompt_eval_count] || @data.dig("usage", "prompt_tokens") || @data.dig(:usage, :prompt_tokens) end |
#prompt_eval_duration ⇒ Object
Time spent evaluating the prompt in nanoseconds
66 67 68 |
# File 'lib/ollama/response.rb', line 66 def prompt_eval_duration @data["prompt_eval_duration"] || @data[:prompt_eval_duration] end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
127 128 129 |
# File 'lib/ollama/response.rb', line 127 def respond_to_missing?(method, include_private = false) @data.respond_to?(method, include_private) || super end |
#to_h ⇒ Object
Access raw data as hash
105 106 107 |
# File 'lib/ollama/response.rb', line 105 def to_h @data end |
#total_duration ⇒ Object
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 |
#usage ⇒ Hash
Aggregated token usage for observability and cost tracking.
87 88 89 90 91 92 93 |
# File 'lib/ollama/response.rb', line 87 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 |