Class: PromptBuilder::Items::FunctionCall
- 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
Instance Attribute Summary collapse
-
#arguments ⇒ String
readonly
The JSON-encoded arguments string (always a Hash-shaped JSON object).
-
#call_id ⇒ String
readonly
The unique call identifier.
-
#extra ⇒ Hash?
readonly
Provider-specific extra data.
-
#id ⇒ String?
readonly
The item identifier.
-
#name ⇒ String
readonly
The function name.
-
#status ⇒ String?
readonly
The call status.
Class Method Summary collapse
-
.from_h(hash) ⇒ FunctionCall
Deserialize a FunctionCall from a Hash.
Instance Method Summary collapse
-
#initialize(name:, call_id:, arguments:, id: nil, status: nil, **extra) ⇒ FunctionCall
constructor
Create a new FunctionCall item.
-
#parsed_arguments ⇒ Hash
Parse the JSON arguments string into a Hash.
-
#to_h ⇒ Hash
Serialize to a Hash with string keys.
Constructor Details
#initialize(name:, call_id:, arguments:, id: nil, status: nil, **extra) ⇒ FunctionCall
Create a new FunctionCall item.
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
#arguments ⇒ String (readonly)
Returns 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_id ⇒ String (readonly)
Returns the unique call identifier.
14 15 16 |
# File 'lib/prompt_builder/items/function_call.rb', line 14 def call_id @call_id end |
#extra ⇒ Hash? (readonly)
Returns provider-specific extra data.
26 27 28 |
# File 'lib/prompt_builder/items/function_call.rb', line 26 def extra @extra end |
#id ⇒ String? (readonly)
Returns the item identifier.
20 21 22 |
# File 'lib/prompt_builder/items/function_call.rb', line 20 def id @id end |
#name ⇒ String (readonly)
Returns the function name.
11 12 13 |
# File 'lib/prompt_builder/items/function_call.rb', line 11 def name @name end |
#status ⇒ String? (readonly)
Returns 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.
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_arguments ⇒ Hash
Parse the JSON arguments string into a Hash.
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.}" end |
#to_h ⇒ Hash
Serialize to a Hash with string keys. Nil values are omitted.
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 |