Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit aef9a03

Browse files
authored
FEATURE: Truncate AI Captions to a reasonable max size (#907)
1 parent 9583964 commit aef9a03

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/ai_helper/assistant.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module DiscourseAi
44
module AiHelper
55
class Assistant
6+
IMAGE_CAPTION_MAX_WORDS = 50
7+
68
def self.prompt_cache
79
@prompt_cache ||= ::DiscourseAi::MultisiteHash.new("prompt_cache")
810
end
@@ -164,7 +166,7 @@ def generate_image_caption(upload, user)
164166
feature_name: "image_caption",
165167
)
166168

167-
raw_caption.delete("|").squish
169+
raw_caption.delete("|").squish.truncate_words(IMAGE_CAPTION_MAX_WORDS)
168170
end
169171

170172
private

spec/requests/ai_helper/assistant_controller_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ def request_caption(params, caption = "A picture of a cat sitting on a table")
163163
end
164164
end
165165

166+
it "truncates the caption from the LLM" do
167+
request_caption({ image_url: image_url, image_url_type: "long_url" }, caption * 10) do |r|
168+
expect(r.status).to eq(200)
169+
expect(r.parsed_body["caption"].size).to be < caption.size * 10
170+
end
171+
end
172+
166173
context "when the image_url is a short_url" do
167174
let(:image_url) { upload.short_url }
168175

0 commit comments

Comments
 (0)