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

Commit 96f5f8c

Browse files
authored
FIX: Basic cleanup of AI Caption to remove line breaks and pipes (#857)
1 parent 9af0c2e commit 96f5f8c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/ai_helper/assistant.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,15 @@ def generate_image_caption(upload, user)
156156
],
157157
)
158158

159-
DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_helper_image_caption_model).generate(
160-
prompt,
161-
user: user,
162-
max_tokens: 1024,
163-
feature_name: "image_caption",
164-
)
159+
raw_caption =
160+
DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_helper_image_caption_model).generate(
161+
prompt,
162+
user: user,
163+
max_tokens: 1024,
164+
feature_name: "image_caption",
165+
)
166+
167+
raw_caption.delete("|").squish
165168
end
166169

167170
private

spec/requests/ai_helper/assistant_controller_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@
128128
let(:caption_with_attrs) do
129129
"A picture of a cat sitting on a table (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})"
130130
end
131+
let(:bad_caption) { "A picture of a cat \nsitting on a |table|" }
131132

132133
before { assign_fake_provider_to(:ai_helper_image_caption_model) }
133134

134-
def request_caption(params)
135+
def request_caption(params, caption = "A picture of a cat sitting on a table")
135136
DiscourseAi::Completions::Llm.with_prepared_responses([caption]) do
136137
post "/discourse-ai/ai-helper/caption_image", params: params
137138

@@ -155,6 +156,13 @@ def request_caption(params)
155156
end
156157
end
157158

159+
it "returns a cleaned up caption from the LLM" do
160+
request_caption({ image_url: image_url, image_url_type: "long_url" }, bad_caption) do |r|
161+
expect(r.status).to eq(200)
162+
expect(r.parsed_body["caption"]).to eq(caption_with_attrs)
163+
end
164+
end
165+
158166
context "when the image_url is a short_url" do
159167
let(:image_url) { upload.short_url }
160168

0 commit comments

Comments
 (0)