langfuse-rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,353 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langfuse
4
+ # Type definitions and constants for Langfuse domain models
5
+ #
6
+ # Provides observation type constants and attribute classes for type-safe
7
+ # handling of Langfuse observations and traces.
8
+ #
9
+ # @example Using observation types
10
+ # Langfuse::Types::OBSERVATION_TYPES # => ["span", "generation", ...]
11
+ # Langfuse::Types::LEVELS # => ["DEBUG", "DEFAULT", "WARNING", "ERROR"]
12
+ #
13
+ # @example Using attribute classes
14
+ # attrs = Langfuse::Types::SpanAttributes.new(
15
+ # input: { query: "test" },
16
+ # level: "DEFAULT",
17
+ # metadata: { source: "api" }
18
+ # )
19
+ # attrs.to_h # => { input: {...}, level: "DEFAULT", metadata: {...} }
20
+ #
21
+ module Types
22
+ # Types of observations that can be created in Langfuse
23
+ #
24
+ # - `span`: General-purpose observations for tracking operations, functions, or logical units of work
25
+ # - `generation`: Specialized observations for LLM calls with model parameters, usage, and costs
26
+ # - `event`: Point-in-time occurrences or log entries within a trace
27
+ # - `embedding`: Observations for embedding generation calls
28
+ # - `agent`: Observations for agent-based workflows
29
+ # - `tool`: Observations for tool/function calls
30
+ # - `chain`: Observations for chain-based workflows
31
+ # - `retriever`: Observations for retrieval operations
32
+ # - `evaluator`: Observations for evaluation operations
33
+ # - `guardrail`: Observations for guardrail checks
34
+ #
35
+ # @return [Array<String>] Array of observation type strings
36
+ OBSERVATION_TYPES = %w[
37
+ span generation event embedding
38
+ agent tool chain retriever
39
+ evaluator guardrail
40
+ ].freeze
41
+
42
+ # Severity levels for observations in Langfuse
43
+ #
44
+ # Used to categorize the importance or severity of observations:
45
+ # - `DEBUG`: Detailed diagnostic information
46
+ # - `DEFAULT`: Normal operation information
47
+ # - `WARNING`: Potentially problematic situations
48
+ # - `ERROR`: Error conditions that need attention
49
+ #
50
+ # @return [Array<String>] Array of level strings
51
+ LEVELS = %w[DEBUG DEFAULT WARNING ERROR].freeze
52
+
53
+ # Data types for Langfuse scores
54
+ #
55
+ # Used to categorize score values:
56
+ # - `numeric`: Numeric values (Integer, Float, BigDecimal)
57
+ # - `boolean`: Boolean values (true/false or 0/1)
58
+ # - `categorical`: String values for categorical scores
59
+ #
60
+ # @return [Hash<Symbol, String>] Hash mapping symbol keys to API string values
61
+ SCORE_DATA_TYPES = {
62
+ numeric: "NUMERIC",
63
+ boolean: "BOOLEAN",
64
+ categorical: "CATEGORICAL"
65
+ }.freeze
66
+
67
+ # Valid score data type symbols
68
+ #
69
+ # @return [Array<Symbol>] Array of valid data type symbols
70
+ VALID_SCORE_DATA_TYPES = SCORE_DATA_TYPES.keys.freeze
71
+
72
+ # Attributes for Langfuse span observations
73
+ #
74
+ # Spans are used to track operations, functions, or logical units of work.
75
+ # They can contain other spans, generations, or events as children.
76
+ #
77
+ # @example
78
+ # attrs = SpanAttributes.new(
79
+ # input: { query: "SELECT * FROM users" },
80
+ # output: { count: 42 },
81
+ # level: "DEFAULT",
82
+ # metadata: { source: "database" }
83
+ # )
84
+ # attrs.to_h # => { input: {...}, output: {...}, level: "DEFAULT", metadata: {...} }
85
+ #
86
+ class SpanAttributes
87
+ # @return [Object, nil] Input data for the operation being tracked
88
+ attr_accessor :input
89
+
90
+ # @return [Object, nil] Output data from the operation
91
+ attr_accessor :output
92
+
93
+ # @return [Hash, nil] Additional metadata as key-value pairs
94
+ attr_accessor :metadata
95
+
96
+ # @return [String, nil] Severity level of the observation (DEBUG, DEFAULT, WARNING, ERROR)
97
+ attr_accessor :level
98
+
99
+ # @return [String, nil] Human-readable status message
100
+ attr_accessor :status_message
101
+
102
+ # @return [String, nil] Version identifier for the code/model being tracked
103
+ attr_accessor :version
104
+
105
+ # @return [String, nil] Environment where the operation is running (e.g., 'production', 'staging')
106
+ attr_accessor :environment
107
+
108
+ # Initialize a new SpanAttributes instance
109
+ #
110
+ # @param input [Object, nil] Input data for the operation
111
+ # @param output [Object, nil] Output data from the operation
112
+ # @param metadata [Hash, nil] Additional metadata as key-value pairs
113
+ # @param level [String, nil] Severity level (DEBUG, DEFAULT, WARNING, ERROR)
114
+ # @param status_message [String, nil] Human-readable status message
115
+ # @param version [String, nil] Version identifier
116
+ # @param environment [String, nil] Environment identifier
117
+ # rubocop:disable Metrics/ParameterLists
118
+ def initialize(input: nil, output: nil, metadata: nil, level: nil, status_message: nil, version: nil,
119
+ environment: nil)
120
+ # rubocop:enable Metrics/ParameterLists
121
+ @input = input
122
+ @output = output
123
+ @metadata = metadata
124
+ @level = level
125
+ @status_message = status_message
126
+ @version = version
127
+ @environment = environment
128
+ end
129
+
130
+ # Convert attributes to a hash representation
131
+ #
132
+ # Returns a hash with all non-nil attributes. Nil values are excluded.
133
+ #
134
+ # @return [Hash] Hash representation of attributes
135
+ def to_h
136
+ {
137
+ input: @input,
138
+ output: @output,
139
+ metadata: @metadata,
140
+ level: @level,
141
+ status_message: @status_message,
142
+ version: @version,
143
+ environment: @environment
144
+ }.compact
145
+ end
146
+ end
147
+
148
+ # Attributes for Langfuse generation observations
149
+ #
150
+ # Generations are specialized observations for tracking LLM interactions,
151
+ # including model parameters, usage metrics, costs, and prompt information.
152
+ #
153
+ # @example
154
+ # attrs = GenerationAttributes.new(
155
+ # model: "gpt-4",
156
+ # input: { messages: [...] },
157
+ # output: { content: "Hello!" },
158
+ # model_parameters: { temperature: 0.7 },
159
+ # usage_details: { prompt_tokens: 100, completion_tokens: 50 },
160
+ # prompt: { name: "greeting", version: 1, is_fallback: false }
161
+ # )
162
+ #
163
+ class GenerationAttributes < SpanAttributes
164
+ # @return [Time, nil] Timestamp when the model started generating completion
165
+ attr_accessor :completion_start_time
166
+
167
+ # @return [String, nil] Name of the language model used (e.g., 'gpt-4', 'claude-3-opus')
168
+ attr_accessor :model
169
+
170
+ # @return [Hash, nil] Parameters passed to the model (temperature, max_tokens, etc.)
171
+ attr_accessor :model_parameters
172
+
173
+ # @return [Hash, nil] Token usage and other model-specific usage metrics
174
+ attr_accessor :usage_details
175
+
176
+ # @return [Hash, nil] Cost breakdown for the generation (total_cost, etc.)
177
+ attr_accessor :cost_details
178
+
179
+ # @return [Hash, nil] Information about the prompt used from Langfuse prompt management
180
+ # Hash should contain :name (String), :version (Integer), :is_fallback (Boolean)
181
+ attr_accessor :prompt
182
+
183
+ # Initialize a new GenerationAttributes instance
184
+ #
185
+ # @param completion_start_time [Time, nil] Timestamp when completion started
186
+ # @param model [String, nil] Model name
187
+ # @param model_parameters [Hash, nil] Model parameters
188
+ # @param usage_details [Hash, nil] Usage metrics
189
+ # @param cost_details [Hash, nil] Cost breakdown
190
+ # @param prompt [Hash, nil] Prompt information with :name, :version, :is_fallback keys
191
+ # @param kwargs [Hash] Additional keyword arguments passed to SpanAttributes
192
+ # rubocop:disable Metrics/ParameterLists
193
+ def initialize(completion_start_time: nil, model: nil, model_parameters: nil, usage_details: nil,
194
+ cost_details: nil, prompt: nil, **)
195
+ # rubocop:enable Metrics/ParameterLists
196
+ super(**)
197
+ @completion_start_time = completion_start_time
198
+ @model = model
199
+ @model_parameters = model_parameters
200
+ @usage_details = usage_details
201
+ @cost_details = cost_details
202
+ @prompt = prompt
203
+ end
204
+
205
+ # Convert attributes to a hash representation
206
+ #
207
+ # Returns a hash with all non-nil attributes, including both span and generation-specific fields.
208
+ #
209
+ # @return [Hash] Hash representation of attributes
210
+ def to_h
211
+ super.merge(
212
+ completion_start_time: @completion_start_time,
213
+ model: @model,
214
+ model_parameters: @model_parameters,
215
+ usage_details: @usage_details,
216
+ cost_details: @cost_details,
217
+ prompt: @prompt
218
+ ).compact
219
+ end
220
+ end
221
+
222
+ # Attributes for Langfuse embedding observations
223
+ #
224
+ # Embeddings are specialized observations for tracking embedding generation calls.
225
+ # They extend GenerationAttributes to include all generation-specific fields.
226
+ #
227
+ class EmbeddingAttributes < GenerationAttributes
228
+ end
229
+
230
+ # Attributes for Langfuse traces
231
+ #
232
+ # Traces are the top-level containers that group related observations together.
233
+ # They represent a complete workflow, request, or user interaction.
234
+ #
235
+ # @example
236
+ # attrs = TraceAttributes.new(
237
+ # name: "user-request",
238
+ # user_id: "user-123",
239
+ # session_id: "session-456",
240
+ # input: { query: "What is Ruby?" },
241
+ # output: { answer: "Ruby is a programming language" },
242
+ # tags: ["api", "v1"],
243
+ # public: false
244
+ # )
245
+ #
246
+ class TraceAttributes
247
+ # @return [String, nil] Human-readable name for the trace
248
+ attr_accessor :name
249
+
250
+ # @return [String, nil] Identifier for the user associated with this trace
251
+ attr_accessor :user_id
252
+
253
+ # @return [String, nil] Session identifier for grouping related traces
254
+ attr_accessor :session_id
255
+
256
+ # @return [String, nil] Version identifier for the code/application
257
+ attr_accessor :version
258
+
259
+ # @return [String, nil] Release identifier for deployment tracking
260
+ attr_accessor :release
261
+
262
+ # @return [Object, nil] Input data that initiated the trace
263
+ attr_accessor :input
264
+
265
+ # @return [Object, nil] Final output data from the trace
266
+ attr_accessor :output
267
+
268
+ # @return [Hash, nil] Additional metadata for the trace
269
+ attr_accessor :metadata
270
+
271
+ # @return [Array<String>, nil] Tags for categorizing and filtering traces
272
+ attr_accessor :tags
273
+
274
+ # @return [Boolean, nil] Whether this trace should be publicly visible
275
+ attr_accessor :public
276
+
277
+ # @return [String, nil] Environment where the trace was captured
278
+ attr_accessor :environment
279
+
280
+ # Initialize a new TraceAttributes instance
281
+ #
282
+ # @param name [String, nil] Human-readable name for the trace
283
+ # @param user_id [String, nil] User identifier
284
+ # @param session_id [String, nil] Session identifier
285
+ # @param version [String, nil] Version identifier
286
+ # @param release [String, nil] Release identifier
287
+ # @param input [Object, nil] Input data
288
+ # @param output [Object, nil] Output data
289
+ # @param metadata [Hash, nil] Additional metadata
290
+ # @param tags [Array<String>, nil] Tags array
291
+ # @param public [Boolean, nil] Public visibility flag
292
+ # @param environment [String, nil] Environment identifier
293
+ # rubocop:disable Metrics/ParameterLists
294
+ def initialize(name: nil, user_id: nil, session_id: nil, version: nil, release: nil, input: nil, output: nil,
295
+ metadata: nil, tags: nil, public: nil, environment: nil)
296
+ # rubocop:enable Metrics/ParameterLists
297
+ @name = name
298
+ @user_id = user_id
299
+ @session_id = session_id
300
+ @version = version
301
+ @release = release
302
+ @input = input
303
+ @output = output
304
+ @metadata = metadata
305
+ @tags = tags
306
+ @public = public
307
+ @environment = environment
308
+ end
309
+
310
+ # Convert attributes to a hash representation
311
+ #
312
+ # Returns a hash with all non-nil attributes. Nil values are excluded.
313
+ #
314
+ # @return [Hash] Hash representation of attributes
315
+ def to_h
316
+ {
317
+ name: @name,
318
+ user_id: @user_id,
319
+ session_id: @session_id,
320
+ version: @version,
321
+ release: @release,
322
+ input: @input,
323
+ output: @output,
324
+ metadata: @metadata,
325
+ tags: @tags,
326
+ public: @public,
327
+ environment: @environment
328
+ }.compact
329
+ end
330
+ end
331
+
332
+ # Alias for event observation attributes (same as SpanAttributes)
333
+ EventAttributes = SpanAttributes
334
+
335
+ # Alias for agent observation attributes (same as SpanAttributes)
336
+ AgentAttributes = SpanAttributes
337
+
338
+ # Alias for tool observation attributes (same as SpanAttributes)
339
+ ToolAttributes = SpanAttributes
340
+
341
+ # Alias for chain observation attributes (same as SpanAttributes)
342
+ ChainAttributes = SpanAttributes
343
+
344
+ # Alias for retriever observation attributes (same as SpanAttributes)
345
+ RetrieverAttributes = SpanAttributes
346
+
347
+ # Alias for evaluator observation attributes (same as SpanAttributes)
348
+ EvaluatorAttributes = SpanAttributes
349
+
350
+ # Alias for guardrail observation attributes (same as SpanAttributes)
351
+ GuardrailAttributes = SpanAttributes
352
+ end
353
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Langfuse
4
+ VERSION = "0.1.0"
5
+ end