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
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#data ⇒ Object
Returns the value of attribute data.
-
#usage ⇒ Object
Returns the value of attribute usage.
Class Method Summary collapse
Instance Method Summary collapse
- #=( = (value)) ⇒ Array<Choice>
-
#initialize(data:, choices: [], usage: nil) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ String
- #messages ⇒ Array<Message>
- #messages? ⇒ Boolean
- #serialize(context:) ⇒ Hash
- #text ⇒ String?
- #text? ⇒ Boolean
- #tool_call_list ⇒ ToolCallList?
- #tool_call_list? ⇒ Boolean
Constructor Details
#initialize(data:, choices: [], usage: nil) ⇒ Response
Returns a new instance of Response.
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
#choices ⇒ Object
Returns the value of attribute choices.
17 18 19 |
# File 'lib/omniai/chat/response.rb', line 17 def choices @choices end |
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'lib/omniai/chat/response.rb', line 9 def data @data end |
#usage ⇒ Object
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
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
#inspect ⇒ String
29 30 31 |
# File 'lib/omniai/chat/response.rb', line 29 def inspect "#<#{self.class.name} choices=#{@choices.inspect} usage=#{@usage.inspect}>" end |
#messages ⇒ Array<Message>
61 62 63 |
# File 'lib/omniai/chat/response.rb', line 61 def @choices.map(&:message).compact end |
#messages? ⇒ Boolean
66 67 68 |
# File 'lib/omniai/chat/response.rb', line 66 def .any? end |
#serialize(context:) ⇒ 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 |
#text ⇒ String?
71 72 73 74 75 |
# File 'lib/omniai/chat/response.rb', line 71 def text return unless text? .filter(&:text?).map(&:text).join("\n\n") end |
#text? ⇒ Boolean
78 79 80 |
# File 'lib/omniai/chat/response.rb', line 78 def text? .any?(&:text?) end |
#tool_call_list ⇒ ToolCallList?
83 84 85 86 87 88 |
# File 'lib/omniai/chat/response.rb', line 83 def tool_call_list tool_call_lists = .map(&:tool_call_list).compact return if tool_call_lists.empty? tool_call_lists.reduce(&:+) end |
#tool_call_list? ⇒ Boolean
91 92 93 |
# File 'lib/omniai/chat/response.rb', line 91 def tool_call_list? !tool_call_list.nil? end |