Class: HermesAgent::Client::Entities::ChatCompletionChunk

Inherits:
HermesAgent::Client::Entity show all
Defined in:
lib/hermes_agent/client/entities/chat_completion.rb

Overview

One streamed chunk of a chat completion (object: "chat.completion.chunk"), as emitted by Resources::Chat#stream_create. The convenience readers reflect the first choice (choices[0]), which is the common single-choice case; use HermesAgent::Client::Entity#to_h / HermesAgent::Client::Entity#[] for multi-choice streams.

Instance Method Summary collapse

Methods inherited from HermesAgent::Client::Entity

#==, #[], #eql?, #hash, #to_h

Instance Method Details

#createdInteger?

When the completion was created, as a Unix timestamp (seconds).

Returns:

  • (Integer, nil)


121
122
123
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 121

def created
  self["created"]
end

#deltaString?

The incremental text carried by this chunk — the first choice's delta.content. nil on chunks that carry no text (e.g. the opening role chunk and the final chunk).

Returns:

  • (String, nil)


139
140
141
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 139

def delta
  first_delta["content"]
end

#finish_reasonString?

Why generation stopped, present on the final chunk — the first choice's finish_reason.

Returns:

  • (String, nil)


157
158
159
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 157

def finish_reason
  first_choice["finish_reason"]
end

#idString?

The completion id (carried on every chunk for a turn).

Returns:

  • (String, nil)


105
106
107
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 105

def id
  self["id"]
end

#modelString?

The model producing the completion.

Returns:

  • (String, nil)


129
130
131
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 129

def model
  self["model"]
end

#objectString?

The object type, "chat.completion.chunk".

Returns:

  • (String, nil)


113
114
115
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 113

def object
  self["object"]
end

#roleString?

The author role, present on the opening chunk — the first choice's delta.role.

Returns:

  • (String, nil)


148
149
150
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 148

def role
  first_delta["role"]
end

#usageChatUsage?

The token usage, present on the final chunk, wrapped in a HermesAgent::Client::Entities::ChatUsage. Returns nil when absent.

Returns:



166
167
168
169
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 166

def usage
  raw = self["usage"]
  raw.is_a?(::Hash) ? ChatUsage.new(raw) : nil
end