Class: RubyPi::Tools::Definition
- Inherits:
-
Object
- Object
- RubyPi::Tools::Definition
- Defined in:
- lib/ruby_pi/tools/definition.rb
Instance Attribute Summary collapse
-
#category ⇒ Symbol?
readonly
An optional category for grouping related tools.
-
#description ⇒ String
readonly
A human-readable description of what this tool does.
-
#name ⇒ Symbol
readonly
The unique name identifying this tool.
-
#parameters ⇒ Hash
readonly
A JSON Schema hash describing the tool’s parameters.
Instance Method Summary collapse
-
#call(args = {}) ⇒ Object
Invokes the tool with the given arguments.
-
#initialize(name:, description:, category: nil, parameters: {}) {|Hash| ... } ⇒ Definition
constructor
Creates a new tool definition.
-
#inspect ⇒ String
Provides a human-readable string representation of the definition.
-
#to_anthropic_format ⇒ Hash
Converts this tool definition to Anthropic’s tool format.
-
#to_gemini_format ⇒ Hash
Converts this tool definition to Google Gemini function declaration format.
-
#to_openai_format ⇒ Hash
Converts this tool definition to OpenAI’s function calling format.
Constructor Details
#initialize(name:, description:, category: nil, parameters: {}) {|Hash| ... } ⇒ Definition
Creates a new tool definition.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby_pi/tools/definition.rb', line 48 def initialize(name:, description:, category: nil, parameters: {}, &block) raise ArgumentError, "Tool name is required" if name.nil? || name.to_s.strip.empty? raise ArgumentError, "Tool description is required" if description.nil? || description.strip.empty? raise ArgumentError, "Tool implementation block is required" unless block_given? @name = name.to_sym @description = description @category = category&.to_sym @parameters = parameters @implementation = block end |
Instance Attribute Details
#category ⇒ Symbol? (readonly)
Returns An optional category for grouping related tools.
35 36 37 |
# File 'lib/ruby_pi/tools/definition.rb', line 35 def category @category end |
#description ⇒ String (readonly)
Returns A human-readable description of what this tool does.
32 33 34 |
# File 'lib/ruby_pi/tools/definition.rb', line 32 def description @description end |
#name ⇒ Symbol (readonly)
Returns The unique name identifying this tool.
29 30 31 |
# File 'lib/ruby_pi/tools/definition.rb', line 29 def name @name end |
#parameters ⇒ Hash (readonly)
Returns A JSON Schema hash describing the tool’s parameters.
38 39 40 |
# File 'lib/ruby_pi/tools/definition.rb', line 38 def parameters @parameters end |
Instance Method Details
#call(args = {}) ⇒ Object
Invokes the tool with the given arguments.
64 65 66 |
# File 'lib/ruby_pi/tools/definition.rb', line 64 def call(args = {}) @implementation.call(args) end |
#inspect ⇒ String
Provides a human-readable string representation of the definition.
119 120 121 |
# File 'lib/ruby_pi/tools/definition.rb', line 119 def inspect "#<RubyPi::Tools::Definition name=#{@name.inspect} category=#{@category.inspect}>" end |
#to_anthropic_format ⇒ Hash
Converts this tool definition to Anthropic’s tool format.
Anthropic expects:
{ name: "...", description: "...", input_schema: { ... } }
89 90 91 92 93 94 95 96 |
# File 'lib/ruby_pi/tools/definition.rb', line 89 def to_anthropic_format tool = { name: @name.to_s, description: @description } tool[:input_schema] = @parameters unless @parameters.empty? tool end |
#to_gemini_format ⇒ Hash
Converts this tool definition to Google Gemini function declaration format.
Gemini expects:
{ name: "...", description: "...", parameters: { ... } }
74 75 76 77 78 79 80 81 |
# File 'lib/ruby_pi/tools/definition.rb', line 74 def to_gemini_format declaration = { name: @name.to_s, description: @description } declaration[:parameters] = @parameters unless @parameters.empty? declaration end |
#to_openai_format ⇒ Hash
Converts this tool definition to OpenAI’s function calling format.
OpenAI expects:
{ type: "function", function: { name: "...", description: "...", parameters: { ... } } }
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby_pi/tools/definition.rb', line 104 def to_openai_format function = { name: @name.to_s, description: @description } function[:parameters] = @parameters unless @parameters.empty? { type: "function", function: function } end |