Class: Genai::BaseChat

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

Direct Known Subclasses

Chat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BaseChat.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/genai/chat.rb', line 69

def initialize(model:, config: nil, history: [])
  @model = model
  @config = config
  
  content_models = history.map do |content|
    content.is_a?(Hash) ? content : content
  end
  
  @comprehensive_history = content_models
  @curated_history = ChatValidator.extract_curated_history(content_models)
end

Instance Attribute Details

#comprehensive_historyObject (readonly)

Returns the value of attribute comprehensive_history.



67
68
69
# File 'lib/genai/chat.rb', line 67

def comprehensive_history
  @comprehensive_history
end

#configObject (readonly)

Returns the value of attribute config.



67
68
69
# File 'lib/genai/chat.rb', line 67

def config
  @config
end

#curated_historyObject (readonly)

Returns the value of attribute curated_history.



67
68
69
# File 'lib/genai/chat.rb', line 67

def curated_history
  @curated_history
end

#modelObject (readonly)

Returns the value of attribute model.



67
68
69
# File 'lib/genai/chat.rb', line 67

def model
  @model
end

Instance Method Details

#get_history(curated: false) ⇒ Object



99
100
101
# File 'lib/genai/chat.rb', line 99

def get_history(curated: false)
  curated ? @curated_history : @comprehensive_history
end

#record_history(user_input:, model_output:, automatic_function_calling_history: [], is_valid: true) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/genai/chat.rb', line 81

def record_history(user_input:, model_output:, automatic_function_calling_history: [], is_valid: true)
  input_contents = if !automatic_function_calling_history.empty?
    automatic_function_calling_history[@curated_history.length..-1] || [user_input]
  else
    [user_input]
  end
  
  output_contents = model_output.empty? ? [{ role: "model", parts: [] }] : model_output
  
  @comprehensive_history.concat(input_contents)
  @comprehensive_history.concat(output_contents)
  
  if is_valid
    @curated_history.concat(input_contents)
    @curated_history.concat(output_contents)
  end
end