FEATURE: Allow excluding closed topics from semantic related (#55)

This commit is contained in:
Rafael dos Santos Silva 2023-05-09 15:30:50 -03:00 committed by GitHub
parent f1133f66a6
commit e5537d4c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 7 deletions

View File

@ -148,6 +148,7 @@ plugins:
ai_embeddings_generate_for_pms: false
ai_embeddings_semantic_related_topics_enabled: false
ai_embeddings_semantic_related_topics: 5
ai_embeddings_semantic_related_include_closed_topics: true
ai_embeddings_pg_connection_string: ""
ai_embeddings_semantic_search_enabled:
default: false

View File

@ -34,13 +34,16 @@ module DiscourseAi
return ::Topic.none
end
# array_position forces the order of the topics to be preserved
::Topic
.visible
.listable_topics
.secured
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
.where("id <> ?", topic.id)
.where(id: candidate_ids)
# array_position forces the order of the topics to be preserved
.order("array_position(ARRAY#{candidate_ids}, id)")
.limit(SiteSetting.ai_embeddings_semantic_related_topics)
end

View File

@ -33,7 +33,7 @@ module DiscourseAi
topic_id = :topic_id
LIMIT 1
)
LIMIT 11
LIMIT 100
SQL
# Happens when the topic doesn't have any embeddings

View File

@ -11,6 +11,7 @@ describe DiscourseAi::Embeddings::SemanticRelated do
fab!(:private_topic) { Fabricate(:private_message_topic) }
fab!(:secured_category) { Fabricate(:category, read_restricted: true) }
fab!(:secured_category_topic) { Fabricate(:topic, category: secured_category) }
fab!(:closed_topic) { Fabricate(:topic, closed: true) }
before { SiteSetting.ai_embeddings_semantic_related_topics_enabled = true }
@ -20,7 +21,7 @@ describe DiscourseAi::Embeddings::SemanticRelated do
DiscourseAi::Embeddings::Topic
.any_instance
.expects(:symmetric_semantic_search)
.returns(Topic.unscoped.order(id: :desc).limit(10).pluck(:id))
.returns(Topic.unscoped.order(id: :desc).limit(100).pluck(:id))
end
after { Discourse.cache.clear }
@ -30,10 +31,19 @@ describe DiscourseAi::Embeddings::SemanticRelated do
expect(results).to include(normal_topic_1)
expect(results).to include(normal_topic_2)
expect(results).to include(normal_topic_3)
expect(results).to include(closed_topic)
expect(results).to_not include(target)
expect(results).to_not include(unlisted_topic)
expect(results).to_not include(private_topic)
expect(results).to_not include(secured_category_topic)
end
context "when ai_embeddings_semantic_related_include_closed_topics is false" do
before { SiteSetting.ai_embeddings_semantic_related_include_closed_topics = false }
it "do not return closed topics" do
results = described_class.candidates_for(target).to_a
expect(results).to_not include(closed_topic)
end
end
end
end