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

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

Instance Method Summary collapse

Instance Method Details

#create(**params) ⇒ Object

Wrap create method for non-streaming completions



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/braintrust/contrib/openai/instrumentation/chat.rb', line 36

def create(**params)
  client = instance_variable_get(:@client)
  tracer = Braintrust::Contrib.tracer_for(client)

  tracer.in_span("Chat Completion") do |span|
     = (params)

    set_input(span, params)

    response = nil
    time_to_first_token = Braintrust::Internal::Time.measure do
      response = super
    end

    set_output(span, response)
    set_metrics(span, response, time_to_first_token)
    (span, , response)

    response
  end
end

#stream(**params) ⇒ Object

Wrap stream for streaming chat completions (returns ChatCompletionStream) Stores context on stream object for span creation during consumption



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/braintrust/contrib/openai/instrumentation/chat.rb', line 77

def stream(**params)
  client = instance_variable_get(:@client)
  tracer = Braintrust::Contrib.tracer_for(client)
   = (params, stream: true)

  stream_obj = super
  Braintrust::Contrib::Context.set!(stream_obj,
    tracer: tracer,
    params: params,
    metadata: ,
    completions_instance: self,
    stream_type: :chat_completion)
  stream_obj
end

#stream_raw(**params) ⇒ Object

Wrap stream_raw for streaming chat completions (returns Internal::Stream) Stores context on stream object for span creation during consumption



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/braintrust/contrib/openai/instrumentation/chat.rb', line 60

def stream_raw(**params)
  client = instance_variable_get(:@client)
  tracer = Braintrust::Contrib.tracer_for(client)
   = (params, stream: true)

  stream_obj = super
  Braintrust::Contrib::Context.set!(stream_obj,
    tracer: tracer,
    params: params,
    metadata: ,
    completions_instance: self,
    stream_type: :raw)
  stream_obj
end