chore: Refactor Response Bot Data Schema (#8011)

This PR refactors the schema we introduced in #7518 based on the feedback from production tests. Here is the change log

- Decouple Inbox association to a new table inbox_response_sources -> this lets us share the same response source between multiple inboxes
- Add a status field to responses. This ensures that, by default, responses are created in pending status. You can do quality assurance before making them active. In future, this status can be leveraged by the bot to auto-generate response questions from conversations which require a handoff
- Add response_source association to responses and remove hard dependency from response_documents. This lets users write free-form question answers based on conversations, which doesn't necessarily need a response source.
This commit is contained in:
Sojan Jose
2023-10-01 19:31:38 -07:00
committed by GitHub
parent d8b53f5d2f
commit 826d9ec5a7
18 changed files with 138 additions and 36 deletions

View File

@@ -2,7 +2,6 @@ json.id resource.id
json.name resource.name json.name resource.name
json.source_link resource.source_link json.source_link resource.source_link
json.source_type resource.source_type json.source_type resource.source_type
json.inbox_id resource.inbox_id
json.account_id resource.account_id json.account_id resource.account_id
json.created_at resource.created_at.to_i json.created_at resource.created_at.to_i
json.updated_at resource.updated_at.to_i json.updated_at resource.updated_at.to_i

View File

@@ -33,3 +33,4 @@ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.ignore_extentions <<
ActiveRecord::SchemaDumper.ignore_tables << 'responses' ActiveRecord::SchemaDumper.ignore_tables << 'responses'
ActiveRecord::SchemaDumper.ignore_tables << 'response_sources' ActiveRecord::SchemaDumper.ignore_tables << 'response_sources'
ActiveRecord::SchemaDumper.ignore_tables << 'response_documents' ActiveRecord::SchemaDumper.ignore_tables << 'response_documents'
ActiveRecord::SchemaDumper.ignore_tables << 'inbox_response_sources'

View File

@@ -28,7 +28,7 @@ class Api::V1::Accounts::ResponseSourcesController < Api::V1::Accounts::BaseCont
end end
def response_source_params def response_source_params
params.require(:response_source).permit(:name, :source_link, :inbox_id, params.require(:response_source).permit(:name, :source_link,
response_documents_attributes: [:document_link]) response_documents_attributes: [:document_link])
end end
end end

View File

@@ -10,8 +10,13 @@ class ResponseDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = { ATTRIBUTE_TYPES = {
id: Field::Number.with_options(searchable: true), id: Field::Number.with_options(searchable: true),
account: Field::BelongsToSearch.with_options(class_name: 'Account', searchable_field: [:name, :id], order: 'id DESC'), account: Field::BelongsToSearch.with_options(class_name: 'Account', searchable_field: [:name, :id], order: 'id DESC'),
response_source: Field::BelongsToSearch.with_options(class_name: 'ResponseSource', searchable_field: [:name, :id, :source_link],
order: 'id DESC'),
answer: Field::Text.with_options(searchable: true), answer: Field::Text.with_options(searchable: true),
question: Field::String.with_options(searchable: true), question: Field::String.with_options(searchable: true),
status: Field::Select.with_options(searchable: false, collection: lambda { |field|
field.resource.class.send(field.attribute.to_s.pluralize).keys
}),
response_document: Field::BelongsToSearch.with_options(class_name: 'ResponseDocument', searchable_field: [:document_link, :content, :id], response_document: Field::BelongsToSearch.with_options(class_name: 'ResponseDocument', searchable_field: [:document_link, :content, :id],
order: 'id DESC'), order: 'id DESC'),
created_at: Field::DateTime, created_at: Field::DateTime,
@@ -27,7 +32,9 @@ class ResponseDashboard < Administrate::BaseDashboard
id id
question question
answer answer
status
response_document response_document
response_source
account account
].freeze ].freeze
@@ -35,9 +42,11 @@ class ResponseDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed on the model's show page. # an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[ SHOW_PAGE_ATTRIBUTES = %i[
id id
status
question question
answer answer
response_document response_document
response_source
account account
created_at created_at
updated_at updated_at
@@ -47,10 +56,11 @@ class ResponseDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed # an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages. # on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[ FORM_ATTRIBUTES = %i[
response_source
response_document
question question
answer answer
response_document status
account
].freeze ].freeze
# COLLECTION_FILTERS # COLLECTION_FILTERS
@@ -63,7 +73,12 @@ class ResponseDashboard < Administrate::BaseDashboard
# COLLECTION_FILTERS = { # COLLECTION_FILTERS = {
# open: ->(resources) { resources.where(open: true) } # open: ->(resources) { resources.where(open: true) }
# }.freeze # }.freeze
COLLECTION_FILTERS = {}.freeze COLLECTION_FILTERS = {
account: ->(resources, attr) { resources.where(account_id: attr) },
response_source: ->(resources, attr) { resources.where(response_source_id: attr) },
response_document: ->(resources, attr) { resources.where(response_document_id: attr) },
status: ->(resources, attr) { resources.where(status: attr) }
}.freeze
# Overwrite this method to customize how responses are displayed # Overwrite this method to customize how responses are displayed
# across all pages of the admin dashboard. # across all pages of the admin dashboard.

