-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
Open
nwumnn
wants to merge
16
commits into
crmne:main
Choose a base branch
from
nwumnn:tool-control
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
af21430
feat: add tool choice and parallel tool calls control
nwumnn 47923bc
docs: add Tool Choice Control section to tools.md
nwumnn f1a6a4e
Merge branch 'main' into tool-control
nwumnn 4a9efc4
Reset tool_choice after forced tool use to prevent infinite loops
nwumnn 70831d1
Replace :any with :required
nwumnn a932b78
Refactor tool options into unified tool_prefs hash
nwumnn 3a2b830
Update docs
nwumnn 54e9554
Merge branch 'main' into tool-control
nwumnn fb06e7d
Pass `tool_prefs` after `tools`
nwumnn fefd1a2
Update docs
nwumnn 8f34386
Merge branch 'main' into tool-control
nwumnn 9c6172b
Merge branch 'main' into tool-control
nwumnn d5d5389
Merge branch 'main' into tool-control
nwumnn 5359c79
Namespace build_tool_choice with Tools module
nwumnn cef8c06
Add tool control capability methods to providers
nwumnn 9a3d682
Add tests
nwumnn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_prefs, :params, :headers, :schema | ||
|
|
||
| def initialize(model: nil, provider: nil, assume_model_exists: false, context: nil) | ||
| if assume_model_exists && !provider | ||
|
|
@@ -17,6 +17,7 @@ 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_prefs = { choice: nil, parallel: nil } | ||
| @messages = [] | ||
| @tools = {} | ||
|
||
| @params = {} | ||
|
|
@@ -44,15 +45,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 +135,7 @@ def complete(&) # rubocop:disable Metrics/PerceivedComplexity | |
| params: @params, | ||
| headers: @headers, | ||
| schema: @schema, | ||
| tool_prefs: @tool_prefs, | ||
|
||
| &wrap_streaming_block(&) | ||
| ) | ||
|
|
||
|
|
@@ -196,6 +202,7 @@ def handle_tool_calls(response, &) # rubocop:disable Metrics/PerceivedComplexity | |
| halt_result = result if result.is_a?(Tool::Halt) | ||
| end | ||
|
|
||
| reset_tool_choice if forced_tool_choice? | ||
| halt_result || complete(&) | ||
| end | ||
|
|
||
|
|
@@ -205,6 +212,28 @@ def execute_tool(tool_call) | |
| tool.call(args) | ||
| end | ||
|
|
||
| def update_tool_options(choice:, parallel:) | ||
| unless choice.nil? | ||
| valid_tool_choices = %i[auto none required] + 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_prefs[:choice] = choice.to_sym | ||
| end | ||
|
|
||
| @tool_prefs[:parallel] = !!parallel unless parallel.nil? | ||
| end | ||
|
|
||
| def forced_tool_choice? | ||
| @tool_prefs[:choice] && !%i[auto none].include?(@tool_prefs[:choice]) | ||
| end | ||
|
|
||
| def reset_tool_choice | ||
| @tool_prefs[:choice] = nil | ||
| end | ||
|
|
||
| def instance_variables | ||
| super - %i[@connection @config] | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'd prefer that to be code first, then parameter values. Also when specifying a tool in choice, it should accept the tool class too.