Files
chatwoot/enterprise/app/services/features/response_bot_service.rb
Sojan Jose 480f34803b feat: Response Bot using GPT and Webpage Sources (#7518)
This commit introduces the ability to associate response sources to an inbox, allowing external webpages to be parsed by Chatwoot. The parsed data is converted into embeddings for use with GPT models when managing customer queries.

The implementation relies on the `pgvector` extension for PostgreSQL. Database migrations related to this feature are handled separately by `Features::ResponseBotService`. A future update will integrate these migrations into the default rails migrations, once compatibility with Postgres extensions across all self-hosted installation options is confirmed.

Additionally, a new GitHub action has been added to the CI pipeline to ensure the execution of specs related to this feature.
2023-07-21 18:11:51 +03:00

84 lines
2.3 KiB
Ruby

class Features::ResponseBotService
MIGRATION_VERSION = ActiveRecord::Migration[7.0]
def enable_in_installation
enable_vector_extension
create_tables
end
def enable_vector_extension
MIGRATION_VERSION.enable_extension 'vector'
rescue ActiveRecord::StatementInvalid
print 'Vector extension not available'
end
def disable_vector_extension
MIGRATION_VERSION.disable_extension 'vector'
end
def vector_extension_enabled?
ActiveRecord::Base.connection.extension_enabled?('vector')
end
def create_tables
return unless vector_extension_enabled?
%i[response_sources response_documents responses].each do |table|
send("create_#{table}_table")
end
end
def drop_tables
%i[responses response_documents response_sources].each do |table|
MIGRATION_VERSION.drop_table table if MIGRATION_VERSION.table_exists?(table)
end
end
private
def create_response_sources_table
return if MIGRATION_VERSION.table_exists?(:response_sources)
MIGRATION_VERSION.create_table :response_sources do |t|
t.integer :source_type, null: false, default: 0
t.string :name, null: false
t.string :source_link
t.references :source_model, polymorphic: true
t.bigint :account_id, null: false
t.bigint :inbox_id, null: false
t.timestamps
end
end
def create_response_documents_table
return if MIGRATION_VERSION.table_exists?(:response_documents)
MIGRATION_VERSION.create_table :response_documents do |t|
t.bigint :response_source_id, null: false
t.string :document_link
t.references :document, polymorphic: true
t.text :content
t.bigint :account_id, null: false
t.timestamps
end
MIGRATION_VERSION.add_index :response_documents, :response_source_id
end
def create_responses_table
return if MIGRATION_VERSION.table_exists?(:responses)
MIGRATION_VERSION.create_table :responses do |t|
t.bigint :response_document_id
t.string :question, null: false
t.text :answer, null: false
t.bigint :account_id, null: false
t.vector :embedding, limit: 1536
t.timestamps
end
MIGRATION_VERSION.add_index :responses, :response_document_id
MIGRATION_VERSION.add_index :responses, :embedding, using: :ivfflat, opclass: :vector_l2_ops
end
end