@langfuse/client 4.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2023 Finto Technologies GmbH
2
+
3
+ Copyright (c) 2022 PostHog (part of Hiberly Inc)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,90 @@
1
+ import { LangfuseAPIClient } from "@langfuse/core";
2
+ import { DatasetManager } from "./dataset/index.js";
3
+ import { MediaManager } from "./media/index.js";
4
+ import { PromptManager } from "./prompt/index.js";
5
+ import { ScoreManager } from "./score/index.js";
6
+ export interface LangfuseClientParams {
7
+ publicKey?: string;
8
+ secretKey?: string;
9
+ baseUrl?: string;
10
+ timeout?: number;
11
+ additionalHeaders?: Record<string, string>;
12
+ }
13
+ export declare class LangfuseClient {
14
+ api: LangfuseAPIClient;
15
+ prompt: PromptManager;
16
+ dataset: DatasetManager;
17
+ score: ScoreManager;
18
+ media: MediaManager;
19
+ private baseUrl;
20
+ private projectId;
21
+ /**
22
+ * @deprecated Use prompt.get instead
23
+ */
24
+ getPrompt: typeof PromptManager.prototype.get;
25
+ /**
26
+ * @deprecated Use prompt.create instead
27
+ */
28
+ createPrompt: typeof PromptManager.prototype.create;
29
+ /**
30
+ * @deprecated Use prompt.update instead
31
+ */
32
+ updatePrompt: typeof PromptManager.prototype.update;
33
+ /**
34
+ * @deprecated Use dataset.get instead
35
+ */
36
+ getDataset: typeof DatasetManager.prototype.get;
37
+ /**
38
+ * @deprecated Use api.trace.get instead
39
+ */
40
+ fetchTrace: typeof LangfuseAPIClient.prototype.trace.get;
41
+ /**
42
+ * @deprecated Use api.trace.list instead
43
+ */
44
+ fetchTraces: typeof LangfuseAPIClient.prototype.trace.list;
45
+ /**
46
+ * @deprecated Use api.observations.get instead
47
+ */
48
+ fetchObservation: typeof LangfuseAPIClient.prototype.observations.get;
49
+ /**
50
+ * @deprecated Use api.observations.list instead
51
+ */
52
+ fetchObservations: typeof LangfuseAPIClient.prototype.observations.getMany;
53
+ /**
54
+ * @deprecated Use api.sessions.get instead
55
+ */
56
+ fetchSessions: typeof LangfuseAPIClient.prototype.sessions.get;
57
+ /**
58
+ * @deprecated Use api.datasets.getRun instead
59
+ */
60
+ getDatasetRun: typeof LangfuseAPIClient.prototype.datasets.getRun;
61
+ /**
62
+ * @deprecated Use api.datasets.getRuns instead
63
+ */
64
+ getDatasetRuns: typeof LangfuseAPIClient.prototype.datasets.getRuns;
65
+ /**
66
+ * @deprecated Use api.datasets.create instead
67
+ */
68
+ createDataset: typeof LangfuseAPIClient.prototype.datasets.create;
69
+ /**
70
+ * @deprecated Use api.datasetItems.get instead
71
+ */
72
+ getDatasetItem: typeof LangfuseAPIClient.prototype.datasetItems.get;
73
+ /**
74
+ * @deprecated Use api.datasetItems.create instead
75
+ */
76
+ createDatasetItem: typeof LangfuseAPIClient.prototype.datasetItems.create;
77
+ /**
78
+ * @deprecated Use api.media.get instead
79
+ */
80
+ fetchMedia: typeof LangfuseAPIClient.prototype.media.get;
81
+ /**
82
+ * @deprecated Use media.resolveReferences instead
83
+ */
84
+ resolveMediaReferences: typeof MediaManager.prototype.resolveReferences;
85
+ constructor(params?: LangfuseClientParams);
86
+ flush(): Promise<void>;
87
+ shutdown(): Promise<void>;
88
+ getTraceUrl(traceId: string): Promise<string>;
89
+ }
90
+ //# sourceMappingURL=LangfuseClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LangfuseClient.d.ts","sourceRoot":"","sources":["../src/LangfuseClient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAIlB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5C;AAED,qBAAa,cAAc;IAClB,GAAG,EAAE,iBAAiB,CAAC;IACvB,MAAM,EAAE,aAAa,CAAC;IACtB,OAAO,EAAE,cAAc,CAAC;IACxB,KAAK,EAAE,YAAY,CAAC;IACpB,KAAK,EAAE,YAAY,CAAC;IAE3B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAuB;IAExC;;OAEG;IACI,SAAS,EAAE,OAAO,aAAa,CAAC,SAAS,CAAC,GAAG,CAAC;IACrD;;OAEG;IACI,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iBAAiB,CAAC;gBAEnE,MAAM,CAAC,EAAE,oBAAoB;IAiE5B,KAAK;IAIL,QAAQ;IAIR,WAAW,CAAC,OAAO,EAAE,MAAM;CAYzC"}
@@ -0,0 +1,74 @@
1
+ import { LangfuseAPIClient, LANGFUSE_SDK_VERSION, getGlobalLogger, getEnv, } from "@langfuse/core";
2
+ import { DatasetManager } from "./dataset/index.js";
3
+ import { MediaManager } from "./media/index.js";
4
+ import { PromptManager } from "./prompt/index.js";
5
+ import { ScoreManager } from "./score/index.js";
6
+ export class LangfuseClient {
7
+ constructor(params) {
8
+ var _a, _b, _c, _d, _e, _f, _g;
9
+ this.projectId = null;
10
+ const logger = getGlobalLogger();
11
+ const publicKey = (_a = params === null || params === void 0 ? void 0 : params.publicKey) !== null && _a !== void 0 ? _a : getEnv("LANGFUSE_PUBLIC_KEY");
12
+ const secretKey = (_b = params === null || params === void 0 ? void 0 : params.secretKey) !== null && _b !== void 0 ? _b : getEnv("LANGFUSE_SECRET_KEY");
13
+ this.baseUrl =
14
+ (_e = (_d = (_c = params === null || params === void 0 ? void 0 : params.baseUrl) !== null && _c !== void 0 ? _c : getEnv("LANGFUSE_BASE_URL")) !== null && _d !== void 0 ? _d : getEnv("LANGFUSE_BASEURL")) !== null && _e !== void 0 ? _e : "https://cloud.langfuse.com";
15
+ if (!publicKey) {
16
+ logger.warn("No public key provided in constructor or as LANGFUSE_PUBLIC_KEY env var. Client operations will fail.");
17
+ }
18
+ if (!secretKey) {
19
+ logger.warn("No secret key provided in constructor or as LANGFUSE_SECRET_KEY env var. Client operations will fail.");
20
+ }
21
+ const timeoutSeconds = (_f = params === null || params === void 0 ? void 0 : params.timeout) !== null && _f !== void 0 ? _f : Number((_g = getEnv("LANGFUSE_TIMEOUT")) !== null && _g !== void 0 ? _g : 5);
22
+ this.api = new LangfuseAPIClient({
23
+ baseUrl: this.baseUrl,
24
+ username: publicKey,
25
+ password: secretKey,
26
+ xLangfusePublicKey: publicKey,
27
+ xLangfuseSdkVersion: LANGFUSE_SDK_VERSION,
28
+ xLangfuseSdkName: "javascript",
29
+ environment: "", // noop as baseUrl is set
30
+ headers: params === null || params === void 0 ? void 0 : params.additionalHeaders,
31
+ });
32
+ logger.debug("Initialized LangfuseClient with params:", {
33
+ publicKey,
34
+ baseUrl: this.baseUrl,
35
+ timeoutSeconds,
36
+ });
37
+ this.prompt = new PromptManager({ apiClient: this.api });
38
+ this.dataset = new DatasetManager({ apiClient: this.api });
39
+ this.score = new ScoreManager({ apiClient: this.api });
40
+ this.media = new MediaManager({ apiClient: this.api });
41
+ // Keep v3 compat by exposing old interface
42
+ this.getPrompt = this.prompt.get.bind(this.prompt); // keep correct this context for cache access
43
+ this.createPrompt = this.prompt.create.bind(this.prompt);
44
+ this.updatePrompt = this.prompt.update.bind(this.prompt);
45
+ this.getDataset = this.dataset.get;
46
+ this.fetchTrace = this.api.trace.get;
47
+ this.fetchTraces = this.api.trace.list;
48
+ this.fetchObservation = this.api.observations.get;
49
+ this.fetchObservations = this.api.observations.getMany;
50
+ this.fetchSessions = this.api.sessions.get;
51
+ this.getDatasetRun = this.api.datasets.getRun;
52
+ this.getDatasetRuns = this.api.datasets.getRuns;
53
+ this.createDataset = this.api.datasets.create;
54
+ this.getDatasetItem = this.api.datasetItems.get;
55
+ this.createDatasetItem = this.api.datasetItems.create;
56
+ this.fetchMedia = this.api.media.get;
57
+ this.resolveMediaReferences = this.media.resolveReferences;
58
+ }
59
+ async flush() {
60
+ return this.score.flush();
61
+ }
62
+ async shutdown() {
63
+ return this.score.shutdown();
64
+ }
65
+ async getTraceUrl(traceId) {
66
+ let projectId = this.projectId;
67
+ if (!projectId) {
68
+ projectId = (await this.api.projects.get()).data[0].id;
69
+ this.projectId = projectId;
70
+ }
71
+ const traceUrl = `${this.baseUrl}/project/${projectId}/traces/${traceId}`;
72
+ return traceUrl;
73
+ }
74
+ }
@@ -0,0 +1,23 @@
1
+ import { LangfuseAPIClient, Dataset, DatasetRunItem, DatasetItem } from "@langfuse/core";
2
+ import { Span } from "@opentelemetry/api";
3
+ export type LinkDatasetItemFunction = (obj: {
4
+ otelSpan: Span;
5
+ }, runName: string, runArgs?: {
6
+ description?: string;
7
+ metadata?: any;
8
+ }) => Promise<DatasetRunItem>;
9
+ export declare class DatasetManager {
10
+ private apiClient;
11
+ constructor(params: {
12
+ apiClient: LangfuseAPIClient;
13
+ });
14
+ get(name: string, options?: {
15
+ fetchItemsPageSize: number;
16
+ }): Promise<Dataset & {
17
+ items: (DatasetItem & {
18
+ link: LinkDatasetItemFunction;
19
+ })[];
20
+ }>;
21
+ private createDatasetItemLinkFunction;
22
+ }
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dataset/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,OAAO,EACP,cAAc,EACd,WAAW,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,MAAM,MAAM,uBAAuB,GAAG,CACpC,GAAG,EAAE;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,EACvB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;IACR,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,GAAG,CAAC;CAChB,KACE,OAAO,CAAC,cAAc,CAAC,CAAC;AAE7B,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAoB;gBAEzB,MAAM,EAAE;QAAE,SAAS,EAAE,iBAAiB,CAAA;KAAE;IAI9C,GAAG,CACP,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QACR,kBAAkB,EAAE,MAAM,CAAC;KAC5B,GACA,OAAO,CACR,OAAO,GAAG;QACR,KAAK,EAAE,CAAC,WAAW,GAAG;YAAE,IAAI,EAAE,uBAAuB,CAAA;SAAE,CAAC,EAAE,CAAC;KAC5D,CACF;IAiCD,OAAO,CAAC,6BAA6B;CAsBtC"}
@@ -0,0 +1,43 @@
1
+ export class DatasetManager {
2
+ constructor(params) {
3
+ this.apiClient = params.apiClient;
4
+ }
5
+ async get(name, options) {
6
+ var _a;
7
+ const dataset = await this.apiClient.datasets.get(name);
8
+ const items = [];
9
+ let page = 1;
10
+ while (true) {
11
+ const itemsResponse = await this.apiClient.datasetItems.list({
12
+ datasetName: name,
13
+ limit: (_a = options === null || options === void 0 ? void 0 : options.fetchItemsPageSize) !== null && _a !== void 0 ? _a : 50,
14
+ page,
15
+ });
16
+ items.push(...itemsResponse.data);
17
+ if (itemsResponse.meta.totalPages <= page) {
18
+ break;
19
+ }
20
+ page++;
21
+ }
22
+ const returnDataset = {
23
+ ...dataset,
24
+ items: items.map((item) => ({
25
+ ...item,
26
+ link: this.createDatasetItemLinkFunction(item),
27
+ })),
28
+ };
29
+ return returnDataset;
30
+ }
31
+ createDatasetItemLinkFunction(item) {
32
+ const linkFunction = async (obj, runName, runArgs) => {
33
+ return await this.apiClient.datasetRunItems.create({
34
+ runName,
35
+ datasetItemId: item.id,
36
+ traceId: obj.otelSpan.spanContext().traceId,
37
+ runDescription: runArgs === null || runArgs === void 0 ? void 0 : runArgs.description,
38
+ metadata: runArgs === null || runArgs === void 0 ? void 0 : runArgs.metadata,
39
+ });
40
+ };
41
+ return linkFunction;
42
+ }
43
+ }