Class: HermesAgent::Client::Resources::Chat
- Inherits:
-
Object
- Object
- HermesAgent::Client::Resources::Chat
- Defined in:
- lib/hermes_agent/client/resources/chat.rb
Overview
The chat resource: OpenAI-compatible chat completions
(POST /v1/chat/completions). This endpoint is stateless — each call
is independent — and, on a server configured with an API key, requires
a bearer token (see HermesAgent::Client / Configuration).
Constant Summary collapse
- TOOL_PROGRESS_EVENT =
The SSE
event:name of the server's custom tool-progress frames, which are routed to Entities::ChatToolProgress rather than treated as completion chunks. "hermes.tool.progress"
Instance Method Summary collapse
-
#create(messages:, session_id: nil, session_key: nil, idempotency_key: nil, **extra) ⇒ Entities::ChatCompletion
Create a chat completion.
-
#stream_create(messages:, session_id: nil, session_key: nil, **extra) {|event| ... } ⇒ Entities::ChatCompletion, Stream
Create a chat completion, streaming the response.
Instance Method Details
#create(messages:, session_id: nil, session_key: nil, idempotency_key: nil, **extra) ⇒ Entities::ChatCompletion
Create a chat completion.
No model is sent: the model is configured server-side and the
server ignores a client-supplied one. (A caller who really wants to
send fields we have not modeled — including model — can pass them
through extra.)
66 67 68 69 70 71 72 |
# File 'lib/hermes_agent/client/resources/chat.rb', line 66 def create(messages:, session_id: nil, session_key: nil, idempotency_key: nil, **extra) body = {messages: , **extra} headers = session_request_headers(session_id, session_key) headers["Idempotency-Key"] = idempotency_key if idempotency_key result = @transport.post("/v1/chat/completions", body, headers: headers) Entities::ChatCompletion.new(result.body, **Util.session_headers(result.headers)) end |
#stream_create(messages:, session_id: nil, session_key: nil, **extra) {|event| ... } ⇒ Entities::ChatCompletion, Stream
Create a chat completion, streaming the response.
While the server agent executes tools it interleaves custom
hermes.tool.progress frames; these are surfaced as
Entities::ChatToolProgress events (distinct from the text
Entities::ChatCompletionChunks) and are not folded into the
assembled completion.
With a block, each event is yielded as it arrives and the assembled Entities::ChatCompletion is returned once the stream closes. Without a block, a Stream is returned for the caller to iterate; its Stream#result is the assembled completion.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/hermes_agent/client/resources/chat.rb', line 103 def stream_create(messages:, session_id: nil, session_key: nil, **extra, &block) body = {messages: , stream: true, **extra} result = @transport.stream_post("/v1/chat/completions", body, headers: session_request_headers(session_id, session_key)) session = Util.session_headers(result.headers) event_class = lambda do |name| name == TOOL_PROGRESS_EVENT ? Entities::ChatToolProgress : Entities::ChatCompletionChunk end stream = Stream.new(result.body, event_class: event_class, terminator: "[DONE]") do |events| Entities::ChatCompletion.from_chunks(events, **session) end return stream unless block stream.each(&block) stream.result end |