2024-01-04 08:44:07 -05:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::AiBot::Tools::Search do
|
|
|
|
before { SearchIndexer.enable }
|
|
|
|
after { SearchIndexer.disable }
|
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
before { SiteSetting.ai_openai_api_key = "asd" }
|
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-3.5-turbo") }
|
2024-01-29 14:04:25 -05:00
|
|
|
let(:llm) { DiscourseAi::Completions::Llm.proxy("open_ai:gpt-3.5-turbo") }
|
2024-01-04 08:44:07 -05:00
|
|
|
let(:progress_blk) { Proc.new {} }
|
|
|
|
|
|
|
|
fab!(:admin)
|
|
|
|
fab!(:parent_category) { Fabricate(:category, name: "animals") }
|
|
|
|
fab!(:category) { Fabricate(:category, parent_category: parent_category, name: "amazing-cat") }
|
|
|
|
fab!(:tag_funny) { Fabricate(:tag, name: "funny") }
|
|
|
|
fab!(:tag_sad) { Fabricate(:tag, name: "sad") }
|
|
|
|
fab!(:tag_hidden) { Fabricate(:tag, name: "hidden") }
|
|
|
|
fab!(:staff_tag_group) do
|
|
|
|
tag_group = Fabricate.build(:tag_group, name: "Staff only", tag_names: ["hidden"])
|
|
|
|
|
|
|
|
tag_group.permissions = [
|
|
|
|
[Group::AUTO_GROUPS[:staff], TagGroupPermission.permission_types[:full]],
|
|
|
|
]
|
|
|
|
tag_group.save!
|
|
|
|
tag_group
|
|
|
|
end
|
|
|
|
fab!(:topic_with_tags) do
|
|
|
|
Fabricate(:topic, category: category, tags: [tag_funny, tag_sad, tag_hidden])
|
|
|
|
end
|
|
|
|
|
2024-05-09 21:32:34 -04:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:group)
|
|
|
|
fab!(:private_category) do
|
|
|
|
c = Fabricate(:category_with_definition)
|
|
|
|
c.set_permissions(group => :readonly)
|
|
|
|
c.save
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
before { SiteSetting.ai_bot_enabled = true }
|
|
|
|
|
|
|
|
describe "#invoke" do
|
2024-01-12 18:28:06 -05:00
|
|
|
it "can retrieve options from persona correctly" do
|
2024-05-09 21:32:34 -04:00
|
|
|
persona_options = {
|
|
|
|
"base_query" => "#funny",
|
|
|
|
"search_private" => "true",
|
|
|
|
"max_results" => "10",
|
|
|
|
}
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
search_post = Fabricate(:post, topic: topic_with_tags)
|
2024-05-09 21:32:34 -04:00
|
|
|
private_search_post = Fabricate(:post, topic: Fabricate(:topic, category: private_category))
|
|
|
|
private_search_post.topic.tags = [tag_funny]
|
|
|
|
private_search_post.topic.save!
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-02-18 22:52:12 -05:00
|
|
|
_bot_post = Fabricate(:post)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
search =
|
|
|
|
described_class.new(
|
|
|
|
{ order: "latest" },
|
|
|
|
persona_options: persona_options,
|
|
|
|
bot_user: bot_user,
|
|
|
|
llm: llm,
|
|
|
|
context: {
|
2024-05-09 21:32:34 -04:00
|
|
|
user: user,
|
2024-05-07 07:55:46 -04:00
|
|
|
},
|
|
|
|
)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-05-09 21:32:34 -04:00
|
|
|
expect(search.options[:base_query]).to eq("#funny")
|
|
|
|
expect(search.options[:search_private]).to eq(true)
|
|
|
|
expect(search.options[:max_results]).to eq(10)
|
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
results = search.invoke(&progress_blk)
|
2024-01-04 08:44:07 -05:00
|
|
|
expect(results[:rows].length).to eq(1)
|
|
|
|
|
2024-06-14 01:28:40 -04:00
|
|
|
expect(search.last_query).to eq("#funny order:latest")
|
|
|
|
|
2024-05-09 21:32:34 -04:00
|
|
|
GroupUser.create!(group: group, user: user)
|
|
|
|
|
|
|
|
results = search.invoke(&progress_blk)
|
|
|
|
expect(results[:rows].length).to eq(2)
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
search_post.topic.tags = []
|
|
|
|
search_post.topic.save!
|
|
|
|
|
2024-05-09 21:32:34 -04:00
|
|
|
# no longer has the tag funny, but secure one does
|
2024-05-07 07:55:46 -04:00
|
|
|
results = search.invoke(&progress_blk)
|
2024-05-09 21:32:34 -04:00
|
|
|
expect(results[:rows].length).to eq(1)
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can handle no results" do
|
2024-02-18 22:52:12 -05:00
|
|
|
_post1 = Fabricate(:post, topic: topic_with_tags)
|
2024-05-07 07:55:46 -04:00
|
|
|
search =
|
|
|
|
described_class.new(
|
|
|
|
{ search_query: "ABDDCDCEDGDG", order: "fake" },
|
|
|
|
bot_user: bot_user,
|
|
|
|
llm: llm,
|
|
|
|
)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
results = search.invoke(&progress_blk)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
expect(results[:args]).to eq({ search_query: "ABDDCDCEDGDG", order: "fake" })
|
|
|
|
expect(results[:rows]).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "semantic search" do
|
2024-01-12 18:28:06 -05:00
|
|
|
let(:query) { "this is an expanded search" }
|
2024-01-04 08:44:07 -05:00
|
|
|
after { DiscourseAi::Embeddings::SemanticSearch.clear_cache_for(query) }
|
|
|
|
|
|
|
|
it "supports semantic search when enabled" do
|
2024-06-19 17:01:35 -04:00
|
|
|
assign_fake_provider_to(:ai_embeddings_semantic_search_hyde_model)
|
2024-01-04 08:44:07 -05:00
|
|
|
SiteSetting.ai_embeddings_semantic_search_enabled = true
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
|
|
|
|
|
|
|
hyde_embedding = [0.049382, 0.9999]
|
|
|
|
EmbeddingsGenerationStubs.discourse_service(
|
|
|
|
SiteSetting.ai_embeddings_model,
|
|
|
|
query,
|
|
|
|
hyde_embedding,
|
|
|
|
)
|
|
|
|
|
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
2024-05-07 07:55:46 -04:00
|
|
|
search =
|
|
|
|
described_class.new(
|
|
|
|
{ search_query: "hello world, sam", status: "public" },
|
|
|
|
llm: llm,
|
|
|
|
bot_user: bot_user,
|
|
|
|
)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-01-11 12:16:25 -05:00
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::BgeLargeEn
|
2024-01-04 08:44:07 -05:00
|
|
|
.any_instance
|
|
|
|
.expects(:asymmetric_topics_similarity_search)
|
|
|
|
.returns([post1.topic_id])
|
|
|
|
|
|
|
|
results =
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["<ai>#{query}</ai>"]) do
|
2024-05-07 07:55:46 -04:00
|
|
|
search.invoke(&progress_blk)
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(results[:args]).to eq({ search_query: "hello world, sam", status: "public" })
|
|
|
|
expect(results[:rows].length).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports subfolder properly" do
|
|
|
|
Discourse.stubs(:base_path).returns("/subfolder")
|
|
|
|
|
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
search =
|
|
|
|
described_class.new({ limit: 1, user: post1.user.username }, bot_user: bot_user, llm: llm)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
results = search.invoke(&progress_blk)
|
2024-01-04 08:44:07 -05:00
|
|
|
expect(results[:rows].to_s).to include("/subfolder" + post1.url)
|
|
|
|
end
|
|
|
|
|
2024-02-18 22:52:12 -05:00
|
|
|
it "passes on all search params" do
|
|
|
|
params =
|
|
|
|
described_class.signature[:parameters]
|
|
|
|
.map do |param|
|
|
|
|
if param[:type] == "integer"
|
|
|
|
[param[:name], 1]
|
|
|
|
else
|
|
|
|
[param[:name], "test"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
.to_h
|
|
|
|
.symbolize_keys
|
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
search = described_class.new(params, bot_user: bot_user, llm: llm)
|
|
|
|
results = search.invoke(&progress_blk)
|
2024-02-18 22:52:12 -05:00
|
|
|
|
|
|
|
expect(results[:args]).to eq(params)
|
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
it "returns rich topic information" do
|
|
|
|
post1 = Fabricate(:post, like_count: 1, topic: topic_with_tags)
|
2024-05-07 07:55:46 -04:00
|
|
|
search = described_class.new({ user: post1.user.username }, bot_user: bot_user, llm: llm)
|
2024-01-04 08:44:07 -05:00
|
|
|
post1.topic.update!(views: 100, posts_count: 2, like_count: 10)
|
|
|
|
|
2024-05-07 07:55:46 -04:00
|
|
|
results = search.invoke(&progress_blk)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
row = results[:rows].first
|
|
|
|
category = row[results[:column_names].index("category")]
|
|
|
|
|
|
|
|
expect(category).to eq("animals > amazing-cat")
|
|
|
|
|
|
|
|
tags = row[results[:column_names].index("tags")]
|
|
|
|
expect(tags).to eq("funny, sad")
|
|
|
|
|
|
|
|
likes = row[results[:column_names].index("likes")]
|
|
|
|
expect(likes).to eq(1)
|
|
|
|
|
|
|
|
username = row[results[:column_names].index("username")]
|
|
|
|
expect(username).to eq(post1.user.username)
|
|
|
|
|
|
|
|
likes = row[results[:column_names].index("topic_likes")]
|
|
|
|
expect(likes).to eq(10)
|
|
|
|
|
|
|
|
views = row[results[:column_names].index("topic_views")]
|
|
|
|
expect(views).to eq(100)
|
|
|
|
|
|
|
|
replies = row[results[:column_names].index("topic_replies")]
|
|
|
|
expect(replies).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|