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
9 changes: 9 additions & 0 deletions lib/ruby_llm/active_record/chat_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ def with_params(...)
self
end

def with_provider_options(...)
to_llm.with_provider_options(...)
self
end

def provider_options
to_llm.provider_options
end

def with_headers(...)
to_llm.with_headers(...)
self
Expand Down
27 changes: 26 additions & 1 deletion lib/ruby_llm/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ def with_params(**params)
self
end

# RubyLLM 2.0 name for with_params; shares @params so both names always agree.
def with_provider_options(provider_options)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with_provider_options isn't delegated from RubyLLM::ActiveRecord::ChatMethods, so moving acts_as_chat callers off with_params raises NoMethodError.

lib/ruby_llm/active_record/chat_methods.rb delegates with_params(...) to to_llm (around line 120), but this PR adds no matching with_provider_options there. Any Supernova code that calls with_params on a persisted chat record can't make the 2.0 move this PR is meant to enable.

Suggested fix: add the same delegation, plus a spec.

def with_provider_options(...)
  to_llm.with_provider_options(...)
  self
end

@params = provider_options.to_h
self
end

def provider_options
@params
end

def with_protocol(protocol)
@protocol = protocol
self
Expand Down Expand Up @@ -461,14 +471,29 @@ def execute_tool(tool_call)
}

RubyLLM.instrument('tool_call.ruby_llm', payload, config: @config) do |event|
result = tool.call(args)
result = call_tool(tool, args, tool_call)
event[:result] = result
event[:result_content] = result
event[:result_class] = result.class.name
result
end
end

# A 1.x override of call(args) keeps getting the positional Hash, so a missed conversion
# still works on the default config and fails loudly when positional calls are disallowed.
def call_tool(tool, args, tool_call)
kwargs = args.respond_to?(:to_hash) ? args.to_hash : {}
return tool.call(**kwargs, tool_call:) if tool_call_accepts_keywords?(tool)

tool.call(kwargs)
end

def tool_call_accepts_keywords?(tool)
tool.method(:call).parameters.any? do |kind, name|
kind == :keyrest || (Tool::KEYWORD_PARAMETER_KINDS.include?(kind) && name == Tool::TOOL_CALL_KEYWORD)
end
end

def update_tool_options(choice:, calls:)
unless choice.nil?
normalized_choice = normalize_tool_choice(choice)
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_llm/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def defaults = @defaults ||= {}
option :http_proxy, nil
option :tool_concurrency, false
option :auto_upload_large_files, true
option :allow_positional_tool_arguments, true

