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,7 +8,12 @@ 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
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 SELECT
topic_id topic_id
FROM FROM
@ -27,8 +32,12 @@ module DiscourseAI
) )
LIMIT 10 LIMIT 10
SQL 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