Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5c7a658
229: Support removing tools
tpaulshippy Jun 11, 2025
6a9b6d5
229: Add to docs
tpaulshippy Jun 11, 2025
5a57d23
Merge branch 'main' into issue-229
tpaulshippy Jun 11, 2025
5cc1176
Merge branch 'main' into issue-229
crmne Jul 16, 2025
fa6efec
Resolve rubocop offenses
tpaulshippy Jun 11, 2025
90a27cd
Merge branch 'main' into issue-229
tpaulshippy Jul 20, 2025
06a1d18
Remove unused method accidentally added during merge
tpaulshippy Jul 20, 2025
d47be42
Add integration test as suggested
tpaulshippy Jul 20, 2025
87bdeb4
Remove extra spacing
tpaulshippy Jul 20, 2025
09ccf35
Merge branch 'main' into issue-229
tpaulshippy Aug 3, 2025
c7198ec
Switch default image generation model to gpt-image-1 (#321)
tpaulshippy Aug 3, 2025
eeab34b
Add log_stream_debug configuration option
crmne Aug 6, 2025
4837ee4
Updated models. Yes, Opus 4.1 and GPT-OSS are here!
crmne Aug 6, 2025
9702c4e
Add on_tool_result callback and fix Rails callback chaining
crmne Aug 6, 2025
80bc774
Refactor providers from modules to classes
crmne Aug 6, 2025
55335c3
Fix JRuby compatibility issue with ErrorMiddleware initialization
crmne Aug 6, 2025
235ad69
Fix JRuby compatibility with optional hash argument
crmne Aug 6, 2025
6e10bf7
Fix Anthropic models incorrectly reporting structured output support
crmne Aug 6, 2025
1b31780
Add halt mechanism for tools to stop conversation continuation
crmne Aug 6, 2025
cdb2921
Improve halt documentation with clearer examples and flow explanation
crmne Aug 6, 2025
a24f472
Simplify halt documentation to prevent confusion
crmne Aug 7, 2025
0876d45
Skip providers that don't support function calling
tpaulshippy Aug 7, 2025
74822a5
Merge branch 'main' into issue-229
tpaulshippy Aug 7, 2025
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
40 changes: 40 additions & 0 deletions docs/guides/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,46 @@ puts response.content
{: .warning }
Ensure the model you select supports function calling/tools. Check model capabilities using `RubyLLM.models.find('your-model-id').supports_functions?`. Attempting to use `with_tool` on an unsupported model will raise `RubyLLM::UnsupportedFunctionsError`.

## Removing Tools

Each tool definition adds to the input tokens for every request, so it is often a good idea to remove tools that are not needed on a given request. You can remove tools from a `Chat` instance using these methods:

### Remove a Single Tool

Use `without_tool` to remove a specific tool by passing either the tool class or instance:

```ruby
# Remove by class
chat.without_tool(Weather)

# Or by instance
chat.without_tool(weather_tool)
```

### Remove Multiple Tools

Use `without_tools` to remove multiple tools at once:

```ruby
chat.without_tools(Weather, AnotherTool, some_tool_instance)
```

### Clear All Tools

To remove all tools from the chat instance:

```ruby
chat.clear_tools
```

All these methods return the `Chat` instance, allowing for method chaining:

```ruby
chat.clear_tools
.with_tool(Weather.new)
.ask("What's the weather?")
```

## The Tool Execution Flow

When you `ask` a question that the model determines requires a tool:
Expand Down
28 changes: 26 additions & 2 deletions lib/ruby_llm/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def with_tool(tool)
raise UnsupportedFunctionsError, "Model #{@model.id} doesn't support function calling"
end

tool_instance = tool.is_a?(Class) ? tool.new : tool
@tools[tool_instance.name.to_sym] = tool_instance
tool_instance = instantiate_tool(tool)
@tools[tool_name(tool_instance)] = tool_instance
self
end

Expand All @@ -60,6 +60,22 @@ def with_tools(*tools)
self
end

def without_tool(tool)
tool_instance = instantiate_tool(tool)
@tools.delete(tool_name(tool_instance))
self
end

def without_tools(*tools)
tools.each { |tool| without_tool(tool) }
self
end

def clear_tools
@tools.clear
self
end

def with_model(model_id, provider: nil, assume_exists: false)
@model, @provider = Models.resolve(model_id, provider:, assume_exists:)
@connection = @context ? @context.connection_for(@provider) : @provider.connection(@config)
Expand Down Expand Up @@ -148,5 +164,13 @@ def add_tool_result(tool_use_id, result)
tool_call_id: tool_use_id
)
end

def tool_name(tool)
tool.name.to_sym
end

def instantiate_tool(tool)
tool.is_a?(Class) ? tool.new : tool
end
end
end
71 changes: 71 additions & 0 deletions spec/ruby_llm/chat_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,75 @@ def execute
end
end
end

describe 'tool management' do
let(:chat) { RubyLLM.chat }
let(:weather_tool) { Weather.new }
let(:best_language_tool) { BestLanguageToLearn.new }

before do
chat.with_tool(Weather)
chat.with_tool(BestLanguageToLearn)
end

describe '#without_tool' do
it 'removes a tool by class' do
expect(chat.tools.keys).to include(:weather, :best_language_to_learn)

chat.without_tool(Weather)

expect(chat.tools.keys).not_to include(:weather)
expect(chat.tools.keys).to include(:best_language_to_learn)
end

it 'removes a tool by instance' do
expect(chat.tools.keys).to include(:weather, :best_language_to_learn)

chat.without_tool(weather_tool)

expect(chat.tools.keys).not_to include(:weather)
expect(chat.tools.keys).to include(:best_language_to_learn)
end

it 'returns self for method chaining' do
expect(chat.without_tool(weather_tool)).to be(chat)
end
end

describe '#without_tools' do
it 'removes multiple tools by class' do
expect(chat.tools.keys).to include(:weather, :best_language_to_learn)

chat.without_tools(Weather, BestLanguageToLearn)

expect(chat.tools).to be_empty
end

it 'removes multiple tools by instance' do
expect(chat.tools.keys).to include(:weather, :best_language_to_learn)

chat.without_tools(weather_tool, best_language_tool)

expect(chat.tools).to be_empty
end

it 'returns self for method chaining' do
expect(chat.without_tools(weather_tool, best_language_tool)).to be(chat)
end
end

describe '#clear_tools' do
it 'removes all tools' do
expect(chat.tools).not_to be_empty

chat.clear_tools

expect(chat.tools).to be_empty
end

it 'returns self for method chaining' do
expect(chat.clear_tools).to be(chat)
end
end
end
end