Class: Aigen::Google::Chat
- Inherits:
-
Object
- Object
- Aigen::Google::Chat
- Defined in:
- lib/aigen/google/chat.rb
Overview
Chat manages stateful multi-turn conversations with the Gemini API. It maintains conversation history and automatically includes context in each API request for coherent, context-aware responses.
Instance Method Summary collapse
-
#history ⇒ Array<Hash>
Returns the conversation history as an array of message hashes.
-
#initialize(client:, model: nil, history: []) ⇒ Chat
constructor
Initializes a new Chat instance.
-
#send_message(message, **options) ⇒ Hash
Sends a message in the chat context and returns the model’s response.
-
#send_message_stream(message, **options) {|chunk| ... } ⇒ nil, Enumerator
Sends a message in the chat context with streaming response delivery.
Constructor Details
#initialize(client:, model: nil, history: []) ⇒ Chat
Initializes a new Chat instance.
51 52 53 54 55 |
# File 'lib/aigen/google/chat.rb', line 51 def initialize(client:, model: nil, history: []) @client = client @model = model || @client.config.default_model @history = history.dup end |
Instance Method Details
#history ⇒ Array<Hash>
The returned array is frozen to prevent external modification. The history is managed internally by the Chat instance.
Returns the conversation history as an array of message hashes. Each message has a role (“user” or “model”) and parts array.
35 36 37 |
# File 'lib/aigen/google/chat.rb', line 35 def history @history.dup.freeze end |
#send_message(message, **options) ⇒ Hash
Sends a message in the chat context and returns the model’s response. Automatically includes conversation history for context and updates history with both the user message and model response.
Accepts both simple String messages and Content objects for multimodal support.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/aigen/google/chat.rb', line 91 def (, **) # Validate message parameter raise ArgumentError, "message cannot be nil" if .nil? raise ArgumentError, "message cannot be empty" if .respond_to?(:empty?) && .empty? # Build user message part # Handle both String (simple text) and Content objects (multimodal) = if .is_a?(Content) # Content object - use its to_h serialization and add role .to_h.merge(role: "user") else # String - wrap in standard format { role: "user", parts: [{text: }] } end # Build payload with full history + new message payload = { contents: @history + [] } # Merge any additional generation config options payload.merge!() if .any? # Make API request endpoint = "models/#{@model}:generateContent" response = @client.http_client.post(endpoint, payload) # Extract model response model_response = (response) # Update history with both user message and model response @history << @history << model_response response end |
#send_message_stream(message, **options) {|chunk| ... } ⇒ nil, Enumerator
The conversation history is NOT updated until the entire stream completes. This ensures the history remains consistent even if streaming is interrupted.
Sends a message in the chat context with streaming response delivery. Automatically includes conversation history for context and updates history with the full accumulated response after streaming completes.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/aigen/google/chat.rb', line 165 def (, **, &block) # Validate message parameter raise ArgumentError, "message cannot be nil" if .nil? raise ArgumentError, "message cannot be empty" if .respond_to?(:empty?) && .empty? # Build user message part = { role: "user", parts: [{text: }] } # Build payload with full history + new message payload = { contents: @history + [] } # Merge any additional generation config options payload.merge!() if .any? # Make API request endpoint = "models/#{@model}:streamGenerateContent" # Accumulate full response text while streaming accumulated_text = "" # If block given, stream with block and accumulate if block_given? @client.http_client.post_stream(endpoint, payload) do |chunk| # Extract text from chunk text = extract_chunk_text(chunk) accumulated_text += text if text # Yield chunk to user's block block.call(chunk) end # After streaming completes, update history with full response update_history_after_stream(, accumulated_text) nil else # Return Enumerator that accumulates and updates history when consumed Enumerator.new do |yielder| @client.http_client.post_stream(endpoint, payload) do |chunk| text = extract_chunk_text(chunk) accumulated_text += text if text yielder << chunk end # Update history after enumeration completes update_history_after_stream(, accumulated_text) end end end |