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 - all-mpnet-base-v2
- msmarco-distilbert-base-v4 - msmarco-distilbert-base-v4
- text-embedding-ada-002 - text-embedding-ada-002
ai_embeddings_generate_for_pms: false
ai_embeddings_semantic_suggested_topics_anons_enabled: false ai_embeddings_semantic_suggested_topics_anons_enabled: false
ai_embeddings_pg_connection_string: ai_embeddings_pg_connection_string: ""
default: "postgresql://localhost/embeddings"

View File

@ -6,6 +6,8 @@ module Jobs
return unless SiteSetting.ai_embeddings_enabled return unless SiteSetting.ai_embeddings_enabled
return if (topic_id = args[:topic_id]).blank? 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 post = Topic.find_by_id(topic_id).first_post
return if post.nil? || post.raw.blank? return if post.nil? || post.raw.blank?

View File

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