Module: Genai::ChatValidator
- Defined in:
- lib/genai/chat.rb
Class Method Summary collapse
- .extract_curated_history(comprehensive_history) ⇒ Object
- .validate_content(content) ⇒ Object
- .validate_contents(contents) ⇒ Object
- .validate_response(response) ⇒ Object
Class Method Details
.extract_curated_history(comprehensive_history) ⇒ Object
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 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/genai/chat.rb', line 31 def self.extract_curated_history(comprehensive_history) return [] if comprehensive_history.empty? curated_history = [] length = comprehensive_history.length i = 0 while i < length role = comprehensive_history[i][:role] || comprehensive_history[i]["role"] unless ["user", "model", "assistant", "system", "developer"].include?(role) raise ArgumentError, "Unsupported role: #{role}" end if ["user", "system", "developer"].include?(role) current_input = comprehensive_history[i] curated_history << current_input i += 1 else current_output = [] is_valid = true while i < length current_role = comprehensive_history[i][:role] || comprehensive_history[i]["role"] break unless ["model", "assistant"].include?(current_role) 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 11 12 13 14 15 |
# File 'lib/genai/chat.rb', line 3 def self.validate_content(content) = content[:content] || content["content"] return true if .is_a?(String) && !.empty? parts = content[:parts] || content["parts"] return false unless parts && !parts.empty? parts.each do |part| return false if part.empty? text = part[:text] || part["text"] return false if text && text.empty? end true end |
.validate_contents(contents) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/genai/chat.rb', line 17 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
25 26 27 28 29 |
# File 'lib/genai/chat.rb', line 25 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 |