-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
response_bot_service.rb
96 lines (78 loc) 路 2.94 KB
/
response_bot_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 inbox_response_sources].each do |table|
send("create_#{table}_table")
end
end
def drop_tables
%i[responses response_documents response_sources inbox_response_sources].each do |table|
MIGRATION_VERSION.drop_table table if MIGRATION_VERSION.table_exists?(table)
end
end
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
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.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_source_id, null: false
t.bigint :response_document_id
t.string :question, null: false
t.text :answer, null: false
t.integer :status, default: 0
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