Class: OmniAI::Chat::Payload

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

Overview

A chunk or completion.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(choices:, usage: nil) ⇒ Payload

Returns a new instance of Payload.

Parameters:

  • choices (Array<Choice>)
  • usage (Usage, nil) (defaults to: nil)


15
16
17
18
# File 'lib/omniai/chat/payload.rb', line 15

def initialize(choices:, usage: nil)
  @choices = choices
  @usage = usage
end

Instance Attribute Details

#choicesArray<Choice>

Returns:



8
9
10
# File 'lib/omniai/chat/payload.rb', line 8

def choices
  @choices
end

#usageUsage?

Returns:



11
12
13
# File 'lib/omniai/chat/payload.rb', line 11

def usage
  @usage
end

Class Method Details

.deserialize(data, context: nil) ⇒ Object

Parameters:



27
28
29
30
31
32
33
34
35
# File 'lib/omniai/chat/payload.rb', line 27

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

  choices = data['choices'].map { |choice_data| Choice.deserialize(choice_data, context:) }
  usage = Usage.deserialize(data['usage'], context:) if data['usage']

  new(choices:, usage:)
end

Instance Method Details

#choice(index: 0) ⇒ Choice

Parameters:

  • index (Integer) (defaults to: 0)

Returns:



51
52
53
# File 'lib/omniai/chat/payload.rb', line 51

def choice(index: 0)
  @choices[index]
end

#inspectString

Returns:

  • (String)


21
22
23
# File 'lib/omniai/chat/payload.rb', line 21

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

#message(index: 0) ⇒ Message

Parameters:

  • index (Integer) (defaults to: 0)

Returns:



57
58
59
# File 'lib/omniai/chat/payload.rb', line 57

def message(index: 0)
  choice(index:).message
end

#messagesArray<Message>

Returns:



62
63
64
# File 'lib/omniai/chat/payload.rb', line 62

def messages
  @choices.map(&:message)
end

#serialize(context:) ⇒ Hash

Parameters:

Returns:

  • (Hash)


39
40
41
42
43
44
45
46
47
# File 'lib/omniai/chat/payload.rb', line 39

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

  {
    choices: choices.map { |choice| choice.serialize(context:) },
    usage: usage&.serialize(context:),
  }
end

#text(index: 0) ⇒ String?

Parameters:

  • index (Integer) (defaults to: 0)

Returns:

  • (String, nil)


68
69
70
# File 'lib/omniai/chat/payload.rb', line 68

def text(index: 0)
  message(index:).text
end

#text?(index: 0) ⇒ Boolean

Parameters:

  • index (Integer) (defaults to: 0)

Returns:

  • (Boolean)


74
75
76
# File 'lib/omniai/chat/payload.rb', line 74

def text?(index: 0)
  message(index:).text?
end

#tool_call_list(index:) ⇒ Array<ToolCall>

Parameters:

  • index (Integer)

Returns:



80
81
82
# File 'lib/omniai/chat/payload.rb', line 80

def tool_call_list(index:)
  message(index:).tool_call_list
end