module AiChatHistory
Constants
- MAX_CONTEXT
- MAX_LINE_CHARS
Public Instance Methods
Source
# File lib/ai/chat_history.rb, line 44 def exchange_relevance_score(entry, tokens) combined = "#{entry[:question]} #{entry[:answer]}" AiContext.relevance_score(combined, tokens) end
Source
# File lib/ai/chat_history.rb, line 40 def follow_up_question?(question) AiContext.follow_up_question?(question) end
Source
# File lib/ai/chat_history.rb, line 57 def format_exchange(entry, max_chars: MAX_LINE_CHARS) asker = (entry[:asker] || entry["asker"]).to_s question = truncate(entry[:question] || entry["question"], max_chars) answer = truncate(entry[:answer] || entry["answer"], max_chars) [ "#{asker} asked: #{question}", "Rabbot answered: #{answer}" ] end
Source
# File lib/ai/chat_history.rb, line 36 def merge_entries(*groups) groups.flatten.uniq { |entry| [entry[:asker], entry[:question], entry[:answer]] } end
Source
# File lib/ai/chat_history.rb, line 49 def normalize_entry(entry) { asker: (entry[:asker] || entry["asker"]).to_s, question: (entry[:question] || entry["question"]).to_s, answer: (entry[:answer] || entry["answer"]).to_s } end
Source
# File lib/ai/chat_history.rb, line 16 def select_entries(entries, question, stop_words: [], max_context: MAX_CONTEXT) list = Array(entries).map { |entry| normalize_entry(entry) } return [] if list.empty? tokens = AiContext.query_tokens(question, stop_words: stop_words) selected = [] if tokens.any? relevant = list.select { |entry| exchange_relevance_score(entry, tokens).positive? } selected = relevant.last(max_context) if relevant.any? end last = list.last if follow_up_question?(question) || selected.empty? selected = merge_entries(selected, [last]) end selected.last(max_context) end
Source
# File lib/ai/chat_history.rb, line 67 def truncate(text, max_chars) line = text.to_s.strip return line if line.length <= max_chars "#{line[0, max_chars - 3]}..." end