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.
Files changed (42) hide show
  1. package/dist/client.cjs +17 -2
  2. package/dist/client.d.ts +9 -2
  3. package/dist/client.js +17 -2
  4. package/dist/env.cjs +17 -0
  5. package/dist/env.d.ts +1 -0
  6. package/dist/env.js +13 -0
  7. package/dist/evaluation/_runner.cjs +29 -3
  8. package/dist/evaluation/_runner.d.ts +2 -1
  9. package/dist/evaluation/_runner.js +29 -3
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +1 -1
  13. package/dist/langchain.cjs +105 -0
  14. package/dist/langchain.d.ts +29 -0
  15. package/dist/langchain.js +100 -0
  16. package/dist/run_trees.cjs +25 -16
  17. package/dist/run_trees.d.ts +1 -1
  18. package/dist/run_trees.js +25 -16
  19. package/dist/schemas.d.ts +20 -0
  20. package/dist/singletons/traceable.cjs +62 -0
  21. package/dist/singletons/traceable.d.ts +23 -0
  22. package/dist/singletons/traceable.js +57 -0
  23. package/dist/singletons/types.cjs +2 -0
  24. package/dist/singletons/types.d.ts +38 -0
  25. package/dist/singletons/types.js +1 -0
  26. package/dist/traceable.cjs +38 -72
  27. package/dist/traceable.d.ts +14 -47
  28. package/dist/traceable.js +32 -66
  29. package/dist/utils/error.cjs +25 -0
  30. package/dist/utils/error.d.ts +1 -0
  31. package/dist/utils/error.js +21 -0
  32. package/dist/wrappers/openai.cjs +32 -0
  33. package/dist/wrappers/openai.js +32 -0
  34. package/langchain.cjs +1 -0
  35. package/langchain.d.cts +1 -0
  36. package/langchain.d.ts +1 -0
  37. package/langchain.js +1 -0
  38. package/package.json +33 -3
  39. package/singletons/traceable.cjs +1 -0
  40. package/singletons/traceable.d.cts +1 -0
  41. package/singletons/traceable.d.ts +1 -0
  42. package/singletons/traceable.js +1 -0
package/dist/traceable.js CHANGED
@@ -1,23 +1,34 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { RunTree, isRunTree, isRunnableConfigLike, } from "./run_trees.js";
3
- import { getEnvironmentVariable } from "./utils/env.js";
3
+ import { isTracingEnabled } from "./env.js";
4
+ import { ROOT, AsyncLocalStorageProviderSingleton, } from "./singletons/traceable.js";
5
+ AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
4
6
  function isPromiseMethod(x) {
5
7
  if (x === "then" || x === "catch" || x === "finally") {
6
8
  return true;
7
9
  }
8
10
  return false;
9
11
  }
10
- const asyncLocalStorage = new AsyncLocalStorage();
11
- export const ROOT = Symbol("langsmith:traceable:root");
12
+ function isKVMap(x) {
13
+ if (typeof x !== "object" || x == null) {
14
+ return false;
15
+ }
16
+ const prototype = Object.getPrototypeOf(x);
17
+ return ((prototype === null ||
18
+ prototype === Object.prototype ||
19
+ Object.getPrototypeOf(prototype) === null) &&
20
+ !(Symbol.toStringTag in x) &&
21
+ !(Symbol.iterator in x));
22
+ }
12
23
  const isAsyncIterable = (x) => x != null &&
13
24
  typeof x === "object" &&
14
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
26
  typeof x[Symbol.asyncIterator] === "function";
16
- const GeneratorFunction = function* () { }.constructor;
17
27
  const isIteratorLike = (x) => x != null &&
18
28
  typeof x === "object" &&
19
29
  "next" in x &&
20
30
  typeof x.next === "function";
31
+ const GeneratorFunction = function* () { }.constructor;
21
32
  const isGenerator = (x) =>
22
33
  // eslint-disable-next-line no-instanceof/no-instanceof
23
34
  x != null && typeof x === "function" && x instanceof GeneratorFunction;
@@ -29,18 +40,6 @@ const isReadableStream = (x) => x != null &&
29
40
  typeof x === "object" &&
30
41
  "getReader" in x &&
31
42
  typeof x.getReader === "function";
