Skip to content

Support removing tools #238

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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