Class: Ask::ToolDef
- Inherits:
-
Object
- Object
- Ask::ToolDef
- Defined in:
- lib/ask/tool_def.rb
Overview
Immutable value object representing a tool (function) definition that can be passed to LLM providers. Describes a callable tool’s name, description, and parameter schema.
ToolDef.new(
name: "get_weather",
description: "Get current weather for a location",
parameters: {
type: "object",
properties: { location: { type: "string" } },
required: ["location"]
}
)
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
Description of what the tool does.
-
#name ⇒ String
readonly
Tool name (must match /\A[a-zA-Z0-9_-]*\z/).
-
#parameters ⇒ Hash?
readonly
JSON Schema parameter definition.
-
#provider_params ⇒ Hash
readonly
Provider-specific parameters (e.g. concurrency limits).
Class Method Summary collapse
-
.from_tool(tool) ⇒ Ask::ToolDef
Build a ToolDef from an object that responds to #name, #description, #params_schema, and #provider_params.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Two ToolDefs are equal if they have the same name.
- #hash ⇒ Object
-
#initialize(name:, description: "", parameters: nil, provider_params: {}) ⇒ ToolDef
constructor
A new instance of ToolDef.
- #inspect ⇒ String
-
#to_h ⇒ Hash
Serialized representation.
-
#to_provider_format {|self| ... } ⇒ Hash
Convert to a provider-specific format.
Constructor Details
#initialize(name:, description: "", parameters: nil, provider_params: {}) ⇒ ToolDef
Returns a new instance of ToolDef.
47 48 49 50 51 52 53 |
# File 'lib/ask/tool_def.rb', line 47 def initialize(name:, description: "", parameters: nil, provider_params: {}) @name = validate_name!(name) @description = description.to_s @parameters = parameters @provider_params = provider_params.dup.freeze freeze end |
Instance Attribute Details
#description ⇒ String (readonly)
Returns description of what the tool does.
39 40 41 |
# File 'lib/ask/tool_def.rb', line 39 def description @description end |
#name ⇒ String (readonly)
Returns tool name (must match /\A[a-zA-Z0-9_-]*\z/).
36 37 38 |
# File 'lib/ask/tool_def.rb', line 36 def name @name end |
#parameters ⇒ Hash? (readonly)
Returns JSON Schema parameter definition.
42 43 44 |
# File 'lib/ask/tool_def.rb', line 42 def parameters @parameters end |
#provider_params ⇒ Hash (readonly)
Returns provider-specific parameters (e.g. concurrency limits).
45 46 47 |
# File 'lib/ask/tool_def.rb', line 45 def provider_params @provider_params end |
Class Method Details
.from_tool(tool) ⇒ Ask::ToolDef
Build a ToolDef from an object that responds to #name, #description, #params_schema, and #provider_params.
24 25 26 27 28 29 30 31 32 |
# File 'lib/ask/tool_def.rb', line 24 def from_tool(tool) schema = tool.params_schema new( name: tool.name, description: tool.description, parameters: schema, provider_params: tool.provider_params ) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Two ToolDefs are equal if they have the same name.
66 67 68 69 70 |
# File 'lib/ask/tool_def.rb', line 66 def ==(other) return false unless other.is_a?(ToolDef) name == other.name end |
#hash ⇒ Object
73 74 75 |
# File 'lib/ask/tool_def.rb', line 73 def hash name.hash end |
#inspect ⇒ String
88 89 90 |
# File 'lib/ask/tool_def.rb', line 88 def inspect "#<Ask::ToolDef name=#{@name.inspect}>" end |
#to_h ⇒ Hash
Returns serialized representation.
78 79 80 81 82 83 84 85 |
# File 'lib/ask/tool_def.rb', line 78 def to_h { name: @name, description: @description, parameters: @parameters, provider_params: @provider_params } end |
#to_provider_format {|self| ... } ⇒ Hash
Convert to a provider-specific format.
58 59 60 61 62 |
# File 'lib/ask/tool_def.rb', line 58 def to_provider_format(&block) return @parameters || default_parameters unless block block.call(self) end |