Class: Langfuse::Generation

Inherits:
BaseObservation show all
Defined in:
lib/langfuse/observations.rb

Overview

Observation for LLM calls. Provides methods to set output, usage, and other LLM-specific metadata.

Examples:

Block-based API

Langfuse.observe("chat-completion", as_type: :generation) do |gen|
  gen.model = "gpt-4"
  gen.input = [{ role: "user", content: "Hello" }]
  response = call_llm(gen.input)
  gen.output = response
  gen.usage = { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 }
end

Stateful API

gen = Langfuse.start_observation("chat-completion", {
  model: "gpt-3.5-turbo",
  input: [{ role: "user", content: "Summarize this" }]
}, as_type: :generation)
response = call_llm(gen.input)
gen.update(
  output: response,
  usage_details: { prompt_tokens: 50, completion_tokens: 25, total_tokens: 75 }
)
gen.end

Instance Attribute Summary

Attributes inherited from BaseObservation

#otel_span, #otel_tracer, #type

Instance Method Summary collapse

Methods inherited from BaseObservation

#current_span, #end, #event, #id, #input=, #level=, #metadata=, #output=, #start_observation, #trace_id, #trace_url, #update_trace

Constructor Details

#initialize(otel_span, otel_tracer, attributes: nil) ⇒ Generation

Returns a new instance of Generation.



284
285
286
# File 'lib/langfuse/observations.rb', line 284

def initialize(otel_span, otel_tracer, attributes: nil)
  super(otel_span, otel_tracer, attributes: attributes, type: OBSERVATION_TYPES[:generation])
end

Instance Method Details

#model=(value) ⇒ Object

Parameters:

  • value (String)

    Model name (e.g., “gpt-4”, “claude-3-opus”)



311
312
313
314
315
# File 'lib/langfuse/observations.rb', line 311

def model=(value)
  return unless @otel_span.recording?

  @otel_span.set_attribute("langfuse.observation.model", value.to_s)
end

#model_parameters=(value) ⇒ Object

Parameters:

  • value (Hash)

    Model parameters (temperature, max_tokens, etc.)



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/langfuse/observations.rb', line 318

def model_parameters=(value)
  return unless @otel_span.recording?

  # Convert to Langfuse API format (camelCase keys)
  params_hash = {}
  value.each do |k, v|
    key_str = k.to_s
    # Convert snake_case to camelCase
    camel_key = key_str.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
    params_hash[camel_key] = v
  end
  params_json = params_hash.to_json
  @otel_span.set_attribute("langfuse.observation.modelParameters", params_json)
end

#update(attrs) ⇒ self

Parameters:

Returns:

  • (self)


290
291
292
293
# File 'lib/langfuse/observations.rb', line 290

def update(attrs)
  update_observation_attributes(attrs)
  self
end

#usage=(value) ⇒ Object

Parameters:

  • value (Hash)

    Usage hash with token counts (:prompt_tokens, :completion_tokens, :total_tokens)



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/langfuse/observations.rb', line 296

def usage=(value)
  return unless @otel_span.recording?

  # Convert to Langfuse API format (camelCase keys)
  usage_hash = {
    promptTokens: value[:prompt_tokens] || value["prompt_tokens"],
    completionTokens: value[:completion_tokens] || value["completion_tokens"],
    totalTokens: value[:total_tokens] || value["total_tokens"]
  }.compact

  usage_json = usage_hash.to_json
  @otel_span.set_attribute("langfuse.observation.usage", usage_json)
end