Class: Langfuse::Generation
- Inherits:
-
BaseObservation
- Object
- BaseObservation
- Langfuse::Generation
- Defined in:
- lib/langfuse/observations.rb
Overview
Observation for LLM calls. Provides methods to set output, usage, and other LLM-specific metadata.
Instance Attribute Summary
Attributes inherited from BaseObservation
#otel_span, #otel_tracer, #type
Instance Method Summary collapse
-
#initialize(otel_span, otel_tracer, attributes: nil) ⇒ Generation
constructor
A new instance of Generation.
- #model=(value) ⇒ void
- #model_parameters=(value) ⇒ void
- #update(attrs) ⇒ self
- #usage=(value) ⇒ void
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.
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.
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.
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
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.
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 |