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 }
return false if tool_klass.nil?
arguments =
tool_klass.signature[:parameters]
.to_a
.reduce({}) do |memo, p|
argument = parsed_function.at(p[:name])&.text
next(memo) unless argument
arguments = {}
tool_klass.signature[:parameters].to_a.each do |param|
name = param[:name]
value = parsed_function.at(name)&.text
memo[p[:name].to_sym] = argument
memo
if param[:type] == "array" && value
value =
begin
JSON.parse(value)
rescue JSON::ParserError
nil
end
end
arguments[name.to_sym] = value if value
end
tool_klass.new(

View File

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

View File

@ -20,8 +20,7 @@ class TestPersona < DiscourseAi::AiBot::Personas::Persona
end
end
module DiscourseAi::AiBot::Personas
RSpec.describe Persona do
RSpec.describe DiscourseAi::AiBot::Personas::Persona do
let :persona do
TestPersona.new
end
@ -69,6 +68,25 @@ module DiscourseAi::AiBot::Personas
expect(tools.find { |t| t[:name] == "image" }).to be_nil
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
it "is able to find custom personas" do
Group.refresh_automatic_groups!
@ -121,7 +139,14 @@ module DiscourseAi::AiBot::Personas
# should be ordered by priority and then alpha
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
@ -129,22 +154,23 @@ module DiscourseAi::AiBot::Personas
SiteSetting.ai_google_custom_search_api_key = ""
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
General,
SqlHelper,
SettingsExplorer,
Creative,
DiscourseAi::AiBot::Personas::General,
DiscourseAi::AiBot::Personas::SqlHelper,
DiscourseAi::AiBot::Personas::SettingsExplorer,
DiscourseAi::AiBot::Personas::Creative,
)
AiPersona.find(DiscourseAi::AiBot::Personas::Persona.system_personas[General]).update!(
enabled: false,
)
AiPersona.find(
DiscourseAi::AiBot::Personas::Persona.system_personas[
DiscourseAi::AiBot::Personas::General
],
).update!(enabled: false)
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
SqlHelper,
SettingsExplorer,
Creative,
DiscourseAi::AiBot::Personas::SqlHelper,
DiscourseAi::AiBot::Personas::SettingsExplorer,
DiscourseAi::AiBot::Personas::Creative,
)
end
end
end
end