Module: LangfuseHelper

Extended by:
T::Sig
Defined in:
lib/langfuse_helper.rb

Instance Method Summary collapse

Instance Method Details

#score_trace(trace_id:, name:, value:, comment: nil) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/langfuse_helper.rb', line 203

def score_trace(trace_id:, name:, value:, comment: nil)
  T.unsafe(Langfuse).score(
    trace_id: trace_id,
    name: name,
    value: value,
    comment: comment
  )
end

#with_context_span(name:, input: nil, **attributes, &block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/langfuse_helper.rb', line 172

def with_context_span(name:, input: nil, **attributes, &block)
  # Get trace_id from context
  trace_id = LangfuseContext.current_trace_id
  parent_id = LangfuseContext.current_span_id

  # Use Kernel.raise
  Kernel.raise 'No trace context found. Make sure to call within with_context_trace' if trace_id.nil?

  span = T.unsafe(Langfuse).span(
    name: name,
    trace_id: T.must(trace_id), # Must be present due to check above
    parent_observation_id: parent_id,
    input: input,
    **attributes
  )

  LangfuseContext.with_span(span) do
    # Pass the block to the implementation
    with_span_implementation(span, &block)
  end
end

#with_context_trace(name:, user_id: nil, **attributes, &block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/langfuse_helper.rb', line 151

def with_context_trace(name:, user_id: nil, **attributes, &block)
  trace = T.unsafe(Langfuse).trace(
    name: name,
    user_id: user_id,
    **attributes
  )

  LangfuseContext.with_trace(trace) do
    block.call(trace)
  end
end

#with_generation(name:, trace_id:, model:, input:, parent_id: nil, model_parameters: {}, **attributes, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/langfuse_helper.rb', line 47

def with_generation(name:, trace_id:, model:, input:, parent_id: nil, model_parameters: {}, **attributes, &block)
  # Create the generation
  generation = T.unsafe(Langfuse).generation(
    name: name,
    trace_id: trace_id,
    parent_observation_id: parent_id,
    model: model,
    input: input,
    model_parameters: model_parameters,
    **attributes
  )

  T.let(Time.now, Time)
  result = T.let(nil, T.untyped)
  error = T.let(nil, T.nilable(StandardError))

  begin
    # Execute the block with the generation passed as argument
    result = block.call(generation)
    result
  rescue StandardError => e
    # Capture any error
    error = e
    Kernel.raise # Use Kernel.raise
  ensure
    # Always update the generation with results
    generation.end_time = Time.now.utc
    # generation.start_time = start_time.utc # start_time is already UTC if using Time.now.utc

    # Add output if there was a result and it wasn't already set
    generation.output = result if result && generation.output.nil?

    # Add error information if there was an error
    if error
      generation.level = 'ERROR'
      generation.status_message = error.message
      generation. ||= {}
      backtrace = error.backtrace
      generation.[:error_backtrace] = backtrace.first(10) if backtrace # Check if backtrace is nil
    end

    # Update the generation
    T.unsafe(Langfuse).update_generation(generation)
  end
end

#with_span(name:, trace_id:, parent_id: nil, input: nil, **attributes, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/langfuse_helper.rb', line 20

def with_span(name:, trace_id:, parent_id: nil, input: nil, **attributes, &block)
  # Create the span
  span = T.unsafe(Langfuse).span(
    name: name,
    trace_id: trace_id,
    parent_observation_id: parent_id,
    input: input,
    **attributes
  )

  # Pass the block to the implementation
  with_span_implementation(span, &block)
end

#with_trace(name:, user_id: nil, **attributes, &_block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/langfuse_helper.rb', line 102

def with_trace(name:, user_id: nil, **attributes, &_block)
  # Create the trace
  trace = T.unsafe(Langfuse).trace(
    name: name,
    user_id: user_id,
    **attributes
  )

  result = T.let(nil, T.untyped)
  error = T.let(nil, T.nilable(StandardError))

  begin
    # Execute the block with the trace passed as argument
    result = yield(trace)
    result
  rescue StandardError => e
    # Capture any error
    error = e
    Kernel.raise # Use Kernel.raise
  ensure
    # Update trace output if available
    if result && trace.output.nil?
      # Assuming trace.output is writable and can be inferred or is T.untyped
      T.unsafe(trace).output = result # Use T.unsafe if Trace model type isn't fully defined

      # Create a new trace event to update the trace - Reuse trace object
      # This seems incorrect, updating should likely use an update method or modify the object
      # directly if it's mutable and the original trace object is used later.
      # Re-creating a trace event just to update seems wrong. Commenting out for now.
      # Langfuse.trace(
      #   id: trace.id,
      #   output: trace.output
      # )
    end

    # Ensure all events are sent (only in case of error, otherwise let the automatic flushing handle it)
    T.unsafe(Langfuse).flush if error
  end
end