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_generate_for_pms: false
ai_embeddings_semantic_related_topics_enabled: false ai_embeddings_semantic_related_topics_enabled: false
ai_embeddings_semantic_related_topics: 5 ai_embeddings_semantic_related_topics: 5
ai_embeddings_semantic_related_include_closed_topics: true
ai_embeddings_pg_connection_string: "" ai_embeddings_pg_connection_string: ""
ai_embeddings_semantic_search_enabled: ai_embeddings_semantic_search_enabled:
default: false default: false

View File

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

View File

@ -33,7 +33,7 @@ module DiscourseAi
topic_id = :topic_id topic_id = :topic_id
LIMIT 1 LIMIT 1
) )
LIMIT 11 LIMIT 100
SQL SQL
# Happens when the topic doesn't have any embeddings # 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!(:private_topic) { Fabricate(:private_message_topic) }
fab!(:secured_category) { Fabricate(:category, read_restricted: true) } fab!(:secured_category) { Fabricate(:category, read_restricted: true) }
fab!(:secured_category_topic) { Fabricate(:topic, category: secured_category) } 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 } before { SiteSetting.ai_embeddings_semantic_related_topics_enabled = true }
@ -20,7 +21,7 @@ describe DiscourseAi::Embeddings::SemanticRelated do
DiscourseAi::Embeddings::Topic DiscourseAi::Embeddings::Topic
.any_instance .any_instance
.expects(:symmetric_semantic_search) .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 end
after { Discourse.cache.clear } 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_1)
expect(results).to include(normal_topic_2) expect(results).to include(normal_topic_2)
expect(results).to include(normal_topic_3) expect(results).to include(normal_topic_3)
expect(results).to include(closed_topic)
expect(results).to_not include(target) expect(results).to_not include(target)
expect(results).to_not include(unlisted_topic) expect(results).to_not include(unlisted_topic)
expect(results).to_not include(private_topic) expect(results).to_not include(private_topic)
expect(results).to_not include(secured_category_topic) expect(results).to_not include(secured_category_topic)
end 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
end end