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
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe RagDocumentFragment do
|
|
|
|
fab!(:persona) { Fabricate(:ai_persona) }
|
|
|
|
fab!(:upload_1) { Fabricate(:upload) }
|
|
|
|
fab!(:upload_2) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_embeddings_enabled = true
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".link_uploads_and_persona" do
|
|
|
|
it "does nothing if there is no persona" do
|
2024-09-15 18:17:17 -04:00
|
|
|
expect { described_class.link_target_and_uploads(nil, [upload_1.id]) }.not_to change(
|
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
|
|
|
Jobs::DigestRagUpload.jobs,
|
|
|
|
:size,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing if there are no uploads" do
|
2024-09-15 18:17:17 -04:00
|
|
|
expect { described_class.link_target_and_uploads(persona, []) }.not_to change(
|
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
|
|
|
Jobs::DigestRagUpload.jobs,
|
|
|
|
:size,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "queues a job for each upload to generate fragments" do
|
|
|
|
expect {
|
2024-09-15 18:17:17 -04:00
|
|
|
described_class.link_target_and_uploads(persona, [upload_1.id, upload_2.id])
|
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
|
|
|
}.to change(Jobs::DigestRagUpload.jobs, :size).by(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates references between the persona an each upload" do
|
2024-09-15 18:17:17 -04:00
|
|
|
described_class.link_target_and_uploads(persona, [upload_1.id, upload_2.id])
|
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
|
|
|
|
|
|
|
refs = UploadReference.where(target: persona).pluck(:upload_id)
|
|
|
|
|
|
|
|
expect(refs).to contain_exactly(upload_1.id, upload_2.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-15 18:17:17 -04:00
|
|
|
describe ".update_target_uploads" do
|
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
|
|
|
it "does nothing if there is no persona" do
|
2024-09-15 18:17:17 -04:00
|
|
|
expect { described_class.update_target_uploads(nil, [upload_1.id]) }.not_to change(
|
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
|
|
|
Jobs::DigestRagUpload.jobs,
|
|
|
|
:size,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the fragment if its not present in the uploads list" do
|
2024-09-15 18:17:17 -04:00
|
|
|
fragment = Fabricate(:rag_document_fragment, target: persona)
|
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
|
|
|
|
2024-09-15 18:17:17 -04:00
|
|
|
described_class.update_target_uploads(persona, [])
|
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
|
|
|
|
|
|
|
expect { fragment.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "delete references between the upload and the persona" do
|
2024-09-15 18:17:17 -04:00
|
|
|
described_class.link_target_and_uploads(persona, [upload_1.id, upload_2.id])
|
|
|
|
described_class.update_target_uploads(persona, [upload_2.id])
|
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
|
|
|
|
|
|
|
refs = UploadReference.where(target: persona).pluck(:upload_id)
|
|
|
|
|
|
|
|
expect(refs).to contain_exactly(upload_2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "queues jobs to generate new fragments" do
|
2024-09-15 18:17:17 -04:00
|
|
|
expect { described_class.update_target_uploads(persona, [upload_1.id]) }.to change(
|
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
|
|
|
Jobs::DigestRagUpload.jobs,
|
|
|
|
:size,
|
|
|
|
).by(1)
|
|
|
|
end
|
|
|
|
end
|
2024-04-09 10:03:07 -04:00
|
|
|
|
|
|
|
describe ".indexing_status" do
|
|
|
|
let(:truncation) { DiscourseAi::Embeddings::Strategies::Truncation.new }
|
|
|
|
let(:vector_rep) do
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(truncation)
|
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:rag_document_fragment_1) do
|
2024-09-15 18:17:17 -04:00
|
|
|
Fabricate(:rag_document_fragment, upload: upload_1, target: persona)
|
2024-04-09 10:03:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:rag_document_fragment_2) do
|
2024-09-15 18:17:17 -04:00
|
|
|
Fabricate(:rag_document_fragment, upload: upload_1, target: persona)
|
2024-04-09 10:03:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_embedding) { [0.0038493] * vector_rep.dimensions }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_embeddings_enabled = true
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
2024-04-12 09:32:46 -04:00
|
|
|
SiteSetting.ai_embeddings_model = "bge-large-en"
|
2024-04-09 10:03:07 -04:00
|
|
|
|
|
|
|
WebMock.stub_request(
|
|
|
|
:post,
|
|
|
|
"#{SiteSetting.ai_embeddings_discourse_service_api_endpoint}/api/v1/classify",
|
|
|
|
).to_return(status: 200, body: JSON.dump(expected_embedding))
|
|
|
|
|
|
|
|
vector_rep.generate_representation_from(rag_document_fragment_1)
|
|
|
|
end
|
|
|
|
|
2024-04-12 09:32:46 -04:00
|
|
|
it "regenerates all embeddings if ai_embeddings_model changes" do
|
|
|
|
old_id = rag_document_fragment_1.id
|
|
|
|
|
|
|
|
UploadReference.create!(upload_id: upload_1.id, target: persona)
|
|
|
|
UploadReference.create!(upload_id: upload_2.id, target: persona)
|
|
|
|
|
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
SiteSetting.ai_embeddings_model = "all-mpnet-base-v2"
|
|
|
|
expect(RagDocumentFragment.exists?(old_id)).to eq(false)
|
|
|
|
expect(Jobs::DigestRagUpload.jobs.size).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-09 10:03:07 -04:00
|
|
|
it "returns total, indexed and unindexed fragments for each upload" do
|
|
|
|
results = described_class.indexing_status(persona, [upload_1, upload_2])
|
|
|
|
|
|
|
|
upload_1_status = results[upload_1.id]
|
|
|
|
expect(upload_1_status[:total]).to eq(2)
|
|
|
|
expect(upload_1_status[:indexed]).to eq(1)
|
|
|
|
expect(upload_1_status[:left]).to eq(1)
|
|
|
|
|
|
|
|
upload_1_status = results[upload_2.id]
|
|
|
|
expect(upload_1_status[:total]).to eq(0)
|
|
|
|
expect(upload_1_status[:indexed]).to eq(0)
|
|
|
|
expect(upload_1_status[:left]).to eq(0)
|
|
|
|
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
|
|
|
end
|