FEATURE: add filter support to ai bot semantic search (#222)

Previously we would bypass semantic search if any filters were
present

Also shows progress now.
This commit is contained in:
Sam 2023-09-13 14:59:45 +10:00 committed by GitHub
parent d1642533fb
commit cdd6faa648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 11 deletions

View File

@ -100,6 +100,9 @@ module DiscourseAi
end
def show_progress(text, progress_caret: false)
return if !@post
return if !@placeholder
# during tests we may have none
caret = progress_caret ? PROGRESS_CARET : CARET
new_placeholder = @placeholder.sub(caret, text + caret)

View File

@ -116,6 +116,9 @@ module DiscourseAi::AiBot::Commands
.join(" ")
@last_query = search_string
show_progress(localized_description)
results =
Search.execute(
search_string.to_s + " status:public",
@ -129,7 +132,6 @@ module DiscourseAi::AiBot::Commands
should_try_semantic_search = SiteSetting.ai_embeddings_semantic_search_enabled
should_try_semantic_search &&= (limit == MAX_RESULTS)
should_try_semantic_search &&= (search_args.keys - %i[search_query order]).length == 0
should_try_semantic_search &&= (search_args[:search_query].present?)
limit = limit - MIN_SEMANTIC_RESULTS if should_try_semantic_search
@ -141,16 +143,19 @@ module DiscourseAi::AiBot::Commands
semantic_search = DiscourseAi::Embeddings::SemanticSearch.new(Guardian.new())
topic_ids = Set.new(posts.map(&:topic_id))
semantic_search
.search_for_topics(search_args[:search_query])
.each do |post|
next if topic_ids.include?(post.topic_id)
search = Search.new(search_string, guardian: Guardian.new)
topic_ids << post.topic_id
posts << post
results = semantic_search.search_for_topics(search.term)
results = search.apply_filters(results)
break if posts.length >= MAX_RESULTS
end
results.each do |post|
next if topic_ids.include?(post.topic_id)
topic_ids << post.topic_id
posts << post
break if posts.length >= MAX_RESULTS
end
end
@last_num_results = posts.length

View File

@ -69,9 +69,9 @@ RSpec.describe DiscourseAi::AiBot::Commands::SearchCommand do
.expects(:asymmetric_topics_similarity_search)
.returns([post1.topic_id])
results = search.process(search_query: "hello world, sam")
results = search.process(search_query: "hello world, sam", status: "public")
expect(results[:args]).to eq({ search_query: "hello world, sam" })
expect(results[:args]).to eq({ search_query: "hello world, sam", status: "public" })
expect(results[:rows].length).to eq(1)
end
end