langsmith 0.1.25 → 0.1.27
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/client.cjs +17 -2
- package/dist/client.d.ts +9 -2
- package/dist/client.js +17 -2
- package/dist/env.cjs +17 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +13 -0
- package/dist/evaluation/_runner.cjs +29 -3
- package/dist/evaluation/_runner.d.ts +2 -1
- package/dist/evaluation/_runner.js +29 -3
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/langchain.cjs +105 -0
- package/dist/langchain.d.ts +29 -0
- package/dist/langchain.js +100 -0
- package/dist/run_trees.cjs +25 -16
- package/dist/run_trees.d.ts +1 -1
- package/dist/run_trees.js +25 -16
- package/dist/schemas.d.ts +20 -0
- package/dist/singletons/traceable.cjs +62 -0
- package/dist/singletons/traceable.d.ts +23 -0
- package/dist/singletons/traceable.js +57 -0
- package/dist/singletons/types.cjs +2 -0
- package/dist/singletons/types.d.ts +38 -0
- package/dist/singletons/types.js +1 -0
- package/dist/traceable.cjs +38 -72
- package/dist/traceable.d.ts +14 -47
- package/dist/traceable.js +32 -66
- package/dist/utils/error.cjs +25 -0
- package/dist/utils/error.d.ts +1 -0
- package/dist/utils/error.js +21 -0
- package/dist/wrappers/openai.cjs +32 -0
- package/dist/wrappers/openai.js +32 -0
- package/langchain.cjs +1 -0
- package/langchain.d.cts +1 -0
- package/langchain.d.ts +1 -0
- package/langchain.js +1 -0
- package/package.json +33 -3
- package/singletons/traceable.cjs +1 -0
- package/singletons/traceable.d.cts +1 -0
- package/singletons/traceable.d.ts +1 -0
- package/singletons/traceable.js +1 -0
package/dist/run_trees.cjs
CHANGED
|
@@ -27,6 +27,7 @@ exports.isRunnableConfigLike = exports.isRunTree = exports.RunTree = exports.con
|
|
|
27
27
|
const uuid = __importStar(require("uuid"));
|
|
28
28
|
const env_js_1 = require("./utils/env.cjs");
|
|
29
29
|
const client_js_1 = require("./client.cjs");
|
|
30
|
+
const env_js_2 = require("./env.cjs");
|
|
30
31
|
const warnedMessages = {};
|
|
31
32
|
function warnOnce(message) {
|
|
32
33
|
if (!warnedMessages[message]) {
|
|
@@ -208,34 +209,42 @@ class RunTree {
|
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
211
|
}
|
|
211
|
-
static fromRunnableConfig(
|
|
212
|
+
static fromRunnableConfig(parentConfig, props) {
|
|
212
213
|
// We only handle the callback manager case for now
|
|
213
|
-
const callbackManager =
|
|
214
|
+
const callbackManager = parentConfig?.callbacks;
|
|
214
215
|
let parentRun;
|
|
215
216
|
let projectName;
|
|
217
|
+
let client;
|
|
218
|
+
let tracingEnabled = (0, env_js_2.isTracingEnabled)();
|
|
216
219
|
if (callbackManager) {
|
|
217
220
|
const parentRunId = callbackManager?.getParentRunId?.() ?? "";
|
|
218
221
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
|
|
219
222
|
parentRun = langChainTracer?.getRun?.(parentRunId);
|
|
220
223
|
projectName = langChainTracer?.projectName;
|
|
224
|
+
client = langChainTracer?.client;
|
|
225
|
+
tracingEnabled = tracingEnabled || !!langChainTracer;
|
|
221
226
|
}
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
parent_run: parentRun,
|
|
232
|
-
tags: dedupedTags,
|
|
227
|
+
const parentRunTree = new RunTree({
|
|
228
|
+
name: parentRun?.name ?? "<parent>",
|
|
229
|
+
id: parentRun?.id,
|
|
230
|
+
client,
|
|
231
|
+
tracingEnabled,
|
|
232
|
+
project_name: projectName,
|
|
233
|
+
tags: [
|
|
234
|
+
...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
|
|
235
|
+
],
|
|
233
236
|
extra: {
|
|
234
|
-
metadata:
|
|
237
|
+
metadata: {
|
|
238
|
+
...parentRun?.extra?.metadata,
|
|
239
|
+
...parentConfig?.metadata,
|
|
240
|
+
},
|
|
235
241
|
},
|
|
236
|
-
project_name: projectName,
|
|
237
242
|
});
|
|
238
|
-
return
|
|
243
|
+
return parentRunTree.createChild({
|
|
244
|
+
name: props?.name ?? "<lambda>",
|
|
245
|
+
tags: props.tags,
|
|
246
|
+
metadata: props.metadata,
|
|
247
|
+
});
|
|
239
248
|
}
|
|
240
249
|
static getDefaultConfig() {
|
|
241
250
|
return {
|
package/dist/run_trees.d.ts
CHANGED
|
@@ -66,7 +66,7 @@ export declare class RunTree implements BaseRun {
|
|
|
66
66
|
execution_order: number;
|
|
67
67
|
child_execution_order: number;
|
|
68
68
|
constructor(originalConfig: RunTreeConfig);
|
|
69
|
-
static fromRunnableConfig(
|
|
69
|
+
static fromRunnableConfig(parentConfig: RunnableConfigLike, props: {
|
|
70
70
|
name: string;
|
|
71
71
|
tags?: string[];
|
|
72
72
|
metadata?: KVMap;
|
package/dist/run_trees.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as uuid from "uuid";
|
|
2
2
|
import { getEnvironmentVariable, getRuntimeEnvironment, } from "./utils/env.js";
|
|
3
3
|
import { Client } from "./client.js";
|
|
4
|
+
import { isTracingEnabled } from "./env.js";
|
|
4
5
|
const warnedMessages = {};
|
|
5
6
|
function warnOnce(message) {
|
|
6
7
|
if (!warnedMessages[message]) {
|
|
@@ -181,34 +182,42 @@ export class RunTree {
|
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
184
|
}
|
|
184
|
-
static fromRunnableConfig(
|
|
185
|
+
static fromRunnableConfig(parentConfig, props) {
|
|
185
186
|
// We only handle the callback manager case for now
|
|
186
|
-
const callbackManager =
|
|
187
|
+
const callbackManager = parentConfig?.callbacks;
|
|
187
188
|
let parentRun;
|
|
188
189
|
let projectName;
|
|
190
|
+
let client;
|
|
191
|
+
let tracingEnabled = isTracingEnabled();
|
|
189
192
|
if (callbackManager) {
|
|
190
193
|
const parentRunId = callbackManager?.getParentRunId?.() ?? "";
|
|
191
194
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
|
|
192
195
|
parentRun = langChainTracer?.getRun?.(parentRunId);
|
|
193
196
|
projectName = langChainTracer?.projectName;
|
|
197
|
+
client = langChainTracer?.client;
|
|
198
|
+
tracingEnabled = tracingEnabled || !!langChainTracer;
|
|
194
199
|
}
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
parent_run: parentRun,
|
|
205
|
-
tags: dedupedTags,
|
|
200
|
+
const parentRunTree = new RunTree({
|
|
201
|
+
name: parentRun?.name ?? "<parent>",
|
|
202
|
+
id: parentRun?.id,
|
|
203
|
+
client,
|
|
204
|
+
tracingEnabled,
|
|
205
|
+
project_name: projectName,
|
|
206
|
+
tags: [
|
|
207
|
+
...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
|
|
208
|
+
],
|
|
206
209
|
extra: {
|
|
207
|
-
metadata:
|
|
210
|
+
metadata: {
|
|
211
|
+
...parentRun?.extra?.metadata,
|
|
212
|
+
...parentConfig?.metadata,
|
|
213
|
+
},
|
|
208
214
|
},
|
|
209
|
-
project_name: projectName,
|
|
210
215
|
});
|
|
211
|
-
return
|
|
216
|
+
return parentRunTree.createChild({
|
|
217
|
+
name: props?.name ?? "<lambda>",
|
|
218
|
+
tags: props.tags,
|
|
219
|
+
metadata: props.metadata,
|
|
220
|
+
});
|
|
212
221
|
}
|
|
213
222
|
static getDefaultConfig() {
|
|
214
223
|
return {
|
package/dist/schemas.d.ts
CHANGED
|
@@ -162,6 +162,7 @@ export interface RunUpdate {
|
|
|
162
162
|
export interface ExampleCreate extends BaseExample {
|
|
163
163
|
id?: string;
|
|
164
164
|
created_at?: string;
|
|
165
|
+
split?: string | string[];
|
|
165
166
|
}
|
|
166
167
|
export interface Example extends BaseExample {
|
|
167
168
|
id: string;
|
|
@@ -175,6 +176,7 @@ export interface ExampleUpdate {
|
|
|
175
176
|
inputs?: KVMap;
|
|
176
177
|
outputs?: KVMap;
|
|
177
178
|
metadata?: KVMap;
|
|
179
|
+
split?: string | string[];
|
|
178
180
|
}
|
|
179
181
|
export interface BaseDataset {
|
|
180
182
|
name: string;
|
|
@@ -293,4 +295,22 @@ export interface ComparativeExperiment {
|
|
|
293
295
|
experiments_info?: Array<Record<string, unknown>>;
|
|
294
296
|
feedback_stats?: Record<string, unknown>;
|
|
295
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Represents the expected output schema returned by traceable
|
|
300
|
+
* or by run tree output for LangSmith to correctly display
|
|
301
|
+
* documents in the UI
|
|
302
|
+
*/
|
|
303
|
+
export type RetrieverOutput = Array<{
|
|
304
|
+
page_content: string;
|
|
305
|
+
type: "Document";
|
|
306
|
+
metadata?: KVMap;
|
|
307
|
+
}>;
|
|
308
|
+
export interface InvocationParamsSchema {
|
|
309
|
+
ls_provider?: string;
|
|
310
|
+
ls_model_name?: string;
|
|
311
|
+
ls_model_type: "chat";
|
|
312
|
+
ls_temperature?: number;
|
|
313
|
+
ls_max_tokens?: number;
|
|
314
|
+
ls_stop?: string[];
|
|
315
|
+
}
|
|
296
316
|
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTraceableFunction = exports.ROOT = exports.getCurrentRunTree = exports.AsyncLocalStorageProviderSingleton = void 0;
|
|
4
|
+
class MockAsyncLocalStorage {
|
|
5
|
+
getStore() {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
run(_, callback) {
|
|
9
|
+
return callback();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class AsyncLocalStorageProvider {
|
|
13
|
+
constructor() {
|
|
14
|
+
Object.defineProperty(this, "asyncLocalStorage", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: new MockAsyncLocalStorage()
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "hasBeenInitialized", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: false
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getInstance() {
|
|
28
|
+
return this.asyncLocalStorage;
|
|
29
|
+
}
|
|
30
|
+
initializeGlobalInstance(instance) {
|
|
31
|
+
if (!this.hasBeenInitialized) {
|
|
32
|
+
this.hasBeenInitialized = true;
|
|
33
|
+
this.asyncLocalStorage = instance;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
|
|
38
|
+
/**
|
|
39
|
+
* Return the current run tree from within a traceable-wrapped function.
|
|
40
|
+
* Will throw an error if called outside of a traceable function.
|
|
41
|
+
*
|
|
42
|
+
* @returns The run tree for the given context.
|
|
43
|
+
*/
|
|
44
|
+
const getCurrentRunTree = () => {
|
|
45
|
+
const runTree = exports.AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
46
|
+
if (runTree === undefined) {
|
|
47
|
+
throw new Error([
|
|
48
|
+
"Could not get the current run tree.",
|
|
49
|
+
"",
|
|
50
|
+
"Please make sure you are calling this method within a traceable function.",
|
|
51
|
+
].join("\n"));
|
|
52
|
+
}
|
|
53
|
+
return runTree;
|
|
54
|
+
};
|
|
55
|
+
exports.getCurrentRunTree = getCurrentRunTree;
|
|
56
|
+
exports.ROOT = Symbol.for("langsmith:traceable:root");
|
|
57
|
+
function isTraceableFunction(x
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
) {
|
|
60
|
+
return typeof x === "function" && "langsmith:traceable" in x;
|
|
61
|
+
}
|
|
62
|
+
exports.isTraceableFunction = isTraceableFunction;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RunTree } from "../run_trees.js";
|
|
2
|
+
import { TraceableFunction } from "./types.js";
|
|
3
|
+
interface AsyncLocalStorageInterface {
|
|
4
|
+
getStore: () => RunTree | undefined;
|
|
5
|
+
run: (context: RunTree | undefined, fn: () => void) => void;
|
|
6
|
+
}
|
|
7
|
+
declare class AsyncLocalStorageProvider {
|
|
8
|
+
private asyncLocalStorage;
|
|
9
|
+
private hasBeenInitialized;
|
|
10
|
+
getInstance(): AsyncLocalStorageInterface;
|
|
11
|
+
initializeGlobalInstance(instance: AsyncLocalStorageInterface): void;
|
|
12
|
+
}
|
|
13
|
+
export declare const AsyncLocalStorageProviderSingleton: AsyncLocalStorageProvider;
|
|
14
|
+
/**
|
|
15
|
+
* Return the current run tree from within a traceable-wrapped function.
|
|
16
|
+
* Will throw an error if called outside of a traceable function.
|
|
17
|
+
*
|
|
18
|
+
* @returns The run tree for the given context.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getCurrentRunTree: () => RunTree;
|
|
21
|
+
export declare const ROOT: unique symbol;
|
|
22
|
+
export declare function isTraceableFunction(x: unknown): x is TraceableFunction<any>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
class MockAsyncLocalStorage {
|
|
2
|
+
getStore() {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
run(_, callback) {
|
|
6
|
+
return callback();
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class AsyncLocalStorageProvider {
|
|
10
|
+
constructor() {
|
|
11
|
+
Object.defineProperty(this, "asyncLocalStorage", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: new MockAsyncLocalStorage()
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "hasBeenInitialized", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: false
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
getInstance() {
|
|
25
|
+
return this.asyncLocalStorage;
|
|
26
|
+
}
|
|
27
|
+
initializeGlobalInstance(instance) {
|
|
28
|
+
if (!this.hasBeenInitialized) {
|
|
29
|
+
this.hasBeenInitialized = true;
|
|
30
|
+
this.asyncLocalStorage = instance;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
|
|
35
|
+
/**
|
|
36
|
+
* Return the current run tree from within a traceable-wrapped function.
|
|
37
|
+
* Will throw an error if called outside of a traceable function.
|
|
38
|
+
*
|
|
39
|
+
* @returns The run tree for the given context.
|
|
40
|
+
*/
|
|
41
|
+
export const getCurrentRunTree = () => {
|
|
42
|
+
const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
43
|
+
if (runTree === undefined) {
|
|
44
|
+
throw new Error([
|
|
45
|
+
"Could not get the current run tree.",
|
|
46
|
+
"",
|
|
47
|
+
"Please make sure you are calling this method within a traceable function.",
|
|
48
|
+
].join("\n"));
|
|
49
|
+
}
|
|
50
|
+
return runTree;
|
|
51
|
+
};
|
|
52
|
+
export const ROOT = Symbol.for("langsmith:traceable:root");
|
|
53
|
+
export function isTraceableFunction(x
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
+
) {
|
|
56
|
+
return typeof x === "function" && "langsmith:traceable" in x;
|
|
57
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RunTree, RunnableConfigLike } from "../run_trees.js";
|
|
2
|
+
import { ROOT } from "./traceable.js";
|
|
3
|
+
type SmartPromise<T> = T extends AsyncGenerator ? T : T extends Promise<unknown> ? T : Promise<T>;
|
|
4
|
+
type WrapArgReturnPair<Pair> = Pair extends [
|
|
5
|
+
infer Args extends any[],
|
|
6
|
+
infer Return
|
|
7
|
+
] ? Args extends [RunTree, ...infer RestArgs] ? {
|
|
8
|
+
(runTree: RunTree | typeof ROOT, ...args: RestArgs): SmartPromise<Return>;
|
|
9
|
+
(config: RunnableConfigLike, ...args: RestArgs): SmartPromise<Return>;
|
|
10
|
+
} : {
|
|
11
|
+
(...args: Args): SmartPromise<Return>;
|
|
12
|
+
(runTree: RunTree, ...rest: Args): SmartPromise<Return>;
|
|
13
|
+
(config: RunnableConfigLike, ...args: Args): SmartPromise<Return>;
|
|
14
|
+
} : never;
|
|
15
|
+
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
16
|
+
export type TraceableFunction<Func extends (...args: any[]) => any> = Func extends {
|
|
17
|
+
(...args: infer A1): infer R1;
|
|
18
|
+
(...args: infer A2): infer R2;
|
|
19
|
+
(...args: infer A3): infer R3;
|
|
20
|
+
(...args: infer A4): infer R4;
|
|
21
|
+
(...args: infer A5): infer R5;
|
|
22
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4] | [A5, R5]>> : Func extends {
|
|
23
|
+
(...args: infer A1): infer R1;
|
|
24
|
+
(...args: infer A2): infer R2;
|
|
25
|
+
(...args: infer A3): infer R3;
|
|
26
|
+
(...args: infer A4): infer R4;
|
|
27
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4]>> : Func extends {
|
|
28
|
+
(...args: infer A1): infer R1;
|
|
29
|
+
(...args: infer A2): infer R2;
|
|
30
|
+
(...args: infer A3): infer R3;
|
|
31
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3]>> : Func extends {
|
|
32
|
+
(...args: infer A1): infer R1;
|
|
33
|
+
(...args: infer A2): infer R2;
|
|
34
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2]>> : Func extends {
|
|
35
|
+
(...args: infer A1): infer R1;
|
|
36
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1]>> : never;
|
|
37
|
+
export type RunTreeLike = RunTree;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/traceable.cjs
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.ROOT = exports.isTraceableFunction = exports.getCurrentRunTree = exports.traceable = void 0;
|
|
4
4
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
5
|
const run_trees_js_1 = require("./run_trees.cjs");
|
|
6
|
-
const env_js_1 = require("./
|
|
6
|
+
const env_js_1 = require("./env.cjs");
|
|
7
|
+
const traceable_js_1 = require("./singletons/traceable.cjs");
|
|
8
|
+
traceable_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
|
|
7
9
|
function isPromiseMethod(x) {
|
|
8
10
|
if (x === "then" || x === "catch" || x === "finally") {
|
|
9
11
|
return true;
|
|
10
12
|
}
|
|
11
13
|
return false;
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
function isKVMap(x) {
|
|
16
|
+
if (typeof x !== "object" || x == null) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const prototype = Object.getPrototypeOf(x);
|
|
20
|
+
return ((prototype === null ||
|
|
21
|
+
prototype === Object.prototype ||
|
|
22
|
+
Object.getPrototypeOf(prototype) === null) &&
|
|
23
|
+
!(Symbol.toStringTag in x) &&
|
|
24
|
+
!(Symbol.iterator in x));
|
|
25
|
+
}
|
|
15
26
|
const isAsyncIterable = (x) => x != null &&
|
|
16
27
|
typeof x === "object" &&
|
|
17
28
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
29
|
typeof x[Symbol.asyncIterator] === "function";
|
|
19
|
-
const GeneratorFunction = function* () { }.constructor;
|
|
20
30
|
const isIteratorLike = (x) => x != null &&
|
|
21
31
|
typeof x === "object" &&
|
|
22
32
|
"next" in x &&
|
|
23
33
|
typeof x.next === "function";
|
|
34
|
+
const GeneratorFunction = function* () { }.constructor;
|
|
24
35
|
const isGenerator = (x) =>
|
|
25
36
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
26
37
|
x != null && typeof x === "function" && x instanceof GeneratorFunction;
|
|
@@ -32,18 +43,6 @@ const isReadableStream = (x) => x != null &&
|
|
|
32
43
|
typeof x === "object" &&
|
|
33
44
|
"getReader" in x &&
|
|
34
45
|
typeof x.getReader === "function";
|
|
35
|
-
const tracingIsEnabled = (tracingEnabled) => {
|
|
36
|
-
if (tracingEnabled !== undefined) {
|
|
37
|
-
return tracingEnabled;
|
|
38
|
-
}
|
|
39
|
-
const envVars = [
|
|
40
|
-
"LANGSMITH_TRACING_V2",
|
|
41
|
-
"LANGCHAIN_TRACING_V2",
|
|
42
|
-
"LANGSMITH_TRACING",
|
|
43
|
-
"LANGCHAIN_TRACING",
|
|
44
|
-
];
|
|
45
|
-
return Boolean(envVars.find((envVar) => (0, env_js_1.getEnvironmentVariable)(envVar) === "true"));
|
|
46
|
-
};
|
|
47
46
|
const handleRunInputs = (rawInputs) => {
|
|
48
47
|
const firstInput = rawInputs[0];
|
|
49
48
|
if (firstInput == null) {
|
|
@@ -63,12 +62,19 @@ const handleRunOutputs = (rawOutputs) => {
|
|
|
63
62
|
}
|
|
64
63
|
return { outputs: rawOutputs };
|
|
65
64
|
};
|
|
66
|
-
const getTracingRunTree = (runTree, inputs) => {
|
|
67
|
-
|
|
68
|
-
if (!tracingEnabled_) {
|
|
65
|
+
const getTracingRunTree = (runTree, inputs, getInvocationParams) => {
|
|
66
|
+
if (!(0, env_js_1.isTracingEnabled)(runTree.tracingEnabled)) {
|
|
69
67
|
return undefined;
|
|
70
68
|
}
|
|
71
69
|
runTree.inputs = handleRunInputs(inputs);
|
|
70
|
+
const invocationParams = getInvocationParams?.(...inputs);
|
|
71
|
+
if (invocationParams != null) {
|
|
72
|
+
runTree.extra ??= {};
|
|
73
|
+
runTree.extra.metadata = {
|
|
74
|
+
...runTree.extra.metadata,
|
|
75
|
+
...invocationParams,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
72
78
|
return runTree;
|
|
73
79
|
};
|
|
74
80
|
// idea: store the state of the promise outside
|
|
@@ -269,6 +275,7 @@ function traceable(wrappedFunc, config) {
|
|
|
269
275
|
...runTreeConfig,
|
|
270
276
|
};
|
|
271
277
|
}
|
|
278
|
+
const asyncLocalStorage = traceable_js_1.AsyncLocalStorageProviderSingleton.getInstance();
|
|
272
279
|
// TODO: deal with possible nested promises and async iterables
|
|
273
280
|
const processedArgs = args;
|
|
274
281
|
for (let i = 0; i < processedArgs.length; i++) {
|
|
@@ -279,11 +286,11 @@ function traceable(wrappedFunc, config) {
|
|
|
279
286
|
// used for handoff between LangChain.JS and traceable functions
|
|
280
287
|
if ((0, run_trees_js_1.isRunnableConfigLike)(firstArg)) {
|
|
281
288
|
return [
|
|
282
|
-
getTracingRunTree(run_trees_js_1.RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs),
|
|
289
|
+
getTracingRunTree(run_trees_js_1.RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs, config?.getInvocationParams),
|
|
283
290
|
restArgs,
|
|
284
291
|
];
|
|
285
292
|
}
|
|
286
|
-
// legacy CallbackManagerRunTree used in runOnDataset
|
|
293
|
+
// deprecated: legacy CallbackManagerRunTree used in runOnDataset
|
|
287
294
|
// override ALS and do not pass-through the run tree
|
|
288
295
|
if ((0, run_trees_js_1.isRunTree)(firstArg) &&
|
|
289
296
|
"callbackManager" in firstArg &&
|
|
@@ -292,10 +299,10 @@ function traceable(wrappedFunc, config) {
|
|
|
292
299
|
}
|
|
293
300
|
// when ALS is unreliable, users can manually
|
|
294
301
|
// pass in the run tree
|
|
295
|
-
if (firstArg ===
|
|
296
|
-
const currentRunTree = getTracingRunTree(firstArg ===
|
|
302
|
+
if (firstArg === traceable_js_1.ROOT || (0, run_trees_js_1.isRunTree)(firstArg)) {
|
|
303
|
+
const currentRunTree = getTracingRunTree(firstArg === traceable_js_1.ROOT
|
|
297
304
|
? new run_trees_js_1.RunTree(ensuredConfig)
|
|
298
|
-
: firstArg.createChild(ensuredConfig), restArgs);
|
|
305
|
+
: firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams);
|
|
299
306
|
return [currentRunTree, [currentRunTree, ...restArgs]];
|
|
300
307
|
}
|
|
301
308
|
// Node.JS uses AsyncLocalStorage (ALS) and AsyncResource
|
|
@@ -303,11 +310,11 @@ function traceable(wrappedFunc, config) {
|
|
|
303
310
|
const prevRunFromStore = asyncLocalStorage.getStore();
|
|
304
311
|
if (prevRunFromStore) {
|
|
305
312
|
return [
|
|
306
|
-
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs),
|
|
313
|
+
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs, config?.getInvocationParams),
|
|
307
314
|
processedArgs,
|
|
308
315
|
];
|
|
309
316
|
}
|
|
310
|
-
const currentRunTree = getTracingRunTree(new run_trees_js_1.RunTree(ensuredConfig), processedArgs);
|
|
317
|
+
const currentRunTree = getTracingRunTree(new run_trees_js_1.RunTree(ensuredConfig), processedArgs, config?.getInvocationParams);
|
|
311
318
|
return [currentRunTree, processedArgs];
|
|
312
319
|
})();
|
|
313
320
|
return asyncLocalStorage.run(currentRunTree, () => {
|
|
@@ -444,48 +451,7 @@ function traceable(wrappedFunc, config) {
|
|
|
444
451
|
return traceableFunc;
|
|
445
452
|
}
|
|
446
453
|
exports.traceable = traceable;
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
* @returns The run tree for the given context.
|
|
452
|
-
*/
|
|
453
|
-
function getCurrentRunTree() {
|
|
454
|
-
const runTree = asyncLocalStorage.getStore();
|
|
455
|
-
if (runTree === undefined) {
|
|
456
|
-
throw new Error([
|
|
457
|
-
"Could not get the current run tree.",
|
|
458
|
-
"",
|
|
459
|
-
"Please make sure you are calling this method within a traceable function.",
|
|
460
|
-
].join("\n"));
|
|
461
|
-
}
|
|
462
|
-
return runTree;
|
|
463
|
-
}
|
|
464
|
-
exports.getCurrentRunTree = getCurrentRunTree;
|
|
465
|
-
function isTraceableFunction(x
|
|
466
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
467
|
-
) {
|
|
468
|
-
return typeof x === "function" && "langsmith:traceable" in x;
|
|
469
|
-
}
|
|
470
|
-
exports.isTraceableFunction = isTraceableFunction;
|
|
471
|
-
function isKVMap(x) {
|
|
472
|
-
if (typeof x !== "object" || x == null) {
|
|
473
|
-
return false;
|
|
474
|
-
}
|
|
475
|
-
const prototype = Object.getPrototypeOf(x);
|
|
476
|
-
return ((prototype === null ||
|
|
477
|
-
prototype === Object.prototype ||
|
|
478
|
-
Object.getPrototypeOf(prototype) === null) &&
|
|
479
|
-
!(Symbol.toStringTag in x) &&
|
|
480
|
-
!(Symbol.iterator in x));
|
|
481
|
-
}
|
|
482
|
-
function wrapFunctionAndEnsureTraceable(target, options, name = "target") {
|
|
483
|
-
if (typeof target === "function") {
|
|
484
|
-
return traceable(target, {
|
|
485
|
-
...options,
|
|
486
|
-
name,
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
throw new Error("Target must be runnable function");
|
|
490
|
-
}
|
|
491
|
-
exports.wrapFunctionAndEnsureTraceable = wrapFunctionAndEnsureTraceable;
|
|
454
|
+
var traceable_js_2 = require("./singletons/traceable.cjs");
|
|
455
|
+
Object.defineProperty(exports, "getCurrentRunTree", { enumerable: true, get: function () { return traceable_js_2.getCurrentRunTree; } });
|
|
456
|
+
Object.defineProperty(exports, "isTraceableFunction", { enumerable: true, get: function () { return traceable_js_2.isTraceableFunction; } });
|
|
457
|
+
Object.defineProperty(exports, "ROOT", { enumerable: true, get: function () { return traceable_js_2.ROOT; } });
|
package/dist/traceable.d.ts
CHANGED
|
@@ -1,40 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type SmartPromise<T> = T extends AsyncGenerator ? T : T extends Promise<unknown> ? T : Promise<T>;
|
|
5
|
-
type WrapArgReturnPair<Pair> = Pair extends [
|
|
6
|
-
infer Args extends any[],
|
|
7
|
-
infer Return
|
|
8
|
-
] ? Args extends [RunTreeLike, ...infer RestArgs] ? {
|
|
9
|
-
(runTree: RunTreeLike | typeof ROOT, ...args: RestArgs): SmartPromise<Return>;
|
|
10
|
-
(config: RunnableConfigLike, ...args: RestArgs): SmartPromise<Return>;
|
|
11
|
-
} : {
|
|
12
|
-
(...args: Args): SmartPromise<Return>;
|
|
13
|
-
(runTree: RunTreeLike, ...rest: Args): SmartPromise<Return>;
|
|
14
|
-
(config: RunnableConfigLike, ...args: Args): SmartPromise<Return>;
|
|
15
|
-
} : never;
|
|
16
|
-
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
17
|
-
export type TraceableFunction<Func extends (...args: any[]) => any> = Func extends {
|
|
18
|
-
(...args: infer A1): infer R1;
|
|
19
|
-
(...args: infer A2): infer R2;
|
|
20
|
-
(...args: infer A3): infer R3;
|
|
21
|
-
(...args: infer A4): infer R4;
|
|
22
|
-
(...args: infer A5): infer R5;
|
|
23
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4] | [A5, R5]>> : Func extends {
|
|
24
|
-
(...args: infer A1): infer R1;
|
|
25
|
-
(...args: infer A2): infer R2;
|
|
26
|
-
(...args: infer A3): infer R3;
|
|
27
|
-
(...args: infer A4): infer R4;
|
|
28
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4]>> : Func extends {
|
|
29
|
-
(...args: infer A1): infer R1;
|
|
30
|
-
(...args: infer A2): infer R2;
|
|
31
|
-
(...args: infer A3): infer R3;
|
|
32
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3]>> : Func extends {
|
|
33
|
-
(...args: infer A1): infer R1;
|
|
34
|
-
(...args: infer A2): infer R2;
|
|
35
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2]>> : Func extends {
|
|
36
|
-
(...args: infer A1): infer R1;
|
|
37
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1]>> : never;
|
|
1
|
+
import { RunTreeConfig } from "./run_trees.js";
|
|
2
|
+
import { InvocationParamsSchema } from "./schemas.js";
|
|
3
|
+
import { TraceableFunction } from "./singletons/types.js";
|
|
38
4
|
/**
|
|
39
5
|
* Higher-order function that takes function as input and returns a
|
|
40
6
|
* "TraceableFunction" - a wrapped version of the input that
|
|
@@ -52,14 +18,15 @@ export type TraceableFunction<Func extends (...args: any[]) => any> = Func exten
|
|
|
52
18
|
export declare function traceable<Func extends (...args: any[]) => any>(wrappedFunc: Func, config?: Partial<RunTreeConfig> & {
|
|
53
19
|
aggregator?: (args: any[]) => any;
|
|
54
20
|
argsConfigPath?: [number] | [number, string];
|
|
21
|
+
/**
|
|
22
|
+
* Extract invocation parameters from the arguments of the traced function.
|
|
23
|
+
* This is useful for LangSmith to properly track common metadata like
|
|
24
|
+
* provider, model name and temperature.
|
|
25
|
+
*
|
|
26
|
+
* @param args Arguments of the traced function
|
|
27
|
+
* @returns Key-value map of the invocation parameters, which will be merged with the existing metadata
|
|
28
|
+
*/
|
|
29
|
+
getInvocationParams?: (...args: Parameters<Func>) => InvocationParamsSchema | undefined;
|
|
55
30
|
}): TraceableFunction<Func>;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* Will throw an error if called outside of a traceable function.
|
|
59
|
-
*
|
|
60
|
-
* @returns The run tree for the given context.
|
|
61
|
-
*/
|
|
62
|
-
export declare function getCurrentRunTree(): RunTree;
|
|
63
|
-
export declare function isTraceableFunction(x: unknown): x is TraceableFunction<any>;
|
|
64
|
-
export declare function wrapFunctionAndEnsureTraceable<Func extends (...args: any[]) => any>(target: Func, options: Partial<RunTreeConfig>, name?: string): TraceableFunction<Func>;
|
|
65
|
-
export {};
|
|
31
|
+
export { getCurrentRunTree, isTraceableFunction, ROOT, } from "./singletons/traceable.js";
|
|
32
|
+
export type { RunTreeLike, TraceableFunction } from "./singletons/types.js";
|