Class: Langfuse::Types::GenerationAttributes

Inherits:
SpanAttributes show all
Defined in:
lib/langfuse/types.rb

Overview

Attributes for Langfuse generation observations

Generations are specialized observations for tracking LLM interactions, including model parameters, usage metrics, costs, and prompt information.

Examples:

attrs = GenerationAttributes.new(
  model: "gpt-4",
  input: { messages: [...] },
  output: { content: "Hello!" },
  model_parameters: { temperature: 0.7 },
  usage_details: { prompt_tokens: 100, completion_tokens: 50 },
  prompt: { name: "greeting", version: 1, is_fallback: false }
)

Direct Known Subclasses

EmbeddingAttributes

Instance Attribute Summary collapse

Attributes inherited from SpanAttributes

#environment, #input, #level, #metadata, #output, #status_message, #version

Instance Method Summary collapse

Constructor Details

#initialize(completion_start_time: nil, model: nil, model_parameters: nil, usage_details: nil, cost_details: nil, prompt: nil) ⇒ GenerationAttributes

Initialize a new GenerationAttributes instance

rubocop:disable Metrics/ParameterLists

Parameters:

  • completion_start_time (Time, nil) (defaults to: nil)

    Timestamp when completion started

  • model (String, nil) (defaults to: nil)

    Model name

  • model_parameters (Hash, nil) (defaults to: nil)

    Model parameters

  • usage_details (Hash, nil) (defaults to: nil)

    Usage metrics

  • cost_details (Hash, nil) (defaults to: nil)

    Cost breakdown (prefer :input, :output, :total)

  • prompt (Hash, nil) (defaults to: nil)

    Prompt information with :name, :version, :is_fallback keys

  • kwargs (Hash)

    Additional keyword arguments passed to SpanAttributes



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/langfuse/types.rb', line 193

def initialize(completion_start_time: nil, model: nil, model_parameters: nil, usage_details: nil,
               cost_details: nil, prompt: nil, **)
  # rubocop:enable Metrics/ParameterLists
  super(**)
  @completion_start_time = completion_start_time
  @model = model
  @model_parameters = model_parameters
  @usage_details = usage_details
  @cost_details = cost_details
  @prompt = prompt
end

Instance Attribute Details

#completion_start_timeTime?

Returns Timestamp when the model started generating completion.

Returns:

  • (Time, nil)

    Timestamp when the model started generating completion



165
166
167
# File 'lib/langfuse/types.rb', line 165

def completion_start_time
  @completion_start_time
end

#cost_detailsHash?

Returns Cost breakdown for the generation (prefer :input, :output, :total).

Returns:

  • (Hash, nil)

    Cost breakdown for the generation (prefer :input, :output, :total)



177
178
179
# File 'lib/langfuse/types.rb', line 177

def cost_details
  @cost_details
end

#modelString?

Returns Name of the language model used (e.g., ‘gpt-4’, ‘claude-3-opus’).

Returns:

  • (String, nil)

    Name of the language model used (e.g., ‘gpt-4’, ‘claude-3-opus’)



168
169
170
# File 'lib/langfuse/types.rb', line 168

def model
  @model
end

#model_parametersHash?

Returns Parameters passed to the model (temperature, max_tokens, etc.).

Returns:

  • (Hash, nil)

    Parameters passed to the model (temperature, max_tokens, etc.)



171
172
173
# File 'lib/langfuse/types.rb', line 171

def model_parameters
  @model_parameters
end

#promptHash?

Returns Information about the prompt used from Langfuse prompt management Hash should contain :name (String), :version (Integer), :is_fallback (Boolean).

Returns:

  • (Hash, nil)

    Information about the prompt used from Langfuse prompt management Hash should contain :name (String), :version (Integer), :is_fallback (Boolean)



181
182
183
# File 'lib/langfuse/types.rb', line 181

def prompt
  @prompt
end

#usage_detailsHash?

Returns Token usage and other model-specific usage metrics.

Returns:

  • (Hash, nil)

    Token usage and other model-specific usage metrics



174
175
176
# File 'lib/langfuse/types.rb', line 174

def usage_details
  @usage_details
end

Instance Method Details

#to_hHash

Convert attributes to a hash representation

Returns a hash with all non-nil attributes, including both span and generation-specific fields.

Returns:

  • (Hash)

    Hash representation of attributes



210
211
212
213
214
215
216
217
218
219
# File 'lib/langfuse/types.rb', line 210

def to_h
  super.merge(
    completion_start_time: @completion_start_time,
    model: @model,
    model_parameters: @model_parameters,
    usage_details: @usage_details,
    cost_details: @cost_details,
    prompt: @prompt
  ).compact
end