Class: Tracekit::LLM::OpenAIInstrumentation::OpenAIStreamAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/tracekit/llm/openai_instrumentation.rb

Overview

Accumulates streaming chunk data for span attributes via proc interception

Instance Method Summary collapse

Constructor Details

#initialize(span, capture_content) ⇒ OpenAIStreamAccumulator

Returns a new instance of OpenAIStreamAccumulator.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tracekit/llm/openai_instrumentation.rb', line 133

def initialize(span, capture_content)
  @span = span
  @capture = capture_content
  @model = nil
  @id = nil
  @finish_reason = nil
  @input_tokens = nil
  @output_tokens = nil
  @output_chunks = []
  @tool_calls = {}
end

Instance Method Details

#finalizeObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tracekit/llm/openai_instrumentation.rb', line 176

def finalize
  Common.set_response_attributes(@span,
    model: @model,
    id: @id,
    finish_reasons: @finish_reason ? [@finish_reason] : nil,
    input_tokens: @input_tokens,
    output_tokens: @output_tokens
  )

  @tool_calls.each_value do |tc|
    Common.record_tool_call(@span, **tc)
  end

  if @capture && @output_chunks.any?
    full_content = @output_chunks.join
    Common.capture_output_messages(@span, [{ "role" => "assistant", "content" => full_content }])
  end
rescue => _e
  # Never break user code
ensure
  @span.finish
end

#process_chunk(chunk) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/tracekit/llm/openai_instrumentation.rb', line 145

def process_chunk(chunk)
  @model ||= chunk.dig("model")
  @id ||= chunk.dig("id")

  if (usage = chunk["usage"])
    @input_tokens = usage["prompt_tokens"] if usage["prompt_tokens"]
    @output_tokens = usage["completion_tokens"] if usage["completion_tokens"]
  end

  (chunk["choices"] || []).each do |choice|
    @finish_reason = choice["finish_reason"] if choice["finish_reason"]
    delta = choice["delta"] || {}
    @output_chunks << delta["content"] if @capture && delta["content"]

    (delta["tool_calls"] || []).each do |tc|
      idx = tc["index"] || 0
      if @tool_calls[idx]
        @tool_calls[idx][:arguments] = (@tool_calls[idx][:arguments] || "") + (tc.dig("function", "arguments") || "")
      else
        @tool_calls[idx] = {
          name: tc.dig("function", "name") || "unknown",
          id: tc["id"],
          arguments: tc.dig("function", "arguments") || ""
        }
      end
    end
  end
rescue => _e
  # Never fail on chunk processing
end