Skip to content

A platform for community discussion. Free, open, simple.

PERF: Speed up embedding text preparation. (#33791)

Roman Rizzia27e20c300e2
diff --git a/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb b/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb
index f747c1beb0e..1991e565bf4 100644
--- a/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb
+++ b/plugins/discourse-ai/lib/embeddings/strategies/truncation.rb
@@ -72,12 +72,19 @@ module DiscourseAi
text << "\n\n"
end
+ posts_text = +""
+ posts_text_size = 0
+
topic.posts.find_each do |post|
- text << Nokogiri::HTML5.fragment(post.cooked).text
- break if tokenizer.size(text) >= max_length #maybe keep a partial counter to speed this up?
+ posts_text_size += tokenizer.size(post.cooked)
+ posts_text << post.cooked
+ break if posts_text_size >= max_length
+
text << "\n\n"
end
+ text << Nokogiri::HTML5.fragment(posts_text).text
+
tokenizer.truncate(text, max_length, strict: SiteSetting.ai_strict_token_counting)
end