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 Jobs::GenerateRagEmbeddings do
|
|
|
|
describe "#execute" do
|
|
|
|
let(:truncation) { DiscourseAi::Embeddings::Strategies::Truncation.new }
|
|
|
|
let(:vector_rep) do
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(truncation)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expected_embedding) { [0.0038493] * vector_rep.dimensions }
|
|
|
|
|
|
|
|
fab!(:ai_persona)
|
|
|
|
|
2024-09-15 18:17:17 -04:00
|
|
|
fab!(:rag_document_fragment_1) { Fabricate(:rag_document_fragment, target: ai_persona) }
|
|
|
|
fab!(:rag_document_fragment_2) { Fabricate(:rag_document_fragment, target: ai_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
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_embeddings_enabled = true
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
|
|
|
|
|
|
|
WebMock.stub_request(
|
|
|
|
:post,
|
|
|
|
"#{SiteSetting.ai_embeddings_discourse_service_api_endpoint}/api/v1/classify",
|
|
|
|
).to_return(status: 200, body: JSON.dump(expected_embedding))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates a new vector for each fragment" do
|
|
|
|
expected_embeddings = 2
|
|
|
|
|
|
|
|
subject.execute(fragment_ids: [rag_document_fragment_1.id, rag_document_fragment_2.id])
|
|
|
|
|
|
|
|
embeddings_count =
|
|
|
|
DB.query_single("SELECT COUNT(*) from #{vector_rep.rag_fragments_table_name}").first
|
|
|
|
|
|
|
|
expect(embeddings_count).to eq(expected_embeddings)
|
|
|
|
end
|
2024-04-09 10:03:07 -04:00
|
|
|
|
|
|
|
describe "Publishing progress updates" do
|
|
|
|
it "sends an update through mb after a batch finishes" do
|
|
|
|
updates =
|
2024-09-30 03:27:50 -04:00
|
|
|
MessageBus.track_publish("/discourse-ai/rag/#{rag_document_fragment_1.upload_id}") do
|
|
|
|
subject.execute(fragment_ids: [rag_document_fragment_1.id])
|
|
|
|
end
|
2024-04-09 10:03:07 -04:00
|
|
|
|
|
|
|
upload_index_stats = updates.last.data
|
|
|
|
|
|
|
|
expect(upload_index_stats[:total]).to eq(1)
|
|
|
|
expect(upload_index_stats[:indexed]).to eq(1)
|
|
|
|
expect(upload_index_stats[: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
|
|
|
|
end
|