fix: Show sources in bot response only if documents are present (#8055)

- show sources section in bot only based on appropriate conditions
This commit is contained in:
Sojan Jose
2023-10-05 00:11:01 -07:00
committed by GitHub
parent 3227cf7ff3
commit 72ba429159
2 changed files with 36 additions and 7 deletions

View File

@@ -63,23 +63,22 @@ class Enterprise::MessageTemplates::ResponseBotService
def create_messages
message_content = @response['response']
message_content = append_message_with_sources(message_content)
message_content += generate_sources_section if @response['context_ids'].present?
create_outgoing_message(message_content)
end
def append_message_with_sources(message_content)
def generate_sources_section
article_ids = @response['context_ids']
return message_content if article_ids.blank?
sources_content = ''
message_content += "\n \n \n **Sources** \n"
articles_hash = get_article_hash(article_ids.uniq)
articles_hash.first(3).each do |article_hash|
message_content += " - [#{article_hash[:response].question}](#{article_hash[:response_document].document_link}) \n"
sources_content += " - [#{article_hash[:response].question}](#{article_hash[:response_document].document_link}) \n"
end
message_content
sources_content = "\n \n \n **Sources** \n#{sources_content}" if sources_content.present?
sources_content
end
def create_outgoing_message(message_content)

View File

@@ -30,6 +30,36 @@ RSpec.describe Enterprise::MessageTemplates::ResponseBotService, type: :service
last_message = conversation.messages.last
expect(last_message.content).to include('some_response')
expect(last_message.content).to include(Response.first.question)
expect(last_message.content).to include('**Sources**')
end
end
context 'when context_ids are not present' do
it 'creates an outgoing message without article references' do
allow(chat_gpt_double).to receive(:generate_response).and_return({ 'response' => 'some_response' })
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
last_message = conversation.messages.last
expect(last_message.content).to include('some_response')
expect(last_message.content).not_to include('**Sources**')
end
end
context 'when response doesnt have response document' do
it 'creates an outgoing message without article references' do
response = create(:response, response_source: response_source, response_document: nil)
allow(chat_gpt_double).to receive(:generate_response).and_return({ 'response' => 'some_response', 'context_ids' => [response.id] })
expect do
service.perform
end.to change { conversation.messages.where(message_type: :outgoing).count }.by(1)
last_message = conversation.messages.last
expect(last_message.content).to include('some_response')
expect(last_message.content).not_to include('**Sources**')
end
end