FIX: array arguments not parsed correctly (#405)

DALL E command accepts an Array as a tool argument, this was not
parsed correctly by the invoker leading to errors generating
images with DALL E

Side quest ... don't use update! it calls validations and will now
fail due to email validation
This commit is contained in:
Sam 2024-01-05 14:39:32 +11:00 committed by GitHub
parent 971e03bdf2
commit dd42a4e47b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 154 additions and 122 deletions

View File

@ -122,15 +122,21 @@ module DiscourseAi
tool_klass = available_tools.find { |c| c.signature.dig(:name) == function_name } tool_klass = available_tools.find { |c| c.signature.dig(:name) == function_name }
return false if tool_klass.nil? return false if tool_klass.nil?
arguments = arguments = {}
tool_klass.signature[:parameters] tool_klass.signature[:parameters].to_a.each do |param|
.to_a name = param[:name]
.reduce({}) do |memo, p| value = parsed_function.at(name)&.text
argument = parsed_function.at(p[:name])&.text
next(memo) unless argument
memo[p[:name].to_sym] = argument if param[:type] == "array" && value
memo value =
begin
JSON.parse(value)
rescue JSON::ParserError
nil
end
end
arguments[name.to_sym] = value if value
end end
tool_klass.new( tool_klass.new(

View File

@ -24,14 +24,14 @@ module DiscourseAi::AiBot::SiteSettingsExtension
) )
user.save!(validate: false) user.save!(validate: false)
else else
user.update!(active: true) user.update_columns(active: true)
end end
elsif !active && user elsif !active && user
# will include deleted # will include deleted
has_posts = DB.query_single("SELECT 1 FROM posts WHERE user_id = #{id} LIMIT 1").present? has_posts = DB.query_single("SELECT 1 FROM posts WHERE user_id = #{id} LIMIT 1").present?
if has_posts if has_posts
user.update!(active: false) if user.active user.update_columns(active: false) if user.active
else else
user.destroy user.destroy
end end

View File

@ -20,8 +20,7 @@ class TestPersona < DiscourseAi::AiBot::Personas::Persona
end end
end end
module DiscourseAi::AiBot::Personas RSpec.describe DiscourseAi::AiBot::Personas::Persona do
RSpec.describe Persona do
let :persona do let :persona do
TestPersona.new TestPersona.new
end end
@ -69,6 +68,25 @@ module DiscourseAi::AiBot::Personas
expect(tools.find { |t| t[:name] == "image" }).to be_nil expect(tools.find { |t| t[:name] == "image" }).to be_nil
end end
it "can correctly parse arrays in tools" do
SiteSetting.ai_openai_api_key = "123"
# Dall E tool uses an array for params
xml = <<~XML
<function_calls>
<invoke>
<tool_name>dall_e</tool_name>
<tool_id>call_JtYQMful5QKqw97XFsHzPweB</tool_id>
<parameters>
<prompts>["cat oil painting", "big car"]</prompts>
</parameters>
</invoke>
</function_calls>
XML
dall_e = DiscourseAi::AiBot::Personas::DallE3.new.find_tool(xml)
expect(dall_e.parameters[:prompts]).to eq(["cat oil painting", "big car"])
end
describe "custom personas" do describe "custom personas" do
it "is able to find custom personas" do it "is able to find custom personas" do
Group.refresh_automatic_groups! Group.refresh_automatic_groups!
@ -121,7 +139,14 @@ module DiscourseAi::AiBot::Personas
# should be ordered by priority and then alpha # should be ordered by priority and then alpha
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to eq( expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to eq(
[General, Artist, Creative, Researcher, SettingsExplorer, SqlHelper], [
DiscourseAi::AiBot::Personas::General,
DiscourseAi::AiBot::Personas::Artist,
DiscourseAi::AiBot::Personas::Creative,
DiscourseAi::AiBot::Personas::Researcher,
DiscourseAi::AiBot::Personas::SettingsExplorer,
DiscourseAi::AiBot::Personas::SqlHelper,
],
) )
# omits personas if key is missing # omits personas if key is missing
@ -129,22 +154,23 @@ module DiscourseAi::AiBot::Personas
SiteSetting.ai_google_custom_search_api_key = "" SiteSetting.ai_google_custom_search_api_key = ""
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly( expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
General, DiscourseAi::AiBot::Personas::General,
SqlHelper, DiscourseAi::AiBot::Personas::SqlHelper,
SettingsExplorer, DiscourseAi::AiBot::Personas::SettingsExplorer,
Creative, DiscourseAi::AiBot::Personas::Creative,
) )
AiPersona.find(DiscourseAi::AiBot::Personas::Persona.system_personas[General]).update!( AiPersona.find(
enabled: false, DiscourseAi::AiBot::Personas::Persona.system_personas[
) DiscourseAi::AiBot::Personas::General
],
).update!(enabled: false)
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly( expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
SqlHelper, DiscourseAi::AiBot::Personas::SqlHelper,
SettingsExplorer, DiscourseAi::AiBot::Personas::SettingsExplorer,
Creative, DiscourseAi::AiBot::Personas::Creative,
) )
end end
end end
end end
end