Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/ai_helper/assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,15 @@ def generate_image_caption(upload, user)
],
)

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
Expand Down
10 changes: 9 additions & 1 deletion spec/requests/ai_helper/assistant_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@
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

Expand All @@ -155,6 +156,13 @@ def request_caption(params)
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 }

Expand Down