Module: Braintrust::Contrib::OpenAI::Instrumentation::Chat::InternalStream::InstanceMethods

Defined in:
lib/braintrust/contrib/openai/instrumentation/chat.rb

Instance Method Summary collapse

Instance Method Details

#each(&block) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/braintrust/contrib/openai/instrumentation/chat.rb', line 239

def each(&block)
  ctx = Braintrust::Contrib::Context.from(self)
  # Only trace if context present and is for chat completions (not other endpoints)
  return super unless ctx&.[](:tracer) && !ctx[:consumed] && ctx[:stream_type] == :raw

  ctx[:consumed] = true

  tracer = ctx[:tracer]
  params = ctx[:params]
   = ctx[:metadata]
  completions_instance = ctx[:completions_instance]
  aggregated_chunks = []
  start_time = Braintrust::Internal::Time.measure
  time_to_first_token = nil

  tracer.in_span("Chat Completion") do |span|
    completions_instance.send(:set_input, span, params)
    Support::OTel.set_json_attr(span, "braintrust.metadata", )

    super do |chunk|
      time_to_first_token ||= Braintrust::Internal::Time.measure(start_time)
      aggregated_chunks << chunk.to_h
      block&.call(chunk)
    end

    finalize_stream_span(span, aggregated_chunks, time_to_first_token, )
  end
end