option :logger, nil
option :instrumenter, nil
Expand Down
29 changes: 26 additions & 3 deletions lib/ruby_llm/tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def initialize(name, type: 'string', desc: nil, description: nil, required: true
# Base class for creating tools that AI models can use
class Tool
POSITIONAL_PARAMETER_KINDS = %i[req opt rest].freeze
KEYWORD_PARAMETER_KINDS = %i[keyreq key].freeze # :nodoc:
TOOL_CALL_KEYWORD = :tool_call # :nodoc:

class << self
def description(text = nil)
Expand Down Expand Up @@ -127,12 +129,15 @@ def parameters_schema
end
end

def call(args)
normalized_args = normalize_args(args)
# Keywords are the RubyLLM 2.0 form; the positional Hash is the 1.x form. `tool_call` is
# reserved and reaches execute only when execute declares a `tool_call:` keyword.
def call(args = nil, tool_call: nil, **arguments)
normalized_args = normalize_args(tool_arguments(args, arguments))
validation_error = validate_keyword_arguments(normalized_args)
return { error: "Invalid tool arguments: #{validation_error}" } if validation_error

RubyLLM.logger.debug { "Tool #{name} called with: #{normalized_args.inspect}" }
normalized_args[TOOL_CALL_KEYWORD] = tool_call if execute_accepts_tool_call?
result = execute(**normalized_args)
RubyLLM.logger.debug { "Tool #{name} returned: #{result.inspect}" }
result
Expand All @@ -142,8 +147,26 @@ def execute(...)
raise NotImplementedError, 'Subclasses must implement #execute'
end

def execute_accepts_tool_call? # :nodoc:
method(:execute).parameters.any? do |kind, name|
name == TOOL_CALL_KEYWORD && KEYWORD_PARAMETER_KINDS.include?(kind)
end
end

protected

def tool_arguments(args, arguments)
return arguments if args.nil?

unless RubyLLM.config.allow_positional_tool_arguments
raise ArgumentError,
'Tool#call takes keyword arguments; positional Hash arguments are removed in RubyLLM 2.0'
end
raise ArgumentError, 'pass tool arguments either as one Hash or as keywords, not both' unless arguments.empty?

args
end

def normalize_args(args)
return {} if args.nil?
return args.transform_keys(&:to_sym) if args.respond_to?(:transform_keys)
Expand All @@ -169,7 +192,7 @@ def validate_keyword_arguments(arguments)
end

def execute_keyword_signature
keyword_signature = method(:execute).parameters
keyword_signature = method(:execute).parameters.reject { |_, name| name == TOOL_CALL_KEYWORD }
required_keywords = keyword_signature.filter_map { |kind, name| name if kind == :keyreq }
optional_keywords = keyword_signature.filter_map { |kind, name| name if kind == :key }
accepts_extra_keywords = keyword_signature.any? { |kind, _| kind == :keyrest }
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby_llm/active_record/acts_as_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ def execute(expression:)
llm_chat = chat.instance_variable_get(:@chat)
expect(llm_chat.params).to eq(max_tokens: 100, temperature: 0.5)
end

it 'supports with_provider_options and provider_options' do
chat = Chat.create!(model: model)

expect(chat.with_provider_options(foo: 1)).to eq(chat)
expect(chat.provider_options).to eq({ foo: 1 })
expect(chat.to_llm.params).to eq({ foo: 1 })
end
end

describe 'tool functionality' do
Expand Down
24 changes: 24 additions & 0 deletions spec/ruby_llm/chat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ def expect_token_usage(message)
expect(message.output_tokens).to be_positive
end

describe '#with_provider_options' do
it 'sets provider_options and params to the same value and returns the chat' do
chat = RubyLLM.chat

expect(chat.with_provider_options(a: 1)).to be(chat)
expect(chat.provider_options).to eq({ a: 1 })
expect(chat.params).to eq({ a: 1 })
end

it 'is replaced by a later with_params' do
chat = RubyLLM.chat.with_provider_options(a: 1).with_params(b: 2)

expect(chat.provider_options).to eq({ b: 2 })
expect(chat.params).to eq({ b: 2 })
end

it 'replaces an earlier with_params' do
chat = RubyLLM.chat.with_params(b: 2).with_provider_options(a: 1)

expect(chat.provider_options).to eq({ a: 1 })
expect(chat.params).to eq({ a: 1 })
end
end

describe 'basic chat functionality' do
CHAT_MODELS.each do |model_info|
model = model_info[:model]
Expand Down
95 changes: 95 additions & 0 deletions spec/ruby_llm/chat_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,101 @@ def stub_tool_response(chat, tool_calls)
end
end

describe 'keyword tool calls' do
it 'calls the tool with keywords and passes the ToolCall to a call override' do
received = []
tool_class = Class.new(RubyLLM::Tool) do
description 'Records what call receives'

define_method(:call) do |tool_call: nil, **args|
received << [tool_call, args]
'recorded'
end

def execute(query:)
query
end
end
stub_const('KeywordRecorder', tool_class)
tool_call = RubyLLM::ToolCall.new(id: 'call_1', name: 'keyword_recorder', arguments: { 'query' => 'x' })
chat = RubyLLM.chat.with_tool(KeywordRecorder)
stub_tool_response(chat, { tool_call.id => tool_call })

chat.ask('Record it')

expect(received).to eq([[tool_call, { 'query' => 'x' }]])
expect(chat.messages.find { |message| message.role == :tool }.content).to eq('recorded')
end

context 'with a legacy call(args) override' do
let(:received) { [] }
let(:tool_call) { RubyLLM::ToolCall.new(id: 'call_1', name: 'legacy_call', arguments: { 'query' => 'x' }) }
let(:chat) do
sink = received
stub_const('LegacyCall', Class.new(RubyLLM::Tool) do
description 'Overrides call with the 1.x positional form'

def call(args) = super(args) # rubocop:disable Lint/UselessMethodDefinition,Style/SuperArguments

define_method(:execute) do |query:|
sink << { query: }
'legacy ok'
end
end)
RubyLLM.chat.with_tool(LegacyCall).tap { |c| stub_tool_response(c, { tool_call.id => tool_call }) }
end

it 'passes a positional Hash on the default config' do
chat.ask('Run it')

expect(received).to eq([{ query: 'x' }])
expect(chat.messages.find { |message| message.role == :tool }.content).to eq('legacy ok')
end

it 'raises when positional tool arguments are disallowed' do
original = RubyLLM.config.allow_positional_tool_arguments
RubyLLM.config.allow_positional_tool_arguments = false

expect { chat.ask('Run it') }.to raise_error(
ArgumentError,
'Tool#call takes keyword arguments; positional Hash arguments are removed in RubyLLM 2.0'
)
ensure
RubyLLM.config.allow_positional_tool_arguments = original
end
end

context 'with non-Hash tool call arguments' do
[[], 'not json'].each do |arguments|
it "runs a tool without arguments when arguments is #{arguments.class}" do
stub_const('NoArgs', Class.new(RubyLLM::Tool) do
description 'Takes nothing'

def execute = 'no args ok'
end)
tool_call = RubyLLM::ToolCall.new(id: 'call_1', name: 'no_args', arguments:)
chat = RubyLLM.chat.with_tool(NoArgs)
stub_tool_response(chat, { tool_call.id => tool_call })

chat.ask('Run it')

expect(chat.messages.find { |message| message.role == :tool }.content).to eq('no args ok')
end

it "returns the invalid arguments error for a required keyword when arguments is #{arguments.class}" do
tool_call = RubyLLM::ToolCall.new(id: 'call_1', name: 'content_returning', arguments:)
results = []
chat = RubyLLM.chat.with_tool(ContentReturningTool).after_tool_result { |result| results << result }
stub_tool_response(chat, { tool_call.id => tool_call })

chat.ask('Run it')

expect(results).to eq([{ error: 'Invalid tool arguments: missing keyword: query' }])
end
end
end
end

describe 'concurrent tool execution' do
let(:tool_calls) do
{
Expand Down
Loading
Loading