Class: HermesAgent::Client::Entities::ChatCompletion

Inherits:
HermesAgent::Client::Entity show all
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

#session_id, #session_key

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SessionHeaders

#==, #hash

Methods inherited from HermesAgent::Client::Entity

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

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.

Parameters:

  • events (Array<Entity>)

    The streamed events, in order; only HermesAgent::Client::Entities::ChatCompletionChunks contribute to the result.

  • session_id (String, nil) (defaults to: nil)

    The session id from the response headers, carried onto the assembled completion.

  • session_key (String, nil) (defaults to: nil)

    The session key from the response headers, carried onto the assembled completion.

Returns:



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

#choicesArray<ChatChoice>?

The generated choices, each wrapped in a HermesAgent::Client::Entities::ChatChoice. Returns nil when the field is absent.

Returns:



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

#createdInteger?

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

Returns:

  • (Integer, nil)


334
335
336
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 334

def created
  self["created"]
end

#idString?

The completion id, e.g. "chatcmpl-…".

Returns:

  • (String, nil)


318
319
320
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 318

def id
  self["id"]
end

#modelString?

The model that produced the completion, e.g. "hermes-test".

Returns:

  • (String, nil)


342
343
344
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 342

def model
  self["model"]
end

#objectString?

The object type, "chat.completion".

Returns:

  • (String, nil)


326
327
328
# File 'lib/hermes_agent/client/entities/chat_completion.rb', line 326

def object
  self["object"]
end

#usageChatUsage?

The token usage, wrapped in a HermesAgent::Client::Entities::ChatUsage. Returns nil when the field is absent.

Returns:



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