diff --git a/lib/ai_helper/assistant.rb b/lib/ai_helper/assistant.rb index 2e2c303d..b7014295 100644 --- a/lib/ai_helper/assistant.rb +++ b/lib/ai_helper/assistant.rb @@ -156,12 +156,15 @@ module DiscourseAi ], ) - DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_helper_image_caption_model).generate( - prompt, - user: user, - max_tokens: 1024, - feature_name: "image_caption", - ) + raw_caption = + DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_helper_image_caption_model).generate( + prompt, + user: user, + max_tokens: 1024, + feature_name: "image_caption", + ) + + raw_caption.delete("|").squish end private diff --git a/spec/requests/ai_helper/assistant_controller_spec.rb b/spec/requests/ai_helper/assistant_controller_spec.rb index 21890411..ca78396b 100644 --- a/spec/requests/ai_helper/assistant_controller_spec.rb +++ b/spec/requests/ai_helper/assistant_controller_spec.rb @@ -128,10 +128,11 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do let(:caption_with_attrs) do "A picture of a cat sitting on a table (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})" end + let(:bad_caption) { "A picture of a cat \nsitting on a |table|" } before { assign_fake_provider_to(:ai_helper_image_caption_model) } - def request_caption(params) + def request_caption(params, caption = "A picture of a cat sitting on a table") DiscourseAi::Completions::Llm.with_prepared_responses([caption]) do post "/discourse-ai/ai-helper/caption_image", params: params @@ -155,6 +156,13 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do end end + it "returns a cleaned up caption from the LLM" do + request_caption({ image_url: image_url, image_url_type: "long_url" }, bad_caption) do |r| + expect(r.status).to eq(200) + expect(r.parsed_body["caption"]).to eq(caption_with_attrs) + end + end + context "when the image_url is a short_url" do let(:image_url) { upload.short_url }