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
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.
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 |
#content ⇒ Object
Convenient content accessor (shorthand for message&.content)
109 110 111 |
# File 'lib/ollama/response.rb', line 109 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
70 71 72 |
# File 'lib/ollama/response.rb', line 70 def eval_count @data["eval_count"] || @data[:eval_count] end |
#eval_duration ⇒ Object
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_ms ⇒ Float?
Total generation latency in milliseconds (converted from nanoseconds). Returns nil when timing data is unavailable.
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_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
80 81 82 |
# File 'lib/ollama/response.rb', line 80 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 |
# File 'lib/ollama/response.rb', line 60 def prompt_eval_count @data["prompt_eval_count"] || @data[:prompt_eval_count] end |
#prompt_eval_duration ⇒ Object
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
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_h ⇒ Object
Access raw data as hash
104 105 106 |
# File 'lib/ollama/response.rb', line 104 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.
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 |