Cache, rescue, disable on PMs

This commit is contained in:
Rafael dos Santos Silva 2023-03-14 12:46:38 -03:00
parent 7055a290ae
commit c3b9b46813
No known key found for this signature in database
GPG Key ID: 5E50360227B34938
3 changed files with 33 additions and 22 deletions

View File

@ -102,7 +102,7 @@ plugins:
- all-mpnet-base-v2
- msmarco-distilbert-base-v4
- text-embedding-ada-002
ai_embeddings_generate_for_pms: false
ai_embeddings_semantic_suggested_topics_anons_enabled: false
ai_embeddings_pg_connection_string:
default: "postgresql://localhost/embeddings"
ai_embeddings_pg_connection_string: ""

View File

@ -6,6 +6,8 @@ module Jobs
return unless SiteSetting.ai_embeddings_enabled
return if (topic_id = args[:topic_id]).blank?
topic = Topic.find_by_id(topic_id)
return if topic.private_message? && !SiteSetting.ai_embeddings_enabled_for_private_messages
post = Topic.find_by_id(topic_id).first_post
return if post.nil? || post.raw.blank?

View File

@ -8,27 +8,36 @@ module DiscourseAI
return if topic_query.user
return if topic.private_message?
candidate_ids = DiscourseAI::Database::Connection.db.query(<<~SQL, topic_id: topic.id)
SELECT
topic_id
FROM
topic_embeddings_symetric_discourse
WHERE
topic_id != :topic_id
ORDER BY
embeddings <#> (
SELECT
embeddings
FROM
topic_embeddings_symetric_discourse
WHERE
topic_id = :topic_id
LIMIT 1
)
LIMIT 10
SQL
begin
candidate_ids =
Discourse
.cache
.fetch("semantic-suggested-topic-#{topic.id}", expires_in: 1.hour) do
DiscourseAI::Database::Connection.db.query(<<~SQL, topic_id: topic.id).map(&:topic_id)
SELECT
topic_id
FROM
topic_embeddings_symetric_discourse
WHERE
topic_id != :topic_id
ORDER BY
embeddings <#> (
SELECT
embeddings
FROM
topic_embeddings_symetric_discourse
WHERE
topic_id = :topic_id
LIMIT 1
)
LIMIT 10
SQL
end
rescue StandardError => e
Rails.logger.error("SemanticSuggested: #{e}")
end
candidates = ::Topic.where(id: candidate_ids.map(&:topic_id))
candidates = ::Topic.where(id: candidate_ids)
{ result: candidates, params: {} }
end
end