2023-03-15 16:21:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
desc "Creates tables to store embeddings"
|
|
|
|
task "ai:embeddings:create_table" => [:environment] do
|
2023-03-20 15:44:55 -04:00
|
|
|
DiscourseAi::Database::Connection.db.exec(<<~SQL)
|
2023-03-31 14:29:56 -04:00
|
|
|
CREATE EXTENSION IF NOT EXISTS vector;
|
2023-03-20 15:44:55 -04:00
|
|
|
SQL
|
|
|
|
|
2023-03-31 14:29:56 -04:00
|
|
|
DiscourseAi::Embeddings::Model.enabled_models.each do |model|
|
2023-03-15 16:21:45 -04:00
|
|
|
DiscourseAi::Database::Connection.db.exec(<<~SQL)
|
|
|
|
CREATE TABLE IF NOT EXISTS topic_embeddings_#{model.name.underscore} (
|
|
|
|
topic_id bigint PRIMARY KEY,
|
|
|
|
embedding vector(#{model.dimensions})
|
|
|
|
);
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Backfill embeddings for all topics"
|
2023-05-17 19:21:28 -04:00
|
|
|
task "ai:embeddings:backfill", [:start_topic] => [:environment] do |_, args|
|
2023-03-15 16:21:45 -04:00
|
|
|
public_categories = Category.where(read_restricted: false).pluck(:id)
|
2023-03-31 14:29:56 -04:00
|
|
|
topic_embeddings = DiscourseAi::Embeddings::Topic.new
|
2023-03-15 16:21:45 -04:00
|
|
|
Topic
|
2023-05-09 12:45:16 -04:00
|
|
|
.where("id >= ?", args[:start_topic] || 0)
|
2023-03-31 14:29:56 -04:00
|
|
|
.where("category_id IN (?)", public_categories)
|
2023-03-15 16:21:45 -04:00
|
|
|
.where(deleted_at: nil)
|
2023-05-09 12:45:16 -04:00
|
|
|
.order(id: :asc)
|
2023-03-15 16:21:45 -04:00
|
|
|
.find_each do |t|
|
|
|
|
print "."
|
2023-03-31 14:29:56 -04:00
|
|
|
topic_embeddings.generate_and_store_embeddings_for(t)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Creates indexes for embeddings"
|
2023-03-20 15:44:55 -04:00
|
|
|
task "ai:embeddings:index", [:work_mem] => [:environment] do |_, args|
|
2023-05-09 12:45:16 -04:00
|
|
|
# Using extension maintainer's recommendation for ivfflat indexes
|
2023-03-15 16:21:45 -04:00
|
|
|
# Results are not as good as without indexes, but it's much faster
|
|
|
|
# Disk usage is ~1x the size of the table, so this double table total size
|
2023-05-09 12:45:16 -04:00
|
|
|
count = Topic.count
|
|
|
|
lists = count < 1_000_000 ? count / 1000 : Math.sqrt(count).to_i
|
|
|
|
probes = count < 1_000_000 ? lists / 10 : Math.sqrt(lists).to_i
|
2023-03-15 16:21:45 -04:00
|
|
|
|
2023-03-20 15:44:55 -04:00
|
|
|
DiscourseAi::Database::Connection.db.exec("SET work_mem TO '#{args[:work_mem] || "1GB"}';")
|
2023-03-31 14:29:56 -04:00
|
|
|
DiscourseAi::Embeddings::Model.enabled_models.each do |model|
|
2023-03-15 16:21:45 -04:00
|
|
|
DiscourseAi::Database::Connection.db.exec(<<~SQL)
|
|
|
|
CREATE INDEX IF NOT EXISTS
|
|
|
|
topic_embeddings_#{model.name.underscore}_search
|
|
|
|
ON
|
|
|
|
topic_embeddings_#{model.name.underscore}
|
|
|
|
USING
|
2023-03-31 14:29:56 -04:00
|
|
|
ivfflat (embedding #{model.pg_index})
|
2023-03-15 16:21:45 -04:00
|
|
|
WITH
|
|
|
|
(lists = #{lists});
|
|
|
|
SQL
|
|
|
|
end
|
2023-05-09 12:45:16 -04:00
|
|
|
DiscourseAi::Database::Connection.db.exec("RESET work_mem;")
|
|
|
|
DiscourseAi::Database::Connection.db.exec("SET ivfflat.probes = #{probes};")
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|