Class: Langfuse::Generation

Inherits:
BaseObservation show all
Includes:
ModelSetters
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_details = { 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 included from ModelSetters

#model=, #model_parameters=, #usage=, #usage_details=

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



359
360
361
# File 'lib/langfuse/observations.rb', line 359

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

Instance Method Details

#cost_details=(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (Hash)

    Cost details hash (prefer :input, :output, :total for aggregate Langfuse cost metrics)



372
373
374
# File 'lib/langfuse/observations.rb', line 372

def cost_details=(value)
  update_observation_attributes(cost_details: value)
end

#update(attrs) ⇒ self

Parameters:

Returns:

  • (self)


365
366
367
368
# File 'lib/langfuse/observations.rb', line 365

def update(attrs)
  update_observation_attributes(attrs)
  self
end