Class: Geminize::Models::ChatResponse
- Inherits:
-
Object
- Object
- Geminize::Models::ChatResponse
- Defined in:
- lib/geminize/models/chat_response.rb
Overview
Represents a chat response from the Gemini API
Instance Attribute Summary collapse
-
#finish_reason ⇒ String?
readonly
The reason why generation stopped (if applicable).
-
#raw_response ⇒ Hash
readonly
The raw API response data.
-
#timestamp ⇒ Time
readonly
When the response was created.
-
#usage ⇒ Hash?
readonly
Token counts for the request and response.
Class Method Summary collapse
-
.from_hash(response_data) ⇒ ChatResponse
Create a ChatResponse object from a raw API response.
Instance Method Summary collapse
-
#completion_tokens ⇒ Integer?
Get the completion token count.
-
#has_text? ⇒ Boolean
Check if the response has generated text.
-
#initialize(response_data) ⇒ ChatResponse
constructor
Initialize a new chat response.
-
#prompt_tokens ⇒ Integer?
Get the prompt token count.
-
#text ⇒ String?
Get the generated text from the response.
-
#to_message_hash ⇒ Hash
Get the response as a formatted message hash.
-
#total_tokens ⇒ Integer?
Get the total token count.
Constructor Details
#initialize(response_data) ⇒ ChatResponse
Initialize a new chat response
21 22 23 24 25 |
# File 'lib/geminize/models/chat_response.rb', line 21 def initialize(response_data) @raw_response = response_data = Time.now parse_response end |
Instance Attribute Details
#finish_reason ⇒ String? (readonly)
Returns The reason why generation stopped (if applicable).
11 12 13 |
# File 'lib/geminize/models/chat_response.rb', line 11 def finish_reason @finish_reason end |
#raw_response ⇒ Hash (readonly)
Returns The raw API response data.
8 9 10 |
# File 'lib/geminize/models/chat_response.rb', line 8 def raw_response @raw_response end |
#timestamp ⇒ Time (readonly)
Returns When the response was created.
17 18 19 |
# File 'lib/geminize/models/chat_response.rb', line 17 def end |
#usage ⇒ Hash? (readonly)
Returns Token counts for the request and response.
14 15 16 |
# File 'lib/geminize/models/chat_response.rb', line 14 def usage @usage end |
Class Method Details
.from_hash(response_data) ⇒ ChatResponse
Create a ChatResponse object from a raw API response
92 93 94 |
# File 'lib/geminize/models/chat_response.rb', line 92 def self.from_hash(response_data) new(response_data) end |
Instance Method Details
#completion_tokens ⇒ Integer?
Get the completion token count
83 84 85 86 87 |
# File 'lib/geminize/models/chat_response.rb', line 83 def completion_tokens return nil unless @usage @usage["candidatesTokenCount"] end |
#has_text? ⇒ Boolean
Check if the response has generated text
61 62 63 |
# File 'lib/geminize/models/chat_response.rb', line 61 def has_text? !text.nil? && !text.empty? end |
#prompt_tokens ⇒ Integer?
Get the prompt token count
75 76 77 78 79 |
# File 'lib/geminize/models/chat_response.rb', line 75 def prompt_tokens return nil unless @usage @usage["promptTokenCount"] end |
#text ⇒ String?
Get the generated text from the response
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/geminize/models/chat_response.rb', line 29 def text return @text if defined?(@text) @text = nil candidates = @raw_response["candidates"] if candidates && !candidates.empty? content = candidates.first["content"] if content && content["parts"] && !content["parts"].empty? parts_text = content["parts"].map { |part| part["text"] }.compact @text = parts_text.join(" ") unless parts_text.empty? end end @text end |
#to_message_hash ⇒ Hash
Get the response as a formatted message hash
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/geminize/models/chat_response.rb', line 46 def candidates = @raw_response["candidates"] return nil unless candidates && !candidates.empty? content = candidates.first["content"] return nil unless content && content["parts"] && !content["parts"].empty? { role: "model", parts: content["parts"] } end |
#total_tokens ⇒ Integer?
Get the total token count
67 68 69 70 71 |
# File 'lib/geminize/models/chat_response.rb', line 67 def total_tokens return nil unless @usage (@usage["promptTokenCount"] || 0) + (@usage["candidatesTokenCount"] || 0) end |