@langfuse/client 4.0.0-beta.0 → 4.0.0-beta.2
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/README.md +24 -0
- package/dist/index.cjs +434 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +633 -12
- package/dist/index.d.ts +633 -12
- package/dist/index.mjs +434 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -2,17 +2,73 @@ import * as _langfuse_core from '@langfuse/core';
|
|
|
2
2
|
import { LangfuseAPIClient, Dataset, DatasetItem, DatasetRunItem, ParsedMediaReference, CreatePromptRequest, ChatMessage, ChatMessageWithPlaceholders, PlaceholderMessage, BasePrompt, Prompt, ScoreBody } from '@langfuse/core';
|
|
3
3
|
import { Span } from '@opentelemetry/api';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Function type for linking dataset items to OpenTelemetry spans.
|
|
7
|
+
* This allows dataset items to be associated with specific traces for experiment tracking.
|
|
8
|
+
*
|
|
9
|
+
* @param obj - Object containing the OpenTelemetry span
|
|
10
|
+
* @param runName - Name of the dataset run
|
|
11
|
+
* @param runArgs - Optional arguments for the dataset run
|
|
12
|
+
* @returns Promise that resolves to the created dataset run item
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
5
16
|
type LinkDatasetItemFunction = (obj: {
|
|
6
17
|
otelSpan: Span;
|
|
7
18
|
}, runName: string, runArgs?: {
|
|
19
|
+
/** Description of the dataset run */
|
|
8
20
|
description?: string;
|
|
21
|
+
/** Additional metadata for the dataset run */
|
|
9
22
|
metadata?: any;
|
|
10
23
|
}) => Promise<DatasetRunItem>;
|
|
24
|
+
/**
|
|
25
|
+
* Manager for dataset operations in Langfuse.
|
|
26
|
+
*
|
|
27
|
+
* Provides methods to retrieve datasets and their items, with automatic
|
|
28
|
+
* pagination handling and convenient linking functionality for experiments.
|
|
29
|
+
*
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
11
32
|
declare class DatasetManager {
|
|
12
33
|
private apiClient;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new DatasetManager instance.
|
|
36
|
+
*
|
|
37
|
+
* @param params - Configuration object containing the API client
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
13
40
|
constructor(params: {
|
|
14
41
|
apiClient: LangfuseAPIClient;
|
|
15
42
|
});
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves a dataset by name along with all its items.
|
|
45
|
+
*
|
|
46
|
+
* This method automatically handles pagination to fetch all dataset items
|
|
47
|
+
* and enhances each item with a `link` function for easy experiment tracking.
|
|
48
|
+
*
|
|
49
|
+
* @param name - The name of the dataset to retrieve
|
|
50
|
+
* @param options - Optional configuration for fetching
|
|
51
|
+
* @param options.fetchItemsPageSize - Number of items to fetch per page (default: 50)
|
|
52
|
+
*
|
|
53
|
+
* @returns Promise that resolves to the dataset with enhanced items
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const dataset = await langfuse.dataset.get("my-dataset");
|
|
58
|
+
*
|
|
59
|
+
* for (const item of dataset.items) {
|
|
60
|
+
* // Use the item data for your experiment
|
|
61
|
+
* const result = await processItem(item.input);
|
|
62
|
+
*
|
|
63
|
+
* // Link the result to the dataset item
|
|
64
|
+
* await item.link(
|
|
65
|
+
* { otelSpan: currentSpan },
|
|
66
|
+
* "experiment-run-1",
|
|
67
|
+
* { description: "Testing new model" }
|
|
68
|
+
* );
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
16
72
|
get(name: string, options?: {
|
|
17
73
|
fetchItemsPageSize: number;
|
|
18
74
|
}): Promise<Dataset & {
|
|
@@ -20,34 +76,61 @@ declare class DatasetManager {
|
|
|
20
76
|
link: LinkDatasetItemFunction;
|
|
21
77
|
})[];
|
|
22
78
|
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a link function for a specific dataset item.
|
|
81
|
+
*
|
|
82
|
+
* @param item - The dataset item to create a link function for
|
|
83
|
+
* @returns A function that can link the item to OpenTelemetry spans
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
23
86
|
private createDatasetItemLinkFunction;
|
|
24
87
|
}
|
|
25
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Parameters for resolving media references in objects.
|
|
91
|
+
*
|
|
92
|
+
* @template T - The type of the object being processed
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
26
95
|
type LangfuseMediaResolveMediaReferencesParams<T> = {
|
|
96
|
+
/** The object to process for media references */
|
|
27
97
|
obj: T;
|
|
98
|
+
/** The format to resolve media references to (currently only "base64DataUri" is supported) */
|
|
28
99
|
resolveWith: "base64DataUri";
|
|
100
|
+
/** Maximum depth to traverse when processing nested objects (default: 10) */
|
|
29
101
|
maxDepth?: number;
|
|
30
102
|
};
|
|
103
|
+
/**
|
|
104
|
+
* Manager for media operations in Langfuse.
|
|
105
|
+
*
|
|
106
|
+
* Provides methods to resolve media references in objects by replacing
|
|
107
|
+
* them with actual media content (e.g., base64 data URIs).
|
|
108
|
+
*
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
31
111
|
declare class MediaManager {
|
|
32
112
|
private apiClient;
|
|
113
|
+
/**
|
|
114
|
+
* Creates a new MediaManager instance.
|
|
115
|
+
*
|
|
116
|
+
* @param params - Configuration object containing the API client
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
33
119
|
constructor(params: {
|
|
34
120
|
apiClient: LangfuseAPIClient;
|
|
35
121
|
});
|
|
36
122
|
/**
|
|
37
|
-
* Replaces
|
|
123
|
+
* Replaces media reference strings in an object with base64 data URIs.
|
|
38
124
|
*
|
|
39
|
-
* This method recursively traverses an object
|
|
40
|
-
* in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media
|
|
41
|
-
* Langfuse
|
|
125
|
+
* This method recursively traverses an object looking for media reference strings
|
|
126
|
+
* in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media
|
|
127
|
+
* content from Langfuse and replaces the reference string with a base64 data URI.
|
|
42
128
|
*
|
|
43
|
-
* If fetching media content fails for a reference string, a warning is logged
|
|
129
|
+
* If fetching media content fails for a reference string, a warning is logged
|
|
130
|
+
* and the reference string is left unchanged.
|
|
44
131
|
*
|
|
45
132
|
* @param params - Configuration object
|
|
46
|
-
* @
|
|
47
|
-
* @param params.resolveWith - The representation of the media content to replace the media reference string with. Currently only "base64DataUri" is supported.
|
|
48
|
-
* @param params.maxDepth - Optional. Default is 10. The maximum depth to traverse the object.
|
|
49
|
-
*
|
|
50
|
-
* @returns A deep copy of the input object with all media references replaced with base64 data URIs where possible
|
|
133
|
+
* @returns A deep copy of the input object with media references resolved
|
|
51
134
|
*
|
|
52
135
|
* @example
|
|
53
136
|
* ```typescript
|
|
@@ -58,9 +141,9 @@ declare class MediaManager {
|
|
|
58
141
|
* }
|
|
59
142
|
* };
|
|
60
143
|
*
|
|
61
|
-
* const result = await
|
|
144
|
+
* const result = await langfuse.media.resolveReferences({
|
|
62
145
|
* obj,
|
|
63
|
-
*
|
|
146
|
+
* resolveWith: "base64DataUri"
|
|
64
147
|
* });
|
|
65
148
|
*
|
|
66
149
|
* // Result:
|
|
@@ -87,36 +170,106 @@ declare class MediaManager {
|
|
|
87
170
|
static parseReferenceString(referenceString: string): ParsedMediaReference;
|
|
88
171
|
}
|
|
89
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Enumeration of chat message types in Langfuse prompts.
|
|
175
|
+
*
|
|
176
|
+
* @public
|
|
177
|
+
*/
|
|
90
178
|
declare enum ChatMessageType {
|
|
179
|
+
/** Regular chat message with role and content */
|
|
91
180
|
ChatMessage = "chatmessage",
|
|
181
|
+
/** Placeholder for dynamic content insertion */
|
|
92
182
|
Placeholder = "placeholder"
|
|
93
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Union type representing either a chat message or a placeholder.
|
|
186
|
+
*
|
|
187
|
+
* Used in compiled prompts where placeholders may remain unresolved.
|
|
188
|
+
*
|
|
189
|
+
* @public
|
|
190
|
+
*/
|
|
94
191
|
type ChatMessageOrPlaceholder = ChatMessage | ({
|
|
95
192
|
type: ChatMessageType.Placeholder;
|
|
96
193
|
} & PlaceholderMessage);
|
|
194
|
+
/**
|
|
195
|
+
* Represents a LangChain MessagesPlaceholder object.
|
|
196
|
+
*
|
|
197
|
+
* Used when converting Langfuse prompts to LangChain format,
|
|
198
|
+
* unresolved placeholders become LangChain MessagesPlaceholder objects.
|
|
199
|
+
*
|
|
200
|
+
* @public
|
|
201
|
+
*/
|
|
97
202
|
type LangchainMessagesPlaceholder = {
|
|
203
|
+
/** Name of the variable that will provide the messages */
|
|
98
204
|
variableName: string;
|
|
205
|
+
/** Whether the placeholder is optional (defaults to false) */
|
|
99
206
|
optional?: boolean;
|
|
100
207
|
};
|
|
208
|
+
/**
|
|
209
|
+
* Type for creating chat prompts that support both regular messages and placeholders.
|
|
210
|
+
*
|
|
211
|
+
* Extends the standard chat prompt creation request to allow mixed content types.
|
|
212
|
+
*
|
|
213
|
+
* @public
|
|
214
|
+
*/
|
|
101
215
|
type CreateChatPromptBodyWithPlaceholders = {
|
|
216
|
+
/** Specifies this is a chat prompt */
|
|
102
217
|
type: "chat";
|
|
103
218
|
} & Omit<CreatePromptRequest.Chat, "type" | "prompt"> & {
|
|
219
|
+
/** Array of chat messages and/or placeholders */
|
|
104
220
|
prompt: (ChatMessage | ChatMessageWithPlaceholders)[];
|
|
105
221
|
};
|
|
106
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Base class for all prompt clients.
|
|
225
|
+
*
|
|
226
|
+
* @internal
|
|
227
|
+
*/
|
|
107
228
|
declare abstract class BasePromptClient {
|
|
229
|
+
/** The name of the prompt */
|
|
108
230
|
readonly name: string;
|
|
231
|
+
/** The version number of the prompt */
|
|
109
232
|
readonly version: number;
|
|
233
|
+
/** Configuration object associated with the prompt */
|
|
110
234
|
readonly config: unknown;
|
|
235
|
+
/** Labels associated with the prompt */
|
|
111
236
|
readonly labels: string[];
|
|
237
|
+
/** Tags associated with the prompt */
|
|
112
238
|
readonly tags: string[];
|
|
239
|
+
/** Whether this prompt client is using fallback content */
|
|
113
240
|
readonly isFallback: boolean;
|
|
241
|
+
/** The type of prompt ("text" or "chat") */
|
|
114
242
|
readonly type: "text" | "chat";
|
|
243
|
+
/** Optional commit message for the prompt version */
|
|
115
244
|
readonly commitMessage: string | null | undefined;
|
|
245
|
+
/**
|
|
246
|
+
* Creates a new BasePromptClient instance.
|
|
247
|
+
*
|
|
248
|
+
* @param prompt - The base prompt data
|
|
249
|
+
* @param isFallback - Whether this is fallback content
|
|
250
|
+
* @param type - The prompt type
|
|
251
|
+
* @internal
|
|
252
|
+
*/
|
|
116
253
|
constructor(prompt: BasePrompt, isFallback: boolean | undefined, type: "text" | "chat");
|
|
254
|
+
/** Gets the raw prompt content */
|
|
117
255
|
abstract get prompt(): string | ChatMessageWithPlaceholders[];
|
|
256
|
+
/** Sets the raw prompt content */
|
|
118
257
|
abstract set prompt(value: string | ChatMessageWithPlaceholders[]);
|
|
258
|
+
/**
|
|
259
|
+
* Compiles the prompt by substituting variables and resolving placeholders.
|
|
260
|
+
*
|
|
261
|
+
* @param variables - Key-value pairs for variable substitution
|
|
262
|
+
* @param placeholders - Key-value pairs for placeholder resolution
|
|
263
|
+
* @returns The compiled prompt content
|
|
264
|
+
*/
|
|
119
265
|
abstract compile(variables?: Record<string, string>, placeholders?: Record<string, any>): string | ChatMessage[] | (ChatMessageOrPlaceholder | any)[];
|
|
266
|
+
/**
|
|
267
|
+
* Converts the prompt to a format compatible with LangChain.
|
|
268
|
+
*
|
|
269
|
+
* @param options - Options for conversion
|
|
270
|
+
* @param options.placeholders - Placeholders to resolve during conversion
|
|
271
|
+
* @returns The prompt in LangChain-compatible format
|
|
272
|
+
*/
|
|
120
273
|
abstract getLangchainPrompt(options?: {
|
|
121
274
|
placeholders?: Record<string, any>;
|
|
122
275
|
}): string | ChatMessage[] | ChatMessageOrPlaceholder[] | (ChatMessage | LangchainMessagesPlaceholder | any)[];
|
|
@@ -133,68 +286,264 @@ declare abstract class BasePromptClient {
|
|
|
133
286
|
* @returns The string with JSON-related braces doubled.
|
|
134
287
|
*/
|
|
135
288
|
protected escapeJsonForLangchain(text: string): string;
|
|
289
|
+
/**
|
|
290
|
+
* Serializes the prompt client to JSON.
|
|
291
|
+
*
|
|
292
|
+
* @returns JSON string representation of the prompt
|
|
293
|
+
*/
|
|
136
294
|
abstract toJSON(): string;
|
|
137
295
|
}
|
|
296
|
+
/**
|
|
297
|
+
* Client for working with text-based prompts.
|
|
298
|
+
*
|
|
299
|
+
* Provides methods to compile text prompts with variable substitution
|
|
300
|
+
* and convert them to LangChain-compatible formats.
|
|
301
|
+
*
|
|
302
|
+
* @public
|
|
303
|
+
*/
|
|
138
304
|
declare class TextPromptClient extends BasePromptClient {
|
|
305
|
+
/** The original prompt response from the API */
|
|
139
306
|
readonly promptResponse: Prompt.Text;
|
|
307
|
+
/** The text content of the prompt */
|
|
140
308
|
readonly prompt: string;
|
|
309
|
+
/**
|
|
310
|
+
* Creates a new TextPromptClient instance.
|
|
311
|
+
*
|
|
312
|
+
* @param prompt - The text prompt data
|
|
313
|
+
* @param isFallback - Whether this is fallback content
|
|
314
|
+
*/
|
|
141
315
|
constructor(prompt: Prompt.Text, isFallback?: boolean);
|
|
316
|
+
/**
|
|
317
|
+
* Compiles the text prompt by substituting variables.
|
|
318
|
+
*
|
|
319
|
+
* Uses Mustache templating to replace {{variable}} placeholders with provided values.
|
|
320
|
+
*
|
|
321
|
+
* @param variables - Key-value pairs for variable substitution
|
|
322
|
+
* @param _placeholders - Ignored for text prompts
|
|
323
|
+
* @returns The compiled text with variables substituted
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```typescript
|
|
327
|
+
* const prompt = await langfuse.prompt.get("greeting", { type: "text" });
|
|
328
|
+
* const compiled = prompt.compile({ name: "Alice" });
|
|
329
|
+
* // If prompt is "Hello {{name}}!", result is "Hello Alice!"
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
142
332
|
compile(variables?: Record<string, string>, _placeholders?: Record<string, any>): string;
|
|
333
|
+
/**
|
|
334
|
+
* Converts the prompt to LangChain PromptTemplate format.
|
|
335
|
+
*
|
|
336
|
+
* Transforms Mustache-style {{variable}} syntax to LangChain's {variable} format.
|
|
337
|
+
*
|
|
338
|
+
* @param _options - Ignored for text prompts
|
|
339
|
+
* @returns The prompt string compatible with LangChain PromptTemplate
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```typescript
|
|
343
|
+
* const prompt = await langfuse.prompt.get("greeting", { type: "text" });
|
|
344
|
+
* const langchainFormat = prompt.getLangchainPrompt();
|
|
345
|
+
* // Transforms "Hello {{name}}!" to "Hello {name}!"
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
143
348
|
getLangchainPrompt(_options?: {
|
|
144
349
|
placeholders?: Record<string, any>;
|
|
145
350
|
}): string;
|
|
146
351
|
toJSON(): string;
|
|
147
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* Client for working with chat-based prompts.
|
|
355
|
+
*
|
|
356
|
+
* Provides methods to compile chat prompts with variable substitution and
|
|
357
|
+
* placeholder resolution, and convert them to LangChain-compatible formats.
|
|
358
|
+
*
|
|
359
|
+
* @public
|
|
360
|
+
*/
|
|
148
361
|
declare class ChatPromptClient extends BasePromptClient {
|
|
362
|
+
/** The original prompt response from the API */
|
|
149
363
|
readonly promptResponse: Prompt.Chat;
|
|
364
|
+
/** The chat messages that make up the prompt */
|
|
150
365
|
readonly prompt: ChatMessageWithPlaceholders[];
|
|
366
|
+
/**
|
|
367
|
+
* Creates a new ChatPromptClient instance.
|
|
368
|
+
*
|
|
369
|
+
* @param prompt - The chat prompt data
|
|
370
|
+
* @param isFallback - Whether this is fallback content
|
|
371
|
+
*/
|
|
151
372
|
constructor(prompt: Prompt.Chat, isFallback?: boolean);
|
|
152
373
|
private static normalizePrompt;
|
|
374
|
+
/**
|
|
375
|
+
* Compiles the chat prompt by replacing placeholders and variables.
|
|
376
|
+
*
|
|
377
|
+
* First resolves placeholders with provided values, then applies variable substitution
|
|
378
|
+
* to message content using Mustache templating. Unresolved placeholders remain
|
|
379
|
+
* as placeholder objects in the output.
|
|
380
|
+
*
|
|
381
|
+
* @param variables - Key-value pairs for Mustache variable substitution in message content
|
|
382
|
+
* @param placeholders - Key-value pairs where keys are placeholder names and values are ChatMessage arrays
|
|
383
|
+
* @returns Array of ChatMessage objects and unresolved placeholder objects
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```typescript
|
|
387
|
+
* const prompt = await langfuse.prompt.get("conversation", { type: "chat" });
|
|
388
|
+
* const compiled = prompt.compile(
|
|
389
|
+
* { user_name: "Alice" },
|
|
390
|
+
* { examples: [{ role: "user", content: "Hello" }, { role: "assistant", content: "Hi!" }] }
|
|
391
|
+
* );
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
153
394
|
compile(variables?: Record<string, string>, placeholders?: Record<string, any>): (ChatMessageOrPlaceholder | any)[];
|
|
395
|
+
/**
|
|
396
|
+
* Converts the prompt to LangChain ChatPromptTemplate format.
|
|
397
|
+
*
|
|
398
|
+
* Resolves placeholders with provided values and converts unresolved ones
|
|
399
|
+
* to LangChain MessagesPlaceholder objects. Transforms variables from
|
|
400
|
+
* {{var}} to {var} format without rendering them.
|
|
401
|
+
*
|
|
402
|
+
* @param options - Configuration object
|
|
403
|
+
* @param options.placeholders - Key-value pairs for placeholder resolution
|
|
404
|
+
* @returns Array of ChatMessage objects and LangChain MessagesPlaceholder objects
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```typescript
|
|
408
|
+
* const prompt = await langfuse.prompt.get("conversation", { type: "chat" });
|
|
409
|
+
* const langchainFormat = prompt.getLangchainPrompt({
|
|
410
|
+
* placeholders: { examples: [{ role: "user", content: "Hello" }] }
|
|
411
|
+
* });
|
|
412
|
+
* ```
|
|
413
|
+
*/
|
|
154
414
|
getLangchainPrompt(options?: {
|
|
155
415
|
placeholders?: Record<string, any>;
|
|
156
416
|
}): (ChatMessage | LangchainMessagesPlaceholder | any)[];
|
|
157
417
|
toJSON(): string;
|
|
158
418
|
}
|
|
159
419
|
|
|
420
|
+
/**
|
|
421
|
+
* Manager for prompt operations in Langfuse.
|
|
422
|
+
*
|
|
423
|
+
* Provides methods to create, retrieve, and manage prompts with built-in caching
|
|
424
|
+
* for optimal performance. Supports both text and chat prompts with variable
|
|
425
|
+
* substitution and placeholder functionality.
|
|
426
|
+
*
|
|
427
|
+
* @public
|
|
428
|
+
*/
|
|
160
429
|
declare class PromptManager {
|
|
161
430
|
private cache;
|
|
162
431
|
private apiClient;
|
|
432
|
+
/**
|
|
433
|
+
* Creates a new PromptManager instance.
|
|
434
|
+
*
|
|
435
|
+
* @param params - Configuration object containing the API client
|
|
436
|
+
* @internal
|
|
437
|
+
*/
|
|
163
438
|
constructor(params: {
|
|
164
439
|
apiClient: LangfuseAPIClient;
|
|
165
440
|
});
|
|
166
441
|
get logger(): _langfuse_core.Logger;
|
|
442
|
+
/**
|
|
443
|
+
* Creates a new prompt in Langfuse.
|
|
444
|
+
*
|
|
445
|
+
* @param body - The prompt data to create (chat prompt)
|
|
446
|
+
* @returns Promise that resolves to a ChatPromptClient
|
|
447
|
+
*/
|
|
167
448
|
create(body: CreateChatPromptBodyWithPlaceholders): Promise<ChatPromptClient>;
|
|
449
|
+
/**
|
|
450
|
+
* Creates a new prompt in Langfuse.
|
|
451
|
+
*
|
|
452
|
+
* @param body - The prompt data to create (text prompt)
|
|
453
|
+
* @returns Promise that resolves to a TextPromptClient
|
|
454
|
+
*/
|
|
168
455
|
create(body: Omit<CreatePromptRequest.Text, "type"> & {
|
|
169
456
|
type?: "text";
|
|
170
457
|
}): Promise<TextPromptClient>;
|
|
458
|
+
/**
|
|
459
|
+
* Creates a new prompt in Langfuse.
|
|
460
|
+
*
|
|
461
|
+
* @param body - The prompt data to create (chat prompt)
|
|
462
|
+
* @returns Promise that resolves to a ChatPromptClient
|
|
463
|
+
*/
|
|
171
464
|
create(body: CreatePromptRequest.Chat): Promise<ChatPromptClient>;
|
|
465
|
+
/**
|
|
466
|
+
* Updates the labels of an existing prompt version.
|
|
467
|
+
*
|
|
468
|
+
* @param params - Update parameters
|
|
469
|
+
* @param params.name - Name of the prompt to update
|
|
470
|
+
* @param params.version - Version number of the prompt to update
|
|
471
|
+
* @param params.newLabels - New labels to apply to the prompt version
|
|
472
|
+
*
|
|
473
|
+
* @returns Promise that resolves to the updated prompt
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```typescript
|
|
477
|
+
* const updatedPrompt = await langfuse.prompt.update({
|
|
478
|
+
* name: "my-prompt",
|
|
479
|
+
* version: 1,
|
|
480
|
+
* newLabels: ["production", "v2"]
|
|
481
|
+
* });
|
|
482
|
+
* ```
|
|
483
|
+
*/
|
|
172
484
|
update(params: {
|
|
173
485
|
name: string;
|
|
174
486
|
version: number;
|
|
175
487
|
newLabels: string[];
|
|
176
488
|
}): Promise<Prompt>;
|
|
489
|
+
/**
|
|
490
|
+
* Retrieves a text prompt by name.
|
|
491
|
+
*
|
|
492
|
+
* @param name - Name of the prompt to retrieve
|
|
493
|
+
* @param options - Optional retrieval configuration
|
|
494
|
+
* @returns Promise that resolves to a TextPromptClient
|
|
495
|
+
*/
|
|
177
496
|
get(name: string, options?: {
|
|
497
|
+
/** Specific version to retrieve (defaults to latest) */
|
|
178
498
|
version?: number;
|
|
499
|
+
/** Label to filter by */
|
|
179
500
|
label?: string;
|
|
501
|
+
/** Cache TTL in seconds (0 to disable caching) */
|
|
180
502
|
cacheTtlSeconds?: number;
|
|
503
|
+
/** Fallback text content if prompt fetch fails */
|
|
181
504
|
fallback?: string;
|
|
505
|
+
/** Maximum retry attempts for failed requests */
|
|
182
506
|
maxRetries?: number;
|
|
507
|
+
/** Specify text prompt type */
|
|
183
508
|
type?: "text";
|
|
509
|
+
/** Request timeout in milliseconds */
|
|
184
510
|
fetchTimeoutMs?: number;
|
|
185
511
|
}): Promise<TextPromptClient>;
|
|
512
|
+
/**
|
|
513
|
+
* Retrieves a chat prompt by name.
|
|
514
|
+
*
|
|
515
|
+
* @param name - Name of the prompt to retrieve
|
|
516
|
+
* @param options - Optional retrieval configuration
|
|
517
|
+
* @returns Promise that resolves to a ChatPromptClient
|
|
518
|
+
*/
|
|
186
519
|
get(name: string, options?: {
|
|
520
|
+
/** Specific version to retrieve (defaults to latest) */
|
|
187
521
|
version?: number;
|
|
522
|
+
/** Label to filter by */
|
|
188
523
|
label?: string;
|
|
524
|
+
/** Cache TTL in seconds (0 to disable caching) */
|
|
189
525
|
cacheTtlSeconds?: number;
|
|
526
|
+
/** Fallback chat messages if prompt fetch fails */
|
|
190
527
|
fallback?: ChatMessage[];
|
|
528
|
+
/** Maximum retry attempts for failed requests */
|
|
191
529
|
maxRetries?: number;
|
|
530
|
+
/** Specify chat prompt type */
|
|
192
531
|
type: "chat";
|
|
532
|
+
/** Request timeout in milliseconds */
|
|
193
533
|
fetchTimeoutMs?: number;
|
|
194
534
|
}): Promise<ChatPromptClient>;
|
|
195
535
|
private fetchPromptAndUpdateCache;
|
|
196
536
|
}
|
|
197
537
|
|
|
538
|
+
/**
|
|
539
|
+
* Manager for creating and batching score events in Langfuse.
|
|
540
|
+
*
|
|
541
|
+
* The ScoreManager handles automatic batching and flushing of score events
|
|
542
|
+
* to optimize API usage. Scores are automatically sent when the queue reaches
|
|
543
|
+
* a certain size or after a time interval.
|
|
544
|
+
*
|
|
545
|
+
* @public
|
|
546
|
+
*/
|
|
198
547
|
declare class ScoreManager {
|
|
199
548
|
private apiClient;
|
|
200
549
|
private eventQueue;
|
|
@@ -202,36 +551,247 @@ declare class ScoreManager {
|
|
|
202
551
|
private flushTimer;
|
|
203
552
|
private flushAtCount;
|
|
204
553
|
private flushIntervalSeconds;
|
|
554
|
+
/**
|
|
555
|
+
* Creates a new ScoreManager instance.
|
|
556
|
+
*
|
|
557
|
+
* @param params - Configuration object containing the API client
|
|
558
|
+
* @internal
|
|
559
|
+
*/
|
|
205
560
|
constructor(params: {
|
|
206
561
|
apiClient: LangfuseAPIClient;
|
|
207
562
|
});
|
|
208
563
|
get logger(): _langfuse_core.Logger;
|
|
564
|
+
/**
|
|
565
|
+
* Creates a new score event and adds it to the processing queue.
|
|
566
|
+
*
|
|
567
|
+
* Scores are queued and sent in batches for efficiency. The score will be
|
|
568
|
+
* automatically sent when the queue reaches the flush threshold or after
|
|
569
|
+
* the flush interval expires.
|
|
570
|
+
*
|
|
571
|
+
* @param data - The score data to create
|
|
572
|
+
*
|
|
573
|
+
* @example
|
|
574
|
+
* ```typescript
|
|
575
|
+
* langfuse.score.create({
|
|
576
|
+
* name: "quality",
|
|
577
|
+
* value: 0.85,
|
|
578
|
+
* traceId: "trace-123",
|
|
579
|
+
* comment: "High quality response"
|
|
580
|
+
* });
|
|
581
|
+
* ```
|
|
582
|
+
*/
|
|
209
583
|
create(data: ScoreBody): void;
|
|
584
|
+
/**
|
|
585
|
+
* Creates a score for a specific observation using its OpenTelemetry span.
|
|
586
|
+
*
|
|
587
|
+
* This method automatically extracts the trace ID and observation ID from
|
|
588
|
+
* the provided span context.
|
|
589
|
+
*
|
|
590
|
+
* @param observation - Object containing the OpenTelemetry span
|
|
591
|
+
* @param data - Score data (traceId and observationId will be auto-populated)
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* ```typescript
|
|
595
|
+
* import { startSpan } from '@langfuse/tracing';
|
|
596
|
+
*
|
|
597
|
+
* const span = startSpan({ name: "my-operation" });
|
|
598
|
+
* langfuse.score.observation(
|
|
599
|
+
* { otelSpan: span },
|
|
600
|
+
* { name: "accuracy", value: 0.92 }
|
|
601
|
+
* );
|
|
602
|
+
* ```
|
|
603
|
+
*/
|
|
210
604
|
observation(observation: {
|
|
211
605
|
otelSpan: Span;
|
|
212
606
|
}, data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
|
|
607
|
+
/**
|
|
608
|
+
* Creates a score for a trace using an OpenTelemetry span.
|
|
609
|
+
*
|
|
610
|
+
* This method automatically extracts the trace ID from the provided
|
|
611
|
+
* span context and creates a trace-level score.
|
|
612
|
+
*
|
|
613
|
+
* @param observation - Object containing the OpenTelemetry span
|
|
614
|
+
* @param data - Score data (traceId will be auto-populated)
|
|
615
|
+
*
|
|
616
|
+
* @example
|
|
617
|
+
* ```typescript
|
|
618
|
+
* import { startSpan } from '@langfuse/tracing';
|
|
619
|
+
*
|
|
620
|
+
* const span = startSpan({ name: "my-operation" });
|
|
621
|
+
* langfuse.score.trace(
|
|
622
|
+
* { otelSpan: span },
|
|
623
|
+
* { name: "overall_quality", value: 0.88 }
|
|
624
|
+
* );
|
|
625
|
+
* ```
|
|
626
|
+
*/
|
|
213
627
|
trace(observation: {
|
|
214
628
|
otelSpan: Span;
|
|
215
629
|
}, data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
|
|
630
|
+
/**
|
|
631
|
+
* Creates a score for the currently active observation.
|
|
632
|
+
*
|
|
633
|
+
* This method automatically detects the active OpenTelemetry span and
|
|
634
|
+
* creates an observation-level score. If no active span is found,
|
|
635
|
+
* a warning is logged and the operation is skipped.
|
|
636
|
+
*
|
|
637
|
+
* @param data - Score data (traceId and observationId will be auto-populated)
|
|
638
|
+
*
|
|
639
|
+
* @example
|
|
640
|
+
* ```typescript
|
|
641
|
+
* import { startActiveSpan } from '@langfuse/tracing';
|
|
642
|
+
*
|
|
643
|
+
* startActiveSpan({ name: "my-operation" }, (span) => {
|
|
644
|
+
* // Inside the active span
|
|
645
|
+
* langfuse.score.activeObservation({
|
|
646
|
+
* name: "relevance",
|
|
647
|
+
* value: 0.95
|
|
648
|
+
* });
|
|
649
|
+
* });
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
216
652
|
activeObservation(data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
|
|
653
|
+
/**
|
|
654
|
+
* Creates a score for the currently active trace.
|
|
655
|
+
*
|
|
656
|
+
* This method automatically detects the active OpenTelemetry span and
|
|
657
|
+
* creates a trace-level score. If no active span is found,
|
|
658
|
+
* a warning is logged and the operation is skipped.
|
|
659
|
+
*
|
|
660
|
+
* @param data - Score data (traceId will be auto-populated)
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* ```typescript
|
|
664
|
+
* import { startActiveSpan } from '@langfuse/tracing';
|
|
665
|
+
*
|
|
666
|
+
* startActiveSpan({ name: "my-operation" }, (span) => {
|
|
667
|
+
* // Inside the active span
|
|
668
|
+
* langfuse.score.activeTrace({
|
|
669
|
+
* name: "user_satisfaction",
|
|
670
|
+
* value: 4,
|
|
671
|
+
* comment: "User rated 4 out of 5 stars"
|
|
672
|
+
* });
|
|
673
|
+
* });
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
217
676
|
activeTrace(data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
|
|
218
677
|
private handleFlush;
|
|
678
|
+
/**
|
|
679
|
+
* Flushes all pending score events to the Langfuse API.
|
|
680
|
+
*
|
|
681
|
+
* This method ensures all queued scores are sent immediately rather than
|
|
682
|
+
* waiting for the automatic flush interval or batch size threshold.
|
|
683
|
+
*
|
|
684
|
+
* @returns Promise that resolves when all pending scores have been sent
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```typescript
|
|
688
|
+
* langfuse.score.create({ name: "quality", value: 0.8 });
|
|
689
|
+
* await langfuse.score.flush(); // Ensures the score is sent immediately
|
|
690
|
+
* ```
|
|
691
|
+
*/
|
|
219
692
|
flush(): Promise<void>;
|
|
693
|
+
/**
|
|
694
|
+
* Gracefully shuts down the score manager by flushing all pending scores.
|
|
695
|
+
*
|
|
696
|
+
* This method should be called before your application exits to ensure
|
|
697
|
+
* all score data is sent to Langfuse.
|
|
698
|
+
*
|
|
699
|
+
* @returns Promise that resolves when shutdown is complete
|
|
700
|
+
*
|
|
701
|
+
* @example
|
|
702
|
+
* ```typescript
|
|
703
|
+
* // Before application exit
|
|
704
|
+
* await langfuse.score.shutdown();
|
|
705
|
+
* ```
|
|
706
|
+
*/
|
|
220
707
|
shutdown(): Promise<void>;
|
|
221
708
|
}
|
|
222
709
|
|
|
710
|
+
/**
|
|
711
|
+
* Configuration parameters for initializing a LangfuseClient instance.
|
|
712
|
+
*
|
|
713
|
+
* @public
|
|
714
|
+
*/
|
|
223
715
|
interface LangfuseClientParams {
|
|
716
|
+
/**
|
|
717
|
+
* Public API key for authentication with Langfuse.
|
|
718
|
+
* Can also be provided via LANGFUSE_PUBLIC_KEY environment variable.
|
|
719
|
+
*/
|
|
224
720
|
publicKey?: string;
|
|
721
|
+
/**
|
|
722
|
+
* Secret API key for authentication with Langfuse.
|
|
723
|
+
* Can also be provided via LANGFUSE_SECRET_KEY environment variable.
|
|
724
|
+
*/
|
|
225
725
|
secretKey?: string;
|
|
726
|
+
/**
|
|
727
|
+
* Base URL of the Langfuse instance to connect to.
|
|
728
|
+
* Can also be provided via LANGFUSE_BASE_URL environment variable.
|
|
729
|
+
*
|
|
730
|
+
* @defaultValue "https://cloud.langfuse.com"
|
|
731
|
+
*/
|
|
226
732
|
baseUrl?: string;
|
|
733
|
+
/**
|
|
734
|
+
* Request timeout in seconds.
|
|
735
|
+
* Can also be provided via LANGFUSE_TIMEOUT environment variable.
|
|
736
|
+
*
|
|
737
|
+
* @defaultValue 5
|
|
738
|
+
*/
|
|
227
739
|
timeout?: number;
|
|
740
|
+
/**
|
|
741
|
+
* Additional HTTP headers to include with API requests.
|
|
742
|
+
*/
|
|
228
743
|
additionalHeaders?: Record<string, string>;
|
|
229
744
|
}
|
|
745
|
+
/**
|
|
746
|
+
* Main client for interacting with the Langfuse API.
|
|
747
|
+
*
|
|
748
|
+
* The LangfuseClient provides access to all Langfuse functionality including:
|
|
749
|
+
* - Prompt management and retrieval
|
|
750
|
+
* - Dataset operations
|
|
751
|
+
* - Score creation and management
|
|
752
|
+
* - Media upload and handling
|
|
753
|
+
* - Direct API access for advanced use cases
|
|
754
|
+
*
|
|
755
|
+
* @example
|
|
756
|
+
* ```typescript
|
|
757
|
+
* // Initialize with explicit credentials
|
|
758
|
+
* const langfuse = new LangfuseClient({
|
|
759
|
+
* publicKey: "pk_...",
|
|
760
|
+
* secretKey: "sk_...",
|
|
761
|
+
* baseUrl: "https://cloud.langfuse.com"
|
|
762
|
+
* });
|
|
763
|
+
*
|
|
764
|
+
* // Or use environment variables
|
|
765
|
+
* const langfuse = new LangfuseClient();
|
|
766
|
+
*
|
|
767
|
+
* // Use the client
|
|
768
|
+
* const prompt = await langfuse.prompt.get("my-prompt");
|
|
769
|
+
* const compiledPrompt = prompt.compile({ variable: "value" });
|
|
770
|
+
* ```
|
|
771
|
+
*
|
|
772
|
+
* @public
|
|
773
|
+
*/
|
|
230
774
|
declare class LangfuseClient {
|
|
775
|
+
/**
|
|
776
|
+
* Direct access to the underlying Langfuse API client.
|
|
777
|
+
* Use this for advanced API operations not covered by the high-level managers.
|
|
778
|
+
*/
|
|
231
779
|
api: LangfuseAPIClient;
|
|
780
|
+
/**
|
|
781
|
+
* Manager for prompt operations including creation, retrieval, and caching.
|
|
782
|
+
*/
|
|
232
783
|
prompt: PromptManager;
|
|
784
|
+
/**
|
|
785
|
+
* Manager for dataset operations including retrieval and item linking.
|
|
786
|
+
*/
|
|
233
787
|
dataset: DatasetManager;
|
|
788
|
+
/**
|
|
789
|
+
* Manager for score creation and batch processing.
|
|
790
|
+
*/
|
|
234
791
|
score: ScoreManager;
|
|
792
|
+
/**
|
|
793
|
+
* Manager for media upload and reference resolution.
|
|
794
|
+
*/
|
|
235
795
|
media: MediaManager;
|
|
236
796
|
private baseUrl;
|
|
237
797
|
private projectId;
|
|
@@ -299,9 +859,70 @@ declare class LangfuseClient {
|
|
|
299
859
|
* @deprecated Use media.resolveReferences instead
|
|
300
860
|
*/
|
|
301
861
|
resolveMediaReferences: typeof MediaManager.prototype.resolveReferences;
|
|
862
|
+
/**
|
|
863
|
+
* Creates a new LangfuseClient instance.
|
|
864
|
+
*
|
|
865
|
+
* @param params - Configuration parameters. If not provided, will use environment variables.
|
|
866
|
+
*
|
|
867
|
+
* @throws Will log warnings if required credentials are not provided
|
|
868
|
+
*
|
|
869
|
+
* @example
|
|
870
|
+
* ```typescript
|
|
871
|
+
* // With explicit configuration
|
|
872
|
+
* const client = new LangfuseClient({
|
|
873
|
+
* publicKey: "pk_...",
|
|
874
|
+
* secretKey: "sk_...",
|
|
875
|
+
* baseUrl: "https://your-instance.langfuse.com"
|
|
876
|
+
* });
|
|
877
|
+
*
|
|
878
|
+
* // Using environment variables
|
|
879
|
+
* const client = new LangfuseClient();
|
|
880
|
+
* ```
|
|
881
|
+
*/
|
|
302
882
|
constructor(params?: LangfuseClientParams);
|
|
883
|
+
/**
|
|
884
|
+
* Flushes any pending score events to the Langfuse API.
|
|
885
|
+
*
|
|
886
|
+
* This method ensures all queued scores are sent immediately rather than
|
|
887
|
+
* waiting for the automatic flush interval or batch size threshold.
|
|
888
|
+
*
|
|
889
|
+
* @returns Promise that resolves when all pending scores have been sent
|
|
890
|
+
*
|
|
891
|
+
* @example
|
|
892
|
+
* ```typescript
|
|
893
|
+
* langfuse.score.create({ name: "quality", value: 0.8 });
|
|
894
|
+
* await langfuse.flush(); // Ensures the score is sent immediately
|
|
895
|
+
* ```
|
|
896
|
+
*/
|
|
303
897
|
flush(): Promise<void>;
|
|
898
|
+
/**
|
|
899
|
+
* Gracefully shuts down the client by flushing all pending data.
|
|
900
|
+
*
|
|
901
|
+
* This method should be called before your application exits to ensure
|
|
902
|
+
* all data is sent to Langfuse.
|
|
903
|
+
*
|
|
904
|
+
* @returns Promise that resolves when shutdown is complete
|
|
905
|
+
*
|
|
906
|
+
* @example
|
|
907
|
+
* ```typescript
|
|
908
|
+
* // Before application exit
|
|
909
|
+
* await langfuse.shutdown();
|
|
910
|
+
* ```
|
|
911
|
+
*/
|
|
304
912
|
shutdown(): Promise<void>;
|
|
913
|
+
/**
|
|
914
|
+
* Generates a URL to view a specific trace in the Langfuse UI.
|
|
915
|
+
*
|
|
916
|
+
* @param traceId - The ID of the trace to generate a URL for
|
|
917
|
+
* @returns Promise that resolves to the trace URL
|
|
918
|
+
*
|
|
919
|
+
* @example
|
|
920
|
+
* ```typescript
|
|
921
|
+
* const traceId = "trace-123";
|
|
922
|
+
* const url = await langfuse.getTraceUrl(traceId);
|
|
923
|
+
* console.log(`View trace at: ${url}`);
|
|
924
|
+
* ```
|
|
925
|
+
*/
|
|
305
926
|
getTraceUrl(traceId: string): Promise<string>;
|
|
306
927
|
}
|
|
307
928
|
|