Class: ChatgptRb::Function
- Inherits:
-
Object
- Object
- ChatgptRb::Function
- Defined in:
- lib/chatgpt_rb/function.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#implementation ⇒ Object
Returns the value of attribute implementation.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Instance Method Summary collapse
- #as_json ⇒ Hash
-
#initialize(name: nil, description: nil, parameters: [], implementation: nil) ⇒ Function
constructor
A new instance of Function.
- #validate! ⇒ Object
Constructor Details
#initialize(name: nil, description: nil, parameters: [], implementation: nil) ⇒ Function
Returns a new instance of Function.
9 10 11 12 13 14 |
# File 'lib/chatgpt_rb/function.rb', line 9 def initialize(name: nil, description: nil, parameters: [], implementation: nil) @name = name @description = description @parameters = parameters @implementation = implementation end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
3 4 5 |
# File 'lib/chatgpt_rb/function.rb', line 3 def description @description end |
#implementation ⇒ Object
Returns the value of attribute implementation.
3 4 5 |
# File 'lib/chatgpt_rb/function.rb', line 3 def implementation @implementation end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/chatgpt_rb/function.rb', line 3 def name @name end |
#parameters ⇒ Object
Returns the value of attribute parameters.
3 4 5 |
# File 'lib/chatgpt_rb/function.rb', line 3 def parameters @parameters end |
Instance Method Details
#as_json ⇒ Hash
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/chatgpt_rb/function.rb', line 24 def as_json { type: "function", function: { name:, description:, parameters: { type: "object", properties: parameters.each_with_object({}) do |parameter, hash| hash[parameter.name] = parameter.as_json end, required: parameters.select(&:required?).map(&:name), }, }.compact } end |
#validate! ⇒ Object
16 17 18 19 20 21 |
# File 'lib/chatgpt_rb/function.rb', line 16 def validate! = JSON::Validator.validator_for_name("draft4"). return if JSON::Validator.validate(, as_json[:function]) raise ArgumentError, "Invalid function declaration for #{name}: #{as_json[:function]}" end |