Class: HermesAgent::Client::Entities::ChatCompletion
- Inherits:
-
HermesAgent::Client::Entity
- Object
- HermesAgent::Client::Entity
- HermesAgent::Client::Entities::ChatCompletion
- Includes:
- SessionHeaders
- Defined in:
- lib/hermes_agent/client/entities/chat_completion.rb
Overview
The result of a chat completion (POST /v1/chat/completions).
Field readers are best-effort; HermesAgent::Client::Entity#to_h remains the source of truth.
Instance Attribute Summary
Attributes included from SessionHeaders
Class Method Summary collapse
-
.from_chunks(events, session_id: nil, session_key: nil) ⇒ ChatCompletion
Reconstruct a completion from the events of a streamed turn.
Instance Method Summary collapse
-
#choices ⇒ Array<ChatChoice>?
The generated choices, each wrapped in a ChatChoice.
-
#created ⇒ Integer?
When the completion was created, as a Unix timestamp (seconds).
-
#id ⇒ String?
The completion id, e.g.
-
#model ⇒ String?
The model that produced the completion, e.g.
-
#object ⇒ String?
The object type,
"chat.completion". -
#usage ⇒ ChatUsage?
The token usage, wrapped in a ChatUsage.
Methods included from SessionHeaders
Methods inherited from HermesAgent::Client::Entity
Class Method Details
.from_chunks(events, session_id: nil, session_key: nil) ⇒ ChatCompletion
Reconstruct a completion from the events of a streamed turn. Chat
streaming does not send a final aggregate object, so this assembles
one: the message text is the concatenation of every chunk's
delta.content, the role and finish_reason are taken from the chunks
that carry them, and the usage from the final chunk. Single-choice
(choices[0]) is assumed. Non-chunk events (e.g. HermesAgent::Client::Entities::ChatToolProgress)
are ignored, so the assembled text holds only the assistant's reply.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 290 def self.from_chunks(events, session_id: nil, session_key: nil) chunks = events.select { |event| event.is_a?(ChatCompletionChunk) } first = chunks.empty? ? {} : chunks.first.to_h content = +"" role = nil finish_reason = nil usage = nil chunks.each do |chunk| role ||= chunk.role content << chunk.delta if chunk.delta finish_reason = chunk.finish_reason if chunk.finish_reason usage = chunk["usage"] if chunk["usage"] end new( {"id" => first["id"], "object" => "chat.completion", "created" => first["created"], "model" => first["model"], "choices" => [{"index" => 0, "message" => {"role" => role, "content" => content}, "finish_reason" => finish_reason}], "usage" => usage}, session_id: session_id, session_key: session_key ) end |
Instance Method Details
#choices ⇒ Array<ChatChoice>?
The generated choices, each wrapped in a HermesAgent::Client::Entities::ChatChoice. Returns nil
when the field is absent.
351 352 353 354 355 356 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 351 def choices raw = self["choices"] return nil unless raw.is_a?(::Array) raw.map { |item| ChatChoice.new(item) } end |
#created ⇒ Integer?
When the completion was created, as a Unix timestamp (seconds).
334 335 336 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 334 def created self["created"] end |
#id ⇒ String?
The completion id, e.g. "chatcmpl-…".
318 319 320 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 318 def id self["id"] end |
#model ⇒ String?
The model that produced the completion, e.g. "hermes-test".
342 343 344 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 342 def model self["model"] end |
#object ⇒ String?
The object type, "chat.completion".
326 327 328 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 326 def object self["object"] end |
#usage ⇒ ChatUsage?
The token usage, wrapped in a HermesAgent::Client::Entities::ChatUsage. Returns nil when the
field is absent.
363 364 365 366 |
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 363 def usage raw = self["usage"] raw.is_a?(::Hash) ? ChatUsage.new(raw) : nil end |