2023-09-07 10:54:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Jobs::GenerateEmbeddings do
|
|
|
|
subject(:job) { described_class.new }
|
|
|
|
|
|
|
|
describe "#execute" do
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
2024-02-01 11:05:38 -05:00
|
|
|
SiteSetting.ai_embeddings_enabled = true
|
2023-09-07 10:54:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
fab!(:post) { Fabricate(:post, post_number: 1, topic: topic) }
|
|
|
|
|
|
|
|
let(:truncation) { DiscourseAi::Embeddings::Strategies::Truncation.new }
|
|
|
|
let(:vector_rep) do
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(truncation)
|
|
|
|
end
|
|
|
|
|
2023-12-29 10:28:45 -05:00
|
|
|
it "works for topics" do
|
2023-09-07 10:54:43 -04:00
|
|
|
expected_embedding = [0.0038493] * vector_rep.dimensions
|
|
|
|
|
|
|
|
text =
|
|
|
|
truncation.prepare_text_from(
|
|
|
|
topic,
|
|
|
|
vector_rep.tokenizer,
|
|
|
|
vector_rep.max_sequence_length - 2,
|
|
|
|
)
|
2024-02-01 14:54:09 -05:00
|
|
|
EmbeddingsGenerationStubs.discourse_service(vector_rep.class.name, text, expected_embedding)
|
2023-09-07 10:54:43 -04:00
|
|
|
|
2023-12-29 10:28:45 -05:00
|
|
|
job.execute(target_id: topic.id, target_type: "Topic")
|
2023-09-07 10:54:43 -04:00
|
|
|
|
|
|
|
expect(vector_rep.topic_id_from_representation(expected_embedding)).to eq(topic.id)
|
|
|
|
end
|
2023-12-29 10:28:45 -05:00
|
|
|
|
|
|
|
it "works for posts" do
|
|
|
|
expected_embedding = [0.0038493] * vector_rep.dimensions
|
|
|
|
|
|
|
|
text =
|
|
|
|
truncation.prepare_text_from(post, vector_rep.tokenizer, vector_rep.max_sequence_length - 2)
|
2024-02-01 14:54:09 -05:00
|
|
|
EmbeddingsGenerationStubs.discourse_service(vector_rep.class.name, text, expected_embedding)
|
2023-12-29 10:28:45 -05:00
|
|
|
|
|
|
|
job.execute(target_id: post.id, target_type: "Post")
|
|
|
|
|
|
|
|
expect(vector_rep.post_id_from_representation(expected_embedding)).to eq(post.id)
|
|
|
|
end
|
2023-09-07 10:54:43 -04:00
|
|
|
end
|
|
|
|
end
|