A platform for community discussion. Free, open, simple.
FIX: Optimize shortcomings from topic truncation from a27e20c (#33983)
Rafael dos Santos Silvab81e80a6e279
diff --git a/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb b/plugins/discourse-ai/lib/embeddings/strategies/truncation.rbindex 1991e565bf4..504668a0156 100644--- a/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb+++ b/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb@@ -4,6 +4,8 @@ module DiscourseAimodule Embeddingsmodule Strategiesclass Truncation+ TEXT_TO_HTML_TOKEN_RATIO = 3+def id1end@@ -69,7 +71,7 @@ module DiscourseAiif topic&.topic_embed&.embed_content_cache.present?text << Nokogiri::HTML5.fragment(topic.topic_embed.embed_content_cache).text- text << "\n\n"+ text << " "endposts_text = +""@@ -78,9 +80,11 @@ module DiscourseAitopic.posts.find_each do |post|posts_text_size += tokenizer.size(post.cooked)posts_text << post.cooked- break if posts_text_size >= max_length+ posts_text << " "- text << "\n\n"+ # Since we will strip all HTML tags before embedding, we can fit more text+ # than the max_length as it will shrink after Nokogiri extracts the text+ break if posts_text_size >= max_length * TEXT_TO_HTML_TOKEN_RATIOendtext << Nokogiri::HTML5.fragment(posts_text).textdiff --git a/plugins/discourse-ai/spec/lib/modules/embeddings/strategies/truncation_spec.rb b/plugins/discourse-ai/spec/lib/modules/embeddings/strategies/truncation_spec.rbindex 0e303cce8e0..97df53c33eb 100644--- a/plugins/discourse-ai/spec/lib/modules/embeddings/strategies/truncation_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/embeddings/strategies/truncation_spec.rb@@ -39,6 +39,37 @@ RSpec.describe DiscourseAi::Embeddings::Strategies::Truncation doexpect(prepared_text.starts_with?(prefix)).to eq(true)end++ context "if topic contains more tokens in cooked posts than embeddings max sequence lenght" do+ before { open_ai_embedding_def.update!(max_sequence_length: 100) }++ it "applies TEXT_TO_HTML_TOKEN_RATIO multiplier for post content collection" do+ # Create posts with known token counts to test the ratio logic+ large_topic = Fabricate(:topic)+ post_content = "This is a test post with some content"+ first_reply = Fabricate(:post, topic: large_topic, raw: "1: #{post_content}")+ tokenizer = open_ai_embedding_def.tokenizer+ max_length = open_ai_embedding_def.max_sequence_length - 2++ # Create posts that would exceed max_length but not max_length * 3+ expected_size = tokenizer.size(first_reply.cooked)++ # Calculate how many posts we need to exceed max_length but stay under max_length * 3+ posts_needed = (max_length / expected_size) + 1++ posts_needed.times do |i|+ Fabricate(:post, topic: large_topic, raw: "#{i + 1}: #{post_content}")+ end++ prepared_text = truncation.prepare_target_text(large_topic, open_ai_embedding_def)++ # Should still be within max_length after HTML stripping and tokenization+ expect(tokenizer.size(prepared_text)).to be <= max_length++ # Should contain content from multiple posts due to the 3x multiplier+ expect(prepared_text).to include("#{posts_needed}: This is a test post with some content")+ end+ endenddescribe "#prepare_query_text" do