2023-03-15 16:21:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Embeddings
|
2023-03-30 20:04:34 -04:00
|
|
|
class SemanticRelated
|
2023-05-22 20:43:24 -04:00
|
|
|
def self.semantic_suggested_key(topic_id)
|
|
|
|
"semantic-suggested-topic-#{topic_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_semantic_suggested_key(topic_id)
|
|
|
|
"build-semantic-suggested-topic-#{topic_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache_for(topic)
|
|
|
|
Discourse.cache.delete(semantic_suggested_key(topic.id))
|
|
|
|
Discourse.redis.del(build_semantic_suggested_key(topic.id))
|
|
|
|
end
|
|
|
|
|
2023-03-30 18:07:22 -04:00
|
|
|
def self.candidates_for(topic)
|
2023-03-30 20:04:34 -04:00
|
|
|
return ::Topic.none if SiteSetting.ai_embeddings_semantic_related_topics < 1
|
|
|
|
|
2023-03-15 16:21:45 -04:00
|
|
|
cache_for =
|
|
|
|
case topic.created_at
|
|
|
|
when 6.hour.ago..Time.now
|
|
|
|
15.minutes
|
|
|
|
when 1.day.ago..6.hour.ago
|
|
|
|
1.hour
|
|
|
|
else
|
|
|
|
1.day
|
|
|
|
end
|
|
|
|
|
2023-03-31 14:29:56 -04:00
|
|
|
model =
|
|
|
|
DiscourseAi::Embeddings::Model.instantiate(
|
|
|
|
SiteSetting.ai_embeddings_semantic_related_model,
|
|
|
|
)
|
|
|
|
|
2023-03-15 16:21:45 -04:00
|
|
|
begin
|
|
|
|
candidate_ids =
|
|
|
|
Discourse
|
|
|
|
.cache
|
2023-05-22 20:43:24 -04:00
|
|
|
.fetch(semantic_suggested_key(topic.id), expires_in: cache_for) do
|
2023-03-31 14:29:56 -04:00
|
|
|
DiscourseAi::Embeddings::Topic.new.symmetric_semantic_search(model, topic)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
2023-05-22 20:43:24 -04:00
|
|
|
rescue MissingEmbeddingError
|
|
|
|
# avoid a flood of jobs when visiting topic
|
|
|
|
if Discourse.redis.set(
|
|
|
|
build_semantic_suggested_key(topic.id),
|
|
|
|
"queued",
|
|
|
|
ex: 15.minutes.to_i,
|
|
|
|
nx: true,
|
|
|
|
)
|
|
|
|
Jobs.enqueue(:generate_embeddings, topic_id: topic.id)
|
|
|
|
end
|
2023-03-30 18:07:22 -04:00
|
|
|
return ::Topic.none
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
|
2023-05-09 14:30:50 -04:00
|
|
|
topic_list = ::Topic.visible.listable_topics.secured
|
|
|
|
|
|
|
|
unless SiteSetting.ai_embeddings_semantic_related_include_closed_topics
|
|
|
|
topic_list = topic_list.where(closed: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
topic_list
|
2023-03-30 21:04:17 -04:00
|
|
|
.where("id <> ?", topic.id)
|
2023-03-30 18:07:22 -04:00
|
|
|
.where(id: candidate_ids)
|
2023-05-09 14:30:50 -04:00
|
|
|
# array_position forces the order of the topics to be preserved
|
2023-03-30 18:07:22 -04:00
|
|
|
.order("array_position(ARRAY#{candidate_ids}, id)")
|
2023-03-30 20:04:34 -04:00
|
|
|
.limit(SiteSetting.ai_embeddings_semantic_related_topics)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|