32
- const tracingIsEnabled = (tracingEnabled) => {
33
- if (tracingEnabled !== undefined) {
34
- return tracingEnabled;
35
- }
36
- const envVars = [
37
- "LANGSMITH_TRACING_V2",
38
- "LANGCHAIN_TRACING_V2",
39
- "LANGSMITH_TRACING",
40
- "LANGCHAIN_TRACING",
41
- ];
42
- return Boolean(envVars.find((envVar) => getEnvironmentVariable(envVar) === "true"));
43
- };
44
43
  const handleRunInputs = (rawInputs) => {
45
44
  const firstInput = rawInputs[0];
46
45
  if (firstInput == null) {
@@ -60,12 +59,19 @@ const handleRunOutputs = (rawOutputs) => {
60
59
  }
61
60
  return { outputs: rawOutputs };
62
61
  };
63
- const getTracingRunTree = (runTree, inputs) => {
64
- const tracingEnabled_ = tracingIsEnabled(runTree.tracingEnabled);
65
- if (!tracingEnabled_) {
62
+ const getTracingRunTree = (runTree, inputs, getInvocationParams) => {
63
+ if (!isTracingEnabled(runTree.tracingEnabled)) {
66
64
  return undefined;
67
65
  }
68
66
  runTree.inputs = handleRunInputs(inputs);
67
+ const invocationParams = getInvocationParams?.(...inputs);
68
+ if (invocationParams != null) {
69
+ runTree.extra ??= {};
70
+ runTree.extra.metadata = {
71
+ ...runTree.extra.metadata,
72
+ ...invocationParams,
73
+ };
74
+ }
69
75
  return runTree;
70
76
  };
71
77
  // idea: store the state of the promise outside
@@ -266,6 +272,7 @@ export function traceable(wrappedFunc, config) {
266
272
  ...runTreeConfig,
267
273
  };
268
274
  }
275
+ const asyncLocalStorage = AsyncLocalStorageProviderSingleton.getInstance();
269
276
  // TODO: deal with possible nested promises and async iterables
270
277
  const processedArgs = args;
271
278
  for (let i = 0; i < processedArgs.length; i++) {
@@ -276,11 +283,11 @@ export function traceable(wrappedFunc, config) {
276
283
  // used for handoff between LangChain.JS and traceable functions
277
284
  if (isRunnableConfigLike(firstArg)) {
278
285
  return [
279
- getTracingRunTree(RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs),
286
+ getTracingRunTree(RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs, config?.getInvocationParams),
280
287
  restArgs,
281
288
  ];
282
289
  }
283
- // legacy CallbackManagerRunTree used in runOnDataset
290
+ // deprecated: legacy CallbackManagerRunTree used in runOnDataset
284
291
  // override ALS and do not pass-through the run tree
285
292
  if (isRunTree(firstArg) &&
286
293
  "callbackManager" in firstArg &&
@@ -292,7 +299,7 @@ export function traceable(wrappedFunc, config) {
292
299
  if (firstArg === ROOT || isRunTree(firstArg)) {
293
300
  const currentRunTree = getTracingRunTree(firstArg === ROOT
294
301
  ? new RunTree(ensuredConfig)
295
- : firstArg.createChild(ensuredConfig), restArgs);
302
+ : firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams);
296
303
  return [currentRunTree, [currentRunTree, ...restArgs]];
297
304
  }
298
305
  // Node.JS uses AsyncLocalStorage (ALS) and AsyncResource
@@ -300,11 +307,11 @@ export function traceable(wrappedFunc, config) {
300
307
  const prevRunFromStore = asyncLocalStorage.getStore();
301
308
  if (prevRunFromStore) {
302
309
  return [
303
- getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs),
310
+ getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs, config?.getInvocationParams),
304
311
  processedArgs,
305
312
  ];
306
313
  }
307
- const currentRunTree = getTracingRunTree(new RunTree(ensuredConfig), processedArgs);
314
+ const currentRunTree = getTracingRunTree(new RunTree(ensuredConfig), processedArgs, config?.getInvocationParams);
308
315
  return [currentRunTree, processedArgs];
309
316
  })();
310
317
  return asyncLocalStorage.run(currentRunTree, () => {
@@ -440,45 +447,4 @@ export function traceable(wrappedFunc, config) {
440
447
  });
441
448
  return traceableFunc;
442
449
  }
443
- /**
444
- * Return the current run tree from within a traceable-wrapped function.
445
- * Will throw an error if called outside of a traceable function.
446
- *
447
- * @returns The run tree for the given context.
448
- */
449
- export function getCurrentRunTree() {
450
- const runTree = asyncLocalStorage.getStore();
451
- if (runTree === undefined) {
452
- throw new Error([
453
- "Could not get the current run tree.",
454
- "",
455
- "Please make sure you are calling this method within a traceable function.",
456
- ].join("\n"));
457
- }
458
- return runTree;
459
- }
460
- export function isTraceableFunction(x
461
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
462
- ) {
463
- return typeof x === "function" && "langsmith:traceable" in x;
464
- }
465
- function isKVMap(x) {
466
- if (typeof x !== "object" || x == null) {
467
- return false;
468
- }
469
- const prototype = Object.getPrototypeOf(x);
470
- return ((prototype === null ||
471
- prototype === Object.prototype ||
472
- Object.getPrototypeOf(prototype) === null) &&
473
- !(Symbol.toStringTag in x) &&
474
- !(Symbol.iterator in x));
475
- }
476
- export function wrapFunctionAndEnsureTraceable(target, options, name = "target") {
477
- if (typeof target === "function") {
478
- return traceable(target, {
479
- ...options,
480
- name,
481
- });
482
- }
483
- throw new Error("Target must be runnable function");
484
- }
450
+ export { getCurrentRunTree, isTraceableFunction, ROOT, } from "./singletons/traceable.js";
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printErrorStackTrace = void 0;
4
+ function getErrorStackTrace(e) {
5
+ if (typeof e !== "object" || e == null)
6
+ return undefined;
7
+ if (!("stack" in e) || typeof e.stack !== "string")
8
+ return undefined;
9
+ let stack = e.stack;
10
+ const prevLine = `${e}`;
11
+ if (stack.startsWith(prevLine)) {
12
+ stack = stack.slice(prevLine.length);
13
+ }
14
+ if (stack.startsWith("\n")) {
15
+ stack = stack.slice(1);
16
+ }
17
+ return stack;
18
+ }
19
+ function printErrorStackTrace(e) {
20
+ const stack = getErrorStackTrace(e);
21
+ if (stack == null)
22
+ return;
23
+ console.error(stack);
24
+ }
25
+ exports.printErrorStackTrace = printErrorStackTrace;
@@ -0,0 +1 @@
1
+ export declare function printErrorStackTrace(e: unknown): void;
@@ -0,0 +1,21 @@
1
+ function getErrorStackTrace(e) {
2
+ if (typeof e !== "object" || e == null)
3
+ return undefined;
4
+ if (!("stack" in e) || typeof e.stack !== "string")
5
+ return undefined;
6
+ let stack = e.stack;
7
+ const prevLine = `${e}`;
8
+ if (stack.startsWith(prevLine)) {
9
+ stack = stack.slice(prevLine.length);
10
+ }
11
+ if (stack.startsWith("\n")) {
12
+ stack = stack.slice(1);
13
+ }
14
+ return stack;
15
+ }
16
+ export function printErrorStackTrace(e) {
17
+ const stack = getErrorStackTrace(e);
18
+ if (stack == null)
19
+ return;
20
+ console.error(stack);
21
+ }
@@ -147,6 +147,22 @@ const wrapOpenAI = (openai, options) => {
147
147
  run_type: "llm",
148
148
  aggregator: chatAggregator,
149
149
  argsConfigPath: [1, "langsmithExtra"],
150
+ getInvocationParams: (payload) => {
151
+ if (typeof payload !== "object" || payload == null)
152
+ return undefined;
153
+ // we can safely do so, as the types are not exported in TSC
154
+ const params = payload;
155
+ const ls_stop = (typeof params.stop === "string" ? [params.stop] : params.stop) ??
156
+ undefined;
157
+ return {
158
+ ls_provider: "openai",
159
+ ls_model_type: "chat",
160
+ ls_model_name: params.model,
161
+ ls_max_tokens: params.max_tokens ?? undefined,
162
+ ls_temperature: params.temperature ?? undefined,
163
+ ls_stop,
164
+ };
165
+ },
150
166
  ...options,
151
167
  });
152
168
  openai.completions.create = (0, traceable_js_1.traceable)(openai.completions.create.bind(openai.completions), {
@@ -154,6 +170,22 @@ const wrapOpenAI = (openai, options) => {
154
170
  run_type: "llm",
155
171
  aggregator: textAggregator,
156
172
  argsConfigPath: [1, "langsmithExtra"],
173
+ getInvocationParams: (payload) => {
174
+ if (typeof payload !== "object" || payload == null)
175
+ return undefined;
176
+ // we can safely do so, as the types are not exported in TSC
177
+ const params = payload;
178
+ const ls_stop = (typeof params.stop === "string" ? [params.stop] : params.stop) ??
179
+ undefined;
180
+ return {
181
+ ls_provider: "openai",
182
+ ls_model_type: "chat",
183
+ ls_model_name: params.model,
184
+ ls_max_tokens: params.max_tokens ?? undefined,
185
+ ls_temperature: params.temperature ?? undefined,
186
+ ls_stop,
187
+ };
188
+ },
157
189
  ...options,
158
190
  });
159
191
  return openai;
@@ -144,6 +144,22 @@ export const wrapOpenAI = (openai, options) => {
144
144
  run_type: "llm",
145
145
  aggregator: chatAggregator,
146
146
  argsConfigPath: [1, "langsmithExtra"],
147
+ getInvocationParams: (payload) => {
148
+ if (typeof payload !== "object" || payload == null)
149
+ return undefined;
150
+ // we can safely do so, as the types are not exported in TSC
151
+ const params = payload;
152
+ const ls_stop = (typeof params.stop === "string" ? [params.stop] : params.stop) ??
153
+ undefined;
154
+ return {
155
+ ls_provider: "openai",
156
+ ls_model_type: "chat",
157
+ ls_model_name: params.model,
158
+ ls_max_tokens: params.max_tokens ?? undefined,
159
+ ls_temperature: params.temperature ?? undefined,
160
+ ls_stop,
161
+ };
162
+ },
147
163
  ...options,
148
164
  });
149
165
  openai.completions.create = traceable(openai.completions.create.bind(openai.completions), {
@@ -151,6 +167,22 @@ export const wrapOpenAI = (openai, options) => {
151
167
  run_type: "llm",
152
168
  aggregator: textAggregator,
153
169
  argsConfigPath: [1, "langsmithExtra"],
170
+ getInvocationParams: (payload) => {
171
+ if (typeof payload !== "object" || payload == null)
172
+ return undefined;
173
+ // we can safely do so, as the types are not exported in TSC
174
+ const params = payload;
175
+ const ls_stop = (typeof params.stop === "string" ? [params.stop] : params.stop) ??
176
+ undefined;
177
+ return {
178
+ ls_provider: "openai",
179
+ ls_model_type: "chat",
180
+ ls_model_name: params.model,
181
+ ls_max_tokens: params.max_tokens ?? undefined,
182
+ ls_temperature: params.temperature ?? undefined,
183
+ ls_stop,
184
+ };
185
+ },
154
186
  ...options,
155
187
  });
156
188
  return openai;
package/langchain.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/langchain.cjs');
@@ -0,0 +1 @@
1
+ export * from './dist/langchain.js'
package/langchain.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/langchain.js'
package/langchain.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/langchain.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
5
5
  "packageManager": "yarn@1.22.19",
6
6
  "files": [
@@ -25,6 +25,10 @@
25
25
  "schemas.js",
26
26
  "schemas.d.ts",
27
27
  "schemas.d.cts",
28
+ "langchain.cjs",
29
+ "langchain.js",
30
+ "langchain.d.ts",
31
+ "langchain.d.cts",
28
32
  "wrappers.cjs",
29
33
  "wrappers.js",
30
34
  "wrappers.d.ts",
@@ -33,6 +37,10 @@
33
37
  "wrappers/openai.js",
34
38
  "wrappers/openai.d.ts",
35
39
  "wrappers/openai.d.cts",
40
+ "singletons/traceable.cjs",
41
+ "singletons/traceable.js",
42
+ "singletons/traceable.d.ts",
43
+ "singletons/traceable.d.cts",
36
44
  "index.cjs",
37
45
  "index.js",
38
46
  "index.d.ts",
@@ -109,11 +117,15 @@
109
117
  "typescript": "^5.4.5"
110
118
  },
111
119
  "peerDependencies": {
112
- "openai": "*"
120
+ "openai": "*",
121
+ "@langchain/core": "*"
113
122
  },
114
123
  "peerDependenciesMeta": {
115
124
  "openai": {
116
125
  "optional": true
126
+ },
127
+ "@langchain/core": {
128
+ "optional": true
117
129
  }
118
130
  },
119
131
  "lint-staged": {
@@ -177,6 +189,15 @@
177
189
  "import": "./schemas.js",
178
190
  "require": "./schemas.cjs"
179
191
  },
192
+ "./langchain": {
193
+ "types": {
194
+ "import": "./langchain.d.ts",
195
+ "require": "./langchain.d.cts",
196
+ "default": "./langchain.d.ts"
197
+ },
198
+ "import": "./langchain.js",
199
+ "require": "./langchain.cjs"
200
+ },
180
201
  "./wrappers": {
181
202
  "types": {
182
203
  "import": "./wrappers.d.ts",
@@ -195,6 +216,15 @@
195
216
  "import": "./wrappers/openai.js",
196
217
  "require": "./wrappers/openai.cjs"
197
218
  },
219
+ "./singletons/traceable": {
220
+ "types": {
221
+ "import": "./singletons/traceable.d.ts",
222
+ "require": "./singletons/traceable.d.cts",
223
+ "default": "./singletons/traceable.d.ts"
224
+ },
225
+ "import": "./singletons/traceable.js",
226
+ "require": "./singletons/traceable.cjs"
227
+ },
198
228
  "./package.json": "./package.json"
199
229
  }
200
- }
230
+ }
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/singletons/traceable.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/singletons/traceable.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/singletons/traceable.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/singletons/traceable.js'