langsmith 0.1.26 → 0.1.28
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/env.cjs +17 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +13 -0
- package/dist/evaluation/_runner.cjs +1 -0
- package/dist/evaluation/_runner.js +1 -0
- package/dist/evaluation/langchain.cjs +52 -0
- package/dist/evaluation/langchain.d.ts +19 -0
- package/dist/evaluation/langchain.js +48 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/langchain.cjs +126 -0
- package/dist/langchain.d.ts +30 -0
- package/dist/langchain.js +121 -0
- package/dist/run_trees.cjs +37 -24
- package/dist/run_trees.d.ts +1 -1
- package/dist/run_trees.js +36 -23
- package/dist/schemas.d.ts +1 -1
- 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 +24 -102
- package/dist/traceable.d.ts +4 -47
- package/dist/traceable.js +8 -86
- package/dist/utils/asserts.cjs +47 -0
- package/dist/utils/asserts.d.ts +7 -0
- package/dist/utils/asserts.js +37 -0
- package/dist/utils/warn.cjs +11 -0
- package/dist/utils/warn.d.ts +1 -0
- package/dist/utils/warn.js +7 -0
- package/dist/wrappers/openai.cjs +1 -1
- package/dist/wrappers/openai.js +1 -1
- package/evaluation/langchain.cjs +1 -0
- package/evaluation/langchain.d.cts +1 -0
- package/evaluation/langchain.d.ts +1 -0
- package/evaluation/langchain.js +1 -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 +56 -5
- 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/env.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTracingEnabled = void 0;
|
|
4
|
+
const env_js_1 = require("./utils/env.cjs");
|
|
5
|
+
const isTracingEnabled = (tracingEnabled) => {
|
|
6
|
+
if (tracingEnabled !== undefined) {
|
|
7
|
+
return tracingEnabled;
|
|
8
|
+
}
|
|
9
|
+
const envVars = [
|
|
10
|
+
"LANGSMITH_TRACING_V2",
|
|
11
|
+
"LANGCHAIN_TRACING_V2",
|
|
12
|
+
"LANGSMITH_TRACING",
|
|
13
|
+
"LANGCHAIN_TRACING",
|
|
14
|
+
];
|
|
15
|
+
return !!envVars.find((envVar) => (0, env_js_1.getEnvironmentVariable)(envVar) === "true");
|
|
16
|
+
};
|
|
17
|
+
exports.isTracingEnabled = isTracingEnabled;
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isTracingEnabled: (tracingEnabled?: boolean) => boolean;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { getEnvironmentVariable } from "./utils/env.js";
|
|
2
|
+
export const isTracingEnabled = (tracingEnabled) => {
|
|
3
|
+
if (tracingEnabled !== undefined) {
|
|
4
|
+
return tracingEnabled;
|
|
5
|
+
}
|
|
6
|
+
const envVars = [
|
|
7
|
+
"LANGSMITH_TRACING_V2",
|
|
8
|
+
"LANGCHAIN_TRACING_V2",
|
|
9
|
+
"LANGSMITH_TRACING",
|
|
10
|
+
"LANGCHAIN_TRACING",
|
|
11
|
+
];
|
|
12
|
+
return !!envVars.find((envVar) => getEnvironmentVariable(envVar) === "true");
|
|
13
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLangchainStringEvaluator = void 0;
|
|
4
|
+
const evaluation_1 = require("langchain/evaluation");
|
|
5
|
+
const langchain_js_1 = require("../langchain.cjs");
|
|
6
|
+
function isStringifiable(value) {
|
|
7
|
+
return (typeof value === "string" ||
|
|
8
|
+
typeof value === "number" ||
|
|
9
|
+
typeof value === "boolean" ||
|
|
10
|
+
typeof value === "bigint");
|
|
11
|
+
}
|
|
12
|
+
// utility methods for extracting stringified values
|
|
13
|
+
// from unknown inputs and records
|
|
14
|
+
function getPrimitiveValue(value) {
|
|
15
|
+
if (isStringifiable(value))
|
|
16
|
+
return String(value);
|
|
17
|
+
if (!Array.isArray(value) && typeof value === "object" && value != null) {
|
|
18
|
+
const values = Object.values(value);
|
|
19
|
+
if (values.length === 1 && isStringifiable(values[0])) {
|
|
20
|
+
return String(values[0]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* This utility function loads a LangChain string evaluator and returns a function
|
|
27
|
+
* which can be used by newer `evaluate` function.
|
|
28
|
+
*
|
|
29
|
+
* @param type Type of string evaluator, one of "criteria" or "labeled_criteria
|
|
30
|
+
* @param options Options for loading the evaluator
|
|
31
|
+
* @returns Evaluator consumable by `evaluate`
|
|
32
|
+
*/
|
|
33
|
+
async function getLangchainStringEvaluator(type, options) {
|
|
34
|
+
const evaluator = await (0, evaluation_1.loadEvaluator)(type, options);
|
|
35
|
+
const feedbackKey = getPrimitiveValue(options.criteria) ?? type;
|
|
36
|
+
const formatEvaluatorInputs = options.formatEvaluatorInputs ??
|
|
37
|
+
((run, example) => {
|
|
38
|
+
const prediction = getPrimitiveValue(run.outputs);
|
|
39
|
+
const reference = getPrimitiveValue(example.outputs);
|
|
40
|
+
const input = getPrimitiveValue(example.inputs);
|
|
41
|
+
if (prediction == null)
|
|
42
|
+
throw new Error("Missing prediction");
|
|
43
|
+
if (type === "criteria")
|
|
44
|
+
return { prediction, input };
|
|
45
|
+
return { prediction, reference, input };
|
|
46
|
+
});
|
|
47
|
+
return async (run, example) => {
|
|
48
|
+
const score = await evaluator.evaluateStrings(formatEvaluatorInputs(run, example), { callbacks: await (0, langchain_js_1.getLangchainCallbacks)() });
|
|
49
|
+
return { key: feedbackKey, ...score };
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
exports.getLangchainStringEvaluator = getLangchainStringEvaluator;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Run, Example } from "../schemas.js";
|
|
2
|
+
import { type LoadEvaluatorOptions } from "langchain/evaluation";
|
|
3
|
+
/**
|
|
4
|
+
* This utility function loads a LangChain string evaluator and returns a function
|
|
5
|
+
* which can be used by newer `evaluate` function.
|
|
6
|
+
*
|
|
7
|
+
* @param type Type of string evaluator, one of "criteria" or "labeled_criteria
|
|
8
|
+
* @param options Options for loading the evaluator
|
|
9
|
+
* @returns Evaluator consumable by `evaluate`
|
|
10
|
+
*/
|
|
11
|
+
export declare function getLangchainStringEvaluator(type: "criteria" | "labeled_criteria", options: LoadEvaluatorOptions & {
|
|
12
|
+
formatEvaluatorInputs?: (run: Run, example: Example) => {
|
|
13
|
+
prediction: string;
|
|
14
|
+
reference?: string;
|
|
15
|
+
input?: string;
|
|
16
|
+
};
|
|
17
|
+
}): Promise<(run: Run, example: Example) => Promise<{
|
|
18
|
+
key: string;
|
|
19
|
+
}>>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { loadEvaluator } from "langchain/evaluation";
|
|
2
|
+
import { getLangchainCallbacks } from "../langchain.js";
|
|
3
|
+
function isStringifiable(value) {
|
|
4
|
+
return (typeof value === "string" ||
|
|
5
|
+
typeof value === "number" ||
|
|
6
|
+
typeof value === "boolean" ||
|
|
7
|
+
typeof value === "bigint");
|
|
8
|
+
}
|
|
9
|
+
// utility methods for extracting stringified values
|
|
10
|
+
// from unknown inputs and records
|
|
11
|
+
function getPrimitiveValue(value) {
|
|
12
|
+
if (isStringifiable(value))
|
|
13
|
+
return String(value);
|
|
14
|
+
if (!Array.isArray(value) && typeof value === "object" && value != null) {
|
|
15
|
+
const values = Object.values(value);
|
|
16
|
+
if (values.length === 1 && isStringifiable(values[0])) {
|
|
17
|
+
return String(values[0]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This utility function loads a LangChain string evaluator and returns a function
|
|
24
|
+
* which can be used by newer `evaluate` function.
|
|
25
|
+
*
|
|
26
|
+
* @param type Type of string evaluator, one of "criteria" or "labeled_criteria
|
|
27
|
+
* @param options Options for loading the evaluator
|
|
28
|
+
* @returns Evaluator consumable by `evaluate`
|
|
29
|
+
*/
|
|
30
|
+
export async function getLangchainStringEvaluator(type, options) {
|
|
31
|
+
const evaluator = await loadEvaluator(type, options);
|
|
32
|
+
const feedbackKey = getPrimitiveValue(options.criteria) ?? type;
|
|
33
|
+
const formatEvaluatorInputs = options.formatEvaluatorInputs ??
|
|
34
|
+
((run, example) => {
|
|
35
|
+
const prediction = getPrimitiveValue(run.outputs);
|
|
36
|
+
const reference = getPrimitiveValue(example.outputs);
|
|
37
|
+
const input = getPrimitiveValue(example.inputs);
|
|
38
|
+
if (prediction == null)
|
|
39
|
+
throw new Error("Missing prediction");
|
|
40
|
+
if (type === "criteria")
|
|
41
|
+
return { prediction, input };
|
|
42
|
+
return { prediction, reference, input };
|
|
43
|
+
});
|
|
44
|
+
return async (run, example) => {
|
|
45
|
+
const score = await evaluator.evaluateStrings(formatEvaluatorInputs(run, example), { callbacks: await getLangchainCallbacks() });
|
|
46
|
+
return { key: feedbackKey, ...score };
|
|
47
|
+
};
|
|
48
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -6,4 +6,4 @@ Object.defineProperty(exports, "Client", { enumerable: true, get: function () {
|
|
|
6
6
|
var run_trees_js_1 = require("./run_trees.cjs");
|
|
7
7
|
Object.defineProperty(exports, "RunTree", { enumerable: true, get: function () { return run_trees_js_1.RunTree; } });
|
|
8
8
|
// Update using yarn bump-version
|
|
9
|
-
exports.__version__ = "0.1.
|
|
9
|
+
exports.__version__ = "0.1.28";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Client } from "./client.js";
|
|
2
2
|
export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, } from "./schemas.js";
|
|
3
3
|
export { RunTree, type RunTreeConfig } from "./run_trees.js";
|
|
4
|
-
export declare const __version__ = "0.1.
|
|
4
|
+
export declare const __version__ = "0.1.28";
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RunnableTraceable = exports.getLangchainCallbacks = void 0;
|
|
4
|
+
const manager_1 = require("@langchain/core/callbacks/manager");
|
|
5
|
+
const tracer_langchain_1 = require("@langchain/core/tracers/tracer_langchain");
|
|
6
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
7
|
+
const traceable_js_1 = require("./traceable.cjs");
|
|
8
|
+
const asserts_js_1 = require("./utils/asserts.cjs");
|
|
9
|
+
/**
|
|
10
|
+
* Converts the current run tree active within a traceable-wrapped function
|
|
11
|
+
* into a LangChain compatible callback manager. This is useful to handoff tracing
|
|
12
|
+
* from LangSmith to LangChain Runnables and LLMs.
|
|
13
|
+
*
|
|
14
|
+
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
|
|
15
|
+
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
|
|
16
|
+
*/
|
|
17
|
+
async function getLangchainCallbacks(currentRunTree) {
|
|
18
|
+
const runTree = currentRunTree ?? (0, traceable_js_1.getCurrentRunTree)();
|
|
19
|
+
if (!runTree)
|
|
20
|
+
return undefined;
|
|
21
|
+
// TODO: CallbackManager.configure() is only async due to LangChainTracer
|
|
22
|
+
// factory being unnecessarily async.
|
|
23
|
+
let callbacks = await manager_1.CallbackManager.configure();
|
|
24
|
+
if (!callbacks && runTree.tracingEnabled) {
|
|
25
|
+
callbacks = new manager_1.CallbackManager();
|
|
26
|
+
}
|
|
27
|
+
let langChainTracer = callbacks?.handlers.find((handler) => handler?.name === "langchain_tracer");
|
|
28
|
+
if (!langChainTracer && runTree.tracingEnabled) {
|
|
29
|
+
langChainTracer = new tracer_langchain_1.LangChainTracer();
|
|
30
|
+
callbacks?.addHandler(langChainTracer);
|
|
31
|
+
}
|
|
32
|
+
const runMap = new Map();
|
|
33
|
+
// find upward root run
|
|
34
|
+
let rootRun = runTree;
|
|
35
|
+
const rootVisited = new Set();
|
|
36
|
+
while (rootRun.parent_run) {
|
|
37
|
+
if (rootVisited.has(rootRun.id))
|
|
38
|
+
break;
|
|
39
|
+
rootVisited.add(rootRun.id);
|
|
40
|
+
rootRun = rootRun.parent_run;
|
|
41
|
+
}
|
|
42
|
+
const queue = [rootRun];
|
|
43
|
+
const visited = new Set();
|
|
44
|
+
while (queue.length > 0) {
|
|
45
|
+
const current = queue.shift();
|
|
46
|
+
if (!current || visited.has(current.id))
|
|
47
|
+
continue;
|
|
48
|
+
visited.add(current.id);
|
|
49
|
+
runMap.set(current.id, current);
|
|
50
|
+
if (current.child_runs) {
|
|
51
|
+
queue.push(...current.child_runs);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (callbacks != null) {
|
|
55
|
+
Object.assign(callbacks, { _parentRunId: runTree.id });
|
|
56
|
+
}
|
|
57
|
+
if (langChainTracer != null) {
|
|
58
|
+
Object.assign(langChainTracer, {
|
|
59
|
+
runMap,
|
|
60
|
+
client: runTree.client,
|
|
61
|
+
projectName: runTree.project_name || langChainTracer.projectName,
|
|
62
|
+
exampleId: runTree.reference_example_id || langChainTracer.exampleId,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return callbacks;
|
|
66
|
+
}
|
|
67
|
+
exports.getLangchainCallbacks = getLangchainCallbacks;
|
|
68
|
+
/**
|
|
69
|
+
* RunnableTraceable is a Runnable that wraps a traceable function.
|
|
70
|
+
* This allows adding Langsmith traced functions into LangChain sequences.
|
|
71
|
+
*/
|
|
72
|
+
class RunnableTraceable extends runnables_1.Runnable {
|
|
73
|
+
constructor(fields) {
|
|
74
|
+
super(fields);
|
|
75
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: false
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
configurable: true,
|
|
84
|
+
writable: true,
|
|
85
|
+
value: ["langchain_core", "runnables"]
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(this, "func", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
configurable: true,
|
|
90
|
+
writable: true,
|
|
91
|
+
value: void 0
|
|
92
|
+
});
|
|
93
|
+
if (!(0, traceable_js_1.isTraceableFunction)(fields.func)) {
|
|
94
|
+
throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
95
|
+
}
|
|
96
|
+
this.func = fields.func;
|
|
97
|
+
}
|
|
98
|
+
async invoke(input, options) {
|
|
99
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
100
|
+
const callbacks = await (0, runnables_1.getCallbackManagerForConfig)(config);
|
|
101
|
+
return (await this.func((0, runnables_1.patchConfig)(config, { callbacks }), input));
|
|
102
|
+
}
|
|
103
|
+
async *_streamIterator(input, options) {
|
|
104
|
+
const result = await this.invoke(input, options);
|
|
105
|
+
if ((0, asserts_js_1.isAsyncIterable)(result)) {
|
|
106
|
+
for await (const item of result) {
|
|
107
|
+
yield item;
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if ((0, asserts_js_1.isIteratorLike)(result)) {
|
|
112
|
+
while (true) {
|
|
113
|
+
const state = result.next();
|
|
114
|
+
if (state.done)
|
|
115
|
+
break;
|
|
116
|
+
yield state.value;
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
yield result;
|
|
121
|
+
}
|
|
122
|
+
static from(func) {
|
|
123
|
+
return new RunnableTraceable({ func });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.RunnableTraceable = RunnableTraceable;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
2
|
+
import { Runnable, RunnableConfig } from "@langchain/core/runnables";
|
|
3
|
+
import { RunTree } from "./run_trees.js";
|
|
4
|
+
import { TraceableFunction } from "./traceable.js";
|
|
5
|
+
/**
|
|
6
|
+
* Converts the current run tree active within a traceable-wrapped function
|
|
7
|
+
* into a LangChain compatible callback manager. This is useful to handoff tracing
|
|
8
|
+
* from LangSmith to LangChain Runnables and LLMs.
|
|
9
|
+
*
|
|
10
|
+
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
|
|
11
|
+
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getLangchainCallbacks(currentRunTree?: RunTree | undefined): Promise<CallbackManager | undefined>;
|
|
14
|
+
type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>;
|
|
15
|
+
/**
|
|
16
|
+
* RunnableTraceable is a Runnable that wraps a traceable function.
|
|
17
|
+
* This allows adding Langsmith traced functions into LangChain sequences.
|
|
18
|
+
*/
|
|
19
|
+
export declare class RunnableTraceable<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
|
|
20
|
+
lc_serializable: boolean;
|
|
21
|
+
lc_namespace: string[];
|
|
22
|
+
protected func: AnyTraceableFunction;
|
|
23
|
+
constructor(fields: {
|
|
24
|
+
func: AnyTraceableFunction;
|
|
25
|
+
});
|
|
26
|
+
invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
|
|
27
|
+
_streamIterator(input: RunInput, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
|
|
28
|
+
static from(func: AnyTraceableFunction): RunnableTraceable<unknown, unknown>;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
2
|
+
import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
|
|
3
|
+
import { Runnable, patchConfig, getCallbackManagerForConfig, } from "@langchain/core/runnables";
|
|
4
|
+
import { getCurrentRunTree, isTraceableFunction, } from "./traceable.js";
|
|
5
|
+
import { isAsyncIterable, isIteratorLike } from "./utils/asserts.js";
|
|
6
|
+
/**
|
|
7
|
+
* Converts the current run tree active within a traceable-wrapped function
|
|
8
|
+
* into a LangChain compatible callback manager. This is useful to handoff tracing
|
|
9
|
+
* from LangSmith to LangChain Runnables and LLMs.
|
|
10
|
+
*
|
|
11
|
+
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
|
|
12
|
+
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
|
|
13
|
+
*/
|
|
14
|
+
export async function getLangchainCallbacks(currentRunTree) {
|
|
15
|
+
const runTree = currentRunTree ?? getCurrentRunTree();
|
|
16
|
+
if (!runTree)
|
|
17
|
+
return undefined;
|
|
18
|
+
// TODO: CallbackManager.configure() is only async due to LangChainTracer
|
|
19
|
+
// factory being unnecessarily async.
|
|
20
|
+
let callbacks = await CallbackManager.configure();
|
|
21
|
+
if (!callbacks && runTree.tracingEnabled) {
|
|
22
|
+
callbacks = new CallbackManager();
|
|
23
|
+
}
|
|
24
|
+
let langChainTracer = callbacks?.handlers.find((handler) => handler?.name === "langchain_tracer");
|
|
25
|
+
if (!langChainTracer && runTree.tracingEnabled) {
|
|
26
|
+
langChainTracer = new LangChainTracer();
|
|
27
|
+
callbacks?.addHandler(langChainTracer);
|
|
28
|
+
}
|
|
29
|
+
const runMap = new Map();
|
|
30
|
+
// find upward root run
|
|
31
|
+
let rootRun = runTree;
|
|
32
|
+
const rootVisited = new Set();
|
|
33
|
+
while (rootRun.parent_run) {
|
|
34
|
+
if (rootVisited.has(rootRun.id))
|
|
35
|
+
break;
|
|
36
|
+
rootVisited.add(rootRun.id);
|
|
37
|
+
rootRun = rootRun.parent_run;
|
|
38
|
+
}
|
|
39
|
+
const queue = [rootRun];
|
|
40
|
+
const visited = new Set();
|
|
41
|
+
while (queue.length > 0) {
|
|
42
|
+
const current = queue.shift();
|
|
43
|
+
if (!current || visited.has(current.id))
|
|
44
|
+
continue;
|
|
45
|
+
visited.add(current.id);
|
|
46
|
+
runMap.set(current.id, current);
|
|
47
|
+
if (current.child_runs) {
|
|
48
|
+
queue.push(...current.child_runs);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (callbacks != null) {
|
|
52
|
+
Object.assign(callbacks, { _parentRunId: runTree.id });
|
|
53
|
+
}
|
|
54
|
+
if (langChainTracer != null) {
|
|
55
|
+
Object.assign(langChainTracer, {
|
|
56
|
+
runMap,
|
|
57
|
+
client: runTree.client,
|
|
58
|
+
projectName: runTree.project_name || langChainTracer.projectName,
|
|
59
|
+
exampleId: runTree.reference_example_id || langChainTracer.exampleId,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return callbacks;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* RunnableTraceable is a Runnable that wraps a traceable function.
|
|
66
|
+
* This allows adding Langsmith traced functions into LangChain sequences.
|
|
67
|
+
*/
|
|
68
|
+
export class RunnableTraceable extends Runnable {
|
|
69
|
+
constructor(fields) {
|
|
70
|
+
super(fields);
|
|
71
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: false
|
|
76
|
+
});
|
|
77
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: ["langchain_core", "runnables"]
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "func", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
if (!isTraceableFunction(fields.func)) {
|
|
90
|
+
throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
91
|
+
}
|
|
92
|
+
this.func = fields.func;
|
|
93
|
+
}
|
|
94
|
+
async invoke(input, options) {
|
|
95
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
96
|
+
const callbacks = await getCallbackManagerForConfig(config);
|
|
97
|
+
return (await this.func(patchConfig(config, { callbacks }), input));
|
|
98
|
+
}
|
|
99
|
+
async *_streamIterator(input, options) {
|
|
100
|
+
const result = await this.invoke(input, options);
|
|
101
|
+
if (isAsyncIterable(result)) {
|
|
102
|
+
for await (const item of result) {
|
|
103
|
+
yield item;
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (isIteratorLike(result)) {
|
|
108
|
+
while (true) {
|
|
109
|
+
const state = result.next();
|
|
110
|
+
if (state.done)
|
|
111
|
+
break;
|
|
112
|
+
yield state.value;
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
yield result;
|
|
117
|
+
}
|
|
118
|
+
static from(func) {
|
|
119
|
+
return new RunnableTraceable({ func });
|
|
120
|
+
}
|
|
121
|
+
}
|
package/dist/run_trees.cjs
CHANGED
|
@@ -27,13 +27,8 @@ 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
|
|
31
|
-
|
|
32
|
-
if (!warnedMessages[message]) {
|
|
33
|
-
console.warn(message);
|
|
34
|
-
warnedMessages[message] = true;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
30
|
+
const env_js_2 = require("./env.cjs");
|
|
31
|
+
const warn_js_1 = require("./utils/warn.cjs");
|
|
37
32
|
function stripNonAlphanumeric(input) {
|
|
38
33
|
return input.replace(/[-:.]/g, "");
|
|
39
34
|
}
|
|
@@ -208,34 +203,52 @@ class RunTree {
|
|
|
208
203
|
}
|
|
209
204
|
}
|
|
210
205
|
}
|
|
211
|
-
static fromRunnableConfig(
|
|
206
|
+
static fromRunnableConfig(parentConfig, props) {
|
|
212
207
|
// We only handle the callback manager case for now
|
|
213
|
-
const callbackManager =
|
|
208
|
+
const callbackManager = parentConfig?.callbacks;
|
|
214
209
|
let parentRun;
|
|
215
210
|
let projectName;
|
|
211
|
+
let client;
|
|
212
|
+
let tracingEnabled = (0, env_js_2.isTracingEnabled)();
|
|
216
213
|
if (callbackManager) {
|
|
217
214
|
const parentRunId = callbackManager?.getParentRunId?.() ?? "";
|
|
218
215
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
|
|
219
216
|
parentRun = langChainTracer?.getRun?.(parentRunId);
|
|
220
217
|
projectName = langChainTracer?.projectName;
|
|
218
|
+
client = langChainTracer?.client;
|
|
219
|
+
tracingEnabled = tracingEnabled || !!langChainTracer;
|
|
221
220
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
221
|
+
if (!parentRun) {
|
|
222
|
+
return new RunTree({
|
|
223
|
+
client,
|
|
224
|
+
tracingEnabled,
|
|
225
|
+
project_name: projectName,
|
|
226
|
+
name: props.name,
|
|
227
|
+
tags: props.tags,
|
|
228
|
+
metadata: props.metadata,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const parentRunTree = new RunTree({
|
|
232
|
+
name: parentRun.name,
|
|
233
|
+
id: parentRun.id,
|
|
234
|
+
client,
|
|
235
|
+
tracingEnabled,
|
|
236
|
+
project_name: projectName,
|
|
237
|
+
tags: [
|
|
238
|
+
...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
|
|
239
|
+
],
|
|
233
240
|
extra: {
|
|
234
|
-
metadata:
|
|
241
|
+
metadata: {
|
|
242
|
+
...parentRun?.extra?.metadata,
|
|
243
|
+
...parentConfig?.metadata,
|
|
244
|
+
},
|
|
235
245
|
},
|
|
236
|
-
project_name: projectName,
|
|
237
246
|
});
|
|
238
|
-
return
|
|
247
|
+
return parentRunTree.createChild({
|
|
248
|
+
name: props.name,
|
|
249
|
+
tags: props.tags,
|
|
250
|
+
metadata: props.metadata,
|
|
251
|
+
});
|
|
239
252
|
}
|
|
240
253
|
static getDefaultConfig() {
|
|
241
254
|
return {
|
|
@@ -329,7 +342,7 @@ class RunTree {
|
|
|
329
342
|
const runCreate = await this._convertToCreate(this, runtimeEnv, true);
|
|
330
343
|
await this.client.createRun(runCreate);
|
|
331
344
|
if (!excludeChildRuns) {
|
|
332
|
-
warnOnce("Posting with excludeChildRuns=false is deprecated and will be removed in a future version.");
|
|
345
|
+
(0, warn_js_1.warnOnce)("Posting with excludeChildRuns=false is deprecated and will be removed in a future version.");
|
|
333
346
|
for (const childRun of this.child_runs) {
|
|
334
347
|
await childRun.postRun(false);
|
|
335
348
|
}
|
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,13 +1,8 @@
|
|
|
1
1
|
import * as uuid from "uuid";
|
|
2
2
|
import { getEnvironmentVariable, getRuntimeEnvironment, } from "./utils/env.js";
|
|
3
3
|
import { Client } from "./client.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (!warnedMessages[message]) {
|
|
7
|
-
console.warn(message);
|
|
8
|
-
warnedMessages[message] = true;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
4
|
+
import { isTracingEnabled } from "./env.js";
|
|
5
|
+
import { warnOnce } from "./utils/warn.js";
|
|
11
6
|
function stripNonAlphanumeric(input) {
|
|
12
7
|
return input.replace(/[-:.]/g, "");
|
|
13
8
|
}
|
|
@@ -181,34 +176,52 @@ export class RunTree {
|
|
|
181
176
|
}
|
|
182
177
|
}
|
|
183
178
|
}
|
|
184
|
-
static fromRunnableConfig(
|
|
179
|
+
static fromRunnableConfig(parentConfig, props) {
|
|
185
180
|
// We only handle the callback manager case for now
|
|
186
|
-
const callbackManager =
|
|
181
|
+
const callbackManager = parentConfig?.callbacks;
|
|
187
182
|
let parentRun;
|
|
188
183
|
let projectName;
|
|
184
|
+
let client;
|
|
185
|
+
let tracingEnabled = isTracingEnabled();
|
|
189
186
|
if (callbackManager) {
|
|
190
187
|
const parentRunId = callbackManager?.getParentRunId?.() ?? "";
|
|
191
188
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
|
|
192
189
|
parentRun = langChainTracer?.getRun?.(parentRunId);
|
|
193
190
|
projectName = langChainTracer?.projectName;
|
|
191
|
+
client = langChainTracer?.client;
|
|
192
|
+
tracingEnabled = tracingEnabled || !!langChainTracer;
|
|
194
193
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
194
|
+
if (!parentRun) {
|
|
195
|
+
return new RunTree({
|
|
196
|
+
client,
|
|
197
|
+
tracingEnabled,
|
|
198
|
+
project_name: projectName,
|
|
199
|
+
name: props.name,
|
|
200
|
+
tags: props.tags,
|
|
201
|
+
metadata: props.metadata,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
const parentRunTree = new RunTree({
|
|
205
|
+
name: parentRun.name,
|
|
206
|
+
id: parentRun.id,
|
|
207
|
+
client,
|
|
208
|
+
tracingEnabled,
|
|
209
|
+
project_name: projectName,
|
|
210
|
+
tags: [
|
|
211
|
+
...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
|
|
212
|
+
],
|
|
206
213
|
extra: {
|
|
207
|
-
metadata:
|
|
214
|
+
metadata: {
|
|
215
|
+
...parentRun?.extra?.metadata,
|
|
216
|
+
...parentConfig?.metadata,
|
|
217
|
+
},
|
|
208
218
|
},
|
|
209
|
-
project_name: projectName,
|
|
210
219
|
});
|
|
211
|
-
return
|
|
220
|
+
return parentRunTree.createChild({
|
|
221
|
+
name: props.name,
|
|
222
|
+
tags: props.tags,
|
|
223
|
+
metadata: props.metadata,
|
|
224
|
+
});
|
|
212
225
|
}
|
|
213
226
|
static getDefaultConfig() {
|
|
214
227
|
return {
|