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=, #score_trace, #start_observation, #trace_id, #trace_url, #update_trace

Constructor Details

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

Returns a new instance of Generation.

Parameters:

  • otel_span (OpenTelemetry::SDK::Trace::Span)

    The underlying OTel span

  • otel_tracer (OpenTelemetry::SDK::Trace::Tracer)

    The OTel tracer

  • attributes (Hash, Types::GenerationAttributes, nil) (defaults to: nil)

    Optional initial attributes



321
322
323
# File 'lib/langfuse/observations.rb', line 321

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) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)

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



350
351
352
353
354
# File 'lib/langfuse/observations.rb', line 350

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

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

#model_parameters=(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (Hash)

    Model parameters (temperature, max_tokens, etc.)



358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/langfuse/observations.rb', line 358

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)


327
328
329
330
# File 'lib/langfuse/observations.rb', line 327

def update(attrs)
  update_observation_attributes(attrs)
  self
end

#usage=(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (Hash)

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



334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/langfuse/observations.rb', line 334

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