Class: Genai::Chat
Instance Attribute Summary
Attributes inherited from BaseChat
#comprehensive_history, #config, #curated_history, #model
Instance Method Summary collapse
-
#initialize(client:, model:, config: nil, history: []) ⇒ Chat
constructor
A new instance of Chat.
- #send_message(message, config: nil) ⇒ Object
- #send_message_stream(message, config: nil) ⇒ Object
Methods inherited from BaseChat
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 (, config: nil) input_content = case when String { role: "user", parts: [{ text: }] } when Array { role: "user", parts: } when Hash 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 (, config: nil) input_content = case when String { role: "user", parts: [{ text: }] } when Array { role: "user", parts: } when Hash else raise ArgumentError, "Message must be a String, Array, or Hash" end (, config: config) end |