Class: PromptBuilder::Tools::Definition

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description: nil, parameters: nil, strict: false, **extra) ⇒ Definition

Create a new tool Definition.

Parameters:

  • name (String)

    the tool name

  • description (String, nil) (defaults to: nil)

    the tool description

  • parameters (Hash, nil) (defaults to: nil)

    the JSON Schema for the parameters

  • strict (Boolean) (defaults to: false)

    whether strict mode is enabled

  • extra (Hash)

    provider-specific extra keyword arguments



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

#descriptionString? (readonly)

Returns the tool description.

Returns:

  • (String, nil)

    the tool description



11
12
13
# File 'lib/prompt_builder/tools/definition.rb', line 11

def description
  @description
end

#extraHash? (readonly)

Returns provider-specific extra data (e.g. cache_control).

Returns:

  • (Hash, nil)

    provider-specific extra data (e.g. cache_control)



20
21
22
# File 'lib/prompt_builder/tools/definition.rb', line 20

def extra
  @extra
end

#nameString (readonly)

Returns the tool name.

Returns:

  • (String)

    the tool name



8
9
10
# File 'lib/prompt_builder/tools/definition.rb', line 8

def name
  @name
end

#parametersHash? (readonly)

Returns the JSON Schema for the tool parameters.

Returns:

  • (Hash, nil)

    the JSON Schema for the tool parameters



14
15
16
# File 'lib/prompt_builder/tools/definition.rb', line 14

def parameters
  @parameters
end

#strictBoolean (readonly)

Returns whether strict mode is enabled.

Returns:

  • (Boolean)

    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.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



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_hHash

Serialize to a Hash with string keys. Nil values are omitted.

Returns:

  • (Hash)


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