@langfuse/tracing 4.0.0-beta.6 → 4.0.0-beta.8
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.
- package/dist/index.cjs +325 -236
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1408 -427
- package/dist/index.d.ts +1408 -427
- package/dist/index.mjs +314 -227
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export { LangfuseOtelSpanAttributes } from '@langfuse/core';
|
|
|
12
12
|
*
|
|
13
13
|
* @public
|
|
14
14
|
*/
|
|
15
|
-
type LangfuseObservationType = "span" | "generation" | "event";
|
|
15
|
+
type LangfuseObservationType = "span" | "generation" | "event" | "embedding" | "agent" | "tool" | "chain" | "retriever" | "evaluator" | "guardrail";
|
|
16
16
|
/**
|
|
17
17
|
* Severity levels for observations in Langfuse.
|
|
18
18
|
*
|
|
@@ -49,15 +49,6 @@ type LangfuseSpanAttributes = {
|
|
|
49
49
|
/** Environment where the operation is running (e.g., 'production', 'staging') */
|
|
50
50
|
environment?: string;
|
|
51
51
|
};
|
|
52
|
-
/**
|
|
53
|
-
* Attributes for Langfuse event observations.
|
|
54
|
-
*
|
|
55
|
-
* Events represent point-in-time occurrences or log entries within a trace.
|
|
56
|
-
* Unlike spans, they don't have duration and are automatically ended when created.
|
|
57
|
-
*
|
|
58
|
-
* @public
|
|
59
|
-
*/
|
|
60
|
-
type LangfuseEventAttributes = LangfuseSpanAttributes;
|
|
61
52
|
/**
|
|
62
53
|
* Attributes for Langfuse generation observations.
|
|
63
54
|
*
|
|
@@ -70,10 +61,10 @@ type LangfuseGenerationAttributes = LangfuseSpanAttributes & {
|
|
|
70
61
|
/** Timestamp when the model started generating completion */
|
|
71
62
|
completionStartTime?: Date;
|
|
72
63
|
/** Name of the language model used (e.g., 'gpt-4', 'claude-3') */
|
|
73
|
-
model?: string
|
|
64
|
+
model?: string;
|
|
74
65
|
/** Parameters passed to the model (temperature, max_tokens, etc.) */
|
|
75
66
|
modelParameters?: {
|
|
76
|
-
[key: string]: string | number
|
|
67
|
+
[key: string]: string | number;
|
|
77
68
|
};
|
|
78
69
|
/** Token usage and other model-specific usage metrics */
|
|
79
70
|
usageDetails?: {
|
|
@@ -93,6 +84,14 @@ type LangfuseGenerationAttributes = LangfuseSpanAttributes & {
|
|
|
93
84
|
isFallback: boolean;
|
|
94
85
|
};
|
|
95
86
|
};
|
|
87
|
+
type LangfuseEventAttributes = LangfuseSpanAttributes;
|
|
88
|
+
type LangfuseAgentAttributes = LangfuseSpanAttributes;
|
|
89
|
+
type LangfuseToolAttributes = LangfuseSpanAttributes;
|
|
90
|
+
type LangfuseChainAttributes = LangfuseSpanAttributes;
|
|
91
|
+
type LangfuseRetrieverAttributes = LangfuseSpanAttributes;
|
|
92
|
+
type LangfuseEvaluatorAttributes = LangfuseSpanAttributes;
|
|
93
|
+
type LangfuseGuardrailAttributes = LangfuseSpanAttributes;
|
|
94
|
+
type LangfuseEmbeddingAttributes = LangfuseGenerationAttributes;
|
|
96
95
|
/**
|
|
97
96
|
* Union type representing any Langfuse observation attributes.
|
|
98
97
|
*
|
|
@@ -100,7 +99,7 @@ type LangfuseGenerationAttributes = LangfuseSpanAttributes & {
|
|
|
100
99
|
*
|
|
101
100
|
* @public
|
|
102
101
|
*/
|
|
103
|
-
type
|
|
102
|
+
type LangfuseObservationAttributes = LangfuseSpanAttributes & LangfuseGenerationAttributes & LangfuseEventAttributes & LangfuseAgentAttributes & LangfuseToolAttributes & LangfuseChainAttributes & LangfuseRetrieverAttributes & LangfuseEvaluatorAttributes & LangfuseGuardrailAttributes;
|
|
104
103
|
/**
|
|
105
104
|
* Attributes for Langfuse traces.
|
|
106
105
|
*
|
|
@@ -137,38 +136,121 @@ type LangfuseTraceAttributes = {
|
|
|
137
136
|
/**
|
|
138
137
|
* Union type representing any Langfuse observation wrapper.
|
|
139
138
|
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
139
|
+
* This type encompasses all observation types supported by Langfuse, providing
|
|
140
|
+
* a unified interface for handling different kinds of traced operations. It's
|
|
141
|
+
* particularly useful for generic functions that work with any observation type.
|
|
142
|
+
*
|
|
143
|
+
* ## Included Types
|
|
144
|
+
* - **LangfuseSpan**: General-purpose operations and workflows
|
|
145
|
+
* - **LangfuseGeneration**: LLM calls and AI model interactions
|
|
146
|
+
* - **LangfuseEmbedding**: Text embedding and vector operations
|
|
147
|
+
* - **LangfuseAgent**: AI agent workflows with tool usage
|
|
148
|
+
* - **LangfuseTool**: Individual tool calls and API requests
|
|
149
|
+
* - **LangfuseChain**: Multi-step processes and pipelines
|
|
150
|
+
* - **LangfuseRetriever**: Document retrieval and search operations
|
|
151
|
+
* - **LangfuseEvaluator**: Quality assessment and scoring
|
|
152
|
+
* - **LangfuseGuardrail**: Safety checks and content filtering
|
|
153
|
+
* - **LangfuseEvent**: Point-in-time occurrences and log entries
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* // Function accepting any observation type
|
|
158
|
+
* function logObservation(obs: LangfuseObservation) {
|
|
159
|
+
* console.log(`Observation ${obs.id} in trace ${obs.traceId}`);
|
|
160
|
+
*
|
|
161
|
+
* // All observations have common methods
|
|
162
|
+
* obs.updateTrace({ tags: ['logged'] });
|
|
163
|
+
* obs.end();
|
|
164
|
+
* }
|
|
165
|
+
*
|
|
166
|
+
* // Works with any observation type
|
|
167
|
+
* const span = startObservation('test-span');
|
|
168
|
+
* const generation = startObservation('llm-call', {}, { asType: 'generation' });
|
|
169
|
+
* const agent = startObservation('ai-agent', {}, { asType: 'agent' });
|
|
170
|
+
*
|
|
171
|
+
* logObservation(span);
|
|
172
|
+
* logObservation(generation);
|
|
173
|
+
* logObservation(agent);
|
|
174
|
+
* ```
|
|
142
175
|
*
|
|
143
176
|
* @public
|
|
144
177
|
*/
|
|
145
|
-
type LangfuseObservation = LangfuseSpan | LangfuseGeneration | LangfuseEvent;
|
|
178
|
+
type LangfuseObservation = LangfuseSpan | LangfuseGeneration | LangfuseEvent | LangfuseAgent | LangfuseTool | LangfuseChain | LangfuseRetriever | LangfuseEvaluator | LangfuseGuardrail | LangfuseEmbedding;
|
|
146
179
|
/**
|
|
147
|
-
* Parameters for creating a Langfuse
|
|
180
|
+
* Parameters for creating a Langfuse observation wrapper.
|
|
148
181
|
*
|
|
149
182
|
* @internal
|
|
150
183
|
*/
|
|
151
|
-
type
|
|
184
|
+
type LangfuseObservationParams = {
|
|
152
185
|
otelSpan: Span;
|
|
186
|
+
type: LangfuseObservationType;
|
|
153
187
|
attributes?: LangfuseSpanAttributes | LangfuseGenerationAttributes | LangfuseEventAttributes;
|
|
154
188
|
};
|
|
155
189
|
/**
|
|
156
|
-
* Base class for all Langfuse observation wrappers.
|
|
190
|
+
* Base class for all Langfuse observation wrappers providing unified functionality.
|
|
191
|
+
*
|
|
192
|
+
* This abstract class serves as the foundation for all observation types in Langfuse,
|
|
193
|
+
* encapsulating common operations and properties shared across spans, generations,
|
|
194
|
+
* events, and specialized observation types like agents, tools, and chains.
|
|
195
|
+
*
|
|
196
|
+
* ## Core Capabilities
|
|
197
|
+
* - **OpenTelemetry Integration**: Wraps OTEL spans with Langfuse-specific functionality
|
|
198
|
+
* - **Unique Identification**: Provides span ID and trace ID for correlation
|
|
199
|
+
* - **Lifecycle Management**: Handles observation creation, updates, and completion
|
|
200
|
+
* - **Trace Context**: Enables updating trace-level attributes from any observation
|
|
201
|
+
* - **Hierarchical Structure**: Supports creating nested child observations
|
|
202
|
+
* - **Type Safety**: Ensures type-safe attribute handling based on observation type
|
|
203
|
+
*
|
|
204
|
+
* ## Common Properties
|
|
205
|
+
* - `id`: Unique identifier for this observation (OpenTelemetry span ID)
|
|
206
|
+
* - `traceId`: Identifier of the parent trace containing this observation
|
|
207
|
+
* - `otelSpan`: Direct access to the underlying OpenTelemetry span
|
|
208
|
+
* - `type`: The observation type (span, generation, event, etc.)
|
|
209
|
+
*
|
|
210
|
+
* ## Common Methods
|
|
211
|
+
* - `end()`: Marks the observation as complete with optional timestamp
|
|
212
|
+
* - `updateTrace()`: Sets trace-level attributes like user ID, session ID, tags
|
|
213
|
+
* - `startObservation()`: Creates child observations with inherited context
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* // All observation types share these common capabilities
|
|
218
|
+
* const observation: LangfuseObservation = startObservation('my-operation');
|
|
219
|
+
*
|
|
220
|
+
* // Common properties available on all observations
|
|
221
|
+
* console.log(`Observation ID: ${observation.id}`);
|
|
222
|
+
* console.log(`Trace ID: ${observation.traceId}`);
|
|
223
|
+
* console.log(`Type: ${observation.type}`);
|
|
224
|
+
*
|
|
225
|
+
* // Common methods available on all observations
|
|
226
|
+
* observation.updateTrace({
|
|
227
|
+
* userId: 'user-123',
|
|
228
|
+
* sessionId: 'session-456',
|
|
229
|
+
* tags: ['production', 'api-v2']
|
|
230
|
+
* });
|
|
231
|
+
*
|
|
232
|
+
* // Create child observations
|
|
233
|
+
* const child = observation.startObservation('child-operation', {
|
|
234
|
+
* input: { step: 'processing' }
|
|
235
|
+
* });
|
|
157
236
|
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
237
|
+
* // End observations
|
|
238
|
+
* child.end();
|
|
239
|
+
* observation.end();
|
|
240
|
+
* ```
|
|
161
241
|
*
|
|
162
242
|
* @internal
|
|
163
243
|
*/
|
|
164
|
-
declare abstract class
|
|
244
|
+
declare abstract class LangfuseBaseObservation {
|
|
165
245
|
/** The underlying OpenTelemetry span */
|
|
166
246
|
readonly otelSpan: Span;
|
|
247
|
+
/** The underlying OpenTelemetry span */
|
|
248
|
+
readonly type: LangfuseObservationType;
|
|
167
249
|
/** The span ID from the OpenTelemetry span context */
|
|
168
250
|
id: string;
|
|
169
251
|
/** The trace ID from the OpenTelemetry span context */
|
|
170
252
|
traceId: string;
|
|
171
|
-
constructor(params:
|
|
253
|
+
constructor(params: LangfuseObservationParams);
|
|
172
254
|
/** Gets the Langfuse OpenTelemetry tracer instance */
|
|
173
255
|
protected get tracer(): _opentelemetry_api.Tracer;
|
|
174
256
|
/**
|
|
@@ -177,6 +259,7 @@ declare abstract class LangfuseSpanWrapper {
|
|
|
177
259
|
* @param endTime - Optional end time, defaults to current time
|
|
178
260
|
*/
|
|
179
261
|
end(endTime?: TimeInput): void;
|
|
262
|
+
updateOtelSpanAttributes(attributes: LangfuseObservationAttributes): void;
|
|
180
263
|
/**
|
|
181
264
|
* Updates the parent trace with new attributes.
|
|
182
265
|
*
|
|
@@ -187,26 +270,289 @@ declare abstract class LangfuseSpanWrapper {
|
|
|
187
270
|
* @returns This observation for method chaining
|
|
188
271
|
*/
|
|
189
272
|
updateTrace(attributes: LangfuseTraceAttributes): this;
|
|
273
|
+
/**
|
|
274
|
+
* Creates a new child observation within this observation's context with full type safety.
|
|
275
|
+
*
|
|
276
|
+
* This method enables hierarchical tracing by creating child observations that inherit
|
|
277
|
+
* the parent's trace context. It supports all observation types with automatic TypeScript
|
|
278
|
+
* type inference based on the `asType` parameter, ensuring compile-time safety for
|
|
279
|
+
* attributes and return types.
|
|
280
|
+
*
|
|
281
|
+
* ## Hierarchy & Context
|
|
282
|
+
* - Child observations automatically inherit the parent's trace ID and span context
|
|
283
|
+
* - Creates proper parent-child relationships in the trace structure
|
|
284
|
+
* - Enables distributed tracing across nested operations
|
|
285
|
+
* - Maintains correlation between related operations
|
|
286
|
+
*
|
|
287
|
+
* ## Type Safety
|
|
288
|
+
* - Return type is automatically inferred from `asType` parameter
|
|
289
|
+
* - Attributes parameter is type-checked based on observation type
|
|
290
|
+
* - Compile-time validation prevents type mismatches
|
|
291
|
+
* - Full IntelliSense support for observation-specific attributes
|
|
292
|
+
*
|
|
293
|
+
* @param name - Descriptive name for the child observation
|
|
294
|
+
* @param attributes - Type-specific attributes (varies by observation type)
|
|
295
|
+
* @param options - Configuration including observation type (defaults to 'span')
|
|
296
|
+
* @returns Strongly-typed observation instance based on `asType`
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* // Within any observation (span, generation, agent, etc.)
|
|
301
|
+
* const parentObservation = startObservation('ai-workflow');
|
|
302
|
+
*
|
|
303
|
+
* // Create child span (default)
|
|
304
|
+
* const dataProcessing = parentObservation.startObservation('data-processing', {
|
|
305
|
+
* input: { userId: '123', dataSize: 1024 },
|
|
306
|
+
* metadata: { processor: 'fast-lane', version: '2.1' }
|
|
307
|
+
* }); // Returns LangfuseSpan
|
|
308
|
+
*
|
|
309
|
+
* // Create child generation with full LLM attributes
|
|
310
|
+
* const llmCall = parentObservation.startObservation('openai-gpt-4', {
|
|
311
|
+
* input: [{ role: 'system', content: 'You are a helpful assistant' },
|
|
312
|
+
* { role: 'user', content: 'Explain machine learning' }],
|
|
313
|
+
* model: 'gpt-4-turbo',
|
|
314
|
+
* modelParameters: {
|
|
315
|
+
* temperature: 0.7,
|
|
316
|
+
* maxTokens: 500,
|
|
317
|
+
* topP: 1.0
|
|
318
|
+
* },
|
|
319
|
+
* metadata: { priority: 'high', timeout: 30000 }
|
|
320
|
+
* }, { asType: 'generation' }); // Returns LangfuseGeneration
|
|
321
|
+
*
|
|
322
|
+
* // Create child agent for complex reasoning
|
|
323
|
+
* const reasoningAgent = parentObservation.startObservation('reasoning-agent', {
|
|
324
|
+
* input: {
|
|
325
|
+
* task: 'Analyze market trends',
|
|
326
|
+
* context: 'Q4 2024 financial data'
|
|
327
|
+
* },
|
|
328
|
+
* metadata: {
|
|
329
|
+
* model: 'gpt-4',
|
|
330
|
+
* tools: ['calculator', 'web-search', 'data-analysis'],
|
|
331
|
+
* maxIterations: 5
|
|
332
|
+
* }
|
|
333
|
+
* }, { asType: 'agent' }); // Returns LangfuseAgent
|
|
334
|
+
*
|
|
335
|
+
* // Create child tool for external API calls
|
|
336
|
+
* const apiCall = reasoningAgent.startObservation('market-data-api', {
|
|
337
|
+
* input: {
|
|
338
|
+
* endpoint: '/market/trends',
|
|
339
|
+
* params: { symbol: 'AAPL', period: '1Y' }
|
|
340
|
+
* },
|
|
341
|
+
* metadata: {
|
|
342
|
+
* provider: 'alpha-vantage',
|
|
343
|
+
* rateLimit: 5,
|
|
344
|
+
* timeout: 10000
|
|
345
|
+
* }
|
|
346
|
+
* }, { asType: 'tool' }); // Returns LangfuseTool
|
|
347
|
+
*
|
|
348
|
+
* // Create child retriever for document search
|
|
349
|
+
* const docSearch = parentObservation.startObservation('document-retrieval', {
|
|
350
|
+
* input: {
|
|
351
|
+
* query: 'sustainable energy solutions',
|
|
352
|
+
* filters: { year: '2024', category: 'research' },
|
|
353
|
+
* topK: 10
|
|
354
|
+
* },
|
|
355
|
+
* metadata: {
|
|
356
|
+
* vectorStore: 'pinecone',
|
|
357
|
+
* embeddingModel: 'text-embedding-ada-002',
|
|
358
|
+
* similarity: 'cosine'
|
|
359
|
+
* }
|
|
360
|
+
* }, { asType: 'retriever' }); // Returns LangfuseRetriever
|
|
361
|
+
*
|
|
362
|
+
* // Create child evaluator for quality assessment
|
|
363
|
+
* const qualityCheck = parentObservation.startObservation('response-evaluator', {
|
|
364
|
+
* input: {
|
|
365
|
+
* response: llmCall.output?.content,
|
|
366
|
+
* reference: 'Expected high-quality explanation',
|
|
367
|
+
* criteria: ['accuracy', 'clarity', 'completeness']
|
|
368
|
+
* },
|
|
369
|
+
* metadata: {
|
|
370
|
+
* evaluator: 'custom-bert-scorer',
|
|
371
|
+
* threshold: 0.8,
|
|
372
|
+
* metrics: ['bleu', 'rouge', 'semantic-similarity']
|
|
373
|
+
* }
|
|
374
|
+
* }, { asType: 'evaluator' }); // Returns LangfuseEvaluator
|
|
375
|
+
*
|
|
376
|
+
* // Create child guardrail for safety checking
|
|
377
|
+
* const safetyCheck = parentObservation.startObservation('content-guardrail', {
|
|
378
|
+
* input: {
|
|
379
|
+
* text: llmCall.output?.content,
|
|
380
|
+
* policies: ['no-harmful-content', 'no-personal-info', 'professional-tone']
|
|
381
|
+
* },
|
|
382
|
+
* metadata: {
|
|
383
|
+
* guardrailVersion: 'v2.1',
|
|
384
|
+
* strictMode: true,
|
|
385
|
+
* confidence: 0.95
|
|
386
|
+
* }
|
|
387
|
+
* }, { asType: 'guardrail' }); // Returns LangfuseGuardrail
|
|
388
|
+
*
|
|
389
|
+
* // Create child embedding for vector generation
|
|
390
|
+
* const textEmbedding = parentObservation.startObservation('text-embedder', {
|
|
391
|
+
* input: {
|
|
392
|
+
* texts: ['Document summary', 'Key insights', 'Conclusions'],
|
|
393
|
+
* batchSize: 3
|
|
394
|
+
* },
|
|
395
|
+
* model: 'text-embedding-ada-002',
|
|
396
|
+
* metadata: {
|
|
397
|
+
* dimensions: 1536,
|
|
398
|
+
* normalization: 'l2',
|
|
399
|
+
* purpose: 'semantic-search'
|
|
400
|
+
* }
|
|
401
|
+
* }, { asType: 'embedding' }); // Returns LangfuseEmbedding
|
|
402
|
+
*
|
|
403
|
+
* // Create child event for point-in-time logging
|
|
404
|
+
* const userAction = parentObservation.startObservation('user-interaction', {
|
|
405
|
+
* input: {
|
|
406
|
+
* action: 'button-click',
|
|
407
|
+
* element: 'generate-report',
|
|
408
|
+
* timestamp: new Date().toISOString()
|
|
409
|
+
* },
|
|
410
|
+
* level: 'DEFAULT',
|
|
411
|
+
* metadata: {
|
|
412
|
+
* sessionId: 'sess_123',
|
|
413
|
+
* userId: 'user_456',
|
|
414
|
+
* browser: 'Chrome 120.0'
|
|
415
|
+
* }
|
|
416
|
+
* }, { asType: 'event' }); // Returns LangfuseEvent (auto-ended)
|
|
417
|
+
*
|
|
418
|
+
* // Chain operations - each child inherits context
|
|
419
|
+
* dataProcessing.update({ output: { processed: true, records: 1000 } });
|
|
420
|
+
* dataProcessing.end();
|
|
421
|
+
*
|
|
422
|
+
* llmCall.update({
|
|
423
|
+
* output: { role: 'assistant', content: 'Machine learning is...' },
|
|
424
|
+
* usageDetails: { promptTokens: 25, completionTokens: 150 }
|
|
425
|
+
* });
|
|
426
|
+
* llmCall.end();
|
|
427
|
+
*
|
|
428
|
+
* parentObservation.update({
|
|
429
|
+
* output: {
|
|
430
|
+
* workflowCompleted: true,
|
|
431
|
+
* childOperations: 8,
|
|
432
|
+
* totalDuration: Date.now() - startTime
|
|
433
|
+
* }
|
|
434
|
+
* });
|
|
435
|
+
* parentObservation.end();
|
|
436
|
+
* ```
|
|
437
|
+
*
|
|
438
|
+
* @see {@link startObservation} for creating root-level observations
|
|
439
|
+
* @see {@link startActiveObservation} for function-scoped child observations
|
|
440
|
+
*/
|
|
441
|
+
startObservation(name: string, attributes: LangfuseGenerationAttributes, options: {
|
|
442
|
+
asType: "generation";
|
|
443
|
+
}): LangfuseGeneration;
|
|
444
|
+
startObservation(name: string, attributes: LangfuseEventAttributes, options: {
|
|
445
|
+
asType: "event";
|
|
446
|
+
}): LangfuseEvent;
|
|
447
|
+
startObservation(name: string, attributes: LangfuseAgentAttributes, options: {
|
|
448
|
+
asType: "agent";
|
|
449
|
+
}): LangfuseAgent;
|
|
450
|
+
startObservation(name: string, attributes: LangfuseToolAttributes, options: {
|
|
451
|
+
asType: "tool";
|
|
452
|
+
}): LangfuseTool;
|
|
453
|
+
startObservation(name: string, attributes: LangfuseChainAttributes, options: {
|
|
454
|
+
asType: "chain";
|
|
455
|
+
}): LangfuseChain;
|
|
456
|
+
startObservation(name: string, attributes: LangfuseRetrieverAttributes, options: {
|
|
457
|
+
asType: "retriever";
|
|
458
|
+
}): LangfuseRetriever;
|
|
459
|
+
startObservation(name: string, attributes: LangfuseEvaluatorAttributes, options: {
|
|
460
|
+
asType: "evaluator";
|
|
461
|
+
}): LangfuseEvaluator;
|
|
462
|
+
startObservation(name: string, attributes: LangfuseGuardrailAttributes, options: {
|
|
463
|
+
asType: "guardrail";
|
|
464
|
+
}): LangfuseGuardrail;
|
|
465
|
+
startObservation(name: string, attributes: LangfuseEmbeddingAttributes, options: {
|
|
466
|
+
asType: "embedding";
|
|
467
|
+
}): LangfuseEmbedding;
|
|
468
|
+
startObservation(name: string, attributes?: LangfuseSpanAttributes, options?: {
|
|
469
|
+
asType?: "span";
|
|
470
|
+
}): LangfuseSpan;
|
|
190
471
|
}
|
|
191
|
-
/**
|
|
192
|
-
* Parameters for creating a Langfuse span.
|
|
193
|
-
*
|
|
194
|
-
* @internal
|
|
195
|
-
*/
|
|
196
472
|
type LangfuseSpanParams = {
|
|
197
473
|
otelSpan: Span;
|
|
198
474
|
attributes?: LangfuseSpanAttributes;
|
|
199
475
|
};
|
|
200
476
|
/**
|
|
201
|
-
*
|
|
477
|
+
* General-purpose observation wrapper for tracking operations, functions, and workflows.
|
|
478
|
+
*
|
|
479
|
+
* LangfuseSpan is the default and most versatile observation type, designed for tracing
|
|
480
|
+
* any operation that has a defined start and end time. It serves as the foundation for
|
|
481
|
+
* building hierarchical traces and can contain any other observation type as children.
|
|
482
|
+
*
|
|
483
|
+
* ## Primary Use Cases
|
|
484
|
+
* - **Business Logic**: User workflows, order processing, data transformations
|
|
485
|
+
* - **API Operations**: REST endpoint handling, database queries, external service calls
|
|
486
|
+
* - **System Operations**: File I/O, network requests, background jobs
|
|
487
|
+
* - **Pipeline Steps**: Data processing stages, validation steps, orchestration
|
|
488
|
+
* - **Application Functions**: Any measurable operation in your application
|
|
489
|
+
*
|
|
490
|
+
* ## Key Features
|
|
491
|
+
* - **Hierarchical Structure**: Can contain child observations of any type
|
|
492
|
+
* - **Flexible Attributes**: Supports arbitrary input, output, and metadata
|
|
493
|
+
* - **Duration Tracking**: Automatically measures execution time from start to end
|
|
494
|
+
* - **Status Monitoring**: Tracks success/failure states and error conditions
|
|
495
|
+
* - **Context Propagation**: Maintains trace context for distributed operations
|
|
496
|
+
*
|
|
497
|
+
* ## Span Lifecycle
|
|
498
|
+
* 1. **Creation**: Span starts automatically when created
|
|
499
|
+
* 2. **Updates**: Add input data, intermediate state, metadata as needed
|
|
500
|
+
* 3. **Child Operations**: Create nested observations for sub-operations
|
|
501
|
+
* 4. **Completion**: Update with final output and call `.end()` to finish
|
|
202
502
|
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```typescript
|
|
505
|
+
* // Basic span tracking
|
|
506
|
+
* const span = startObservation('user-authentication', {
|
|
507
|
+
* input: { username: 'john_doe', method: 'oauth' },
|
|
508
|
+
* metadata: { provider: 'google' }
|
|
509
|
+
* });
|
|
510
|
+
*
|
|
511
|
+
* try {
|
|
512
|
+
* const user = await authenticateUser(credentials);
|
|
513
|
+
* span.update({
|
|
514
|
+
* output: { userId: user.id, success: true }
|
|
515
|
+
* });
|
|
516
|
+
* } catch (error) {
|
|
517
|
+
* span.update({
|
|
518
|
+
* level: 'ERROR',
|
|
519
|
+
* output: { success: false, error: error.message }
|
|
520
|
+
* });
|
|
521
|
+
* } finally {
|
|
522
|
+
* span.end();
|
|
523
|
+
* }
|
|
524
|
+
*
|
|
525
|
+
* // Nested operations
|
|
526
|
+
* const workflow = startObservation('order-processing', {
|
|
527
|
+
* input: { orderId: 'ord_123' }
|
|
528
|
+
* });
|
|
529
|
+
*
|
|
530
|
+
* const validation = workflow.startObservation('validation', {
|
|
531
|
+
* input: { items: cartItems }
|
|
532
|
+
* });
|
|
533
|
+
* validation.update({ output: { valid: true } });
|
|
534
|
+
* validation.end();
|
|
535
|
+
*
|
|
536
|
+
* const payment = workflow.startObservation('payment', {
|
|
537
|
+
* input: { amount: 100 }
|
|
538
|
+
* });
|
|
539
|
+
* payment.update({ output: { status: 'completed' } });
|
|
540
|
+
* payment.end();
|
|
541
|
+
*
|
|
542
|
+
* workflow.update({
|
|
543
|
+
* output: { status: 'confirmed', steps: 2 }
|
|
544
|
+
* });
|
|
545
|
+
* workflow.end();
|
|
546
|
+
* ```
|
|
547
|
+
*
|
|
548
|
+
* @see {@link startObservation} - Factory function for creating spans
|
|
549
|
+
* @see {@link startActiveObservation} - Function-scoped span creation
|
|
550
|
+
* @see {@link LangfuseGeneration} - For LLM and AI model interactions
|
|
551
|
+
* @see {@link LangfuseEvent} - For point-in-time occurrences
|
|
206
552
|
*
|
|
207
553
|
* @public
|
|
208
554
|
*/
|
|
209
|
-
declare class LangfuseSpan extends
|
|
555
|
+
declare class LangfuseSpan extends LangfuseBaseObservation {
|
|
210
556
|
constructor(params: LangfuseSpanParams);
|
|
211
557
|
/**
|
|
212
558
|
* Updates this span with new attributes.
|
|
@@ -224,57 +570,574 @@ declare class LangfuseSpan extends LangfuseSpanWrapper {
|
|
|
224
570
|
* ```
|
|
225
571
|
*/
|
|
226
572
|
update(attributes: LangfuseSpanAttributes): LangfuseSpan;
|
|
573
|
+
}
|
|
574
|
+
type LangfuseAgentParams = {
|
|
575
|
+
otelSpan: Span;
|
|
576
|
+
attributes?: LangfuseAgentAttributes;
|
|
577
|
+
};
|
|
578
|
+
/**
|
|
579
|
+
* Specialized observation wrapper for tracking AI agent workflows and autonomous operations.
|
|
580
|
+
*
|
|
581
|
+
* LangfuseAgent is designed for observing intelligent agent systems that combine reasoning,
|
|
582
|
+
* tool usage, memory management, and decision-making in autonomous workflows. It captures
|
|
583
|
+
* the complex multi-step nature of agent operations, including planning, execution, and
|
|
584
|
+
* self-correction cycles typical in advanced AI agent architectures.
|
|
585
|
+
*
|
|
586
|
+
* ## Primary Use Cases
|
|
587
|
+
* - **Autonomous AI Agents**: ReAct, AutoGPT, LangGraph agent implementations
|
|
588
|
+
* - **Tool-Using Agents**: Function calling agents with external API access
|
|
589
|
+
* - **Multi-Step Reasoning**: Chain-of-thought, tree-of-thought agent workflows
|
|
590
|
+
* - **Planning Agents**: Goal decomposition and task planning systems
|
|
591
|
+
* - **Conversational Agents**: Multi-turn dialogue agents with memory
|
|
592
|
+
* - **Code Generation Agents**: AI assistants that write, test, and debug code
|
|
593
|
+
*
|
|
594
|
+
* ## Key Features
|
|
595
|
+
* - **Multi-Step Tracking**: Captures entire agent workflow from planning to execution
|
|
596
|
+
* - **Tool Integration**: Records all tool calls and their results within agent context
|
|
597
|
+
* - **Decision Logic**: Tracks reasoning steps, decisions, and strategy adaptations
|
|
598
|
+
* - **Memory Management**: Observes how agents maintain and use context across steps
|
|
599
|
+
* - **Error Recovery**: Monitors how agents handle failures and adapt their approach
|
|
600
|
+
* - **Performance Metrics**: Measures agent efficiency, success rates, and resource usage
|
|
601
|
+
*
|
|
602
|
+
* ## Agent-Specific Patterns
|
|
603
|
+
* - **Planning Phase**: Initial goal analysis and strategy formulation
|
|
604
|
+
* - **Execution Loop**: Iterative action-observation-reasoning cycles
|
|
605
|
+
* - **Tool Selection**: Dynamic tool choice based on context and goals
|
|
606
|
+
* - **Self-Correction**: Error detection and strategy adjustment
|
|
607
|
+
* - **Memory Updates**: Context retention and knowledge accumulation
|
|
608
|
+
* - **Final Synthesis**: Result compilation and quality assessment
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* ```typescript
|
|
612
|
+
* // Basic agent workflow
|
|
613
|
+
* const agent = startObservation('research-agent', {
|
|
614
|
+
* input: {
|
|
615
|
+
* task: 'Research renewable energy trends',
|
|
616
|
+
* tools: ['web-search', 'summarizer'],
|
|
617
|
+
* maxIterations: 3
|
|
618
|
+
* }
|
|
619
|
+
* }, { asType: 'agent' });
|
|
620
|
+
*
|
|
621
|
+
* // Agent uses tools and makes decisions
|
|
622
|
+
* const searchTool = agent.startObservation('web-search', {
|
|
623
|
+
* input: { query: 'renewable energy 2024' }
|
|
624
|
+
* }, { asType: 'tool' });
|
|
625
|
+
*
|
|
626
|
+
* const results = await webSearch('renewable energy 2024');
|
|
627
|
+
* searchTool.update({ output: results });
|
|
628
|
+
* searchTool.end();
|
|
629
|
+
*
|
|
630
|
+
* // Agent generates final response
|
|
631
|
+
* const generation = agent.startObservation('synthesize-findings', {
|
|
632
|
+
* input: results,
|
|
633
|
+
* model: 'gpt-4'
|
|
634
|
+
* }, { asType: 'generation' });
|
|
635
|
+
*
|
|
636
|
+
* const response = await llm.generate(results);
|
|
637
|
+
* generation.update({ output: response });
|
|
638
|
+
* generation.end();
|
|
639
|
+
*
|
|
640
|
+
* agent.update({
|
|
641
|
+
* output: {
|
|
642
|
+
* completed: true,
|
|
643
|
+
* toolsUsed: 1,
|
|
644
|
+
* finalResponse: response
|
|
645
|
+
* }
|
|
646
|
+
* });
|
|
647
|
+
* agent.end();
|
|
648
|
+
* ```
|
|
649
|
+
*
|
|
650
|
+
* @see {@link startObservation} with `{ asType: 'agent' }` - Factory function
|
|
651
|
+
* @see {@link startActiveObservation} with `{ asType: 'agent' }` - Function-scoped agent
|
|
652
|
+
* @see {@link LangfuseTool} - For individual tool executions within agents
|
|
653
|
+
* @see {@link LangfuseChain} - For structured multi-step workflows
|
|
654
|
+
*
|
|
655
|
+
* @public
|
|
656
|
+
*/
|
|
657
|
+
declare class LangfuseAgent extends LangfuseBaseObservation {
|
|
658
|
+
constructor(params: LangfuseAgentParams);
|
|
227
659
|
/**
|
|
228
|
-
*
|
|
660
|
+
* Updates this agent observation with new attributes.
|
|
229
661
|
*
|
|
230
|
-
* @param
|
|
231
|
-
* @
|
|
232
|
-
* @returns The new child span
|
|
662
|
+
* @param attributes - Agent attributes to set
|
|
663
|
+
* @returns This agent for method chaining
|
|
233
664
|
*
|
|
234
665
|
* @example
|
|
235
666
|
* ```typescript
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
667
|
+
* agent.update({
|
|
668
|
+
* output: {
|
|
669
|
+
* taskCompleted: true,
|
|
670
|
+
* iterationsUsed: 5,
|
|
671
|
+
* toolsInvoked: ['web-search', 'calculator', 'summarizer'],
|
|
672
|
+
* finalResult: 'Research completed with high confidence'
|
|
673
|
+
* },
|
|
674
|
+
* metadata: {
|
|
675
|
+
* efficiency: 0.85,
|
|
676
|
+
* qualityScore: 0.92,
|
|
677
|
+
* resourcesConsumed: { tokens: 15000, apiCalls: 12 }
|
|
678
|
+
* }
|
|
239
679
|
* });
|
|
240
680
|
* ```
|
|
241
681
|
*/
|
|
242
|
-
|
|
682
|
+
update(attributes: LangfuseAgentAttributes): LangfuseAgent;
|
|
683
|
+
}
|
|
684
|
+
type LangfuseToolParams = {
|
|
685
|
+
otelSpan: Span;
|
|
686
|
+
attributes?: LangfuseToolAttributes;
|
|
687
|
+
};
|
|
688
|
+
/**
|
|
689
|
+
* Specialized observation wrapper for tracking individual tool calls and external API interactions.
|
|
690
|
+
*
|
|
691
|
+
* LangfuseTool is designed for observing discrete tool invocations within agent workflows,
|
|
692
|
+
* function calling scenarios, or standalone API integrations. It captures the input parameters,
|
|
693
|
+
* execution results, performance metrics, and error conditions of tool operations, making it
|
|
694
|
+
* essential for debugging tool reliability and optimizing tool selection strategies.
|
|
695
|
+
*
|
|
696
|
+
* ## Primary Use Cases
|
|
697
|
+
* - **Function Calling**: OpenAI function calls, Anthropic tool use, Claude function calling
|
|
698
|
+
* - **External APIs**: REST API calls, GraphQL queries, database operations
|
|
699
|
+
* - **System Tools**: File operations, shell commands, system integrations
|
|
700
|
+
* - **Data Processing Tools**: Calculators, converters, validators, parsers
|
|
701
|
+
* - **Search Tools**: Web search, vector search, document retrieval
|
|
702
|
+
* - **Content Tools**: Image generation, text processing, format conversion
|
|
703
|
+
*
|
|
704
|
+
* ## Key Features
|
|
705
|
+
* - **Parameter Tracking**: Complete input parameter logging and validation
|
|
706
|
+
* - **Result Capture**: Full output data and metadata from tool execution
|
|
707
|
+
* - **Performance Monitoring**: Execution time, success rates, retry attempts
|
|
708
|
+
* - **Error Analysis**: Detailed error tracking with context and recovery info
|
|
709
|
+
* - **Usage Analytics**: Frequency, patterns, and efficiency metrics
|
|
710
|
+
* - **Integration Health**: API status, rate limits, and service availability
|
|
711
|
+
*
|
|
712
|
+
* ## Tool-Specific Patterns
|
|
713
|
+
* - **Input Validation**: Parameter checking and sanitization before execution
|
|
714
|
+
* - **Execution Monitoring**: Real-time performance and status tracking
|
|
715
|
+
* - **Result Processing**: Output validation, transformation, and formatting
|
|
716
|
+
* - **Error Handling**: Retry logic, fallbacks, and graceful degradation
|
|
717
|
+
* - **Caching Integration**: Result caching and cache hit/miss tracking
|
|
718
|
+
* - **Rate Limiting**: Request throttling and quota management
|
|
719
|
+
*
|
|
720
|
+
* @example
|
|
721
|
+
* ```typescript
|
|
722
|
+
* // Web search tool
|
|
723
|
+
* const searchTool = startObservation('web-search', {
|
|
724
|
+
* input: {
|
|
725
|
+
* query: 'latest AI developments',
|
|
726
|
+
* maxResults: 10
|
|
727
|
+
* },
|
|
728
|
+
* metadata: { provider: 'google-api' }
|
|
729
|
+
* }, { asType: 'tool' });
|
|
730
|
+
*
|
|
731
|
+
* try {
|
|
732
|
+
* const results = await webSearch('latest AI developments');
|
|
733
|
+
*
|
|
734
|
+
* searchTool.update({
|
|
735
|
+
* output: {
|
|
736
|
+
* results: results,
|
|
737
|
+
* count: results.length
|
|
738
|
+
* },
|
|
739
|
+
* metadata: {
|
|
740
|
+
* latency: 1200,
|
|
741
|
+
* cacheHit: false
|
|
742
|
+
* }
|
|
743
|
+
* });
|
|
744
|
+
* } catch (error) {
|
|
745
|
+
* searchTool.update({
|
|
746
|
+
* level: 'ERROR',
|
|
747
|
+
* statusMessage: 'Search failed',
|
|
748
|
+
* output: { error: error.message }
|
|
749
|
+
* });
|
|
750
|
+
* } finally {
|
|
751
|
+
* searchTool.end();
|
|
752
|
+
* }
|
|
753
|
+
*
|
|
754
|
+
* // Database query tool
|
|
755
|
+
* const dbTool = startObservation('db-query', {
|
|
756
|
+
* input: {
|
|
757
|
+
* query: 'SELECT * FROM users WHERE active = true',
|
|
758
|
+
* timeout: 30000
|
|
759
|
+
* }
|
|
760
|
+
* }, { asType: 'tool' });
|
|
761
|
+
*
|
|
762
|
+
* const result = await db.query('SELECT * FROM users WHERE active = true');
|
|
763
|
+
* dbTool.update({
|
|
764
|
+
* output: { rowCount: result.length },
|
|
765
|
+
* metadata: { executionTime: 150 }
|
|
766
|
+
* });
|
|
767
|
+
* dbTool.end();
|
|
768
|
+
* ```
|
|
769
|
+
*
|
|
770
|
+
* @see {@link startObservation} with `{ asType: 'tool' }` - Factory function
|
|
771
|
+
* @see {@link startActiveObservation} with `{ asType: 'tool' }` - Function-scoped tool
|
|
772
|
+
* @see {@link LangfuseAgent} - For agent workflows that use multiple tools
|
|
773
|
+
* @see {@link LangfuseChain} - For orchestrated tool sequences
|
|
774
|
+
*
|
|
775
|
+
* @public
|
|
776
|
+
*/
|
|
777
|
+
declare class LangfuseTool extends LangfuseBaseObservation {
|
|
778
|
+
constructor(params: LangfuseToolParams);
|
|
243
779
|
/**
|
|
244
|
-
*
|
|
780
|
+
* Updates this tool observation with new attributes.
|
|
245
781
|
*
|
|
246
|
-
* @param
|
|
247
|
-
* @
|
|
248
|
-
* @returns The new child generation
|
|
782
|
+
* @param attributes - Tool attributes to set
|
|
783
|
+
* @returns This tool for method chaining
|
|
249
784
|
*
|
|
250
785
|
* @example
|
|
251
786
|
* ```typescript
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
787
|
+
* tool.update({
|
|
788
|
+
* output: {
|
|
789
|
+
* result: searchResults,
|
|
790
|
+
* count: searchResults.length,
|
|
791
|
+
* relevanceScore: 0.89,
|
|
792
|
+
* executionTime: 1250
|
|
793
|
+
* },
|
|
794
|
+
* metadata: {
|
|
795
|
+
* cacheHit: false,
|
|
796
|
+
* apiCost: 0.025,
|
|
797
|
+
* rateLimitRemaining: 950
|
|
798
|
+
* }
|
|
256
799
|
* });
|
|
257
800
|
* ```
|
|
258
801
|
*/
|
|
259
|
-
|
|
802
|
+
update(attributes: LangfuseToolAttributes): LangfuseTool;
|
|
803
|
+
}
|
|
804
|
+
type LangfuseChainParams = {
|
|
805
|
+
otelSpan: Span;
|
|
806
|
+
attributes?: LangfuseChainAttributes;
|
|
807
|
+
};
|
|
808
|
+
/**
|
|
809
|
+
* Specialized observation wrapper for tracking structured multi-step workflows and process chains.
|
|
810
|
+
*
|
|
811
|
+
* LangfuseChain is designed for observing sequential, parallel, or conditional workflow orchestration
|
|
812
|
+
* where multiple operations are coordinated to achieve a larger goal. It captures the flow of data
|
|
813
|
+
* between steps, manages dependencies, tracks progress through complex pipelines, and provides
|
|
814
|
+
* insights into workflow performance and reliability patterns.
|
|
815
|
+
*
|
|
816
|
+
* ## Primary Use Cases
|
|
817
|
+
* - **Data Processing Pipelines**: ETL processes, data transformation workflows
|
|
818
|
+
* - **Business Process Automation**: Order processing, approval workflows, document processing
|
|
819
|
+
* - **LangChain Integration**: LangChain chain execution and orchestration
|
|
820
|
+
* - **RAG Pipelines**: Document retrieval → context preparation → generation → post-processing
|
|
821
|
+
* - **Multi-Model Workflows**: Preprocessing → model inference → post-processing → validation
|
|
822
|
+
* - **Content Production**: Research → drafting → review → editing → publishing
|
|
823
|
+
*
|
|
824
|
+
* ## Key Features
|
|
825
|
+
* - **Step Orchestration**: Sequential, parallel, and conditional step execution tracking
|
|
826
|
+
* - **Data Flow Management**: Input/output tracking between pipeline steps
|
|
827
|
+
* - **Dependency Resolution**: Manages complex step dependencies and prerequisites
|
|
828
|
+
* - **Progress Monitoring**: Real-time workflow progress and completion status
|
|
829
|
+
* - **Error Propagation**: Handles failures, retries, and recovery across workflow steps
|
|
830
|
+
* - **Performance Analytics**: Bottleneck identification and optimization insights
|
|
831
|
+
*
|
|
832
|
+
* ## Chain-Specific Patterns
|
|
833
|
+
* - **Pipeline Setup**: Workflow definition, step configuration, and dependency mapping
|
|
834
|
+
* - **Sequential Execution**: Step-by-step processing with state management
|
|
835
|
+
* - **Parallel Processing**: Concurrent step execution with synchronization
|
|
836
|
+
* - **Conditional Logic**: Dynamic branching based on intermediate results
|
|
837
|
+
* - **Error Recovery**: Failure handling, rollback, and alternative path execution
|
|
838
|
+
* - **Result Aggregation**: Combining outputs from multiple workflow branches
|
|
839
|
+
*
|
|
840
|
+
* @example
|
|
841
|
+
* ```typescript
|
|
842
|
+
* // RAG processing chain
|
|
843
|
+
* const ragChain = startObservation('rag-chain', {
|
|
844
|
+
* input: {
|
|
845
|
+
* query: 'What is renewable energy?',
|
|
846
|
+
* steps: ['retrieval', 'generation']
|
|
847
|
+
* }
|
|
848
|
+
* }, { asType: 'chain' });
|
|
849
|
+
*
|
|
850
|
+
* // Step 1: Document retrieval
|
|
851
|
+
* const retrieval = ragChain.startObservation('document-retrieval', {
|
|
852
|
+
* input: { query: 'renewable energy' }
|
|
853
|
+
* }, { asType: 'retriever' });
|
|
854
|
+
*
|
|
855
|
+
* const docs = await vectorSearch('renewable energy');
|
|
856
|
+
* retrieval.update({ output: { documents: docs, count: docs.length } });
|
|
857
|
+
* retrieval.end();
|
|
858
|
+
*
|
|
859
|
+
* // Step 2: Generate response
|
|
860
|
+
* const generation = ragChain.startObservation('response-generation', {
|
|
861
|
+
* input: {
|
|
862
|
+
* query: 'What is renewable energy?',
|
|
863
|
+
* context: docs
|
|
864
|
+
* },
|
|
865
|
+
* model: 'gpt-4'
|
|
866
|
+
* }, { asType: 'generation' });
|
|
867
|
+
*
|
|
868
|
+
* const response = await llm.generate({
|
|
869
|
+
* prompt: buildPrompt('What is renewable energy?', docs)
|
|
870
|
+
* });
|
|
871
|
+
*
|
|
872
|
+
* generation.update({ output: response });
|
|
873
|
+
* generation.end();
|
|
874
|
+
*
|
|
875
|
+
* ragChain.update({
|
|
876
|
+
* output: {
|
|
877
|
+
* finalResponse: response,
|
|
878
|
+
* stepsCompleted: 2,
|
|
879
|
+
* documentsUsed: docs.length
|
|
880
|
+
* }
|
|
881
|
+
* });
|
|
882
|
+
* ragChain.end();
|
|
883
|
+
* ```
|
|
884
|
+
*
|
|
885
|
+
* @see {@link startObservation} with `{ asType: 'chain' }` - Factory function
|
|
886
|
+
* @see {@link startActiveObservation} with `{ asType: 'chain' }` - Function-scoped chain
|
|
887
|
+
* @see {@link LangfuseSpan} - For individual workflow steps
|
|
888
|
+
* @see {@link LangfuseAgent} - For intelligent workflow orchestration
|
|
889
|
+
*
|
|
890
|
+
* @public
|
|
891
|
+
*/
|
|
892
|
+
declare class LangfuseChain extends LangfuseBaseObservation {
|
|
893
|
+
constructor(params: LangfuseChainParams);
|
|
260
894
|
/**
|
|
261
|
-
*
|
|
895
|
+
* Updates this chain observation with new attributes.
|
|
262
896
|
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
* @param name - Name of the event
|
|
266
|
-
* @param attributes - Optional event attributes
|
|
267
|
-
* @returns The created event (already ended)
|
|
897
|
+
* @param attributes - Chain attributes to set
|
|
898
|
+
* @returns This chain for method chaining
|
|
268
899
|
*
|
|
269
900
|
* @example
|
|
270
901
|
* ```typescript
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
902
|
+
* chain.update({
|
|
903
|
+
* output: {
|
|
904
|
+
* stepsCompleted: 5,
|
|
905
|
+
* stepsSuccessful: 4,
|
|
906
|
+
* finalResult: processedData,
|
|
907
|
+
* pipelineEfficiency: 0.87
|
|
908
|
+
* },
|
|
909
|
+
* metadata: {
|
|
910
|
+
* bottleneckStep: 'data-validation',
|
|
911
|
+
* parallelizationOpportunities: ['step-2', 'step-3'],
|
|
912
|
+
* optimizationSuggestions: ['cache-intermediate-results']
|
|
913
|
+
* }
|
|
274
914
|
* });
|
|
275
915
|
* ```
|
|
276
916
|
*/
|
|
277
|
-
|
|
917
|
+
update(attributes: LangfuseChainAttributes): LangfuseChain;
|
|
918
|
+
}
|
|
919
|
+
type LangfuseRetrieverParams = {
|
|
920
|
+
otelSpan: Span;
|
|
921
|
+
attributes?: LangfuseRetrieverAttributes;
|
|
922
|
+
};
|
|
923
|
+
/**
|
|
924
|
+
* Specialized observation wrapper for tracking document retrieval and search operations.
|
|
925
|
+
*
|
|
926
|
+
* LangfuseRetriever is designed for observing information retrieval systems that search,
|
|
927
|
+
* filter, and rank content from various data sources. It captures search queries, retrieval
|
|
928
|
+
* strategies, result quality metrics, and performance characteristics of search operations,
|
|
929
|
+
* making it essential for RAG systems, knowledge bases, and content discovery workflows.
|
|
930
|
+
*
|
|
931
|
+
* ## Primary Use Cases
|
|
932
|
+
* - **Vector Search**: Semantic similarity search using embeddings and vector databases
|
|
933
|
+
* - **Document Retrieval**: Full-text search, keyword matching, and document filtering
|
|
934
|
+
* - **Knowledge Base Query**: FAQ systems, help documentation, and knowledge management
|
|
935
|
+
* - **RAG Systems**: Retrieval step in retrieval-augmented generation pipelines
|
|
936
|
+
* - **Recommendation Systems**: Content recommendations and similarity-based suggestions
|
|
937
|
+
* - **Data Mining**: Information extraction and content discovery from large datasets
|
|
938
|
+
*
|
|
939
|
+
* ## Key Features
|
|
940
|
+
* - **Query Analysis**: Input query processing, expansion, and optimization tracking
|
|
941
|
+
* - **Search Strategy**: Retrieval algorithms, ranking functions, and filtering criteria
|
|
942
|
+
* - **Result Quality**: Relevance scores, diversity metrics, and retrieval effectiveness
|
|
943
|
+
* - **Performance Metrics**: Search latency, index size, and throughput measurements
|
|
944
|
+
* - **Source Tracking**: Data source attribution and content provenance information
|
|
945
|
+
* - **Ranking Intelligence**: Personalization, context awareness, and result optimization
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* ```typescript
|
|
949
|
+
* // Vector search retrieval
|
|
950
|
+
* const retriever = startObservation('vector-search', {
|
|
951
|
+
* input: {
|
|
952
|
+
* query: 'machine learning applications',
|
|
953
|
+
* topK: 10,
|
|
954
|
+
* similarityThreshold: 0.7
|
|
955
|
+
* },
|
|
956
|
+
* metadata: {
|
|
957
|
+
* vectorDB: 'pinecone',
|
|
958
|
+
* embeddingModel: 'text-embedding-ada-002'
|
|
959
|
+
* }
|
|
960
|
+
* }, { asType: 'retriever' });
|
|
961
|
+
*
|
|
962
|
+
* const results = await vectorDB.search({
|
|
963
|
+
* query: 'machine learning applications',
|
|
964
|
+
* topK: 10,
|
|
965
|
+
* threshold: 0.7
|
|
966
|
+
* });
|
|
967
|
+
*
|
|
968
|
+
* retriever.update({
|
|
969
|
+
* output: {
|
|
970
|
+
* documents: results,
|
|
971
|
+
* count: results.length,
|
|
972
|
+
* avgSimilarity: 0.89
|
|
973
|
+
* },
|
|
974
|
+
* metadata: {
|
|
975
|
+
* searchLatency: 150,
|
|
976
|
+
* cacheHit: false
|
|
977
|
+
* }
|
|
978
|
+
* });
|
|
979
|
+
* retriever.end();
|
|
980
|
+
* ```
|
|
981
|
+
*
|
|
982
|
+
* @see {@link startObservation} with `{ asType: 'retriever' }` - Factory function
|
|
983
|
+
* @see {@link LangfuseChain} - For multi-step RAG pipelines
|
|
984
|
+
* @see {@link LangfuseEmbedding} - For embedding generation used in vector search
|
|
985
|
+
*
|
|
986
|
+
* @public
|
|
987
|
+
*/
|
|
988
|
+
declare class LangfuseRetriever extends LangfuseBaseObservation {
|
|
989
|
+
constructor(params: LangfuseRetrieverParams);
|
|
990
|
+
/**
|
|
991
|
+
* Updates this retriever observation with new attributes.
|
|
992
|
+
*
|
|
993
|
+
* @param attributes - Retriever attributes to set
|
|
994
|
+
* @returns This retriever for method chaining
|
|
995
|
+
*/
|
|
996
|
+
update(attributes: LangfuseRetrieverAttributes): LangfuseRetriever;
|
|
997
|
+
}
|
|
998
|
+
type LangfuseEvaluatorParams = {
|
|
999
|
+
otelSpan: Span;
|
|
1000
|
+
attributes?: LangfuseEvaluatorAttributes;
|
|
1001
|
+
};
|
|
1002
|
+
/**
|
|
1003
|
+
* Specialized observation wrapper for tracking quality assessment and evaluation operations.
|
|
1004
|
+
*
|
|
1005
|
+
* LangfuseEvaluator is designed for observing evaluation systems that assess, score, and
|
|
1006
|
+
* validate the quality of AI outputs, content, or system performance. It captures evaluation
|
|
1007
|
+
* criteria, scoring methodologies, benchmark comparisons, and quality metrics, making it
|
|
1008
|
+
* essential for AI system validation, content moderation, and performance monitoring.
|
|
1009
|
+
*
|
|
1010
|
+
* ## Primary Use Cases
|
|
1011
|
+
* - **LLM Output Evaluation**: Response quality, factual accuracy, and relevance assessment
|
|
1012
|
+
* - **Content Quality Assessment**: Writing quality, tone analysis, and style validation
|
|
1013
|
+
* - **Automated Testing**: System performance validation and regression testing
|
|
1014
|
+
* - **Bias Detection**: Fairness evaluation and bias identification in AI systems
|
|
1015
|
+
* - **Safety Evaluation**: Content safety, harm detection, and compliance checking
|
|
1016
|
+
* - **Benchmark Comparison**: Performance comparison against reference standards
|
|
1017
|
+
*
|
|
1018
|
+
* ## Key Features
|
|
1019
|
+
* - **Multi-Criteria Scoring**: Comprehensive evaluation across multiple quality dimensions
|
|
1020
|
+
* - **Automated Assessment**: AI-powered evaluation using specialized models and algorithms
|
|
1021
|
+
* - **Human Evaluation**: Integration with human reviewers and expert assessment
|
|
1022
|
+
* - **Benchmark Tracking**: Performance comparison against established baselines
|
|
1023
|
+
* - **Quality Metrics**: Detailed scoring with confidence intervals and reliability measures
|
|
1024
|
+
* - **Trend Analysis**: Quality tracking over time with improvement recommendations
|
|
1025
|
+
*
|
|
1026
|
+
* @example
|
|
1027
|
+
* ```typescript
|
|
1028
|
+
* // Response quality evaluation
|
|
1029
|
+
* const evaluator = startObservation('response-quality-eval', {
|
|
1030
|
+
* input: {
|
|
1031
|
+
* response: 'Machine learning is a subset of artificial intelligence...',
|
|
1032
|
+
* criteria: ['accuracy', 'completeness', 'clarity']
|
|
1033
|
+
* }
|
|
1034
|
+
* }, { asType: 'evaluator' });
|
|
1035
|
+
*
|
|
1036
|
+
* const evaluation = await evaluateResponse({
|
|
1037
|
+
* response: 'Machine learning is a subset of artificial intelligence...',
|
|
1038
|
+
* criteria: ['accuracy', 'completeness', 'clarity']
|
|
1039
|
+
* });
|
|
1040
|
+
*
|
|
1041
|
+
* evaluator.update({
|
|
1042
|
+
* output: {
|
|
1043
|
+
* overallScore: 0.87,
|
|
1044
|
+
* criteriaScores: {
|
|
1045
|
+
* accuracy: 0.92,
|
|
1046
|
+
* completeness: 0.85,
|
|
1047
|
+
* clarity: 0.90
|
|
1048
|
+
* },
|
|
1049
|
+
* passed: true
|
|
1050
|
+
* }
|
|
1051
|
+
* });
|
|
1052
|
+
* evaluator.end();
|
|
1053
|
+
* ```
|
|
1054
|
+
*
|
|
1055
|
+
* @see {@link startObservation} with `{ asType: 'evaluator' }` - Factory function
|
|
1056
|
+
* @see {@link LangfuseGeneration} - For LLM outputs being evaluated
|
|
1057
|
+
* @see {@link LangfuseGuardrail} - For safety and compliance enforcement
|
|
1058
|
+
*
|
|
1059
|
+
* @public
|
|
1060
|
+
*/
|
|
1061
|
+
declare class LangfuseEvaluator extends LangfuseBaseObservation {
|
|
1062
|
+
constructor(params: LangfuseEvaluatorParams);
|
|
1063
|
+
/**
|
|
1064
|
+
* Updates this evaluator observation with new attributes.
|
|
1065
|
+
*
|
|
1066
|
+
* @param attributes - Evaluator attributes to set
|
|
1067
|
+
* @returns This evaluator for method chaining
|
|
1068
|
+
*/
|
|
1069
|
+
update(attributes: LangfuseEvaluatorAttributes): LangfuseEvaluator;
|
|
1070
|
+
}
|
|
1071
|
+
type LangfuseGuardrailParams = {
|
|
1072
|
+
otelSpan: Span;
|
|
1073
|
+
attributes?: LangfuseGuardrailAttributes;
|
|
1074
|
+
};
|
|
1075
|
+
/**
|
|
1076
|
+
* Specialized observation wrapper for tracking safety checks and compliance enforcement.
|
|
1077
|
+
*
|
|
1078
|
+
* LangfuseGuardrail is designed for observing safety and compliance systems that prevent,
|
|
1079
|
+
* detect, and mitigate harmful, inappropriate, or policy-violating content and behaviors
|
|
1080
|
+
* in AI applications. It captures safety policies, violation detection, risk assessment,
|
|
1081
|
+
* and mitigation actions, ensuring responsible AI deployment and regulatory compliance.
|
|
1082
|
+
*
|
|
1083
|
+
* ## Primary Use Cases
|
|
1084
|
+
* - **Content Moderation**: Harmful content detection and filtering in user inputs/outputs
|
|
1085
|
+
* - **Safety Enforcement**: PII detection, toxicity filtering, and inappropriate content blocking
|
|
1086
|
+
* - **Compliance Monitoring**: Regulatory compliance, industry standards, and policy enforcement
|
|
1087
|
+
* - **Bias Mitigation**: Fairness checks and bias prevention in AI decision-making
|
|
1088
|
+
* - **Privacy Protection**: Data privacy safeguards and sensitive information redaction
|
|
1089
|
+
* - **Behavioral Monitoring**: User behavior analysis and anomaly detection
|
|
1090
|
+
*
|
|
1091
|
+
* ## Key Features
|
|
1092
|
+
* - **Multi-Policy Enforcement**: Simultaneous checking against multiple safety policies
|
|
1093
|
+
* - **Risk Assessment**: Quantitative risk scoring with confidence intervals
|
|
1094
|
+
* - **Real-Time Detection**: Low-latency safety checks for interactive applications
|
|
1095
|
+
* - **Context Awareness**: Contextual safety evaluation considering user and application context
|
|
1096
|
+
* - **Mitigation Actions**: Automatic content blocking, filtering, and redaction capabilities
|
|
1097
|
+
* - **Audit Trail**: Comprehensive logging for compliance and safety incident investigation
|
|
1098
|
+
*
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```typescript
|
|
1101
|
+
* // Content safety guardrail
|
|
1102
|
+
* const guardrail = startObservation('content-safety-check', {
|
|
1103
|
+
* input: {
|
|
1104
|
+
* content: userMessage,
|
|
1105
|
+
* policies: ['no-toxicity', 'no-hate-speech'],
|
|
1106
|
+
* strictMode: false
|
|
1107
|
+
* }
|
|
1108
|
+
* }, { asType: 'guardrail' });
|
|
1109
|
+
*
|
|
1110
|
+
* const safetyCheck = await checkContentSafety({
|
|
1111
|
+
* text: userMessage,
|
|
1112
|
+
* policies: ['no-toxicity', 'no-hate-speech']
|
|
1113
|
+
* });
|
|
1114
|
+
*
|
|
1115
|
+
* guardrail.update({
|
|
1116
|
+
* output: {
|
|
1117
|
+
* safe: safetyCheck.safe,
|
|
1118
|
+
* riskScore: 0.15,
|
|
1119
|
+
* violations: [],
|
|
1120
|
+
* action: 'allow'
|
|
1121
|
+
* }
|
|
1122
|
+
* });
|
|
1123
|
+
* guardrail.end();
|
|
1124
|
+
* ```
|
|
1125
|
+
*
|
|
1126
|
+
* @see {@link startObservation} with `{ asType: 'guardrail' }` - Factory function
|
|
1127
|
+
* @see {@link LangfuseEvaluator} - For detailed quality and safety assessment
|
|
1128
|
+
* @see {@link LangfuseGeneration} - For protecting LLM outputs with guardrails
|
|
1129
|
+
*
|
|
1130
|
+
* @public
|
|
1131
|
+
*/
|
|
1132
|
+
declare class LangfuseGuardrail extends LangfuseBaseObservation {
|
|
1133
|
+
constructor(params: LangfuseGuardrailParams);
|
|
1134
|
+
/**
|
|
1135
|
+
* Updates this guardrail observation with new attributes.
|
|
1136
|
+
*
|
|
1137
|
+
* @param attributes - Guardrail attributes to set
|
|
1138
|
+
* @returns This guardrail for method chaining
|
|
1139
|
+
*/
|
|
1140
|
+
update(attributes: LangfuseGuardrailAttributes): LangfuseGuardrail;
|
|
278
1141
|
}
|
|
279
1142
|
/**
|
|
280
1143
|
* Parameters for creating a Langfuse generation.
|
|
@@ -286,53 +1149,196 @@ type LangfuseGenerationParams = {
|
|
|
286
1149
|
attributes?: LangfuseGenerationAttributes;
|
|
287
1150
|
};
|
|
288
1151
|
/**
|
|
289
|
-
*
|
|
1152
|
+
* Specialized observation wrapper for tracking LLM interactions, AI model calls, and text generation.
|
|
1153
|
+
*
|
|
1154
|
+
* LangfuseGeneration is purpose-built for observing AI model interactions, providing rich
|
|
1155
|
+
* metadata capture for prompts, completions, model parameters, token usage, and costs.
|
|
1156
|
+
* It's the go-to observation type for any operation involving language models, chat APIs,
|
|
1157
|
+
* completion APIs, or other generative AI services.
|
|
1158
|
+
*
|
|
1159
|
+
* ## Primary Use Cases
|
|
1160
|
+
* - **LLM API Calls**: OpenAI, Anthropic, Cohere, Azure OpenAI, AWS Bedrock
|
|
1161
|
+
* - **Chat Completions**: Multi-turn conversations and dialogue systems
|
|
1162
|
+
* - **Text Generation**: Content creation, summarization, translation
|
|
1163
|
+
* - **Code Generation**: AI-powered code completion and generation
|
|
1164
|
+
* - **RAG Systems**: Generation step in retrieval-augmented generation
|
|
1165
|
+
* - **AI Agents**: LLM reasoning and decision-making within agent workflows
|
|
1166
|
+
*
|
|
1167
|
+
* ## Key Features
|
|
1168
|
+
* - **Rich LLM Metadata**: Model name, parameters, prompts, completions
|
|
1169
|
+
* - **Usage Tracking**: Token counts (prompt, completion, total)
|
|
1170
|
+
* - **Cost Monitoring**: Automatic cost calculation and tracking
|
|
1171
|
+
* - **Performance Metrics**: Latency, throughput, tokens per second
|
|
1172
|
+
* - **Prompt Engineering**: Version control for prompts and templates
|
|
1173
|
+
* - **Error Handling**: Rate limits, timeouts, model-specific errors
|
|
1174
|
+
*
|
|
1175
|
+
* ## Generation-Specific Attributes
|
|
1176
|
+
* - `model`: Model identifier (e.g., 'gpt-4-turbo', 'claude-3-sonnet')
|
|
1177
|
+
* - `modelParameters`: Temperature, max tokens, top-p, frequency penalty
|
|
1178
|
+
* - `input`: Prompt or message array for the model
|
|
1179
|
+
* - `output`: Model response, completion, or generated content
|
|
1180
|
+
* - `usageDetails`: Token consumption (prompt, completion, total)
|
|
1181
|
+
* - `costDetails`: Financial cost breakdown and pricing
|
|
1182
|
+
* - `prompt`: Structured prompt object with name, version, variables
|
|
290
1183
|
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
1184
|
+
* @example
|
|
1185
|
+
* ```typescript
|
|
1186
|
+
* // Basic LLM generation tracking
|
|
1187
|
+
* const generation = startObservation('openai-completion', {
|
|
1188
|
+
* model: 'gpt-4-turbo',
|
|
1189
|
+
* input: [
|
|
1190
|
+
* { role: 'system', content: 'You are a helpful assistant.' },
|
|
1191
|
+
* { role: 'user', content: 'Explain quantum computing' }
|
|
1192
|
+
* ],
|
|
1193
|
+
* modelParameters: {
|
|
1194
|
+
* temperature: 0.7,
|
|
1195
|
+
* maxTokens: 500
|
|
1196
|
+
* }
|
|
1197
|
+
* }, { asType: 'generation' });
|
|
1198
|
+
*
|
|
1199
|
+
* try {
|
|
1200
|
+
* const response = await openai.chat.completions.create({
|
|
1201
|
+
* model: 'gpt-4-turbo',
|
|
1202
|
+
* messages: [
|
|
1203
|
+
* { role: 'system', content: 'You are a helpful assistant.' },
|
|
1204
|
+
* { role: 'user', content: 'Explain quantum computing' }
|
|
1205
|
+
* ],
|
|
1206
|
+
* temperature: 0.7,
|
|
1207
|
+
* max_tokens: 500
|
|
1208
|
+
* });
|
|
1209
|
+
*
|
|
1210
|
+
* generation.update({
|
|
1211
|
+
* output: response.choices[0].message,
|
|
1212
|
+
* usageDetails: {
|
|
1213
|
+
* promptTokens: response.usage.prompt_tokens,
|
|
1214
|
+
* completionTokens: response.usage.completion_tokens,
|
|
1215
|
+
* totalTokens: response.usage.total_tokens
|
|
1216
|
+
* },
|
|
1217
|
+
* costDetails: {
|
|
1218
|
+
* totalCost: 0.025,
|
|
1219
|
+
* currency: 'USD'
|
|
1220
|
+
* }
|
|
1221
|
+
* });
|
|
1222
|
+
* } catch (error) {
|
|
1223
|
+
* generation.update({
|
|
1224
|
+
* level: 'ERROR',
|
|
1225
|
+
* statusMessage: `API error: ${error.message}`,
|
|
1226
|
+
* output: { error: error.message }
|
|
1227
|
+
* });
|
|
1228
|
+
* } finally {
|
|
1229
|
+
* generation.end();
|
|
1230
|
+
* }
|
|
1231
|
+
*
|
|
1232
|
+
* // RAG generation example
|
|
1233
|
+
* const ragGeneration = startObservation('rag-response', {
|
|
1234
|
+
* model: 'gpt-4',
|
|
1235
|
+
* input: [
|
|
1236
|
+
* { role: 'system', content: 'Answer based on provided context.' },
|
|
1237
|
+
* { role: 'user', content: `Context: ${context}\n\nQuestion: ${question}` }
|
|
1238
|
+
* ],
|
|
1239
|
+
* modelParameters: { temperature: 0.1 }
|
|
1240
|
+
* }, { asType: 'generation' });
|
|
1241
|
+
*
|
|
1242
|
+
* const response = await llm.generate({ prompt, context });
|
|
1243
|
+
* ragGeneration.update({
|
|
1244
|
+
* output: response,
|
|
1245
|
+
* metadata: { contextSources: 3 }
|
|
1246
|
+
* });
|
|
1247
|
+
* ragGeneration.end();
|
|
1248
|
+
* ```
|
|
1249
|
+
*
|
|
1250
|
+
* @see {@link startObservation} with `{ asType: 'generation' }` - Factory function
|
|
1251
|
+
* @see {@link startActiveObservation} with `{ asType: 'generation' }` - Function-scoped generation
|
|
1252
|
+
* @see {@link LangfuseSpan} - For general-purpose operations
|
|
1253
|
+
* @see {@link LangfuseEmbedding} - For text embedding and vector operations
|
|
293
1254
|
*
|
|
294
1255
|
* @public
|
|
295
1256
|
*/
|
|
296
|
-
declare class LangfuseGeneration extends
|
|
1257
|
+
declare class LangfuseGeneration extends LangfuseBaseObservation {
|
|
297
1258
|
constructor(params: LangfuseGenerationParams);
|
|
298
|
-
/**
|
|
299
|
-
* Updates this generation with new attributes.
|
|
300
|
-
*
|
|
301
|
-
* @param attributes - Generation attributes to set
|
|
302
|
-
* @returns This generation for method chaining
|
|
303
|
-
*
|
|
304
|
-
* @example
|
|
305
|
-
* ```typescript
|
|
306
|
-
* generation.update({
|
|
307
|
-
* output: { role: 'assistant', content: 'Hello there!' },
|
|
308
|
-
* usageDetails: {
|
|
309
|
-
* promptTokens: 10,
|
|
310
|
-
* completionTokens: 15,
|
|
311
|
-
* totalTokens: 25
|
|
312
|
-
* },
|
|
313
|
-
* costDetails: { totalCost: 0.001 }
|
|
314
|
-
* });
|
|
315
|
-
* ```
|
|
316
|
-
*/
|
|
317
1259
|
update(attributes: LangfuseGenerationAttributes): LangfuseGeneration;
|
|
1260
|
+
}
|
|
1261
|
+
type LangfuseEmbeddingParams = {
|
|
1262
|
+
otelSpan: Span;
|
|
1263
|
+
attributes?: LangfuseEmbeddingAttributes;
|
|
1264
|
+
};
|
|
1265
|
+
/**
|
|
1266
|
+
* Specialized observation wrapper for tracking text embedding and vector generation operations.
|
|
1267
|
+
*
|
|
1268
|
+
* LangfuseEmbedding is designed for observing embedding model interactions that convert
|
|
1269
|
+
* text, images, or other content into high-dimensional vector representations. It captures
|
|
1270
|
+
* embedding model parameters, input preprocessing, vector characteristics, and performance
|
|
1271
|
+
* metrics, making it essential for semantic search, RAG systems, and similarity-based applications.
|
|
1272
|
+
*
|
|
1273
|
+
* ## Primary Use Cases
|
|
1274
|
+
* - **Text Embeddings**: Converting text to vectors for semantic search and similarity
|
|
1275
|
+
* - **Document Indexing**: Creating vector representations for large document collections
|
|
1276
|
+
* - **Semantic Search**: Enabling similarity-based search and content discovery
|
|
1277
|
+
* - **RAG Preparation**: Embedding documents and queries for retrieval-augmented generation
|
|
1278
|
+
* - **Clustering Analysis**: Grouping similar content using vector representations
|
|
1279
|
+
* - **Recommendation Systems**: Content similarity for personalized recommendations
|
|
1280
|
+
*
|
|
1281
|
+
* ## Key Features
|
|
1282
|
+
* - **Model Tracking**: Embedding model selection, version, and parameter monitoring
|
|
1283
|
+
* - **Input Processing**: Text preprocessing, tokenization, and normalization tracking
|
|
1284
|
+
* - **Vector Analysis**: Dimensionality, magnitude, and quality metrics for generated embeddings
|
|
1285
|
+
* - **Batch Processing**: Efficient handling of multiple texts in single embedding operations
|
|
1286
|
+
* - **Performance Monitoring**: Embedding generation speed, cost tracking, and efficiency metrics
|
|
1287
|
+
* - **Quality Assessment**: Vector quality evaluation and embedding effectiveness measurement
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```typescript
|
|
1291
|
+
* // Text embedding generation
|
|
1292
|
+
* const embedding = startObservation('text-embedder', {
|
|
1293
|
+
* input: {
|
|
1294
|
+
* texts: [
|
|
1295
|
+
* 'Machine learning is a subset of AI',
|
|
1296
|
+
* 'Deep learning uses neural networks'
|
|
1297
|
+
* ],
|
|
1298
|
+
* batchSize: 2
|
|
1299
|
+
* },
|
|
1300
|
+
* model: 'text-embedding-ada-002'
|
|
1301
|
+
* }, { asType: 'embedding' });
|
|
1302
|
+
*
|
|
1303
|
+
* const embedResult = await generateEmbeddings({
|
|
1304
|
+
* texts: [
|
|
1305
|
+
* 'Machine learning is a subset of AI',
|
|
1306
|
+
* 'Deep learning uses neural networks'
|
|
1307
|
+
* ],
|
|
1308
|
+
* model: 'text-embedding-ada-002'
|
|
1309
|
+
* });
|
|
1310
|
+
*
|
|
1311
|
+
* embedding.update({
|
|
1312
|
+
* output: {
|
|
1313
|
+
* embeddings: embedResult.vectors,
|
|
1314
|
+
* count: embedResult.vectors.length,
|
|
1315
|
+
* dimensions: 1536
|
|
1316
|
+
* },
|
|
1317
|
+
* usageDetails: {
|
|
1318
|
+
* totalTokens: embedResult.tokenCount
|
|
1319
|
+
* },
|
|
1320
|
+
* metadata: {
|
|
1321
|
+
* processingTime: 340
|
|
1322
|
+
* }
|
|
1323
|
+
* });
|
|
1324
|
+
* embedding.end();
|
|
1325
|
+
* ```
|
|
1326
|
+
*
|
|
1327
|
+
* @see {@link startObservation} with `{ asType: 'embedding' }` - Factory function
|
|
1328
|
+
* @see {@link LangfuseRetriever} - For using embeddings in vector search
|
|
1329
|
+
* @see {@link LangfuseGeneration} - For LLM operations that may use embeddings
|
|
1330
|
+
*
|
|
1331
|
+
* @public
|
|
1332
|
+
*/
|
|
1333
|
+
declare class LangfuseEmbedding extends LangfuseBaseObservation {
|
|
1334
|
+
constructor(params: LangfuseEmbeddingParams);
|
|
318
1335
|
/**
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
* Events are point-in-time occurrences and are automatically ended.
|
|
1336
|
+
* Updates this embedding observation with new attributes.
|
|
322
1337
|
*
|
|
323
|
-
* @param
|
|
324
|
-
* @
|
|
325
|
-
* @returns The created event (already ended)
|
|
326
|
-
*
|
|
327
|
-
* @example
|
|
328
|
-
* ```typescript
|
|
329
|
-
* generation.createEvent('token-limit-reached', {
|
|
330
|
-
* level: 'WARNING',
|
|
331
|
-
* metadata: { requestedTokens: 2000, maxTokens: 1500 }
|
|
332
|
-
* });
|
|
333
|
-
* ```
|
|
1338
|
+
* @param attributes - Embedding attributes to set
|
|
1339
|
+
* @returns This embedding for method chaining
|
|
334
1340
|
*/
|
|
335
|
-
|
|
1341
|
+
update(attributes: LangfuseEmbeddingAttributes): LangfuseEmbedding;
|
|
336
1342
|
}
|
|
337
1343
|
/**
|
|
338
1344
|
* Parameters for creating a Langfuse event.
|
|
@@ -353,7 +1359,7 @@ type LangfuseEventParams = {
|
|
|
353
1359
|
*
|
|
354
1360
|
* @public
|
|
355
1361
|
*/
|
|
356
|
-
declare class LangfuseEvent extends
|
|
1362
|
+
declare class LangfuseEvent extends LangfuseBaseObservation {
|
|
357
1363
|
constructor(params: LangfuseEventParams);
|
|
358
1364
|
}
|
|
359
1365
|
|
|
@@ -384,65 +1390,7 @@ declare class LangfuseEvent extends LangfuseSpanWrapper {
|
|
|
384
1390
|
* @public
|
|
385
1391
|
*/
|
|
386
1392
|
declare function createTraceAttributes({ name, userId, sessionId, version, release, input, output, metadata, tags, environment, public: isPublic, }?: LangfuseTraceAttributes): Attributes;
|
|
387
|
-
|
|
388
|
-
* Creates OpenTelemetry attributes from Langfuse span attributes.
|
|
389
|
-
*
|
|
390
|
-
* Converts user-friendly span attributes into the internal OpenTelemetry
|
|
391
|
-
* attribute format required by the span processor.
|
|
392
|
-
*
|
|
393
|
-
* @param attributes - Langfuse span attributes to convert
|
|
394
|
-
* @returns OpenTelemetry attributes object with non-null values
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ```typescript
|
|
398
|
-
* import { createSpanAttributes } from '@langfuse/tracing';
|
|
399
|
-
*
|
|
400
|
-
* const otelAttributes = createSpanAttributes({
|
|
401
|
-
* input: { query: 'SELECT * FROM users' },
|
|
402
|
-
* output: { rowCount: 42 },
|
|
403
|
-
* level: 'DEFAULT',
|
|
404
|
-
* metadata: { database: 'prod' }
|
|
405
|
-
* });
|
|
406
|
-
*
|
|
407
|
-
* span.setAttributes(otelAttributes);
|
|
408
|
-
* ```
|
|
409
|
-
*
|
|
410
|
-
* @public
|
|
411
|
-
*/
|
|
412
|
-
declare function createSpanAttributes({ metadata, input, output, level, statusMessage, version, }: LangfuseSpanAttributes): Attributes;
|
|
413
|
-
/**
|
|
414
|
-
* Creates OpenTelemetry attributes from Langfuse generation attributes.
|
|
415
|
-
*
|
|
416
|
-
* Converts user-friendly generation attributes into the internal OpenTelemetry
|
|
417
|
-
* attribute format required by the span processor. Includes special handling
|
|
418
|
-
* for LLM-specific fields like model parameters, usage, and costs.
|
|
419
|
-
*
|
|
420
|
-
* @param attributes - Langfuse generation attributes to convert
|
|
421
|
-
* @returns OpenTelemetry attributes object with non-null values
|
|
422
|
-
*
|
|
423
|
-
* @example
|
|
424
|
-
* ```typescript
|
|
425
|
-
* import { createGenerationAttributes } from '@langfuse/tracing';
|
|
426
|
-
*
|
|
427
|
-
* const otelAttributes = createGenerationAttributes({
|
|
428
|
-
* input: [{ role: 'user', content: 'Hello!' }],
|
|
429
|
-
* output: { role: 'assistant', content: 'Hi there!' },
|
|
430
|
-
* model: 'gpt-4',
|
|
431
|
-
* modelParameters: { temperature: 0.7 },
|
|
432
|
-
* usageDetails: {
|
|
433
|
-
* promptTokens: 10,
|
|
434
|
-
* completionTokens: 15,
|
|
435
|
-
* totalTokens: 25
|
|
436
|
-
* },
|
|
437
|
-
* costDetails: { totalCost: 0.001 }
|
|
438
|
-
* });
|
|
439
|
-
*
|
|
440
|
-
* span.setAttributes(otelAttributes);
|
|
441
|
-
* ```
|
|
442
|
-
*
|
|
443
|
-
* @public
|
|
444
|
-
*/
|
|
445
|
-
declare function createGenerationAttributes({ completionStartTime, metadata, level, statusMessage, version, model, modelParameters, input, output, usageDetails, costDetails, prompt, }: LangfuseGenerationAttributes): Attributes;
|
|
1393
|
+
declare function createObservationAttributes(type: LangfuseObservationType, attributes: LangfuseObservationAttributes): Attributes;
|
|
446
1394
|
|
|
447
1395
|
/**
|
|
448
1396
|
* Sets an isolated TracerProvider for Langfuse tracing operations.
|
|
@@ -551,201 +1499,80 @@ type StartActiveObservationContext = StartObservationOptions & {
|
|
|
551
1499
|
endOnExit?: boolean;
|
|
552
1500
|
};
|
|
553
1501
|
/**
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
* Spans are used to track operations, functions, or logical units of work.
|
|
557
|
-
* They can contain other spans or generations as children.
|
|
558
|
-
*
|
|
559
|
-
* @param name - Name of the span
|
|
560
|
-
* @param attributes - Optional attributes to set on the span
|
|
561
|
-
* @param options - Optional configuration for the span
|
|
562
|
-
* @returns A LangfuseSpan instance
|
|
563
|
-
*
|
|
564
|
-
* @example
|
|
565
|
-
* ```typescript
|
|
566
|
-
* import { startSpan } from '@langfuse/tracing';
|
|
567
|
-
*
|
|
568
|
-
* const span = startSpan('data-processing', {
|
|
569
|
-
* input: { userId: '123', data: {...} },
|
|
570
|
-
* metadata: { version: '1.0' },
|
|
571
|
-
* level: 'DEFAULT'
|
|
572
|
-
* });
|
|
573
|
-
*
|
|
574
|
-
* try {
|
|
575
|
-
* // Do some work
|
|
576
|
-
* const result = await processData();
|
|
577
|
-
*
|
|
578
|
-
* span.update({ output: result });
|
|
579
|
-
* } catch (error) {
|
|
580
|
-
* span.update({
|
|
581
|
-
* level: 'ERROR',
|
|
582
|
-
* statusMessage: error.message
|
|
583
|
-
* });
|
|
584
|
-
* } finally {
|
|
585
|
-
* span.end();
|
|
586
|
-
* }
|
|
587
|
-
* ```
|
|
588
|
-
*
|
|
589
|
-
* @public
|
|
590
|
-
*/
|
|
591
|
-
declare function startSpan(name: string, attributes?: LangfuseSpanAttributes, options?: {
|
|
592
|
-
startTime?: TimeInput;
|
|
593
|
-
parentSpanContext?: SpanContext;
|
|
594
|
-
}): LangfuseSpan;
|
|
595
|
-
/**
|
|
596
|
-
* Creates and starts a new Langfuse generation for tracking LLM calls.
|
|
597
|
-
*
|
|
598
|
-
* Generations are specialized observations for tracking language model
|
|
599
|
-
* interactions, including model parameters, usage metrics, and costs.
|
|
600
|
-
*
|
|
601
|
-
* @param name - Name of the generation (typically the model or operation)
|
|
602
|
-
* @param attributes - Optional generation-specific attributes
|
|
603
|
-
* @param options - Optional configuration for the generation
|
|
604
|
-
* @returns A LangfuseGeneration instance
|
|
605
|
-
*
|
|
606
|
-
* @example
|
|
607
|
-
* ```typescript
|
|
608
|
-
* import { startGeneration } from '@langfuse/tracing';
|
|
609
|
-
*
|
|
610
|
-
* const generation = startGeneration('openai-gpt-4', {
|
|
611
|
-
* input: [{ role: 'user', content: 'Hello, world!' }],
|
|
612
|
-
* model: 'gpt-4',
|
|
613
|
-
* modelParameters: {
|
|
614
|
-
* temperature: 0.7,
|
|
615
|
-
* max_tokens: 150
|
|
616
|
-
* },
|
|
617
|
-
* metadata: { feature: 'chat' }
|
|
618
|
-
* });
|
|
619
|
-
*
|
|
620
|
-
* try {
|
|
621
|
-
* const response = await callOpenAI(messages);
|
|
622
|
-
*
|
|
623
|
-
* generation.update({
|
|
624
|
-
* output: response.choices[0].message,
|
|
625
|
-
* usageDetails: {
|
|
626
|
-
* promptTokens: response.usage.prompt_tokens,
|
|
627
|
-
* completionTokens: response.usage.completion_tokens,
|
|
628
|
-
* totalTokens: response.usage.total_tokens
|
|
629
|
-
* }
|
|
630
|
-
* });
|
|
631
|
-
* } finally {
|
|
632
|
-
* generation.end();
|
|
633
|
-
* }
|
|
634
|
-
* ```
|
|
635
|
-
*
|
|
636
|
-
* @public
|
|
637
|
-
*/
|
|
638
|
-
declare function startGeneration(name: string, attributes?: LangfuseGenerationAttributes, options?: StartObservationOptions): LangfuseGeneration;
|
|
639
|
-
/**
|
|
640
|
-
* Creates a Langfuse event for point-in-time occurrences.
|
|
641
|
-
*
|
|
642
|
-
* Events are used to capture instantaneous occurrences or log entries
|
|
643
|
-
* within a trace. Unlike spans, they represent a single point in time.
|
|
644
|
-
*
|
|
645
|
-
* @param name - Name of the event
|
|
646
|
-
* @param attributes - Optional attributes for the event
|
|
647
|
-
* @param options - Optional configuration for the event
|
|
648
|
-
* @returns A LangfuseEvent instance (automatically ended)
|
|
649
|
-
*
|
|
650
|
-
* @example
|
|
651
|
-
* ```typescript
|
|
652
|
-
* import { createEvent } from '@langfuse/tracing';
|
|
653
|
-
*
|
|
654
|
-
* // Log a user action
|
|
655
|
-
* createEvent('user-click', {
|
|
656
|
-
* input: { buttonId: 'submit', userId: '123' },
|
|
657
|
-
* metadata: { page: '/checkout' },
|
|
658
|
-
* level: 'DEFAULT'
|
|
659
|
-
* });
|
|
660
|
-
*
|
|
661
|
-
* // Log an error
|
|
662
|
-
* createEvent('api-error', {
|
|
663
|
-
* level: 'ERROR',
|
|
664
|
-
* statusMessage: 'Failed to fetch user data',
|
|
665
|
-
* metadata: { endpoint: '/api/users/123', statusCode: 500 }
|
|
666
|
-
* });
|
|
667
|
-
* ```
|
|
668
|
-
*
|
|
669
|
-
* @public
|
|
670
|
-
*/
|
|
671
|
-
declare function createEvent(name: string, attributes?: LangfuseEventAttributes, options?: StartObservationOptions): LangfuseEvent;
|
|
672
|
-
/**
|
|
673
|
-
* Starts an active span and executes a function within its context.
|
|
674
|
-
*
|
|
675
|
-
* This function creates a span, sets it as the active span in the OpenTelemetry
|
|
676
|
-
* context, executes the provided function, and automatically ends the span.
|
|
677
|
-
* Perfect for wrapping operations where you want child spans to be automatically
|
|
678
|
-
* linked.
|
|
679
|
-
*
|
|
680
|
-
* @param name - Name of the span
|
|
681
|
-
* @param fn - Function to execute within the span context
|
|
682
|
-
* @param options - Optional configuration for the span
|
|
683
|
-
* @returns The return value of the executed function
|
|
684
|
-
*
|
|
685
|
-
* @example
|
|
686
|
-
* ```typescript
|
|
687
|
-
* import { startActiveSpan } from '@langfuse/tracing';
|
|
688
|
-
*
|
|
689
|
-
* // Synchronous function
|
|
690
|
-
* const result = startActiveSpan('calculate-metrics', (span) => {
|
|
691
|
-
* span.update({ input: { data: rawData } });
|
|
692
|
-
*
|
|
693
|
-
* const metrics = calculateMetrics(rawData);
|
|
694
|
-
* span.update({ output: metrics });
|
|
695
|
-
*
|
|
696
|
-
* return metrics;
|
|
697
|
-
* });
|
|
698
|
-
*
|
|
699
|
-
* // Asynchronous function
|
|
700
|
-
* const data = await startActiveSpan('fetch-user-data', async (span) => {
|
|
701
|
-
* span.update({ input: { userId: '123' } });
|
|
702
|
-
*
|
|
703
|
-
* const userData = await api.getUser('123');
|
|
704
|
-
* span.update({ output: userData });
|
|
705
|
-
*
|
|
706
|
-
* return userData;
|
|
707
|
-
* });
|
|
708
|
-
* ```
|
|
1502
|
+
* Options for startObservation function.
|
|
709
1503
|
*
|
|
710
1504
|
* @public
|
|
711
1505
|
*/
|
|
712
|
-
|
|
1506
|
+
type StartObservationOpts = StartObservationOptions & {
|
|
1507
|
+
/** Type of observation to create. Defaults to 'span' */
|
|
1508
|
+
asType?: LangfuseObservationType;
|
|
1509
|
+
};
|
|
713
1510
|
/**
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* Similar to startActiveSpan but creates a generation for tracking LLM calls.
|
|
717
|
-
* The generation is automatically ended when the function completes.
|
|
718
|
-
*
|
|
719
|
-
* @param name - Name of the generation
|
|
720
|
-
* @param fn - Function to execute within the generation context
|
|
721
|
-
* @param options - Optional configuration for the generation
|
|
722
|
-
* @returns The return value of the executed function
|
|
723
|
-
*
|
|
724
|
-
* @example
|
|
725
|
-
* ```typescript
|
|
726
|
-
* import { startActiveGeneration } from '@langfuse/tracing';
|
|
727
|
-
*
|
|
728
|
-
* const response = await startActiveGeneration('openai-completion', async (generation) => {
|
|
729
|
-
* generation.update({
|
|
730
|
-
* input: { messages: [...] },
|
|
731
|
-
* model: 'gpt-4',
|
|
732
|
-
* modelParameters: { temperature: 0.7 }
|
|
733
|
-
* });
|
|
734
|
-
*
|
|
735
|
-
* const result = await openai.chat.completions.create({...});
|
|
736
|
-
*
|
|
737
|
-
* generation.update({
|
|
738
|
-
* output: result.choices[0].message,
|
|
739
|
-
* usageDetails: result.usage
|
|
740
|
-
* });
|
|
741
|
-
*
|
|
742
|
-
* return result;
|
|
743
|
-
* });
|
|
744
|
-
* ```
|
|
1511
|
+
* Options for startActiveObservation function.
|
|
745
1512
|
*
|
|
746
1513
|
* @public
|
|
747
1514
|
*/
|
|
748
|
-
|
|
1515
|
+
type StartActiveObservationOpts = StartActiveObservationContext & {
|
|
1516
|
+
/** Type of observation to create. Defaults to 'span' */
|
|
1517
|
+
asType?: LangfuseObservationType;
|
|
1518
|
+
};
|
|
1519
|
+
declare function startObservation(name: string, attributes: LangfuseGenerationAttributes, options: StartObservationOpts & {
|
|
1520
|
+
asType: "generation";
|
|
1521
|
+
}): LangfuseGeneration;
|
|
1522
|
+
declare function startObservation(name: string, attributes: LangfuseEventAttributes, options: StartObservationOpts & {
|
|
1523
|
+
asType: "event";
|
|
1524
|
+
}): LangfuseEvent;
|
|
1525
|
+
declare function startObservation(name: string, attributes: LangfuseAgentAttributes, options: StartObservationOpts & {
|
|
1526
|
+
asType: "agent";
|
|
1527
|
+
}): LangfuseAgent;
|
|
1528
|
+
declare function startObservation(name: string, attributes: LangfuseToolAttributes, options: StartObservationOpts & {
|
|
1529
|
+
asType: "tool";
|
|
1530
|
+
}): LangfuseTool;
|
|
1531
|
+
declare function startObservation(name: string, attributes: LangfuseChainAttributes, options: StartObservationOpts & {
|
|
1532
|
+
asType: "chain";
|
|
1533
|
+
}): LangfuseChain;
|
|
1534
|
+
declare function startObservation(name: string, attributes: LangfuseRetrieverAttributes, options: StartObservationOpts & {
|
|
1535
|
+
asType: "retriever";
|
|
1536
|
+
}): LangfuseRetriever;
|
|
1537
|
+
declare function startObservation(name: string, attributes: LangfuseEvaluatorAttributes, options: StartObservationOpts & {
|
|
1538
|
+
asType: "evaluator";
|
|
1539
|
+
}): LangfuseEvaluator;
|
|
1540
|
+
declare function startObservation(name: string, attributes: LangfuseGuardrailAttributes, options: StartObservationOpts & {
|
|
1541
|
+
asType: "guardrail";
|
|
1542
|
+
}): LangfuseGuardrail;
|
|
1543
|
+
declare function startObservation(name: string, attributes: LangfuseEmbeddingAttributes, options: StartObservationOpts & {
|
|
1544
|
+
asType: "embedding";
|
|
1545
|
+
}): LangfuseEmbedding;
|
|
1546
|
+
declare function startObservation(name: string, attributes?: LangfuseSpanAttributes, options?: StartObservationOpts & {
|
|
1547
|
+
asType?: "span";
|
|
1548
|
+
}): LangfuseSpan;
|
|
1549
|
+
declare function startActiveObservation<F extends (generation: LangfuseGeneration) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1550
|
+
asType: "generation";
|
|
1551
|
+
}): ReturnType<F>;
|
|
1552
|
+
declare function startActiveObservation<F extends (embedding: LangfuseEmbedding) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1553
|
+
asType: "embedding";
|
|
1554
|
+
}): ReturnType<F>;
|
|
1555
|
+
declare function startActiveObservation<F extends (agent: LangfuseAgent) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1556
|
+
asType: "agent";
|
|
1557
|
+
}): ReturnType<F>;
|
|
1558
|
+
declare function startActiveObservation<F extends (tool: LangfuseTool) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1559
|
+
asType: "tool";
|
|
1560
|
+
}): ReturnType<F>;
|
|
1561
|
+
declare function startActiveObservation<F extends (chain: LangfuseChain) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1562
|
+
asType: "chain";
|
|
1563
|
+
}): ReturnType<F>;
|
|
1564
|
+
declare function startActiveObservation<F extends (retriever: LangfuseRetriever) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1565
|
+
asType: "retriever";
|
|
1566
|
+
}): ReturnType<F>;
|
|
1567
|
+
declare function startActiveObservation<F extends (evaluator: LangfuseEvaluator) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1568
|
+
asType: "evaluator";
|
|
1569
|
+
}): ReturnType<F>;
|
|
1570
|
+
declare function startActiveObservation<F extends (guardrail: LangfuseGuardrail) => unknown>(name: string, fn: F, options: StartActiveObservationOpts & {
|
|
1571
|
+
asType: "guardrail";
|
|
1572
|
+
}): ReturnType<F>;
|
|
1573
|
+
declare function startActiveObservation<F extends (span: LangfuseSpan) => unknown>(name: string, fn: F, options?: StartActiveObservationOpts & {
|
|
1574
|
+
asType?: "span";
|
|
1575
|
+
}): ReturnType<F>;
|
|
749
1576
|
/**
|
|
750
1577
|
* Updates the currently active trace with new attributes.
|
|
751
1578
|
*
|
|
@@ -771,55 +1598,15 @@ declare function startActiveGeneration<F extends (span: LangfuseGeneration) => u
|
|
|
771
1598
|
* @public
|
|
772
1599
|
*/
|
|
773
1600
|
declare function updateActiveTrace(attributes: LangfuseTraceAttributes): void;
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
* ```typescript
|
|
784
|
-
* import { updateActiveSpan } from '@langfuse/tracing';
|
|
785
|
-
*
|
|
786
|
-
* // Inside an active span context
|
|
787
|
-
* updateActiveSpan({
|
|
788
|
-
* level: 'WARNING',
|
|
789
|
-
* statusMessage: 'Operation completed with warnings',
|
|
790
|
-
* metadata: { warningCount: 3 }
|
|
791
|
-
* });
|
|
792
|
-
* ```
|
|
793
|
-
*
|
|
794
|
-
* @public
|
|
795
|
-
*/
|
|
796
|
-
declare function updateActiveSpan(attributes: LangfuseSpanAttributes): void;
|
|
797
|
-
/**
|
|
798
|
-
* Updates the currently active generation with new attributes.
|
|
799
|
-
*
|
|
800
|
-
* This function finds the currently active OpenTelemetry span and updates
|
|
801
|
-
* it with generation-level attributes. If no active span is found, a warning is logged.
|
|
802
|
-
*
|
|
803
|
-
* @param attributes - Generation attributes to set
|
|
804
|
-
*
|
|
805
|
-
* @example
|
|
806
|
-
* ```typescript
|
|
807
|
-
* import { updateActiveGeneration } from '@langfuse/tracing';
|
|
808
|
-
*
|
|
809
|
-
* // Inside an active generation context
|
|
810
|
-
* updateActiveGeneration({
|
|
811
|
-
* usageDetails: {
|
|
812
|
-
* promptTokens: 50,
|
|
813
|
-
* completionTokens: 100,
|
|
814
|
-
* totalTokens: 150
|
|
815
|
-
* },
|
|
816
|
-
* costDetails: { totalCost: 0.003 }
|
|
817
|
-
* });
|
|
818
|
-
* ```
|
|
819
|
-
*
|
|
820
|
-
* @public
|
|
821
|
-
*/
|
|
822
|
-
declare function updateActiveGeneration(attributes: LangfuseGenerationAttributes): void;
|
|
1601
|
+
declare function updateActiveObservation(currentType: "span", attributes: LangfuseSpanAttributes): void;
|
|
1602
|
+
declare function updateActiveObservation(currentType: "generation", attributes: LangfuseGenerationAttributes): void;
|
|
1603
|
+
declare function updateActiveObservation(currentType: "agent", attributes: LangfuseAgentAttributes): void;
|
|
1604
|
+
declare function updateActiveObservation(currentType: "tool", attributes: LangfuseToolAttributes): void;
|
|
1605
|
+
declare function updateActiveObservation(currentType: "chain", attributes: LangfuseChainAttributes): void;
|
|
1606
|
+
declare function updateActiveObservation(currentType: "embedding", attributes: LangfuseEmbeddingAttributes): void;
|
|
1607
|
+
declare function updateActiveObservation(currentType: "evaluator", attributes: LangfuseEvaluatorAttributes): void;
|
|
1608
|
+
declare function updateActiveObservation(currentType: "guardrail", attributes: LangfuseGuardrailAttributes): void;
|
|
1609
|
+
declare function updateActiveObservation(currentType: "retriever", attributes: LangfuseRetrieverAttributes): void;
|
|
823
1610
|
/**
|
|
824
1611
|
* Options for the observe decorator function.
|
|
825
1612
|
*
|
|
@@ -829,7 +1616,7 @@ interface ObserveOptions {
|
|
|
829
1616
|
/** Name for the observation (defaults to function name) */
|
|
830
1617
|
name?: string;
|
|
831
1618
|
/** Type of observation to create */
|
|
832
|
-
asType?:
|
|
1619
|
+
asType?: LangfuseObservationType;
|
|
833
1620
|
/** Whether to capture function input as observation input */
|
|
834
1621
|
captureInput?: boolean;
|
|
835
1622
|
/** Whether to capture function output as observation output */
|
|
@@ -840,58 +1627,252 @@ interface ObserveOptions {
|
|
|
840
1627
|
endOnExit?: boolean;
|
|
841
1628
|
}
|
|
842
1629
|
/**
|
|
843
|
-
* Decorator function that automatically wraps
|
|
844
|
-
*
|
|
845
|
-
* This function creates a
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
*
|
|
850
|
-
* -
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
1630
|
+
* Decorator function that automatically wraps any function with Langfuse observability.
|
|
1631
|
+
*
|
|
1632
|
+
* This higher-order function creates a traced version of your function that automatically
|
|
1633
|
+
* handles observation lifecycle, input/output capture, and error tracking. It's perfect
|
|
1634
|
+
* for instrumenting existing functions without modifying their internal logic.
|
|
1635
|
+
*
|
|
1636
|
+
* ## Key Features
|
|
1637
|
+
* - **Zero Code Changes**: Wrap existing functions without modifying their implementation
|
|
1638
|
+
* - **Automatic I/O Capture**: Optionally captures function arguments and return values
|
|
1639
|
+
* - **Error Tracking**: Automatically captures exceptions and sets error status
|
|
1640
|
+
* - **Type Preservation**: Maintains original function signature and return types
|
|
1641
|
+
* - **Async Support**: Works seamlessly with both sync and async functions
|
|
1642
|
+
* - **Flexible Configuration**: Control observation type, naming, and capture behavior
|
|
1643
|
+
*
|
|
1644
|
+
* ## Use Cases
|
|
1645
|
+
* - Instrumenting business logic functions
|
|
1646
|
+
* - Wrapping API calls and external service interactions
|
|
1647
|
+
* - Adding observability to utility functions
|
|
1648
|
+
* - Creating traced versions of third-party functions
|
|
1649
|
+
* - Decorating class methods for observability
|
|
1650
|
+
*
|
|
1651
|
+
* @param fn - The function to wrap with observability (preserves original signature)
|
|
1652
|
+
* @param options - Configuration for observation behavior and capture settings
|
|
1653
|
+
* @returns An instrumented version of the function with identical behavior plus tracing
|
|
855
1654
|
*
|
|
856
1655
|
* @example
|
|
857
1656
|
* ```typescript
|
|
858
1657
|
* import { observe } from '@langfuse/tracing';
|
|
859
1658
|
*
|
|
860
|
-
* //
|
|
861
|
-
* const
|
|
862
|
-
* async (
|
|
863
|
-
*
|
|
864
|
-
*
|
|
1659
|
+
* // Basic function wrapping with automatic I/O capture
|
|
1660
|
+
* const processOrder = observe(
|
|
1661
|
+
* async (orderId: string, items: CartItem[]) => {
|
|
1662
|
+
* const validation = await validateOrder(orderId, items);
|
|
1663
|
+
* const payment = await processPayment(validation);
|
|
1664
|
+
* const shipping = await scheduleShipping(payment);
|
|
1665
|
+
* return { orderId, status: 'confirmed', trackingId: shipping.id };
|
|
865
1666
|
* },
|
|
866
1667
|
* {
|
|
867
|
-
* name: 'process-
|
|
1668
|
+
* name: 'process-order',
|
|
868
1669
|
* asType: 'span',
|
|
869
1670
|
* captureInput: true,
|
|
870
1671
|
* captureOutput: true
|
|
871
1672
|
* }
|
|
872
1673
|
* );
|
|
873
1674
|
*
|
|
874
|
-
* //
|
|
875
|
-
* const
|
|
876
|
-
* async (
|
|
877
|
-
*
|
|
878
|
-
* model: 'gpt-4',
|
|
879
|
-
* messages: [
|
|
1675
|
+
* // LLM function with generation tracking
|
|
1676
|
+
* const generateSummary = observe(
|
|
1677
|
+
* async (document: string, maxWords: number = 100) => {
|
|
1678
|
+
* const response = await openai.chat.completions.create({
|
|
1679
|
+
* model: 'gpt-4-turbo',
|
|
1680
|
+
* messages: [
|
|
1681
|
+
* { role: 'system', content: `Summarize in ${maxWords} words or less` },
|
|
1682
|
+
* { role: 'user', content: document }
|
|
1683
|
+
* ],
|
|
1684
|
+
* max_tokens: maxWords * 2
|
|
880
1685
|
* });
|
|
1686
|
+
* return response.choices[0].message.content;
|
|
881
1687
|
* },
|
|
882
1688
|
* {
|
|
883
|
-
* name: '
|
|
1689
|
+
* name: 'document-summarizer',
|
|
884
1690
|
* asType: 'generation',
|
|
885
1691
|
* captureInput: true,
|
|
886
1692
|
* captureOutput: true
|
|
887
1693
|
* }
|
|
888
1694
|
* );
|
|
889
1695
|
*
|
|
890
|
-
* //
|
|
891
|
-
* const
|
|
892
|
-
*
|
|
1696
|
+
* // Database query with automatic error tracking
|
|
1697
|
+
* const fetchUserProfile = observe(
|
|
1698
|
+
* async (userId: string) => {
|
|
1699
|
+
* const user = await db.users.findUnique({ where: { id: userId } });
|
|
1700
|
+
* if (!user) throw new Error(`User ${userId} not found`);
|
|
1701
|
+
*
|
|
1702
|
+
* const preferences = await db.preferences.findMany({
|
|
1703
|
+
* where: { userId }
|
|
1704
|
+
* });
|
|
1705
|
+
*
|
|
1706
|
+
* return { ...user, preferences };
|
|
1707
|
+
* },
|
|
1708
|
+
* {
|
|
1709
|
+
* name: 'fetch-user-profile',
|
|
1710
|
+
* asType: 'span',
|
|
1711
|
+
* captureInput: false, // Don't capture sensitive user IDs
|
|
1712
|
+
* captureOutput: true
|
|
1713
|
+
* }
|
|
1714
|
+
* );
|
|
1715
|
+
*
|
|
1716
|
+
* // Vector search with retriever semantics
|
|
1717
|
+
* const searchDocuments = observe(
|
|
1718
|
+
* async (query: string, topK: number = 5) => {
|
|
1719
|
+
* const embedding = await embedText(query);
|
|
1720
|
+
* const results = await vectorDb.search(embedding, topK);
|
|
1721
|
+
* return results.map(r => ({
|
|
1722
|
+
* content: r.metadata.content,
|
|
1723
|
+
* score: r.score,
|
|
1724
|
+
* source: r.metadata.source
|
|
1725
|
+
* }));
|
|
1726
|
+
* },
|
|
1727
|
+
* {
|
|
1728
|
+
* name: 'document-search',
|
|
1729
|
+
* asType: 'retriever',
|
|
1730
|
+
* captureInput: true,
|
|
1731
|
+
* captureOutput: true
|
|
1732
|
+
* }
|
|
1733
|
+
* );
|
|
1734
|
+
*
|
|
1735
|
+
* // Quality evaluation function
|
|
1736
|
+
* const evaluateResponse = observe(
|
|
1737
|
+
* (response: string, reference: string, metric: string = 'similarity') => {
|
|
1738
|
+
* let score: number;
|
|
1739
|
+
*
|
|
1740
|
+
* switch (metric) {
|
|
1741
|
+
* case 'similarity':
|
|
1742
|
+
* score = calculateCosineSimilarity(response, reference);
|
|
1743
|
+
* break;
|
|
1744
|
+
* case 'bleu':
|
|
1745
|
+
* score = calculateBleuScore(response, reference);
|
|
1746
|
+
* break;
|
|
1747
|
+
* default:
|
|
1748
|
+
* throw new Error(`Unknown metric: ${metric}`);
|
|
1749
|
+
* }
|
|
1750
|
+
*
|
|
1751
|
+
* return {
|
|
1752
|
+
* score,
|
|
1753
|
+
* passed: score > 0.8,
|
|
1754
|
+
* metric,
|
|
1755
|
+
* grade: score > 0.9 ? 'excellent' : score > 0.7 ? 'good' : 'needs_improvement'
|
|
1756
|
+
* };
|
|
1757
|
+
* },
|
|
1758
|
+
* {
|
|
1759
|
+
* name: 'response-evaluator',
|
|
1760
|
+
* asType: 'evaluator',
|
|
1761
|
+
* captureInput: true,
|
|
1762
|
+
* captureOutput: true
|
|
1763
|
+
* }
|
|
1764
|
+
* );
|
|
1765
|
+
*
|
|
1766
|
+
* // Content moderation with guardrails
|
|
1767
|
+
* const moderateContent = observe(
|
|
1768
|
+
* async (text: string, policies: string[] = ['profanity', 'spam']) => {
|
|
1769
|
+
* const violations = [];
|
|
1770
|
+
*
|
|
1771
|
+
* for (const policy of policies) {
|
|
1772
|
+
* const result = await checkPolicy(text, policy);
|
|
1773
|
+
* if (result.violation) {
|
|
1774
|
+
* violations.push({ policy, severity: result.severity });
|
|
1775
|
+
* }
|
|
1776
|
+
* }
|
|
1777
|
+
*
|
|
1778
|
+
* return {
|
|
1779
|
+
* allowed: violations.length === 0,
|
|
1780
|
+
* violations,
|
|
1781
|
+
* confidence: 0.95
|
|
1782
|
+
* };
|
|
1783
|
+
* },
|
|
1784
|
+
* {
|
|
1785
|
+
* name: 'content-moderator',
|
|
1786
|
+
* asType: 'guardrail',
|
|
1787
|
+
* captureInput: true,
|
|
1788
|
+
* captureOutput: true
|
|
1789
|
+
* }
|
|
1790
|
+
* );
|
|
1791
|
+
*
|
|
1792
|
+
* // AI agent function with tool usage
|
|
1793
|
+
* const researchAgent = observe(
|
|
1794
|
+
* async (query: string, maxSources: number = 3) => {
|
|
1795
|
+
* // Search for relevant documents
|
|
1796
|
+
* const documents = await searchDocuments(query, maxSources * 2);
|
|
1797
|
+
*
|
|
1798
|
+
* // Filter and rank results
|
|
1799
|
+
* const topDocs = documents
|
|
1800
|
+
* .filter(d => d.score > 0.7)
|
|
1801
|
+
* .slice(0, maxSources);
|
|
1802
|
+
*
|
|
1803
|
+
* // Generate comprehensive answer
|
|
1804
|
+
* const context = topDocs.map(d => d.content).join('\n\n');
|
|
1805
|
+
* const answer = await generateSummary(
|
|
1806
|
+
* `Based on: ${context}\n\nQuestion: ${query}`,
|
|
1807
|
+
* 200
|
|
1808
|
+
* );
|
|
1809
|
+
*
|
|
1810
|
+
* return {
|
|
1811
|
+
* answer,
|
|
1812
|
+
* sources: topDocs.map(d => d.source),
|
|
1813
|
+
* confidence: Math.min(...topDocs.map(d => d.score))
|
|
1814
|
+
* };
|
|
1815
|
+
* },
|
|
1816
|
+
* {
|
|
1817
|
+
* name: 'research-agent',
|
|
1818
|
+
* asType: 'agent',
|
|
1819
|
+
* captureInput: true,
|
|
1820
|
+
* captureOutput: true
|
|
1821
|
+
* }
|
|
1822
|
+
* );
|
|
1823
|
+
*
|
|
1824
|
+
* // Class method decoration
|
|
1825
|
+
* class UserService {
|
|
1826
|
+
* private db: Database;
|
|
1827
|
+
*
|
|
1828
|
+
* // Wrap methods during class construction
|
|
1829
|
+
* constructor(database: Database) {
|
|
1830
|
+
* this.db = database;
|
|
1831
|
+
* this.createUser = observe(this.createUser.bind(this), {
|
|
1832
|
+
* name: 'create-user',
|
|
1833
|
+
* asType: 'span',
|
|
1834
|
+
* captureInput: false, // Sensitive data
|
|
1835
|
+
* captureOutput: true
|
|
1836
|
+
* });
|
|
1837
|
+
* }
|
|
1838
|
+
*
|
|
1839
|
+
* async createUser(userData: UserData) {
|
|
1840
|
+
* // Implementation automatically traced
|
|
1841
|
+
* return await this.db.users.create(userData);
|
|
1842
|
+
* }
|
|
1843
|
+
* }
|
|
1844
|
+
*
|
|
1845
|
+
* // Chain composition - functions remain composable
|
|
1846
|
+
* const processDocument = observe(
|
|
1847
|
+
* async (document: string) => {
|
|
1848
|
+
* const summary = await generateSummary(document, 150);
|
|
1849
|
+
* const moderation = await moderateContent(summary);
|
|
1850
|
+
* const evaluation = evaluateResponse(summary, document, 'similarity');
|
|
1851
|
+
*
|
|
1852
|
+
* return {
|
|
1853
|
+
* summary: moderation.allowed ? summary : '[Content Filtered]',
|
|
1854
|
+
* safe: moderation.allowed,
|
|
1855
|
+
* quality: evaluation.score
|
|
1856
|
+
* };
|
|
1857
|
+
* },
|
|
1858
|
+
* {
|
|
1859
|
+
* name: 'document-processor',
|
|
1860
|
+
* asType: 'chain',
|
|
1861
|
+
* captureInput: true,
|
|
1862
|
+
* captureOutput: true
|
|
1863
|
+
* }
|
|
1864
|
+
* );
|
|
1865
|
+
*
|
|
1866
|
+
* // Usage - functions work exactly as before, just with observability
|
|
1867
|
+
* const order = await processOrder('ord_123', cartItems);
|
|
1868
|
+
* const profile = await fetchUserProfile('user_456');
|
|
1869
|
+
* const research = await researchAgent('What is quantum computing?');
|
|
1870
|
+
* const processed = await processDocument(documentText);
|
|
893
1871
|
* ```
|
|
894
1872
|
*
|
|
1873
|
+
* @see {@link startObservation} for manual observation creation
|
|
1874
|
+
* @see {@link startActiveObservation} for function-scoped observations
|
|
1875
|
+
*
|
|
895
1876
|
* @public
|
|
896
1877
|
*/
|
|
897
1878
|
declare function observe<T extends (...args: unknown[]) => unknown>(fn: T, options?: ObserveOptions): T;
|
|
@@ -922,7 +1903,7 @@ declare function observe<T extends (...args: unknown[]) => unknown>(fn: T, optio
|
|
|
922
1903
|
* console.log(randomId1 === randomId2); // false
|
|
923
1904
|
*
|
|
924
1905
|
* // Use with spans
|
|
925
|
-
* const span =
|
|
1906
|
+
* const span = startObservation("my-span", {}, {
|
|
926
1907
|
* parentSpanContext: {
|
|
927
1908
|
* traceId: await createTraceId("session-456"),
|
|
928
1909
|
* spanId: "0123456789abcdef",
|
|
@@ -963,4 +1944,4 @@ declare function getActiveTraceId(): string | undefined;
|
|
|
963
1944
|
*/
|
|
964
1945
|
declare function getActiveSpanId(): string | undefined;
|
|
965
1946
|
|
|
966
|
-
export {
|
|
1947
|
+
export { LangfuseAgent, LangfuseChain, LangfuseEmbedding, LangfuseEvaluator, LangfuseEvent, type LangfuseEventAttributes, LangfuseGeneration, type LangfuseGenerationAttributes, LangfuseGuardrail, type LangfuseObservation, type LangfuseObservationAttributes, type LangfuseObservationType, LangfuseRetriever, LangfuseSpan, type LangfuseSpanAttributes, LangfuseTool, type LangfuseTraceAttributes, type ObservationLevel, type ObserveOptions, type StartActiveObservationContext, type StartActiveObservationOpts, type StartObservationOptions, type StartObservationOpts, createObservationAttributes, createTraceAttributes, createTraceId, getActiveSpanId, getActiveTraceId, getLangfuseTracer, getLangfuseTracerProvider, observe, setLangfuseTracerProvider, startActiveObservation, startObservation, updateActiveObservation, updateActiveTrace };
|