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)


22
23
24
25
26
# File 'lib/omniai/chat/response.rb', line 22

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

Instance Attribute Details

#choicesObject

Returns the value of attribute choices.



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

def choices
  @choices
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#usageObject

Returns the value of attribute usage.



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

def usage
  @usage
end

Class Method Details

.deserialize(data, context: nil) ⇒ OmniAI::Chat::Response

Parameters:

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/omniai/chat/response.rb', line 37

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

#=( = (value)) ⇒ Array<Choice>

Returns:



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

attr_accessor :data

#inspectString

Returns:

  • (String)


29
30
31
# File 'lib/omniai/chat/response.rb', line 29

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

#messagesArray<Message>

Returns:



61
62
63
# File 'lib/omniai/chat/response.rb', line 61

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

#messages?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/omniai/chat/response.rb', line 66

def messages?
  messages.any?
end

#serialize(context:) ⇒ Hash

Parameters:

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
# File 'lib/omniai/chat/response.rb', line 50

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

#textString?

Returns:

  • (String, nil)


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

def text
  return unless text?

  messages.filter(&:text?).map(&:text).join("\n\n")
end

#text?Boolean

Returns:

  • (Boolean)


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

def text?
  messages.any?(&:text?)
end

#tool_call_listToolCallList?

Returns:



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

def tool_call_list
  tool_call_lists = messages.map(&:tool_call_list).compact
  return if tool_call_lists.empty?

  tool_call_lists.reduce(&:+)
end

#tool_call_list?Boolean

Returns:

  • (Boolean)


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

def tool_call_list?
  !tool_call_list.nil?
end