Migration Guide: https://chwt.app/v4/migration This PR imports all the work related to Captain into the EE codebase. Captain represents the AI-based features in Chatwoot and includes the following key components: - Assistant: An assistant has a persona, the product it would be trained on. At the moment, the data at which it is trained is from websites. Future integrations on Notion documents, PDF etc. This PR enables connecting an assistant to an inbox. The assistant would run the conversation every time before transferring it to an agent. - Copilot for Agents: When an agent is supporting a customer, we will be able to offer additional help to lookup some data or fetch information from integrations etc via copilot. - Conversation FAQ generator: When a conversation is resolved, the Captain integration would identify questions which were not in the knowledge base. - CRM memory: Learns from the conversations and identifies important information about the contact. --------- Co-authored-by: Vishnu Narayanan <vishnu@chatwoot.com> Co-authored-by: Sojan <sojan@pepalo.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
91 lines
2.8 KiB
Ruby
91 lines
2.8 KiB
Ruby
class CreateCaptainTables < ActiveRecord::Migration[7.0]
|
|
def up
|
|
# Post this migration, the 'vector' extension is mandatory to run the application.
|
|
# If the extension is not installed, the migration will raise an error.
|
|
setup_vector_extension
|
|
create_assistants
|
|
create_documents
|
|
create_assistant_responses
|
|
create_old_tables
|
|
end
|
|
|
|
def down
|
|
drop_table :captain_assistant_responses if table_exists?(:captain_assistant_responses)
|
|
drop_table :captain_documents if table_exists?(:captain_documents)
|
|
drop_table :captain_assistants if table_exists?(:captain_assistants)
|
|
drop_table :article_embeddings if table_exists?(:article_embeddings)
|
|
|
|
# We are not disabling the extension here because it might be
|
|
# used by other tables which are not part of this migration.
|
|
end
|
|
|
|
private
|
|
|
|
def setup_vector_extension
|
|
return if extension_enabled?('vector')
|
|
|
|
begin
|
|
enable_extension 'vector'
|
|
rescue ActiveRecord::StatementInvalid
|
|
raise StandardError, "Failed to enable 'vector' extension. Read more at https://chwt.app/v4/migration"
|
|
end
|
|
end
|
|
|
|
def create_assistants
|
|
create_table :captain_assistants do |t|
|
|
t.string :name, null: false
|
|
t.bigint :account_id, null: false
|
|
t.string :description
|
|
|
|
t.timestamps
|
|
end
|
|
|
|
add_index :captain_assistants, :account_id
|
|
add_index :captain_assistants, [:account_id, :name], unique: true
|
|
end
|
|
|
|
def create_documents
|
|
create_table :captain_documents do |t|
|
|
t.string :name, null: false
|
|
t.string :external_link, null: false
|
|
t.text :content
|
|
t.bigint :assistant_id, null: false
|
|
t.bigint :account_id, null: false
|
|
|
|
t.timestamps
|
|
end
|
|
|
|
add_index :captain_documents, :account_id
|
|
add_index :captain_documents, :assistant_id
|
|
add_index :captain_documents, [:assistant_id, :external_link], unique: true
|
|
end
|
|
|
|
def create_assistant_responses
|
|
create_table :captain_assistant_responses do |t|
|
|
t.string :question, null: false
|
|
t.text :answer, null: false
|
|
t.vector :embedding, limit: 1536
|
|
t.bigint :assistant_id, null: false
|
|
t.bigint :document_id
|
|
t.bigint :account_id, null: false
|
|
|
|
t.timestamps
|
|
end
|
|
|
|
add_index :captain_assistant_responses, :account_id
|
|
add_index :captain_assistant_responses, :assistant_id
|
|
add_index :captain_assistant_responses, :document_id
|
|
add_index :captain_assistant_responses, :embedding, using: :ivfflat, name: 'vector_idx_knowledge_entries_embedding', opclass: :vector_l2_ops
|
|
end
|
|
|
|
def create_old_tables
|
|
create_table :article_embeddings, if_not_exists: true do |t|
|
|
t.bigint :article_id, null: false
|
|
t.text :term, null: false
|
|
t.vector :embedding, limit: 1536
|
|
t.timestamps
|
|
end
|
|
add_index :article_embeddings, :embedding, if_not_exists: true, using: :ivfflat, opclass: :vector_l2_ops
|
|
end
|
|
end
|