diff --git a/CHANGELOG.md b/CHANGELOG.md index b92c2664..07ad1039 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`tool_choice` reaches RubyLLM** (`ruby_llm`). The adapter read + `tool_choice` only to clear it between turns and never sent it, so + `tool_choice: "required"` or a named tool was ignored and a generation + could finish without calling a tool. It is now passed as RubyLLM's + `tool_prefs`, and still cleared once the tool has been used. - **An evaluation run is executed by one job** (`actionagent`). The run job read a run's status, then the scenario runner wrote `running`, as two steps, so a job delivered twice, or two workers that both loaded the run diff --git a/lib/active_agent/providers/ruby_llm_provider.rb b/lib/active_agent/providers/ruby_llm_provider.rb index 3515e6c5..dc720084 100644 --- a/lib/active_agent/providers/ruby_llm_provider.rb +++ b/lib/active_agent/providers/ruby_llm_provider.rb @@ -62,6 +62,9 @@ def api_prompt_execute(parameters) schema = ruby_llm_schema(parameters[:response_format]) kwargs[:schema] = schema if schema + tool_prefs = ruby_llm_tool_prefs(tools) + kwargs[:tool_prefs] = tool_prefs if tool_prefs + # RubyLLM 2 renamed params: and exposes a provider-neutral token limit. max_tokens = parameters[:max_tokens] || options.max_tokens if max_tokens @@ -305,6 +308,23 @@ def process_function_calls(tool_calls) private + # Maps the request's tool_choice to RubyLLM's tool_prefs. Without it + # the model is free to answer without calling a tool even when the + # caller requires one. Nil once ToolChoiceClearing has cleared it, so + # a follow-up turn is not forced into another call. + # + # @param tools [Hash, nil] RubyLLM tools sent with this request + # @return [Hash, nil] + def ruby_llm_tool_prefs(tools) + return nil if tools.blank? + + choice = request.tool_choice + choice = (choice[:name] || choice["name"]) if choice.is_a?(Hash) + return nil if choice.blank? + + { choice: choice.to_sym } + end + def ruby_llm_v2? Gem.loaded_specs.fetch("ruby_llm").version.segments.first >= 2 end diff --git a/test/providers/ruby_llm/ruby_llm_provider_test.rb b/test/providers/ruby_llm/ruby_llm_provider_test.rb index 64725a39..ceb8b760 100644 --- a/test/providers/ruby_llm/ruby_llm_provider_test.rb +++ b/test/providers/ruby_llm/ruby_llm_provider_test.rb @@ -964,6 +964,42 @@ def last_kwargs end end + test "tool_choice required is passed to RubyLLM as tool_prefs" do + provider = ActiveAgent::Providers::RubyLLMProvider.new( + service: "RubyLLM", + model: "gpt-4o-mini", + tool_choice: "required", + messages: [ { role: "user", content: "weather?" } ], + tools: [ tool_definition("get_weather") ], + tools_function: ->(_name, **_kwargs) { { temp: 72 } } + ) + + provider.prompt + + assert_equal({ choice: :required }, @fake_provider.last_kwargs[:tool_prefs]) + end + + test "tool_choice naming a tool is passed to RubyLLM as that tool" do + provider = ActiveAgent::Providers::RubyLLMProvider.new( + service: "RubyLLM", + model: "gpt-4o-mini", + tool_choice: { name: "get_weather" }, + messages: [ { role: "user", content: "weather?" } ], + tools: [ tool_definition("get_weather") ], + tools_function: ->(_name, **_kwargs) { { temp: 72 } } + ) + + provider.prompt + + assert_equal({ choice: :get_weather }, @fake_provider.last_kwargs[:tool_prefs]) + end + + test "tool_prefs is not passed without a tool_choice" do + @provider.prompt + + assert_nil @fake_provider.last_kwargs[:tool_prefs] + end + test "max_tokens is not passed when not set" do @provider.prompt