feat: Interface to validate response_source (#8894)
- This PR adds a UI to validate the response source quality quickly. It also helps to test with sample questions and update responses in the database when missing. Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -1,6 +1,39 @@
|
||||
class Enterprise::MessageTemplates::ResponseBotService
|
||||
pattr_initialize [:conversation!]
|
||||
|
||||
def self.generate_sources_section(article_ids)
|
||||
sources_content = ''
|
||||
|
||||
articles_hash = get_article_hash(article_ids.uniq)
|
||||
|
||||
articles_hash.first(3).each do |article_hash|
|
||||
sources_content += " - [#{article_hash[:response].question}](#{article_hash[:response_document].document_link}) \n"
|
||||
end
|
||||
sources_content = "\n \n \n **Sources** \n#{sources_content}" if sources_content.present?
|
||||
sources_content
|
||||
end
|
||||
|
||||
def self.get_article_hash(article_ids)
|
||||
seen_documents = Set.new
|
||||
article_ids.uniq.filter_map do |article_id|
|
||||
response = Response.find(article_id)
|
||||
response_document = response.response_document
|
||||
next if response_document.blank? || seen_documents.include?(response_document)
|
||||
|
||||
seen_documents << response_document
|
||||
{ response: response, response_document: response_document }
|
||||
end
|
||||
end
|
||||
|
||||
def self.response_sections(content, response_source)
|
||||
sections = ''
|
||||
|
||||
response_source.get_responses(content).each do |response|
|
||||
sections += "{context_id: #{response.id}, context: #{response.question} ? #{response.answer}},"
|
||||
end
|
||||
sections
|
||||
end
|
||||
|
||||
def perform
|
||||
ActiveRecord::Base.transaction do
|
||||
@response = get_response(conversation.messages.incoming.last.content)
|
||||
@@ -12,15 +45,6 @@ class Enterprise::MessageTemplates::ResponseBotService
|
||||
true
|
||||
end
|
||||
|
||||
def response_sections(content)
|
||||
sections = ''
|
||||
|
||||
inbox.get_responses(content).each do |response|
|
||||
sections += "{context_id: #{response.id}, context: #{response.question} ? #{response.answer}},"
|
||||
end
|
||||
sections
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
delegate :contact, :account, :inbox, to: :conversation
|
||||
@@ -28,7 +52,7 @@ class Enterprise::MessageTemplates::ResponseBotService
|
||||
def get_response(content)
|
||||
previous_messages = []
|
||||
get_previous_messages(previous_messages)
|
||||
ChatGpt.new(response_sections(content)).generate_response('', previous_messages)
|
||||
ChatGpt.new(self.class.response_sections(content, inbox)).generate_response('', previous_messages)
|
||||
end
|
||||
|
||||
def get_previous_messages(previous_messages)
|
||||
@@ -63,24 +87,11 @@ class Enterprise::MessageTemplates::ResponseBotService
|
||||
|
||||
def create_messages
|
||||
message_content = @response['response']
|
||||
message_content += generate_sources_section if @response['context_ids'].present?
|
||||
message_content += self.class.generate_sources_section(@response['context_ids']) if @response['context_ids'].present?
|
||||
|
||||
create_outgoing_message(message_content)
|
||||
end
|
||||
|
||||
def generate_sources_section
|
||||
article_ids = @response['context_ids']
|
||||
sources_content = ''
|
||||
|
||||
articles_hash = get_article_hash(article_ids.uniq)
|
||||
|
||||
articles_hash.first(3).each do |article_hash|
|
||||
sources_content += " - [#{article_hash[:response].question}](#{article_hash[:response_document].document_link}) \n"
|
||||
end
|
||||
sources_content = "\n \n \n **Sources** \n#{sources_content}" if sources_content.present?
|
||||
sources_content
|
||||
end
|
||||
|
||||
def create_outgoing_message(message_content)
|
||||
conversation.messages.create!(
|
||||
{
|
||||
@@ -91,16 +102,4 @@ class Enterprise::MessageTemplates::ResponseBotService
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def get_article_hash(article_ids)
|
||||
seen_documents = Set.new
|
||||
article_ids.uniq.filter_map do |article_id|
|
||||
response = Response.find(article_id)
|
||||
response_document = response.response_document
|
||||
next if response_document.blank? || seen_documents.include?(response_document)
|
||||
|
||||
seen_documents << response_document
|
||||
{ response: response, response_document: response_document }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user