Class: Rubygpt::Response::ChatCompletion::Choice

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygpt/response/chat_completion.rb

Overview

Represents a Choice object in the ChatCompletion response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, message:, logprobs:, finish_reason:) ⇒ Choice

Initializes the Choice object



25
26
27
28
29
30
# File 'lib/rubygpt/response/chat_completion.rb', line 25

def initialize(index:, message:, logprobs:, finish_reason:)
  @index = index
  @message = Common::Message.new(**message.transform_keys(&:to_sym))
  @logprobs = logprobs
  @finish_reason = finish_reason
end

Instance Attribute Details

#finish_reasonObject (readonly)

The reason the model stopped generating tokens. One of: stop, length, content_filter, tool_calls



22
23
24
# File 'lib/rubygpt/response/chat_completion.rb', line 22

def finish_reason
  @finish_reason
end

#indexObject (readonly)

The index of the choice in the list of choices.



11
12
13
# File 'lib/rubygpt/response/chat_completion.rb', line 11

def index
  @index
end

#logprobsObject (readonly)

Log probability information for the choice.



18
19
20
# File 'lib/rubygpt/response/chat_completion.rb', line 18

def logprobs
  @logprobs
end

#messageObject (readonly)

A chat completion message generated by the model. A Message object



15
16
17
# File 'lib/rubygpt/response/chat_completion.rb', line 15

def message
  @message
end

Instance Method Details

#contentObject

Delegations to the message object



33
34
35
# File 'lib/rubygpt/response/chat_completion.rb', line 33

def content
  message.content
end

#failed?Boolean

Returns true if the choice failed to generate a complete message

Returns:

  • (Boolean)


42
43
44
# File 'lib/rubygpt/response/chat_completion.rb', line 42

def failed?
  finish_reason != "stop"
end

#roleObject



37
38
39
# File 'lib/rubygpt/response/chat_completion.rb', line 37

def role
  message.role
end

#to_hObject



46
47
48
# File 'lib/rubygpt/response/chat_completion.rb', line 46

def to_h
  { index:, message: message.to_h, logprobs:, finish_reason: }.compact
end