2023-08-30 02:15:03 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
class TestPersona < DiscourseAi::AiBot::Personas::Persona
|
2024-01-04 08:44:07 -05:00
|
|
|
def tools
|
2023-08-30 02:15:03 -04:00
|
|
|
[
|
2024-01-04 08:44:07 -05:00
|
|
|
DiscourseAi::AiBot::Tools::ListTags,
|
|
|
|
DiscourseAi::AiBot::Tools::Search,
|
|
|
|
DiscourseAi::AiBot::Tools::Image,
|
2023-08-30 02:15:03 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_prompt
|
|
|
|
<<~PROMPT
|
|
|
|
{site_url}
|
|
|
|
{site_title}
|
|
|
|
{site_description}
|
|
|
|
{participants}
|
|
|
|
{time}
|
|
|
|
PROMPT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
RSpec.describe DiscourseAi::AiBot::Personas::Persona do
|
|
|
|
let :persona do
|
|
|
|
TestPersona.new
|
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
let :topic_with_users do
|
|
|
|
topic = Topic.new
|
|
|
|
topic.allowed_users = [User.new(username: "joe"), User.new(username: "jane")]
|
|
|
|
topic
|
|
|
|
end
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
after do
|
|
|
|
# we are rolling back transactions so we can create poison cache
|
|
|
|
AiPersona.persona_cache.flush!
|
|
|
|
end
|
2023-11-09 19:39:49 -05:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
let(:context) do
|
|
|
|
{
|
|
|
|
site_url: Discourse.base_url,
|
|
|
|
site_title: "test site title",
|
|
|
|
site_description: "test site description",
|
|
|
|
time: Time.zone.now,
|
|
|
|
participants: topic_with_users.allowed_users.map(&:username).join(", "),
|
|
|
|
}
|
|
|
|
end
|
2023-09-14 02:46:56 -04:00
|
|
|
|
2024-11-18 17:22:39 -05:00
|
|
|
fab!(:admin)
|
2024-01-04 22:39:32 -05:00
|
|
|
fab!(:user)
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
fab!(:upload)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
it "renders the system prompt" do
|
|
|
|
freeze_time
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
rendered = persona.craft_prompt(context)
|
2024-01-12 12:36:44 -05:00
|
|
|
system_message = rendered.messages.first[:content]
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
expect(system_message).to include(Discourse.base_url)
|
|
|
|
expect(system_message).to include("test site title")
|
|
|
|
expect(system_message).to include("test site description")
|
|
|
|
expect(system_message).to include("joe, jane")
|
|
|
|
expect(system_message).to include(Time.zone.now.to_s)
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
tools = rendered.tools
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
expect(tools.find { |t| t[:name] == "search" }).to be_present
|
|
|
|
expect(tools.find { |t| t[:name] == "tags" }).to be_present
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# needs to be configured so it is not available
|
|
|
|
expect(tools.find { |t| t[:name] == "image" }).to be_nil
|
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-05-13 05:40:11 -04:00
|
|
|
it "can parse string that are wrapped in quotes" do
|
|
|
|
SiteSetting.ai_stability_api_key = "123"
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
tool_call =
|
|
|
|
DiscourseAi::Completions::ToolCall.new(
|
|
|
|
name: "image",
|
|
|
|
id: "call_JtYQMful5QKqw97XFsHzPweB",
|
|
|
|
parameters: {
|
|
|
|
prompts: ["cat oil painting", "big car"],
|
|
|
|
aspect_ratio: "16:9",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
tool_instance =
|
|
|
|
DiscourseAi::AiBot::Personas::Artist.new.find_tool(
|
|
|
|
tool_call,
|
|
|
|
bot_user: nil,
|
|
|
|
llm: nil,
|
|
|
|
context: nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(tool_instance.parameters[:prompts]).to eq(["cat oil painting", "big car"])
|
|
|
|
expect(tool_instance.parameters[:aspect_ratio]).to eq("16:9")
|
2024-05-13 05:40:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "enforces enums" do
|
2024-11-11 16:14:30 -05:00
|
|
|
tool_call =
|
|
|
|
DiscourseAi::Completions::ToolCall.new(
|
|
|
|
name: "search",
|
|
|
|
id: "call_JtYQMful5QKqw97XFsHzPweB",
|
|
|
|
parameters: {
|
|
|
|
max_posts: "3.2",
|
|
|
|
status: "cow",
|
|
|
|
foo: "bar",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
tool_instance =
|
|
|
|
DiscourseAi::AiBot::Personas::General.new.find_tool(
|
|
|
|
tool_call,
|
|
|
|
bot_user: nil,
|
|
|
|
llm: nil,
|
|
|
|
context: nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(tool_instance.parameters.key?(:status)).to eq(false)
|
|
|
|
|
|
|
|
tool_call =
|
|
|
|
DiscourseAi::Completions::ToolCall.new(
|
|
|
|
name: "search",
|
|
|
|
id: "call_JtYQMful5QKqw97XFsHzPweB",
|
|
|
|
parameters: {
|
|
|
|
max_posts: "3.2",
|
|
|
|
status: "open",
|
|
|
|
foo: "bar",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
tool_instance =
|
|
|
|
DiscourseAi::AiBot::Personas::General.new.find_tool(
|
|
|
|
tool_call,
|
|
|
|
bot_user: nil,
|
|
|
|
llm: nil,
|
|
|
|
context: nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(tool_instance.parameters[:status]).to eq("open")
|
2024-05-13 05:40:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can coerce integers" do
|
2024-11-11 16:14:30 -05:00
|
|
|
tool_call =
|
|
|
|
DiscourseAi::Completions::ToolCall.new(
|
|
|
|
name: "search",
|
|
|
|
id: "call_JtYQMful5QKqw97XFsHzPweB",
|
|
|
|
parameters: {
|
|
|
|
max_posts: "3.2",
|
|
|
|
search_query: "hello world",
|
|
|
|
foo: "bar",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
search =
|
|
|
|
DiscourseAi::AiBot::Personas::General.new.find_tool(
|
|
|
|
tool_call,
|
|
|
|
bot_user: nil,
|
|
|
|
llm: nil,
|
|
|
|
context: nil,
|
|
|
|
)
|
2024-05-13 05:40:11 -04:00
|
|
|
|
|
|
|
expect(search.parameters[:max_posts]).to eq(3)
|
|
|
|
expect(search.parameters[:search_query]).to eq("hello world")
|
|
|
|
expect(search.parameters.key?(:foo)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
it "can correctly parse arrays in tools" do
|
|
|
|
SiteSetting.ai_openai_api_key = "123"
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
tool_call =
|
|
|
|
DiscourseAi::Completions::ToolCall.new(
|
|
|
|
name: "dall_e",
|
|
|
|
id: "call_JtYQMful5QKqw97XFsHzPweB",
|
|
|
|
parameters: {
|
|
|
|
prompts: ["cat oil painting", "big car"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
tool_instance =
|
|
|
|
DiscourseAi::AiBot::Personas::DallE3.new.find_tool(
|
|
|
|
tool_call,
|
|
|
|
bot_user: nil,
|
|
|
|
llm: nil,
|
|
|
|
context: nil,
|
|
|
|
)
|
|
|
|
expect(tool_instance.parameters[:prompts]).to eq(["cat oil painting", "big car"])
|
2024-01-04 22:39:32 -05:00
|
|
|
end
|
2023-11-09 19:39:49 -05:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
describe "custom personas" do
|
|
|
|
it "is able to find custom personas" do
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
|
|
|
|
# define an ai persona everyone can see
|
|
|
|
persona =
|
|
|
|
AiPersona.create!(
|
|
|
|
name: "zzzpun_bot",
|
|
|
|
description: "you write puns",
|
|
|
|
system_prompt: "you are pun bot",
|
2024-06-11 04:14:14 -04:00
|
|
|
tools: ["Image"],
|
2024-01-04 22:39:32 -05:00
|
|
|
allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
|
|
|
|
)
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
custom_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(custom_persona.name).to eq("zzzpun_bot")
|
|
|
|
expect(custom_persona.description).to eq("you write puns")
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
instance = custom_persona.new
|
|
|
|
expect(instance.tools).to eq([DiscourseAi::AiBot::Tools::Image])
|
2024-01-12 12:36:44 -05:00
|
|
|
expect(instance.craft_prompt(context).messages.first[:content]).to eq("you are pun bot")
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# should update
|
|
|
|
persona.update!(name: "zzzpun_bot2")
|
|
|
|
custom_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(custom_persona.name).to eq("zzzpun_bot2")
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# can be disabled
|
|
|
|
persona.update!(enabled: false)
|
|
|
|
last_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(last_persona.name).not_to eq("zzzpun_bot2")
|
2023-09-03 22:05:27 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
persona.update!(enabled: true)
|
|
|
|
# no groups have access
|
|
|
|
persona.update!(allowed_group_ids: [])
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
last_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(last_persona.name).not_to eq("zzzpun_bot2")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "available personas" do
|
|
|
|
it "includes all personas by default" do
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
|
|
|
|
# must be enabled to see it
|
|
|
|
SiteSetting.ai_stability_api_key = "abc"
|
|
|
|
SiteSetting.ai_google_custom_search_api_key = "abc"
|
|
|
|
SiteSetting.ai_google_custom_search_cx = "abc123"
|
|
|
|
|
|
|
|
# should be ordered by priority and then alpha
|
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to eq(
|
|
|
|
[
|
|
|
|
DiscourseAi::AiBot::Personas::General,
|
|
|
|
DiscourseAi::AiBot::Personas::Artist,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
2024-02-18 22:52:12 -05:00
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
2024-03-07 14:37:23 -05:00
|
|
|
DiscourseAi::AiBot::Personas::GithubHelper,
|
2024-01-04 22:39:32 -05:00
|
|
|
DiscourseAi::AiBot::Personas::Researcher,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2024-11-18 17:22:39 -05:00
|
|
|
# it should allow staff access to WebArtifactCreator
|
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: admin)).to eq(
|
|
|
|
[
|
|
|
|
DiscourseAi::AiBot::Personas::General,
|
|
|
|
DiscourseAi::AiBot::Personas::Artist,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::GithubHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::Researcher,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::WebArtifactCreator,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# omits personas if key is missing
|
|
|
|
SiteSetting.ai_stability_api_key = ""
|
|
|
|
SiteSetting.ai_google_custom_search_api_key = ""
|
2024-11-18 17:22:39 -05:00
|
|
|
SiteSetting.ai_artifact_security = "disabled"
|
2024-01-04 22:39:32 -05:00
|
|
|
|
2024-11-18 17:22:39 -05:00
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: admin)).to contain_exactly(
|
2024-01-04 22:39:32 -05:00
|
|
|
DiscourseAi::AiBot::Personas::General,
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
2024-02-18 22:52:12 -05:00
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
2024-03-07 14:37:23 -05:00
|
|
|
DiscourseAi::AiBot::Personas::GithubHelper,
|
2024-01-04 22:39:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
AiPersona.find(
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.system_personas[
|
|
|
|
DiscourseAi::AiBot::Personas::General
|
|
|
|
],
|
|
|
|
).update!(enabled: false)
|
|
|
|
|
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
2024-02-18 22:52:12 -05:00
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
2024-03-07 14:37:23 -05:00
|
|
|
DiscourseAi::AiBot::Personas::GithubHelper,
|
2024-01-04 22:39:32 -05:00
|
|
|
)
|
2023-08-31 03:02:03 -04:00
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
|
|
|
|
describe "#craft_prompt" do
|
|
|
|
before do
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
|
|
|
SiteSetting.ai_embeddings_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:ai_persona) { DiscourseAi::AiBot::Personas::Persona.all(user: user).first.new }
|
|
|
|
|
|
|
|
let(:with_cc) do
|
|
|
|
context.merge(conversation_context: [{ content: "Tell me the time", type: :user }])
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when a persona has no uploads" do
|
|
|
|
it "doesn't include RAG guidance" do
|
|
|
|
guidance_fragment =
|
|
|
|
"The following texts will give you additional guidance to elaborate a response."
|
|
|
|
|
|
|
|
expect(ai_persona.craft_prompt(with_cc).messages.first[:content]).not_to include(
|
|
|
|
guidance_fragment,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
context "when RAG is running with a question consolidator" do
|
|
|
|
let(:consolidated_question) { "what is the time in france?" }
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
fab!(:llm_model) { Fabricate(:fake_model) }
|
|
|
|
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
it "will run the question consolidator" do
|
2024-08-08 10:55:20 -04:00
|
|
|
strategy = DiscourseAi::Embeddings::Strategies::Truncation.new
|
|
|
|
vector_rep =
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(strategy)
|
|
|
|
context_embedding = vector_rep.dimensions.times.map { rand(-1.0...1.0) }
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
EmbeddingsGenerationStubs.discourse_service(
|
|
|
|
SiteSetting.ai_embeddings_model,
|
|
|
|
consolidated_question,
|
|
|
|
context_embedding,
|
|
|
|
)
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
custom_ai_persona =
|
|
|
|
Fabricate(
|
|
|
|
:ai_persona,
|
|
|
|
name: "custom",
|
|
|
|
rag_conversation_chunks: 3,
|
|
|
|
allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
|
2024-07-30 12:44:57 -04:00
|
|
|
question_consolidator_llm: "custom:#{llm_model.id}",
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
UploadReference.ensure_exist!(target: custom_ai_persona, upload_ids: [upload.id])
|
|
|
|
|
|
|
|
custom_persona =
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(id: custom_ai_persona.id, user: user).new
|
|
|
|
|
|
|
|
# this means that we will consolidate
|
|
|
|
ctx =
|
|
|
|
with_cc.merge(
|
|
|
|
conversation_context: [
|
|
|
|
{ content: "Tell me the time", type: :user },
|
|
|
|
{ content: "the time is 1", type: :model },
|
|
|
|
{ content: "in france?", type: :user },
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Endpoints::Fake.with_fake_content(consolidated_question) do
|
|
|
|
custom_persona.craft_prompt(ctx).messages.first[:content]
|
|
|
|
end
|
|
|
|
|
|
|
|
message =
|
|
|
|
DiscourseAi::Completions::Endpoints::Fake.last_call[:dialect].prompt.messages.last[
|
|
|
|
:content
|
|
|
|
]
|
|
|
|
expect(message).to include("Tell me the time")
|
|
|
|
expect(message).to include("the time is 1")
|
|
|
|
expect(message).to include("in france?")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when a persona has RAG uploads" do
|
2024-04-12 09:32:46 -04:00
|
|
|
def stub_fragments(limit, expected_limit: nil)
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
candidate_ids = []
|
|
|
|
|
|
|
|
limit.times do |i|
|
|
|
|
candidate_ids << Fabricate(
|
|
|
|
:rag_document_fragment,
|
|
|
|
fragment: "fragment-n#{i}",
|
2024-09-15 18:17:17 -04:00
|
|
|
target_id: ai_persona.id,
|
|
|
|
target_type: "AiPersona",
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
upload: upload,
|
|
|
|
).id
|
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::BgeLargeEn
|
|
|
|
.any_instance
|
|
|
|
.expects(:asymmetric_rag_fragment_similarity_search)
|
2024-04-12 09:32:46 -04:00
|
|
|
.with { |args, kwargs| kwargs[:limit] == (expected_limit || limit) }
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
.returns(candidate_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stored_ai_persona = AiPersona.find(ai_persona.id)
|
|
|
|
UploadReference.ensure_exist!(target: stored_ai_persona, upload_ids: [upload.id])
|
|
|
|
|
|
|
|
context_embedding = [0.049382, 0.9999]
|
|
|
|
EmbeddingsGenerationStubs.discourse_service(
|
|
|
|
SiteSetting.ai_embeddings_model,
|
|
|
|
with_cc.dig(:conversation_context, 0, :content),
|
|
|
|
context_embedding,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-04-12 09:32:46 -04:00
|
|
|
context "when persona allows for less fragments" do
|
|
|
|
before { stub_fragments(3) }
|
|
|
|
|
|
|
|
it "will only pick 3 fragments" do
|
|
|
|
custom_ai_persona =
|
|
|
|
Fabricate(
|
|
|
|
:ai_persona,
|
|
|
|
name: "custom",
|
|
|
|
rag_conversation_chunks: 3,
|
|
|
|
allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
|
|
|
|
)
|
|
|
|
|
|
|
|
UploadReference.ensure_exist!(target: custom_ai_persona, upload_ids: [upload.id])
|
|
|
|
|
|
|
|
custom_persona =
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(id: custom_ai_persona.id, user: user).new
|
|
|
|
|
|
|
|
expect(custom_persona.class.rag_conversation_chunks).to eq(3)
|
|
|
|
|
|
|
|
crafted_system_prompt = custom_persona.craft_prompt(with_cc).messages.first[:content]
|
|
|
|
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n0")
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n1")
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n2")
|
|
|
|
expect(crafted_system_prompt).not_to include("fragment-n3")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
context "when the reranker is available" do
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_hugging_face_tei_reranker_endpoint = "https://test.reranker.com"
|
|
|
|
|
2024-04-12 09:32:46 -04:00
|
|
|
# hard coded internal implementation, reranker takes x5 number of chunks
|
|
|
|
stub_fragments(15, expected_limit: 50) # Mimic limit being more than 10 results
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the re-ranker to reorder the fragments and pick the top 10 candidates" do
|
|
|
|
expected_reranked = (0..14).to_a.reverse.map { |idx| { index: idx } }
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, "https://test.reranker.com/rerank").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: JSON.dump(expected_reranked),
|
|
|
|
)
|
|
|
|
|
|
|
|
crafted_system_prompt = ai_persona.craft_prompt(with_cc).messages.first[:content]
|
|
|
|
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n14")
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n13")
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n12")
|
|
|
|
expect(crafted_system_prompt).not_to include("fragment-n4") # Fragment #11 not included
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the reranker is not available" do
|
|
|
|
before { stub_fragments(10) }
|
|
|
|
|
|
|
|
it "picks the first 10 candidates from the similarity search" do
|
|
|
|
crafted_system_prompt = ai_persona.craft_prompt(with_cc).messages.first[:content]
|
|
|
|
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n0")
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n1")
|
|
|
|
expect(crafted_system_prompt).to include("fragment-n2")
|
|
|
|
|
|
|
|
expect(crafted_system_prompt).not_to include("fragment-n10") # Fragment #10 not included
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|