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: DEFAULT_INDEX, finish_reason: nil) ⇒ Choice

Returns a new instance of Choice.

Parameters:

  • message (Message)
  • index (Integer) (defaults to: DEFAULT_INDEX)
  • finish_reason (FinishReason, nil) (defaults to: nil)


27
28
29
30
31
# File 'lib/omniai/chat/choice.rb', line 27

def initialize(message:, index: DEFAULT_INDEX, finish_reason: nil)
  @message = message
  @index = index
  @finish_reason = finish_reason
end

Instance Attribute Details

#finish_reasonFinishReason?

Returns the normalized reason generation stopped (carrying both ‘#reason` and the verbatim provider `#value`), or `nil` when absent.

Returns:

  • (FinishReason, nil)

    the normalized reason generation stopped (carrying both ‘#reason` and the verbatim provider `#value`), or `nil` when absent.



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

def finish_reason
  @finish_reason
end

#indexInteger

Returns:

  • (Integer)


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

def index
  @index
end

#messageMessage

Returns:



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

def message
  @message
end

Class Method Details

.deserialize(data, context: nil) ⇒ Choice

Parameters:

Returns:



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

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

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

  new(message:, index:, finish_reason: FinishReason.deserialize(data["finish_reason"]))
end

Instance Method Details

#contentArray<Content>, String

Returns:



70
71
72
# File 'lib/omniai/chat/choice.rb', line 70

def content
  message.content
end

#deltaMessage

Returns:



65
66
67
# File 'lib/omniai/chat/choice.rb', line 65

def delta
  message
end

#inspectString

Returns:

  • (String)


34
35
36
# File 'lib/omniai/chat/choice.rb', line 34

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

#serialize(context: nil) ⇒ Hash

Parameters:

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
# File 'lib/omniai/chat/choice.rb', line 54

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:



75
76
77
# File 'lib/omniai/chat/choice.rb', line 75

def tool_call_list
  message.tool_call_list
end