View File

@@ -38,11 +38,11 @@ class ResponseDocumentDashboard < Administrate::BaseDashboard
SHOW_PAGE_ATTRIBUTES = %i[ SHOW_PAGE_ATTRIBUTES = %i[
id id
account account
content
document_id
document_link
document_type
response_source response_source
document_link
document_id
document_type
content
created_at created_at
updated_at updated_at
responses responses
@@ -53,11 +53,11 @@ class ResponseDocumentDashboard < Administrate::BaseDashboard
# on the model's form (`new` and `edit`) pages. # on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[ FORM_ATTRIBUTES = %i[
account account
content
document_id
document_link
document_type
response_source response_source
document_link
document_id
document_type
content
].freeze ].freeze
# COLLECTION_FILTERS # COLLECTION_FILTERS
@@ -70,7 +70,10 @@ class ResponseDocumentDashboard < Administrate::BaseDashboard
# COLLECTION_FILTERS = { # COLLECTION_FILTERS = {
# open: ->(resources) { resources.where(open: true) } # open: ->(resources) { resources.where(open: true) }
# }.freeze # }.freeze
COLLECTION_FILTERS = {}.freeze COLLECTION_FILTERS = {
account: ->(resources, attr) { resources.where(account_id: attr) },
response_source: ->(resources, attr) { resources.where(response_source_id: attr) }
}.freeze
# Overwrite this method to customize how response documents are displayed # Overwrite this method to customize how response documents are displayed
# across all pages of the admin dashboard. # across all pages of the admin dashboard.

View File

