Class: Tep::Llm::OpenAI::StreamSink

Inherits:
Object
  • Object
show all
Defined in:
lib/tep/openai_server.rb

Overview

The per-token write surface a streaming backend uses (7.2). One method: ‘emit_token(piece)`. The sink formats `piece` as an OpenAI text-completion SSE frame and writes one chunked frame to the outbound stream. Counts emitted tokens for the inference event’s completion_tokens.

Why a sink object instead of a block: spinel can’t lower a block parameter across the backend call boundary; a typed object with one method does the same job through ordinary virtual dispatch.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStreamSink

Returns a new instance of StreamSink.



312
313
314
315
# File 'lib/tep/openai_server.rb', line 312

def initialize
  @model            = ""
  @completion_count = 0
end

Instance Attribute Details

#completion_countObject

Returns the value of attribute completion_count.



310
311
312
# File 'lib/tep/openai_server.rb', line 310

def completion_count
  @completion_count
end

#modelObject

Returns the value of attribute model.



310
311
312
# File 'lib/tep/openai_server.rb', line 310

def model
  @model
end

#outObject

Returns the value of attribute out.



310
311
312
# File 'lib/tep/openai_server.rb', line 310

def out
  @out
end

Instance Method Details

#emit_token(piece) ⇒ Object

Write one SSE event carrying a single text delta. Matches OpenAI’s text_completion streaming shape: one choices[].text per event, finish_reason: null until the streamer sends [DONE]. created uses Time.now.to_i (epoch seconds).



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/tep/openai_server.rb', line 321

def emit_token(piece)
  @completion_count = @completion_count + 1
  frame = "{" +
    SpinelKit::Json.encode_pair_str("id", "cmpl-tep") + "," +
    SpinelKit::Json.encode_pair_str("object", "text_completion") + "," +
    SpinelKit::Json.encode_pair_int("created", Time.now.to_i) + "," +
    SpinelKit::Json.encode_pair_str("model", @model) + "," +
    "\"choices\":[{" +
      SpinelKit::Json.encode_pair_int("index", 0) + "," +
      SpinelKit::Json.encode_pair_str("text", piece) + "," +
      "\"finish_reason\":null" +
    "}]" +
  "}"
  @out.write("data: " + frame + "\n\n")
  0
end