Module: LangfuseHelper

Defined in:
lib/langfuse_helper.rb

Instance Method Summary collapse

Instance Method Details

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

Add a score to a trace



137
138
139
140
141
142
143
144
# File 'lib/langfuse_helper.rb', line 137

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

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

Create a span using the current trace context



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/langfuse_helper.rb', line 115

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

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

  span = Langfuse.span(
    name: name,
    trace_id: trace_id,
    parent_observation_id: parent_id,
    input: input,
    **attributes
  )

  LangfuseContext.with_span(span) do
    # Execute the block with the span
    with_span_implementation(span) { yield(span) }
  end
end

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

Create a trace and set it as the current context



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/langfuse_helper.rb', line 102

def with_context_trace(name:, user_id: nil, **attributes)
  trace = Langfuse.trace(
    name: name,
    user_id: user_id,
    **attributes
  )

  LangfuseContext.with_trace(trace) do
    yield(trace)
  end
end

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

Execute a block within the context of an LLM generation



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/langfuse_helper.rb', line 19

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

  start_time = Time.now
  result = nil
  error = nil

  begin
    # Execute the block with the generation passed as argument
    result = yield(generation)
    result
  rescue StandardError => e
    # Capture any error
    error = e
    raise
  ensure
    # Always update the generation with results
    generation.end_time = Time.now.utc
    generation.start_time = start_time.utc

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

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

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

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

Execute a block within the context of a span



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/langfuse_helper.rb', line 5

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

  with_span_implementation(span) { yield(span) }
end

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

Execute a block within the context of a trace



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
92
93
94
95
96
97
98
99
# File 'lib/langfuse_helper.rb', line 65

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

  result = nil
  error = nil

  begin
    # Execute the block with the trace passed as argument
    result = yield(trace)
    result
  rescue StandardError => e
    # Capture any error
    error = e
    raise
  ensure
    # Update trace output if available
    if result && !trace.output
      trace.output = result.is_a?(String) ? result : result

      # Create a new trace event to update the trace
      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)
    Langfuse.flush if error
  end
end