@@ -8,7 +8,7 @@ class ResponseSourceDashboard < Administrate::BaseDashboard
# which determines how the attribute is displayed # which determines how the attribute is displayed
# on pages throughout the dashboard. # on pages throughout the dashboard.
ATTRIBUTE_TYPES = { ATTRIBUTE_TYPES = {
id: Field::Number, id: Field::Number.with_options(searchable: true),
account: Field::BelongsToSearch.with_options(class_name: 'Account', searchable_field: [:name, :id], order: 'id DESC'), account: Field::BelongsToSearch.with_options(class_name: 'Account', searchable_field: [:name, :id], order: 'id DESC'),
name: Field::String.with_options(searchable: true), name: Field::String.with_options(searchable: true),
response_documents: Field::HasMany, response_documents: Field::HasMany,
@@ -73,7 +73,9 @@ class ResponseSourceDashboard < Administrate::BaseDashboard
# COLLECTION_FILTERS = { # COLLECTION_FILTERS = {
# open: ->(resources) { resources.where(open: true) } # open: ->(resources) { resources.where(open: true) }
# }.freeze # }.freeze
COLLECTION_FILTERS = {}.freeze COLLECTION_FILTERS = {
account: ->(resources, attr) { resources.where(account_id: attr) }
}.freeze
# Overwrite this method to customize how response sources are displayed # Overwrite this method to customize how response sources are displayed
# across all pages of the admin dashboard. # across all pages of the admin dashboard.

View File

@@ -63,14 +63,20 @@ class ResponseBuilderJob < ApplicationJob
def create_responses(response, response_document) def create_responses(response, response_document)
response_body = JSON.parse(response.body) response_body = JSON.parse(response.body)
faqs = JSON.parse(response_body['choices'][0]['message']['content'].strip) content = response_body.dig('choices', 0, 'message', 'content')
return if content.nil?
faqs = JSON.parse(content.strip)
faqs.each do |faq| faqs.each do |faq|
response_document.responses.create!( response_document.responses.create!(
question: faq['question'], question: faq['question'],
answer: faq['answer'], answer: faq['answer'],
account_id: response_document.account_id response_source: response_document.response_source
) )
end end
rescue JSON::ParserError => e
Rails.logger.error "Error in parsing GPT processed response document : #{e.message}"
end end
end end

View File

@@ -1,3 +1,31 @@
# == Schema Information
#
# Table name: audits
#
# id :bigint not null, primary key
# action :string
# associated_type :string
# auditable_type :string
# audited_changes :jsonb
# comment :string
# remote_address :string
# request_uuid :string
# user_type :string
# username :string
# version :integer default(0)
# created_at :datetime
# associated_id :bigint
# auditable_id :bigint
# user_id :bigint
#
# Indexes
#
# associated_index (associated_type,associated_id)
# auditable_index (auditable_type,auditable_id,version)
# index_audits_on_created_at (created_at)
# index_audits_on_request_uuid (request_uuid)
# user_index (user_id,user_type)
#
class Enterprise::AuditLog < Audited::Audit class Enterprise::AuditLog < Audited::Audit
after_save :log_additional_information after_save :log_additional_information

View File

@@ -3,9 +3,10 @@ module Enterprise::Concerns::Inbox
included do included do
def self.add_response_related_associations def self.add_response_related_associations
has_many :response_sources, dependent: :destroy_async has_many :inbox_response_sources, dependent: :destroy_async
has_many :response_sources, through: :inbox_response_sources
has_many :response_documents, through: :response_sources has_many :response_documents, through: :response_sources
has_many :responses, through: :response_documents has_many :responses, through: :response_sources
end end
add_response_related_associations if Features::ResponseBotService.new.vector_extension_enabled? add_response_related_associations if Features::ResponseBotService.new.vector_extension_enabled?

View File

@@ -7,7 +7,7 @@ module Enterprise::Inbox
def get_responses(query) def get_responses(query)
embedding = Openai::EmbeddingsService.new.get_embedding(query) embedding = Openai::EmbeddingsService.new.get_embedding(query)
responses.nearest_neighbors(:embedding, embedding, distance: 'cosine').first(5) responses.active.nearest_neighbors(:embedding, embedding, distance: 'cosine').first(5)
end end
def active_bot? def active_bot?

View File

@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: inbox_response_sources
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# inbox_id :bigint not null
# response_source_id :bigint not null
#
# Indexes
#
# index_inbox_response_sources_on_inbox_id (inbox_id)
# index_inbox_response_sources_on_inbox_id_and_response_source_id (inbox_id,response_source_id) UNIQUE
# index_inbox_response_sources_on_response_source_id (response_source_id)
# index_inbox_response_sources_on_response_source_id_and_inbox_id (response_source_id,inbox_id) UNIQUE
#
class InboxResponseSource < ApplicationRecord
belongs_to :inbox
belongs_to :response_source
end

View File

@@ -6,10 +6,12 @@
# answer :text not null # answer :text not null
# embedding :vector(1536) # embedding :vector(1536)
# question :string not null # question :string not null
# status :integer default(0)
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# account_id :bigint not null # account_id :bigint not null
# response_document_id :bigint # response_document_id :bigint
# response_source_id :bigint not null
# #
# Indexes # Indexes
# #
@@ -17,11 +19,15 @@
# index_responses_on_response_document_id (response_document_id) # index_responses_on_response_document_id (response_document_id)
# #
class Response < ApplicationRecord class Response < ApplicationRecord
belongs_to :response_document belongs_to :response_document, optional: true
belongs_to :account belongs_to :account
belongs_to :response_source
has_neighbors :embedding, normalize: true has_neighbors :embedding, normalize: true
before_save :update_response_embedding before_save :update_response_embedding
before_validation :ensure_account
enum status: { pending: 0, active: 1 }
def self.search(query) def self.search(query)
embedding = Openai::EmbeddingsService.new.get_embedding(query) embedding = Openai::EmbeddingsService.new.get_embedding(query)
@@ -30,6 +36,10 @@ class Response < ApplicationRecord
private private
def ensure_account
self.account = response_source.account
end
def update_response_embedding def update_response_embedding
self.embedding = Openai::EmbeddingsService.new.get_embedding("#{question}: #{answer}") self.embedding = Openai::EmbeddingsService.new.get_embedding("#{question}: #{answer}")
end end

View File

@@ -18,7 +18,7 @@
# index_response_documents_on_response_source_id (response_source_id) # index_response_documents_on_response_source_id (response_source_id)
# #
class ResponseDocument < ApplicationRecord class ResponseDocument < ApplicationRecord
has_many :responses, dependent: :destroy has_many :responses, dependent: :destroy_async
belongs_to :account belongs_to :account
belongs_to :response_source belongs_to :response_source

View File

@@ -10,7 +10,6 @@
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# account_id :bigint not null # account_id :bigint not null
# inbox_id :bigint not null
# source_model_id :bigint # source_model_id :bigint
# #
# Indexes # Indexes
@@ -19,10 +18,11 @@
# #
class ResponseSource < ApplicationRecord class ResponseSource < ApplicationRecord
enum source_type: { external: 0, kbase: 1, inbox: 2 } enum source_type: { external: 0, kbase: 1, inbox: 2 }
has_many :inbox_response_sources, dependent: :destroy_async
has_many :inboxes, through: :inbox_response_sources
belongs_to :account belongs_to :account
belongs_to :inbox has_many :response_documents, dependent: :destroy_async
has_many :response_documents, dependent: :destroy has_many :responses, dependent: :destroy_async
has_many :responses, through: :response_documents
accepts_nested_attributes_for :response_documents accepts_nested_attributes_for :response_documents
end end

View File

@@ -23,19 +23,31 @@ class Features::ResponseBotService
def create_tables def create_tables
return unless vector_extension_enabled? return unless vector_extension_enabled?
%i[response_sources response_documents responses].each do |table| %i[response_sources response_documents responses inbox_response_sources].each do |table|
send("create_#{table}_table") send("create_#{table}_table")
end end
end end
def drop_tables def drop_tables
%i[responses response_documents response_sources].each do |table| %i[responses response_documents response_sources inbox_response_sources].each do |table|
MIGRATION_VERSION.drop_table table if MIGRATION_VERSION.table_exists?(table) MIGRATION_VERSION.drop_table table if MIGRATION_VERSION.table_exists?(table)
end end
end end
private private
def create_inbox_response_sources_table
return if MIGRATION_VERSION.table_exists?(:inbox_response_sources)
MIGRATION_VERSION.create_table :inbox_response_sources do |t|
t.references :inbox, null: false
t.references :response_source, null: false
t.index [:inbox_id, :response_source_id], name: 'index_inbox_response_sources_on_inbox_id_and_response_source_id', unique: true
t.index [:response_source_id, :inbox_id], name: 'index_inbox_response_sources_on_response_source_id_and_inbox_id', unique: true
t.timestamps
end
end
def create_response_sources_table def create_response_sources_table
return if MIGRATION_VERSION.table_exists?(:response_sources) return if MIGRATION_VERSION.table_exists?(:response_sources)
@@ -45,7 +57,6 @@ class Features::ResponseBotService
t.string :source_link t.string :source_link
t.references :source_model, polymorphic: true t.references :source_model, polymorphic: true
t.bigint :account_id, null: false t.bigint :account_id, null: false
t.bigint :inbox_id, null: false
t.timestamps t.timestamps
end end
end end
@@ -69,9 +80,11 @@ class Features::ResponseBotService
return if MIGRATION_VERSION.table_exists?(:responses) return if MIGRATION_VERSION.table_exists?(:responses)
MIGRATION_VERSION.create_table :responses do |t| MIGRATION_VERSION.create_table :responses do |t|
t.bigint :response_source_id, null: false
t.bigint :response_document_id t.bigint :response_document_id
t.string :question, null: false t.string :question, null: false
t.text :answer, null: false t.text :answer, null: false
t.integer :status, default: 0
t.bigint :account_id, null: false t.bigint :account_id, null: false
t.vector :embedding, limit: 1536 t.vector :embedding, limit: 1536
t.timestamps t.timestamps

View File

@@ -20,7 +20,10 @@ if Rails.env.development?
'show_complete_foreign_keys' => 'false', 'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true', 'show_indexes' => 'true',
'simple_indexes' => 'false', 'simple_indexes' => 'false',
'model_dir' => 'app/models', 'model_dir' => [
'app/models',
'enterprise/app/models',
],
'root_dir' => '', 'root_dir' => '',
'include_version' => 'false', 'include_version' => 'false',
'require' => '', 'require' => '',

View File

@@ -3,7 +3,6 @@ require 'rails_helper'
RSpec.describe 'Response Sources API', type: :request do RSpec.describe 'Response Sources API', type: :request do
let!(:account) { create(:account) } let!(:account) { create(:account) }
let!(:admin) { create(:user, account: account, role: :administrator) } let!(:admin) { create(:user, account: account, role: :administrator) }
let!(:inbox) { create(:inbox, account: account) }
before do before do
skip('Skipping since vector is not enabled in this environment') unless Features::ResponseBotService.new.vector_extension_enabled? skip('Skipping since vector is not enabled in this environment') unless Features::ResponseBotService.new.vector_extension_enabled?
@@ -44,7 +43,6 @@ RSpec.describe 'Response Sources API', type: :request do
response_source: { response_source: {
name: 'Test', name: 'Test',
source_link: 'http://test.test', source_link: 'http://test.test',
inbox_id: inbox.id,
response_documents_attributes: [ response_documents_attributes: [
{ document_link: 'http://test1.test' }, { document_link: 'http://test1.test' },
{ document_link: 'http://test2.test' } { document_link: 'http://test2.test' }
@@ -75,7 +73,7 @@ RSpec.describe 'Response Sources API', type: :request do
end end
describe 'POST /api/v1/accounts/{account.id}/response_sources/{response_source.id}/add_document' do describe 'POST /api/v1/accounts/{account.id}/response_sources/{response_source.id}/add_document' do
let!(:response_source) { create(:response_source, account: account, inbox: inbox) } let!(:response_source) { create(:response_source, account: account) }
let(:valid_params) do let(:valid_params) do
{ document_link: 'http://test.test' } { document_link: 'http://test.test' }
end end
@@ -103,7 +101,7 @@ RSpec.describe 'Response Sources API', type: :request do
end end
describe 'POST /api/v1/accounts/{account.id}/response_sources/{response_source.id}/remove_document' do describe 'POST /api/v1/accounts/{account.id}/response_sources/{response_source.id}/remove_document' do
let!(:response_source) { create(:response_source, account: account, inbox: inbox) } let!(:response_source) { create(:response_source, account: account) }
let!(:response_document) { response_source.response_documents.create!(document_link: 'http://test.test') } let!(:response_document) { response_source.response_documents.create!(document_link: 'http://test.test') }
let(:valid_params) do let(:valid_params) do
{ document_id: response_document.id } { document_id: response_document.id }

View File

@@ -71,7 +71,9 @@ RSpec.describe 'Enterprise Inboxes API', type: :request do
end end
it 'returns all response_sources belonging to the inbox to administrators' do it 'returns all response_sources belonging to the inbox to administrators' do
response_source = create(:response_source, account: account, inbox: inbox) response_source = create(:response_source, account: account)
inbox.response_sources << response_source
inbox.save!
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources", get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/response_sources",
headers: administrator.create_new_auth_token, headers: administrator.create_new_auth_token,
as: :json as: :json