Module: Genai::ChatValidator

Defined in:
lib/genai/chat.rb

Class Method Summary collapse

Class Method Details

.extract_curated_history(comprehensive_history) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/genai/chat.rb', line 26

def self.extract_curated_history(comprehensive_history)
  return [] if comprehensive_history.empty?
  
  curated_history = []
  length = comprehensive_history.length
  i = 0
  
  while i < length
    unless ["user", "model"].include?(comprehensive_history[i][:role])
      raise ArgumentError, "Role must be user or model, but got #{comprehensive_history[i][:role]}"
    end

    if comprehensive_history[i][:role] == "user"
      current_input = comprehensive_history[i]
      curated_history << current_input
      i += 1
    else
      current_output = []
      is_valid = true
      
      while i < length && comprehensive_history[i][:role] == "model"
        current_output << comprehensive_history[i]
        if is_valid && !validate_content(comprehensive_history[i])
          is_valid = false
        end
        i += 1
      end
      
      if is_valid
        curated_history.concat(current_output)
      elsif !curated_history.empty?
        curated_history.pop
      end
    end
  end
  
  curated_history
end

.validate_content(content) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/genai/chat.rb', line 3

def self.validate_content(content)
  return false unless content[:parts] && !content[:parts].empty?
  content[:parts].each do |part|
    return false if part.empty?
    return false if part[:text] && part[:text].empty?
  end
  true
end

.validate_contents(contents) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/genai/chat.rb', line 12

def self.validate_contents(contents)
  return false if contents.empty?
  contents.each do |content|
    return false unless validate_content(content)
  end
  true
end

.validate_response(response) ⇒ Object



20
21
22
23
24
# File 'lib/genai/chat.rb', line 20

def self.validate_response(response)
  return false unless response[:candidates] && !response[:candidates].empty?
  return false unless response[:candidates][0][:content]
  validate_content(response[:candidates][0][:content])
end