Class: Rubygpt::Response::ChatCompletion
- Inherits:
-
BaseResponse
- Object
- BaseResponse
- Rubygpt::Response::ChatCompletion
- Defined in:
- lib/rubygpt/response/chat_completion.rb
Overview
Represents the ChatCompletion response from the Chat API platform.openai.com/docs/api-reference/chat/object
Defined Under Namespace
Classes: Choice
Instance Attribute Summary
Attributes inherited from BaseResponse
Instance Method Summary collapse
- #cost ⇒ Object
- #failed? ⇒ Boolean
-
#initialize(api_response) ⇒ ChatCompletion
constructor
Initializes the ChatCompletion object.
-
#messages ⇒ Object
The list of message strings generated by the model.
-
#read(message_separator: "\n") ⇒ Object
Messages combined into a single string, separated by newlines.
- #to_h ⇒ Object
Constructor Details
#initialize(api_response) ⇒ ChatCompletion
Initializes the ChatCompletion object
54 55 56 57 |
# File 'lib/rubygpt/response/chat_completion.rb', line 54 def initialize(api_response) super @choices = @choices.map { |choice| Choice.new(**choice.transform_keys(&:to_sym)) }.sort_by(&:index) if @choices end |
Instance Method Details
#cost ⇒ Object
75 76 77 |
# File 'lib/rubygpt/response/chat_completion.rb', line 75 def cost usage["total_tokens"] || usage[:total_tokens] end |
#failed? ⇒ Boolean
71 72 73 |
# File 'lib/rubygpt/response/chat_completion.rb', line 71 def failed? choices.any?(&:failed?) end |
#messages ⇒ Object
The list of message strings generated by the model.
return [Array<String>]
62 63 64 |
# File 'lib/rubygpt/response/chat_completion.rb', line 62 def @messages ||= choices.map { |choice| choice..content } end |
#read(message_separator: "\n") ⇒ Object
Messages combined into a single string, separated by newlines
67 68 69 |
# File 'lib/rubygpt/response/chat_completion.rb', line 67 def read(message_separator: "\n") @read ||= .join() end |
#to_h ⇒ Object
79 80 81 |
# File 'lib/rubygpt/response/chat_completion.rb', line 79 def to_h { id:, object:, created:, model:, system_fingerprint:, usage:, choices: choices.map(&:to_h) }.compact end |