Class: PromptBuilder::Items::FunctionCall

Inherits:
Base
  • Object
show all
Defined in:
lib/prompt_builder/items/function_call.rb

Overview

Represents a function call item in a conversation. This is output by the model when it wants to invoke a tool.

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, call_id:, arguments:, id: nil, status: nil, **extra) ⇒ FunctionCall

Create a new FunctionCall item.

Parameters:

  • name (String)

    the function name

  • call_id (String)

    the unique call identifier

  • arguments (String, Hash, nil)

    the function arguments; a String is stored as-is (must be valid JSON), a Hash is JSON-encoded, and nil defaults to an empty JSON object (+“{}”+ )

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

    the item identifier

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

    the call status

  • extra (Hash)

    provider-specific extra keyword arguments

Raises:

  • (ArgumentError)

    if arguments is not a String, Hash, or nil



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prompt_builder/items/function_call.rb', line 39

def initialize(name:, call_id:, arguments:, id: nil, status: nil, **extra)
  @name = name&.to_s
  @call_id = call_id&.to_s
  @arguments = case arguments
  when nil then "{}"
  when String then arguments
  when Hash then JSON.generate(arguments)
  else raise ArgumentError, "FunctionCall arguments must be a String, Hash, or nil; got #{arguments.class}"
  end
  @id = id&.to_s
  @status = status&.to_s
  @extra = extra.transform_keys(&:to_s)
end

Instance Attribute Details

#argumentsString (readonly)

Returns the JSON-encoded arguments string (always a Hash-shaped JSON object).

Returns:

  • (String)

    the JSON-encoded arguments string (always a Hash-shaped JSON object)



17
18
19
# File 'lib/prompt_builder/items/function_call.rb', line 17

def arguments
  @arguments
end

#call_idString (readonly)

Returns the unique call identifier.

Returns:

  • (String)

    the unique call identifier



14
15
16
# File 'lib/prompt_builder/items/function_call.rb', line 14

def call_id
  @call_id
end

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



26
27
28
# File 'lib/prompt_builder/items/function_call.rb', line 26

def extra
  @extra
end

#idString? (readonly)

Returns the item identifier.

Returns:

  • (String, nil)

    the item identifier



20
21
22
# File 'lib/prompt_builder/items/function_call.rb', line 20

def id
  @id
end

#nameString (readonly)

Returns the function name.

Returns:

  • (String)

    the function name



11
12
13
# File 'lib/prompt_builder/items/function_call.rb', line 11

def name
  @name
end

#statusString? (readonly)

Returns the call status.

Returns:

  • (String, nil)

    the call status



23
24
25
# File 'lib/prompt_builder/items/function_call.rb', line 23

def status
  @status
end

Class Method Details

.from_h(hash) ⇒ FunctionCall

Deserialize a FunctionCall from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



58
59
60
61
62
63
64
65
66
67
# File 'lib/prompt_builder/items/function_call.rb', line 58

def from_h(hash)
  new(
    name: hash["name"],
    call_id: hash["call_id"],
    arguments: hash["arguments"],
    id: hash["id"],
    status: hash["status"],
    **hash.except("type", "id", "name", "call_id", "arguments", "status").transform_keys(&:to_sym)
  )
end

Instance Method Details

#parsed_argumentsHash

Parse the JSON arguments string into a Hash.

Returns:

  • (Hash)

    the parsed arguments

Raises:



74
75
76
77
78
# File 'lib/prompt_builder/items/function_call.rb', line 74

def parsed_arguments
  JSON.parse(@arguments)
rescue JSON::ParserError => e
  raise PromptBuilder::InvalidItemError, "Invalid JSON in function call arguments for '#{@name}': #{e.message}"
end

#to_hHash

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

Returns:

  • (Hash)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/prompt_builder/items/function_call.rb', line 83

def to_h
  h = {
    "type" => "function_call",
    "name" => @name,
    "call_id" => @call_id,
    "arguments" => @arguments
  }
  h["id"] = @id if @id
  h["status"] = @status if @status
  h = PromptBuilder.jsonify(@extra).merge(h) unless @extra.empty?
  h
end