From a77658e2b1c560b4b9a2ac8f845b7f978c464775 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 11 Apr 2024 15:17:56 +1000 Subject: [PATCH] FIX: tools broke on Claude with no params (#574) Some tools may have no params, allow that --- lib/completions/dialects/dialect.rb | 2 +- spec/lib/completions/dialects/dialect_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/completions/dialects/dialect.rb b/lib/completions/dialects/dialect.rb index 9361d8ba..465884e2 100644 --- a/lib/completions/dialects/dialect.rb +++ b/lib/completions/dialects/dialect.rb @@ -158,7 +158,7 @@ module DiscourseAi return "" if prompt.tools.blank? has_arrays = - prompt.tools.any? { |tool| tool[:parameters].any? { |p| p[:type] == "array" } } + prompt.tools.any? { |tool| tool[:parameters]&.any? { |p| p[:type] == "array" } } (<<~TEXT).strip #{self.class.tool_preamble(include_array_tip: has_arrays)} diff --git a/spec/lib/completions/dialects/dialect_spec.rb b/spec/lib/completions/dialects/dialect_spec.rb index db481e94..2410f415 100644 --- a/spec/lib/completions/dialects/dialect_spec.rb +++ b/spec/lib/completions/dialects/dialect_spec.rb @@ -47,6 +47,15 @@ RSpec.describe DiscourseAi::Completions::Dialects::Dialect do expect(dialect.build_tools_prompt).to include("array") end + + it "does not break if there are no params" do + prompt = DiscourseAi::Completions::Prompt.new("12345") + prompt.tools = [{ name: "categories", description: "lookup all categories" }] + + dialect = TestDialect.new(prompt, "test") + + expect(dialect.build_tools_prompt).not_to include("array") + end end describe "#trim_messages" do