Class: PromptBuilder::Tools::Definition
- Inherits:
-
Object
- Object
- PromptBuilder::Tools::Definition
- Defined in:
- lib/prompt_builder/tools/definition.rb
Overview
Represents a tool definition that describes a function available to the model.
Instance Attribute Summary collapse
-
#description ⇒ String?
readonly
The tool description.
-
#extra ⇒ Hash?
readonly
Provider-specific extra data (e.g. cache_control).
-
#name ⇒ String
readonly
The tool name.
-
#parameters ⇒ Hash?
readonly
The JSON Schema for the tool parameters.
-
#strict ⇒ Boolean
readonly
Whether strict mode is enabled.
Class Method Summary collapse
-
.from_h(hash) ⇒ Definition
Deserialize a Definition from a Hash.
Instance Method Summary collapse
-
#initialize(name:, description: nil, parameters: nil, strict: false, **extra) ⇒ Definition
constructor
Create a new tool Definition.
-
#to_h ⇒ Hash
Serialize to a Hash with string keys.
Constructor Details
#initialize(name:, description: nil, parameters: nil, strict: false, **extra) ⇒ Definition
Create a new tool Definition.
29 30 31 32 33 34 35 |
# File 'lib/prompt_builder/tools/definition.rb', line 29 def initialize(name:, description: nil, parameters: nil, strict: false, **extra) @name = name&.to_s @description = description&.to_s @parameters = PromptBuilder.jsonify(parameters) @strict = strict ? true : false @extra = extra.transform_keys(&:to_s) end |
Instance Attribute Details
#description ⇒ String? (readonly)
Returns the tool description.
11 12 13 |
# File 'lib/prompt_builder/tools/definition.rb', line 11 def description @description end |
#extra ⇒ Hash? (readonly)
Returns provider-specific extra data (e.g. cache_control).
20 21 22 |
# File 'lib/prompt_builder/tools/definition.rb', line 20 def extra @extra end |
#name ⇒ String (readonly)
Returns the tool name.
8 9 10 |
# File 'lib/prompt_builder/tools/definition.rb', line 8 def name @name end |
#parameters ⇒ Hash? (readonly)
Returns the JSON Schema for the tool parameters.
14 15 16 |
# File 'lib/prompt_builder/tools/definition.rb', line 14 def parameters @parameters end |
#strict ⇒ Boolean (readonly)
Returns whether strict mode is enabled.
17 18 19 |
# File 'lib/prompt_builder/tools/definition.rb', line 17 def strict @strict end |
Class Method Details
.from_h(hash) ⇒ Definition
Deserialize a Definition from a Hash.
42 43 44 45 46 47 48 49 50 |
# File 'lib/prompt_builder/tools/definition.rb', line 42 def from_h(hash) new( name: hash["name"], description: hash["description"], parameters: hash["parameters"], strict: hash.fetch("strict", false), **hash.except("type", "name", "description", "parameters", "strict").transform_keys(&:to_sym) ) end |
Instance Method Details
#to_h ⇒ Hash
Serialize to a Hash with string keys. Nil values are omitted.
56 57 58 59 60 61 62 63 |
# File 'lib/prompt_builder/tools/definition.rb', line 56 def to_h h = {"type" => "function", "name" => @name} h["description"] = @description if @description h["parameters"] = @parameters if @parameters h["strict"] = @strict if @strict h = PromptBuilder.jsonify(@extra).merge(h) unless @extra.empty? h end |