Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/active_agent/providers/ruby_llm_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/providers/ruby_llm/ruby_llm_provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading