Class: Tep::Llm::OpenAI::ChatCompletionsStreamer

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

Overview

Runs one streaming chat completion. Subclass of Tep::Streamer. Drives backend.chat_completion_stream through ChatStreamSink, writes the terminating data:, then emits the toy/v1 serving event (kind:eval, phase:serve, name:request) with sink.completion_count (mirrors CompletionsStreamer’s #128 shape).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChatCompletionsStreamer

Returns a new instance of ChatCompletionsStreamer.



443
444
445
446
447
448
449
450
# File 'lib/tep/openai_server.rb', line 443

def initialize
  @req_ref       = Tep::Request.new
  @model         = ""
  @prompt_tokens = 0
  @t0            = 0
  @request_id    = ""
  @principal_id  = ""
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



440
441
442
# File 'lib/tep/openai_server.rb', line 440

def model
  @model
end

#principal_idObject

Returns the value of attribute principal_id.



441
442
443
# File 'lib/tep/openai_server.rb', line 441

def principal_id
  @principal_id
end

#prompt_tokensObject

Returns the value of attribute prompt_tokens.



440
441
442
# File 'lib/tep/openai_server.rb', line 440

def prompt_tokens
  @prompt_tokens
end

#req_refObject

Returns the value of attribute req_ref.



440
441
442
# File 'lib/tep/openai_server.rb', line 440

def req_ref
  @req_ref
end

#request_idObject

Returns the value of attribute request_id.



441
442
443
# File 'lib/tep/openai_server.rb', line 441

def request_id
  @request_id
end

#t0Object

Returns the value of attribute t0.



441
442
443
# File 'lib/tep/openai_server.rb', line 441

def t0
  @t0
end

Instance Method Details

#pump(out) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/tep/openai_server.rb', line 452

def pump(out)
  sink = Tep::Llm::OpenAI::ChatStreamSink.new
  sink.out   = out
  sink.model = @model
  sink.emit_role_prelude("assistant")
  Tep::APP.openai_backend.chat_completion_stream(@req_ref, sink)
  sink.emit_finish("stop")
  out.write("data: [DONE]\n\n")
  wall_us = (Time.now.to_i - @t0) * 1_000_000
  extra = "{" +
    Tep::Json.encode_pair_str("request_id", @request_id) + "," +
    Tep::Json.encode_pair_str("principal_id", @principal_id) +
  "}"
  Tep::APP.openai_events.inference(
    @model, @prompt_tokens, sink.completion_count, wall_us, extra)
  0
end