2023-05-21 22:09:14 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::AiBot::Commands::SearchCommand do
|
|
|
|
before { SearchIndexer.enable }
|
|
|
|
after { SearchIndexer.disable }
|
|
|
|
|
2023-09-12 02:09:28 -04:00
|
|
|
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
|
|
|
|
|
2023-10-23 02:00:58 -04:00
|
|
|
before { SiteSetting.ai_bot_enabled = true }
|
|
|
|
|
2023-05-21 22:09:14 -04:00
|
|
|
describe "#process" do
|
2023-05-22 01:14:26 -04:00
|
|
|
it "can handle no results" do
|
2023-09-12 02:09:28 -04:00
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
search = described_class.new(bot: nil, post: post1, args: nil)
|
2023-05-22 01:14:26 -04:00
|
|
|
|
2023-08-03 19:37:58 -04:00
|
|
|
results = search.process(query: "order:fake ABDDCDCEDGDG")
|
|
|
|
|
|
|
|
expect(results[:args]).to eq({ query: "order:fake ABDDCDCEDGDG" })
|
2023-06-21 03:10:30 -04:00
|
|
|
expect(results[:rows]).to eq([])
|
2023-06-19 18:45:31 -04:00
|
|
|
end
|
|
|
|
|
2023-09-06 23:25:26 -04:00
|
|
|
describe "semantic search" do
|
|
|
|
let (:query) {
|
|
|
|
"this is an expanded search"
|
|
|
|
}
|
|
|
|
after { DiscourseAi::Embeddings::SemanticSearch.clear_cache_for(query) }
|
|
|
|
|
|
|
|
it "supports semantic search when enabled" do
|
|
|
|
SiteSetting.ai_embeddings_semantic_search_enabled = true
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, "https://api.openai.com/v1/chat/completions").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: JSON.dump(OpenAiCompletionsInferenceStubs.response(query)),
|
|
|
|
)
|
|
|
|
|
|
|
|
hyde_embedding = [0.049382, 0.9999]
|
|
|
|
EmbeddingsGenerationStubs.discourse_service(
|
|
|
|
SiteSetting.ai_embeddings_model,
|
|
|
|
query,
|
|
|
|
hyde_embedding,
|
|
|
|
)
|
|
|
|
|
2023-09-12 02:09:28 -04:00
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
search = described_class.new(bot: nil, post: post1, args: nil)
|
2023-09-06 23:25:26 -04:00
|
|
|
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::AllMpnetBaseV2
|
|
|
|
.any_instance
|
|
|
|
.expects(:asymmetric_topics_similarity_search)
|
|
|
|
.returns([post1.topic_id])
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
results =
|
2023-11-28 23:17:46 -05:00
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses(["<ai>#{query}</ai>"]) do
|
2023-11-23 10:58:54 -05:00
|
|
|
search.process(search_query: "hello world, sam", status: "public")
|
|
|
|
end
|
2023-09-06 23:25:26 -04:00
|
|
|
|
2023-09-13 00:59:45 -04:00
|
|
|
expect(results[:args]).to eq({ search_query: "hello world, sam", status: "public" })
|
2023-09-06 23:25:26 -04:00
|
|
|
expect(results[:rows].length).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-19 18:45:31 -04:00
|
|
|
it "supports subfolder properly" do
|
|
|
|
Discourse.stubs(:base_path).returns("/subfolder")
|
|
|
|
|
2023-09-12 02:09:28 -04:00
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
2023-06-19 18:45:31 -04:00
|
|
|
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
search = described_class.new(bot: nil, post: post1, args: nil)
|
2023-06-19 18:45:31 -04:00
|
|
|
|
2023-08-03 19:37:58 -04:00
|
|
|
results = search.process(limit: 1, user: post1.user.username)
|
2023-06-19 18:45:31 -04:00
|
|
|
expect(results[:rows].to_s).to include("/subfolder" + post1.url)
|
2023-05-22 01:14:26 -04:00
|
|
|
end
|
|
|
|
|
2023-09-12 02:09:28 -04:00
|
|
|
it "returns category and tags" do
|
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
search = described_class.new(bot: nil, post: post1, args: nil)
|
2023-09-12 02:09:28 -04:00
|
|
|
results = search.process(user: post1.user.username)
|
|
|
|
|
|
|
|
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")
|
|
|
|
end
|
|
|
|
|
2023-05-21 22:09:14 -04:00
|
|
|
it "can handle limits" do
|
2023-09-12 02:09:28 -04:00
|
|
|
post1 = Fabricate(:post, topic: topic_with_tags)
|
2023-05-21 22:09:14 -04:00
|
|
|
_post2 = Fabricate(:post, user: post1.user)
|
|
|
|
_post3 = Fabricate(:post, user: post1.user)
|
|
|
|
|
|
|
|
# search has no built in support for limit: so handle it from the outside
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
search = described_class.new(bot: nil, post: post1, args: nil)
|
2023-05-21 22:09:14 -04:00
|
|
|
|
2023-08-03 19:37:58 -04:00
|
|
|
results = search.process(limit: 2, user: post1.user.username)
|
2023-05-21 22:09:14 -04:00
|
|
|
|
2023-06-19 18:45:31 -04:00
|
|
|
expect(results[:rows].length).to eq(2)
|
2023-05-23 09:08:17 -04:00
|
|
|
|
|
|
|
# just searching for everything
|
2023-08-03 19:37:58 -04:00
|
|
|
results = search.process(order: "latest_topic")
|
2023-06-19 18:45:31 -04:00
|
|
|
expect(results[:rows].length).to be > 1
|
2023-05-21 22:09:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|