Class: OmniAI::Chat::Choice

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

Overview

A choice wraps a message and index returned by an LLM. The default is to generate a single choice. Some LLMs support generating multiple choices at once (e.g. giving you multiple options to choose from).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, index: 0) ⇒ Choice

Returns a new instance of Choice.

Parameters:

  • message (Message)
  • index (Integer) (defaults to: 0)


16
17
18
19
# File 'lib/omniai/chat/choice.rb', line 16

def initialize(message:, index: 0)
  @message = message
  @index = index
end

Instance Attribute Details

#indexInteger

Returns:

  • (Integer)


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

def index
  @index
end

#messageMessage

Returns:



12
13
14
# File 'lib/omniai/chat/choice.rb', line 12

def message
  @message
end

Class Method Details

.deserialize(data, context: nil) ⇒ Choice

Parameters:

Returns:



30
31
32
33
34
35
36
37
38
# File 'lib/omniai/chat/choice.rb', line 30

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

  index = data['index']
  message = Message.deserialize(data['message'] || data['delta'], context:)

  new(message:, index:)
end

Instance Method Details

#contentArray<Content>, String

Returns:



58
59
60
# File 'lib/omniai/chat/choice.rb', line 58

def content
  message.content
end

#deltaMessage

Returns:



53
54
55
# File 'lib/omniai/chat/choice.rb', line 53

def delta
  message
end

#inspectString

Returns:

  • (String)


22
23
24
# File 'lib/omniai/chat/choice.rb', line 22

def inspect
  "#<#{self.class.name} index=#{@index} message=#{@message.inspect}>"
end

#serialize(context: nil) ⇒ Hash

Parameters:

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
# File 'lib/omniai/chat/choice.rb', line 42

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

  {
    index:,
    message: message.serialize(context:),
  }
end

#tool_call_listArray<ToolCall>?

Returns:



63
64
65
# File 'lib/omniai/chat/choice.rb', line 63

def tool_call_list
  message.tool_call_list
end