Class: OmniAI::Chat::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/chat/function.rb

Overview

A function that includes a name / arguments.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, arguments:) ⇒ Function



17
18
19
20
# File 'lib/omniai/chat/function.rb', line 17

def initialize(name:, arguments:)
  @name = name
  @arguments = arguments
end

Instance Attribute Details

#argumentsHash



13
14
15
# File 'lib/omniai/chat/function.rb', line 13

def arguments
  @arguments
end

#nameString



9
10
11
# File 'lib/omniai/chat/function.rb', line 9

def name
  @name
end

Class Method Details

.deserialize(data, context: nil) ⇒ Function



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/omniai/chat/function.rb', line 31

def self.deserialize(data, context: nil)
  deserialize = context&.deserializer(:function)
  return deserialize.call(data, context:) if deserialize

  name = data["name"]
  arguments = begin
    JSON.parse(data["arguments"]) if data["arguments"]
  rescue JSON::ParserError
    data["arguments"]
  end

  new(name:, arguments:)
end

Instance Method Details

#inspectString



23
24
25
# File 'lib/omniai/chat/function.rb', line 23

def inspect
  "#<#{self.class.name} name=#{name.inspect} arguments=#{arguments.inspect}>"
end

#serialize(context: nil) ⇒ Hash



48
49
50
51
52
53
54
55
56
# File 'lib/omniai/chat/function.rb', line 48

def serialize(context: nil)
  serializer = context&.serializer(:function)
  return serializer.call(self, context:) if serializer

  {
    name: @name,
    arguments: (JSON.generate(@arguments) if @arguments),
  }
end