FIX: Dall E / Artist broken when tool_details is disabled (#667)

We were missing logic to handle custom_html from tools

This also fixes image generation in chat
This commit is contained in:
Sam 2024-06-12 17:58:28 +10:00 committed by GitHub
parent 8a0c2020f2
commit f642a27f11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 18 deletions

View File

@ -152,7 +152,12 @@ module DiscourseAi
end
tool_details = build_placeholder(tool.summary, tool.details, custom_raw: tool.custom_raw)
update_blk.call(tool_details, cancel, nil) if !context[:skip_tool_details]
if context[:skip_tool_details] && tool.custom_raw.present?
update_blk.call(tool.custom_raw, cancel, nil)
elsif !context[:skip_tool_details]
update_blk.call(tool_details, cancel, nil)
end
result
end

View File

@ -647,23 +647,25 @@ RSpec.describe DiscourseAi::AiBot::Playground do
end
context "with Dall E bot" do
let(:bot) do
persona =
AiPersona
.find(
before { SiteSetting.ai_openai_api_key = "123" }
let(:persona) do
AiPersona.find(
DiscourseAi::AiBot::Personas::Persona.system_personas[
DiscourseAi::AiBot::Personas::DallE3
],
)
.class_instance
.new
DiscourseAi::AiBot::Bot.as(bot_user, persona: persona)
end
it "does not include placeholders in conversation context (simulate DALL-E)" do
SiteSetting.ai_openai_api_key = "123"
let(:bot) { DiscourseAi::AiBot::Bot.as(bot_user, persona: persona.class_instance.new) }
let(:data) do
image =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
response = (<<~TXT).strip
[{ b64_json: image, revised_prompt: "a pink cow 1" }]
end
let(:response) { (<<~TXT).strip }
<function_calls>
<invoke>
<tool_name>dall_e</tool_name>
@ -675,11 +677,24 @@ RSpec.describe DiscourseAi::AiBot::Playground do
</function_calls>
TXT
image =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
it "properly returns an image when skipping tool details" do
persona.update!(tool_details: false)
data = [{ b64_json: image, revised_prompt: "a pink cow 1" }]
WebMock.stub_request(:post, SiteSetting.ai_openai_dall_e_3_url).to_return(
status: 200,
body: { data: data }.to_json,
)
DiscourseAi::Completions::Llm.with_prepared_responses([response]) do
playground.reply_to(third_post)
end
last_post = third_post.topic.reload.posts.order(:post_number).last
expect(last_post.raw).to include("a pink cow")
end
it "does not include placeholders in conversation context (simulate DALL-E)" do
WebMock.stub_request(:post, SiteSetting.ai_openai_dall_e_3_url).to_return(
status: 200,
body: { data: data }.to_json,