FIX: Basic cleanup of AI Caption to remove line breaks and pipes (#857)

This commit is contained in:
Rafael dos Santos Silva 2024-10-23 18:38:29 -03:00 committed by GitHub
parent 9af0c2e719
commit 96f5f8cbd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 7 deletions

View File

@ -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

View File

@ -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 }