Class: OmniAI::Chat::Response
- Inherits:
-
Object
- Object
- OmniAI::Chat::Response
- 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
- #choice(index: 0) ⇒ Choice?
- #choice?(index: 0) ⇒ Boolean
-
#initialize(data:, choices: [], usage: nil) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ String
- #message(index: 0) ⇒ Message?
- #message?(index: 0) ⇒ Boolean
- #messages ⇒ Array<Message>
- #serialize(context:) ⇒ Hash
- #text(index: 0) ⇒ String?
- #text?(index: 0) ⇒ Boolean
- #tool_call_list(index: 0) ⇒ ToolCallList
- #tool_call_list?(index: 0) ⇒ Boolean
Constructor Details
#initialize(data:, choices: [], usage: nil) ⇒ Response
Returns a new instance of Response.
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
#choices ⇒ Array<Choice>
11 12 13 |
# File 'lib/omniai/chat/response.rb', line 11 def choices @choices end |
#data ⇒ Hash
8 9 10 |
# File 'lib/omniai/chat/response.rb', line 8 def data @data end |
Class Method Details
.deserialize(data, context: nil) ⇒ OmniAI::Chat::Response
34 35 36 37 38 39 40 41 42 |
# File 'lib/omniai/chat/response.rb', line 34 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?
59 60 61 |
# File 'lib/omniai/chat/response.rb', line 59 def choice(index: 0) @choices[index] end |
#choice?(index: 0) ⇒ Boolean
66 67 68 |
# File 'lib/omniai/chat/response.rb', line 66 def choice?(index: 0) !choice(index:).nil? end |
#inspect ⇒ 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?
73 74 75 |
# File 'lib/omniai/chat/response.rb', line 73 def (index: 0) choice(index:)&. end |
#message?(index: 0) ⇒ Boolean
80 81 82 |
# File 'lib/omniai/chat/response.rb', line 80 def (index: 0) !(index:).nil? end |
#messages ⇒ Array<Message>
85 86 87 |
# File 'lib/omniai/chat/response.rb', line 85 def @choices.map(&:message) end |
#serialize(context:) ⇒ Hash
46 47 48 49 50 51 52 53 54 |
# File 'lib/omniai/chat/response.rb', line 46 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?
92 93 94 |
# File 'lib/omniai/chat/response.rb', line 92 def text(index: 0) (index:)&.text end |
#text?(index: 0) ⇒ Boolean
99 100 101 102 103 |
# File 'lib/omniai/chat/response.rb', line 99 def text?(index: 0) = (index:) !.nil? && .text? end |
#tool_call_list(index: 0) ⇒ ToolCallList
108 109 110 |
# File 'lib/omniai/chat/response.rb', line 108 def tool_call_list(index: 0) (index:)&.tool_call_list end |
#tool_call_list?(index: 0) ⇒ Boolean
113 114 115 116 117 |
# File 'lib/omniai/chat/response.rb', line 113 def tool_call_list?(index: 0) tool_call_list = tool_call_list(index:) !tool_call_list.nil? && tool_call_list.any? end |