Class: LLM::Tool

Inherits:
Struct
  • Object
show all
Defined in:
lib/llm/tool.rb

Overview

The LLM::Tool class represents a platform-native tool that can be activated by an LLM provider. Unlike LLM::Function, these tools are pre-defined by the provider and their capabilities are already known to the underlying LLM.

Examples:

#!/usr/bin/env ruby
llm = LLM.gemini ENV["KEY"]
bot = LLM::Bot.new(llm, tools: [LLM.tool(:google_search)])
bot.chat("Summarize today's news", role: :user)
print bot.messages.find(&:assistant?).content, "\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



15
16
17
# File 'lib/llm/tool.rb', line 15

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



15
16
17
# File 'lib/llm/tool.rb', line 15

def options
  @options
end

#providerObject

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



15
16
17
# File 'lib/llm/tool.rb', line 15

def provider
  @provider
end

Instance Method Details

#to_jsonString

Returns:

  • (String)


18
19
20
# File 'lib/llm/tool.rb', line 18

def to_json(...)
  to_h.to_json(...)
end

#to_hHash Also known as: to_hash

Returns:

  • (Hash)


24
25
26
27
28
29
30
# File 'lib/llm/tool.rb', line 24

def to_h
  case provider.class.to_s
  when "LLM::Anthropic" then options.merge("name" => name.to_s)
  when "LLM::Gemini" then {name => options}
  else options.merge("type" => name.to_s)
  end
end