Class: Genai::Chat

Inherits:
BaseChat show all
Defined in:
lib/genai/chat.rb

Instance Attribute Summary

Attributes inherited from BaseChat

#comprehensive_history, #config, #curated_history, #model

Instance Method Summary collapse

Methods inherited from BaseChat

#get_history, #record_history

Constructor Details

#initialize(client:, model:, config: nil, history: []) ⇒ Chat

Returns a new instance of Chat.



115
116
117
118
# File 'lib/genai/chat.rb', line 115

def initialize(client:, model:, config: nil, history: [])
  @client = client
  super(model: model, config: config, history: history)
end

Instance Method Details

#send_message(message, config: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/genai/chat.rb', line 120

def send_message(message, config: nil)
  input_content = case message
  when String
    { role: "user", parts: [{ text: message }] }
  when Array
    { role: "user", parts: message }
  when Hash
    message
  else
    raise ArgumentError, "Message must be a String, Array, or Hash"
  end

  model_instance = @client.model(@model)
  response = model_instance.generate_content(
    contents: @curated_history + [input_content],
    config: config || @config
  )

  model_output = response.to_s.empty? ? [] : [{ role: "model", parts: [{ text: response.to_s }] }]

  record_history(
    user_input: input_content,
    model_output: model_output,
    is_valid: !response.to_s.empty?
  )

  response
end

#send_message_stream(message, config: nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/genai/chat.rb', line 149

def send_message_stream(message, config: nil)
  input_content = case message
  when String
    { role: "user", parts: [{ text: message }] }
  when Array
    { role: "user", parts: message }
  when Hash
    message
  else
    raise ArgumentError, "Message must be a String, Array, or Hash"
  end


  send_message(message, config: config)
end