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



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/langfuse/types.rb', line 207

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



179
180
181
# File 'lib/langfuse/types.rb', line 179

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)



191
192
193
# File 'lib/langfuse/types.rb', line 191

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')



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

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.)



185
186
187
# File 'lib/langfuse/types.rb', line 185

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)



195
196
197
# File 'lib/langfuse/types.rb', line 195

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



188
189
190
# File 'lib/langfuse/types.rb', line 188

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



224
225
226
227
228
229
230
231
232
233
# File 'lib/langfuse/types.rb', line 224

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