Files

53 lines
1.8 KiB
Ruby
Raw Permalink Normal View History

2023-03-15 17:21:45 -03:00
# frozen_string_literal: true
desc "Backfill embeddings for all topics"
2023-05-17 20:21:28 -03:00
task "ai:embeddings:backfill", [:start_topic] => [:environment] do |_, args|
2023-03-15 17:21:45 -03:00
public_categories = Category.where(read_restricted: false).pluck(:id)
manager = DiscourseAi::Embeddings::Manager.new(Topic.first)
2023-03-15 17:21:45 -03:00
Topic
.joins(
"LEFT JOIN #{manager.topic_embeddings_table} ON #{manager.topic_embeddings_table}.topic_id = topics.id",
)
.where("#{manager.topic_embeddings_table}.topic_id IS NULL")
.where("topics.id >= ?", args[:start_topic].to_i || 0)
.where("category_id IN (?)", public_categories)
2023-03-15 17:21:45 -03:00
.where(deleted_at: nil)
.order("topics.id ASC")
2023-03-15 17:21:45 -03:00
.find_each do |t|
print "."
DiscourseAi::Embeddings::Manager.new(t).generate!
2023-03-15 17:21:45 -03:00
end
end
desc "Creates indexes for embeddings"
task "ai:embeddings:index", [:work_mem] => [:environment] do |_, args|
2023-05-09 13:45:16 -03:00
# Using extension maintainer's recommendation for ivfflat indexes
2023-03-15 17:21:45 -03: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 doubles table total size
2023-05-09 13:45:16 -03: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 17:21:45 -03:00
manager = DiscourseAi::Embeddings::Manager.new(Topic.first)
table = manager.topic_embeddings_table
index = "#{table}_search"
DB.exec("SET work_mem TO '#{args[:work_mem] || "1GB"}';")
DB.exec(<<~SQL)
DROP INDEX IF EXISTS #{index};
CREATE INDEX IF NOT EXISTS
#{index}
ON
#{table}
USING
ivfflat (embeddings #{manager.model.pg_index_type})
WITH
(lists = #{lists})
WHERE
model_version = #{manager.model.version} AND
strategy_version = #{manager.strategy.version};
SQL
DB.exec("RESET work_mem;")
DB.exec("SET ivfflat.probes = #{probes};")
2023-03-15 17:21:45 -03:00
end