Class: OmniAI::Chat::Response

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

Overview

An ‘OmniAI::Chat::Response` encapsulates the result of generating a chat completion.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, choices: [], usage: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • data (Hash)
  • choices (Array<Choice>) (defaults to: [])
  • usage (Usage, nil) (defaults to: nil)


19
20
21
22
23
# File 'lib/omniai/chat/response.rb', line 19

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

Instance Attribute Details

#choicesArray<Choice>

Returns:



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

def choices
  @choices
end

#dataHash

Returns:

  • (Hash)


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

def data
  @data
end

#usageUsage?

Returns:



14
15
16
# File 'lib/omniai/chat/response.rb', line 14

def usage
  @usage
end

Class Method Details

.deserialize(data, context: nil) ⇒ Object

Parameters:



32
33
34
35
36
37
38
39
40
# File 'lib/omniai/chat/response.rb', line 32

def self.deserialize(data, context: nil)
  deserialize = context&.deserializer(:response)
  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(data:, choices:, usage:)
end

Instance Method Details

#choice(index: 0) ⇒ Choice?

Parameters:

  • index (Integer) (defaults to: 0)

Returns:



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

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

#choice?(index: 0) ⇒ Boolean

Parameters:

  • index (Integer) (defaults to: 0)

Returns:

  • (Boolean)


64
65
66
# File 'lib/omniai/chat/response.rb', line 64

def choice?(index: 0)
  !choice(index:).nil?
end

#inspectString

Returns:

  • (String)


26
27
28
# File 'lib/omniai/chat/response.rb', line 26

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

#message(index: 0) ⇒ Message?

Parameters:

  • index (Integer) (defaults to: 0)

Returns:



71
72
73
# File 'lib/omniai/chat/response.rb', line 71

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

#message?Boolean

Parameters:

  • index (Integer)

Returns:

  • (Boolean)


78
79
80
# File 'lib/omniai/chat/response.rb', line 78

def message?
  !message(index:).nil?
end

#messagesArray<Message>

Returns:



83
84
85
# File 'lib/omniai/chat/response.rb', line 83

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

#serialize(context:) ⇒ Hash

Parameters:

Returns:

  • (Hash)


44
45
46
47
48
49
50
51
52
# File 'lib/omniai/chat/response.rb', line 44

def serialize(context:)
  serialize = context&.serializer(:response)
  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)


90
91
92
# File 'lib/omniai/chat/response.rb', line 90

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

#text?(index: 0) ⇒ Boolean

Parameters:

  • index (Integer) (defaults to: 0)

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/omniai/chat/response.rb', line 97

def text?(index: 0)
  message = message(index:)

  !message.nil? && message.text?
end

#tool_call_list(index: 0) ⇒ ToolCallList

Parameters:

  • index (Integer) (defaults to: 0)

Returns:



106
107
108
# File 'lib/omniai/chat/response.rb', line 106

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

#tool_call_list?(index: 0) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/omniai/chat/response.rb', line 111

def tool_call_list?(index: 0)
  tool_call_list = tool_call_list(index:)

  !tool_call_list.nil? && tool_call_list.any?
end