-
-
Notifications
You must be signed in to change notification settings - Fork 287
Add built-in support for tool control parameters #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
af21430
47923bc
f1a6a4e
4a9efc4
70831d1
a932b78
3a2b830
54e9554
fb06e7d
fefd1a2
8f34386
9c6172b
d5d5389
5359c79
cef8c06
9a3d682
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ module RubyLLM | |
class Chat | ||
include Enumerable | ||
|
||
attr_reader :model, :messages, :tools, :params, :headers, :schema | ||
attr_reader :model, :messages, :tools, :tool_choice, :parallel_tool_calls, :params, :headers, :schema | ||
|
||
|
||
def initialize(model: nil, provider: nil, assume_model_exists: false, context: nil) | ||
if assume_model_exists && !provider | ||
|
@@ -17,6 +17,8 @@ def initialize(model: nil, provider: nil, assume_model_exists: false, context: n | |
model_id = model || @config.default_model | ||
with_model(model_id, provider: provider, assume_exists: assume_model_exists) | ||
@temperature = nil | ||
@tool_choice = nil | ||
@parallel_tool_calls = nil | ||
@messages = [] | ||
@tools = {} | ||
@params = {} | ||
|
@@ -44,15 +46,19 @@ def with_instructions(instructions, replace: false) | |
self | ||
end | ||
|
||
def with_tool(tool) | ||
tool_instance = tool.is_a?(Class) ? tool.new : tool | ||
@tools[tool_instance.name.to_sym] = tool_instance | ||
def with_tool(tool, choice: nil, parallel: nil) | ||
unless tool.nil? | ||
tool_instance = tool.is_a?(Class) ? tool.new : tool | ||
@tools[tool_instance.name.to_sym] = tool_instance | ||
end | ||
update_tool_options(choice:, parallel:) | ||
self | ||
end | ||
|
||
def with_tools(*tools, replace: false) | ||
def with_tools(*tools, replace: false, choice: nil, parallel: nil) | ||
@tools.clear if replace | ||
tools.compact.each { |tool| with_tool tool } | ||
update_tool_options(choice:, parallel:) | ||
self | ||
end | ||
|
||
|
@@ -130,6 +136,8 @@ def complete(&) # rubocop:disable Metrics/PerceivedComplexity | |
params: @params, | ||
headers: @headers, | ||
schema: @schema, | ||
tool_choice: @tool_choice, | ||
parallel_tool_calls: @parallel_tool_calls, | ||
&wrap_streaming_block(&) | ||
) | ||
|
||
|
@@ -196,7 +204,9 @@ def handle_tool_calls(response, &) # rubocop:disable Metrics/PerceivedComplexity | |
halt_result = result if result.is_a?(Tool::Halt) | ||
end | ||
|
||
halt_result || complete(&) | ||
return halt_result if halt_result | ||
|
||
should_continue_after_tools? ? complete(&) : response | ||
|
||
end | ||
|
||
def execute_tool(tool_call) | ||
|
@@ -205,6 +215,26 @@ def execute_tool(tool_call) | |
tool.call(args) | ||
end | ||
|
||
def update_tool_options(choice:, parallel:) | ||
unless choice.nil? | ||
valid_tool_choices = %i[auto none any] + tools.keys | ||
|
||
unless valid_tool_choices.include?(choice.to_sym) | ||
raise InvalidToolChoiceError, | ||
"Invalid tool choice: #{choice}. Valid choices are: #{valid_tool_choices.join(', ')}" | ||
end | ||
|
||
@tool_choice = choice.to_sym | ||
end | ||
|
||
@parallel_tool_calls = !!parallel unless parallel.nil? | ||
end | ||
|
||
def should_continue_after_tools? | ||
# Continue conversation only with :auto tool choice to avoid infinite loops. | ||
# With :any or specific tool choices, the model would keep calling tools repeatedly. | ||
tool_choice.nil? || tool_choice == :auto | ||
end | ||
|
||
def instance_variables | ||
super - %i[@connection @config] | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both examples of
with_tool
andwith_tools
should contain bothchoice
andparallel
parameters otherwise people get the false sense that one parameter is for one call only.