langsmith
Advanced tools
Comparing version 0.1.22 to 0.1.23-rc.0
@@ -23,2 +23,3 @@ import { BaseRun, KVMap } from "./schemas.js"; | ||
client?: Client; | ||
tracingEnabled?: boolean; | ||
on_end?: (runTree: RunTree) => void; | ||
@@ -63,2 +64,3 @@ } | ||
dotted_order: string; | ||
tracingEnabled?: boolean; | ||
constructor(originalConfig: RunTreeConfig); | ||
@@ -65,0 +67,0 @@ static fromRunnableConfig(config: RunnableConfigLike, props: { |
@@ -134,2 +134,8 @@ import * as uuid from "uuid"; | ||
}); | ||
Object.defineProperty(this, "tracingEnabled", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
const defaultConfig = RunTree.getDefaultConfig(); | ||
@@ -215,2 +221,3 @@ const { metadata, ...config } = originalConfig; | ||
client: this.client, | ||
tracingEnabled: this.tracingEnabled, | ||
}); | ||
@@ -217,0 +224,0 @@ this.child_runs.push(child); |
import { RunTree, RunTreeConfig, RunnableConfigLike } from "./run_trees.js"; | ||
export declare const ROOT: unique symbol; | ||
export type RunTreeLike = RunTree; | ||
@@ -7,6 +8,9 @@ type SmartPromise<T> = T extends AsyncGenerator ? T : T extends Promise<unknown> ? T : Promise<T>; | ||
infer Return | ||
] ? { | ||
] ? Args extends [RunTreeLike, ...infer RestArgs] ? { | ||
(runTree: RunTreeLike | typeof ROOT, ...args: RestArgs): SmartPromise<Return>; | ||
(config: RunnableConfigLike, ...args: RestArgs): SmartPromise<Return>; | ||
} : { | ||
(...args: Args): SmartPromise<Return>; | ||
(...args: [runTree: RunTreeLike, ...rest: Args]): SmartPromise<Return>; | ||
(...args: [config: RunnableConfigLike, ...rest: Args]): SmartPromise<Return>; | ||
(runTree: RunTreeLike, ...rest: Args): SmartPromise<Return>; | ||
(config: RunnableConfigLike, ...args: Args): SmartPromise<Return>; | ||
} : never; | ||
@@ -52,3 +56,2 @@ type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never; | ||
argsConfigPath?: [number] | [number, string]; | ||
tracingEnabled?: boolean; | ||
}): TraceableFunction<Func>; | ||
@@ -55,0 +58,0 @@ /** |
@@ -11,2 +11,3 @@ import { AsyncLocalStorage } from "async_hooks"; | ||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
export const ROOT = Symbol("langsmith:traceable:root"); | ||
const isAsyncIterable = (x) => x != null && | ||
@@ -28,7 +29,27 @@ typeof x === "object" && | ||
}; | ||
const getTracingRunTree = (runTree, tracingEnabled) => { | ||
const tracingEnabled_ = tracingIsEnabled(tracingEnabled); | ||
const handleRunInputs = (rawInputs) => { | ||
const firstInput = rawInputs[0]; | ||
if (firstInput == null) { | ||
return {}; | ||
} | ||
if (rawInputs.length > 1) { | ||
return { args: rawInputs }; | ||
} | ||
if (isKVMap(firstInput)) { | ||
return firstInput; | ||
} | ||
return { input: firstInput }; | ||
}; | ||
const handleRunOutputs = (rawOutputs) => { | ||
if (isKVMap(rawOutputs)) { | ||
return rawOutputs; | ||
} | ||
return { outputs: rawOutputs }; | ||
}; | ||
const getTracingRunTree = (runTree, inputs) => { | ||
const tracingEnabled_ = tracingIsEnabled(runTree.tracingEnabled); | ||
if (!tracingEnabled_) { | ||
return undefined; | ||
} | ||
runTree.inputs = handleRunInputs(inputs); | ||
return runTree; | ||
@@ -52,6 +73,4 @@ }; | ||
export function traceable(wrappedFunc, config) { | ||
const { aggregator, argsConfigPath, tracingEnabled, ...runTreeConfig } = config ?? {}; | ||
const { aggregator, argsConfigPath, ...runTreeConfig } = config ?? {}; | ||
const traceableFunc = (...args) => { | ||
let currentRunTree; | ||
let rawInputs; | ||
let ensuredConfig; | ||
@@ -102,37 +121,38 @@ try { | ||
} | ||
const previousRunTree = asyncLocalStorage.getStore(); | ||
if (isRunTree(args[0])) { | ||
currentRunTree = args[0]; | ||
rawInputs = args.slice(1); | ||
} | ||
else if (isRunnableConfigLike(args[0])) { | ||
currentRunTree = RunTree.fromRunnableConfig(args[0], ensuredConfig); | ||
rawInputs = args.slice(1); | ||
} | ||
else if (previousRunTree !== undefined) { | ||
currentRunTree = previousRunTree.createChild(ensuredConfig); | ||
rawInputs = args; | ||
} | ||
else { | ||
currentRunTree = new RunTree(ensuredConfig); | ||
rawInputs = args; | ||
} | ||
currentRunTree = getTracingRunTree(currentRunTree, tracingEnabled); | ||
let inputs; | ||
const firstInput = rawInputs[0]; | ||
if (firstInput == null) { | ||
inputs = {}; | ||
} | ||
else if (rawInputs.length > 1) { | ||
inputs = { args: rawInputs }; | ||
} | ||
else if (isKVMap(firstInput)) { | ||
inputs = firstInput; | ||
} | ||
else { | ||
inputs = { input: firstInput }; | ||
} | ||
if (currentRunTree) { | ||
currentRunTree.inputs = inputs; | ||
} | ||
const [currentRunTree, rawInputs] = (() => { | ||
const [firstArg, ...restArgs] = args; | ||
// used for handoff between LangChain.JS and traceable functions | ||
if (isRunnableConfigLike(firstArg)) { | ||
return [ | ||
getTracingRunTree(RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs), | ||
restArgs, | ||
]; | ||
} | ||
// legacy CallbackManagerRunTree used in runOnDataset | ||
// override ALS and do not pass-through the run tree | ||
if (isRunTree(firstArg) && | ||
"callbackManager" in firstArg && | ||
firstArg.callbackManager != null) { | ||
return [firstArg, restArgs]; | ||
} | ||
// when ALS is unreliable, users can manually | ||
// pass in the run tree | ||
if (firstArg === ROOT || isRunTree(firstArg)) { | ||
const currentRunTree = getTracingRunTree(firstArg === ROOT | ||
? new RunTree(ensuredConfig) | ||
: firstArg.createChild(ensuredConfig), restArgs); | ||
return [currentRunTree, [currentRunTree, ...restArgs]]; | ||
} | ||
// Node.JS uses AsyncLocalStorage (ALS) and AsyncResource | ||
// to allow storing context | ||
const prevRunFromStore = asyncLocalStorage.getStore(); | ||
if (prevRunFromStore) { | ||
return [ | ||
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), args), | ||
args, | ||
]; | ||
} | ||
const currentRunTree = getTracingRunTree(new RunTree(ensuredConfig), args); | ||
return [currentRunTree, args]; | ||
})(); | ||
return asyncLocalStorage.run(currentRunTree, () => { | ||
@@ -266,3 +286,3 @@ const postRunPromise = currentRunTree?.postRun(); | ||
try { | ||
await currentRunTree?.end(isKVMap(rawOutput) ? rawOutput : { outputs: rawOutput }); | ||
await currentRunTree?.end(handleRunOutputs(rawOutput)); | ||
const onEnd = config?.on_end; | ||
@@ -269,0 +289,0 @@ if (onEnd) { |
{ | ||
"name": "langsmith", | ||
"version": "0.1.22", | ||
"version": "0.1.23-rc.0", | ||
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.", | ||
@@ -55,2 +55,3 @@ "packageManager": "yarn@1.22.19", | ||
"test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000", | ||
"watch:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --watch --config jest.config.cjs --testTimeout 100000", | ||
"lint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/", | ||
@@ -57,0 +58,0 @@ "lint:fix": "yarn lint --fix", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
396617
10930
63