langsmith 0.1.28 → 0.1.30

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.
@@ -48,7 +48,16 @@ class _ExperimentManager {
48
48
  for await (const example of unresolvedData) {
49
49
  exs.push(example);
50
50
  }
51
- this.setExamples(exs);
51
+ if (this._numRepetitions && this._numRepetitions > 0) {
52
+ const repeatedExamples = [];
53
+ for (let i = 0; i < this._numRepetitions; i++) {
54
+ repeatedExamples.push(...exs);
55
+ }
56
+ this.setExamples(repeatedExamples);
57
+ }
58
+ else {
59
+ this.setExamples(exs);
60
+ }
52
61
  }
53
62
  return this._examples;
54
63
  }
@@ -120,6 +129,12 @@ class _ExperimentManager {
120
129
  writable: true,
121
130
  value: void 0
122
131
  });
132
+ Object.defineProperty(this, "_numRepetitions", {
133
+ enumerable: true,
134
+ configurable: true,
135
+ writable: true,
136
+ value: void 0
137
+ });
123
138
  Object.defineProperty(this, "_runsArray", {
124
139
  enumerable: true,
125
140
  configurable: true,
@@ -188,6 +203,7 @@ class _ExperimentManager {
188
203
  this._runs = args.runs;
189
204
  this._evaluationResults = args.evaluationResults;
190
205
  this._summaryResults = args.summaryResults;
206
+ this._numRepetitions = args.numRepetitions;
191
207
  }
192
208
  _getExperiment() {
193
209
  if (!this._experiment) {
@@ -255,6 +271,7 @@ class _ExperimentManager {
255
271
  const firstExample = examples[0];
256
272
  const project = await this._getProject(firstExample);
257
273
  await this._printExperimentStart();
274
+ this._metadata["num_repetitions"] = this._numRepetitions;
258
275
  return new _ExperimentManager({
259
276
  examples,
260
277
  experiment: project,
@@ -614,6 +631,7 @@ async function _evaluate(target, fields) {
614
631
  metadata: fields.metadata,
615
632
  experiment: experiment_ ?? fields.experimentPrefix,
616
633
  runs: newRuns ?? undefined,
634
+ numRepetitions: fields.numRepetitions ?? 1,
617
635
  }).start();
618
636
  if (_isCallable(target)) {
619
637
  manager = await manager.withPredictions(convertInvokeToTopLevel(target), { maxConcurrency: fields.maxConcurrency });
@@ -23,6 +23,7 @@ interface _ExperimentManagerArgs {
23
23
  evaluationResults?: AsyncGenerator<EvaluationResults>;
24
24
  summaryResults?: AsyncGenerator<(runsArray: Run[]) => AsyncGenerator<EvaluationResults, any, unknown>, any, unknown>;
25
25
  examples?: Example[];
26
+ numRepetitions?: number;
26
27
  _runsArray?: Run[];
27
28
  }
28
29
  export interface EvaluateOptions {
@@ -65,6 +66,12 @@ export interface EvaluateOptions {
65
66
  * @default undefined
66
67
  */
67
68
  client?: Client;
69
+ /**
70
+ * The number of repetitions to perform. Each example
71
+ * will be run this many times.
72
+ * @default 1
73
+ */
74
+ numRepetitions?: number;
68
75
  }
69
76
  export declare function evaluate(
70
77
  /**
@@ -88,6 +95,7 @@ declare class _ExperimentManager {
88
95
  _evaluationResults?: AsyncGenerator<EvaluationResults>;
89
96
  _summaryResults?: AsyncGenerator<(runsArray: Run[]) => AsyncGenerator<EvaluationResults, any, unknown>, any, unknown>;
90
97
  _examples?: Example[];
98
+ _numRepetitions?: number;
91
99
  _runsArray?: Run[];
92
100
  client: Client;
93
101
  _experiment?: TracerSession;
@@ -44,7 +44,16 @@ class _ExperimentManager {
44
44
  for await (const example of unresolvedData) {
45
45
  exs.push(example);
46
46
  }
47
- this.setExamples(exs);
47
+ if (this._numRepetitions && this._numRepetitions > 0) {
48
+ const repeatedExamples = [];
49
+ for (let i = 0; i < this._numRepetitions; i++) {
50
+ repeatedExamples.push(...exs);
51
+ }
52
+ this.setExamples(repeatedExamples);
53
+ }
54
+ else {
55
+ this.setExamples(exs);
56
+ }
48
57
  }
49
58
  return this._examples;
50
59
  }
@@ -116,6 +125,12 @@ class _ExperimentManager {
116
125
  writable: true,
117
126
  value: void 0
118
127
  });
128
+ Object.defineProperty(this, "_numRepetitions", {
129
+ enumerable: true,
130
+ configurable: true,
131
+ writable: true,
132
+ value: void 0
133
+ });
119
134
  Object.defineProperty(this, "_runsArray", {
120
135
  enumerable: true,
121
136
  configurable: true,
@@ -184,6 +199,7 @@ class _ExperimentManager {
184
199
  this._runs = args.runs;
185
200
  this._evaluationResults = args.evaluationResults;
186
201
  this._summaryResults = args.summaryResults;
202
+ this._numRepetitions = args.numRepetitions;
187
203
  }
188
204
  _getExperiment() {
189
205
  if (!this._experiment) {
@@ -251,6 +267,7 @@ class _ExperimentManager {
251
267
  const firstExample = examples[0];
252
268
  const project = await this._getProject(firstExample);
253
269
  await this._printExperimentStart();
270
+ this._metadata["num_repetitions"] = this._numRepetitions;
254
271
  return new _ExperimentManager({
255
272
  examples,
256
273
  experiment: project,
@@ -610,6 +627,7 @@ async function _evaluate(target, fields) {
610
627
  metadata: fields.metadata,
611
628
  experiment: experiment_ ?? fields.experimentPrefix,
612
629
  runs: newRuns ?? undefined,
630
+ numRepetitions: fields.numRepetitions ?? 1,
613
631
  }).start();
614
632
  if (_isCallable(target)) {
615
633
  manager = await manager.withPredictions(convertInvokeToTopLevel(target), { maxConcurrency: fields.maxConcurrency });
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.28";
9
+ exports.__version__ = "0.1.30";
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.28";
4
+ export declare const __version__ = "0.1.30";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { Client } from "./client.js";
2
2
  export { RunTree } from "./run_trees.js";
3
3
  // Update using yarn bump-version
4
- export const __version__ = "0.1.28";
4
+ export const __version__ = "0.1.30";
@@ -39,6 +39,53 @@ function convertToDottedOrderFormat(epoch, runId, executionOrder = 1) {
39
39
  return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
40
40
  }
41
41
  exports.convertToDottedOrderFormat = convertToDottedOrderFormat;
42
+ /**
43
+ * Baggage header information
44
+ */
45
+ class Baggage {
46
+ constructor(metadata, tags) {
47
+ Object.defineProperty(this, "metadata", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "tags", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ this.metadata = metadata;
60
+ this.tags = tags;
61
+ }
62
+ static fromHeader(value) {
63
+ const items = value.split(",");
64
+ let metadata = {};
65
+ let tags = [];
66
+ for (const item of items) {
67
+ const [key, uriValue] = item.split("=");
68
+ const value = decodeURIComponent(uriValue);
69
+ if (key === "langsmith-metadata") {
70
+ metadata = JSON.parse(value);
71
+ }
72
+ else if (key === "langsmith-tags") {
73
+ tags = value.split(",");
74
+ }
75
+ }
76
+ return new Baggage(metadata, tags);
77
+ }
78
+ toHeader() {
79
+ const items = [];
80
+ if (this.metadata && Object.keys(this.metadata).length > 0) {
81
+ items.push(`langsmith-metadata=${encodeURIComponent(JSON.stringify(this.metadata))}`);
82
+ }
83
+ if (this.tags && this.tags.length > 0) {
84
+ items.push(`langsmith-tags=${encodeURIComponent(this.tags.join(","))}`);
85
+ }
86
+ return items.join(",");
87
+ }
88
+ }
42
89
  class RunTree {
43
90
  constructor(originalConfig) {
44
91
  Object.defineProperty(this, "id", {
@@ -203,53 +250,6 @@ class RunTree {
203
250
  }
204
251
  }
205
252
  }
206
- static fromRunnableConfig(parentConfig, props) {
207
- // We only handle the callback manager case for now
208
- const callbackManager = parentConfig?.callbacks;
209
- let parentRun;
210
- let projectName;
211
- let client;
212
- let tracingEnabled = (0, env_js_2.isTracingEnabled)();
213
- if (callbackManager) {
214
- const parentRunId = callbackManager?.getParentRunId?.() ?? "";
215
- const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
216
- parentRun = langChainTracer?.getRun?.(parentRunId);
217
- projectName = langChainTracer?.projectName;
218
- client = langChainTracer?.client;
219
- tracingEnabled = tracingEnabled || !!langChainTracer;
220
- }
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
- ],
240
- extra: {
241
- metadata: {
242
- ...parentRun?.extra?.metadata,
243
- ...parentConfig?.metadata,
244
- },
245
- },
246
- });
247
- return parentRunTree.createChild({
248
- name: props.name,
249
- tags: props.tags,
250
- metadata: props.metadata,
251
- });
252
- }
253
253
  static getDefaultConfig() {
254
254
  return {
255
255
  id: uuid.v4(),
@@ -367,6 +367,82 @@ class RunTree {
367
367
  toJSON() {
368
368
  return this._convertToCreate(this, undefined, false);
369
369
  }
370
+ static fromRunnableConfig(parentConfig, props) {
371
+ // We only handle the callback manager case for now
372
+ const callbackManager = parentConfig?.callbacks;
373
+ let parentRun;
374
+ let projectName;
375
+ let client;
376
+ let tracingEnabled = (0, env_js_2.isTracingEnabled)();
377
+ if (callbackManager) {
378
+ const parentRunId = callbackManager?.getParentRunId?.() ?? "";
379
+ const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
380
+ parentRun = langChainTracer?.getRun?.(parentRunId);
381
+ projectName = langChainTracer?.projectName;
382
+ client = langChainTracer?.client;
383
+ tracingEnabled = tracingEnabled || !!langChainTracer;
384
+ }
385
+ if (!parentRun) {
386
+ return new RunTree({
387
+ ...props,
388
+ client,
389
+ tracingEnabled,
390
+ project_name: projectName,
391
+ });
392
+ }
393
+ const parentRunTree = new RunTree({
394
+ name: parentRun.name,
395
+ id: parentRun.id,
396
+ client,
397
+ tracingEnabled,
398
+ project_name: projectName,
399
+ tags: [
400
+ ...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
401
+ ],
402
+ extra: {
403
+ metadata: {
404
+ ...parentRun?.extra?.metadata,
405
+ ...parentConfig?.metadata,
406
+ },
407
+ },
408
+ });
409
+ return parentRunTree.createChild(props);
410
+ }
411
+ static fromDottedOrder(dottedOrder) {
412
+ return this.fromHeaders({ "langsmith-trace": dottedOrder });
413
+ }
414
+ static fromHeaders(headers, inheritArgs) {
415
+ const headerTrace = headers["langsmith-trace"];
416
+ if (!headerTrace || typeof headerTrace !== "string")
417
+ return undefined;
418
+ const parentDottedOrder = headerTrace.trim();
419
+ const parsedDottedOrder = parentDottedOrder.split(".").map((part) => {
420
+ const [strTime, uuid] = part.split("Z");
421
+ return { strTime, time: Date.parse(strTime + "Z"), uuid };
422
+ });
423
+ const traceId = parsedDottedOrder[0].uuid;
424
+ const config = {
425
+ ...inheritArgs,
426
+ name: inheritArgs?.["name"] ?? "parent",
427
+ run_type: inheritArgs?.["run_type"] ?? "chain",
428
+ start_time: inheritArgs?.["start_time"] ?? Date.now(),
429
+ id: parsedDottedOrder.at(-1)?.uuid,
430
+ trace_id: traceId,
431
+ dotted_order: parentDottedOrder,
432
+ };
433
+ if (headers["baggage"]) {
434
+ const baggage = Baggage.fromHeader(headers["baggage"]);
435
+ config.metadata = baggage.metadata;
436
+ config.tags = baggage.tags;
437
+ }
438
+ return new RunTree(config);
439
+ }
440
+ toHeaders() {
441
+ return {
442
+ "langsmith-trace": this.dotted_order,
443
+ baggage: new Baggage(this.extra?.metadata, this.tags).toHeader(),
444
+ };
445
+ }
370
446
  }
371
447
  exports.RunTree = RunTree;
372
448
  function isRunTree(x) {
@@ -24,6 +24,8 @@ export interface RunTreeConfig {
24
24
  on_end?: (runTree: RunTree) => void;
25
25
  execution_order?: number;
26
26
  child_execution_order?: number;
27
+ trace_id?: string;
28
+ dotted_order?: string;
27
29
  }
28
30
  export interface RunnableConfigLike {
29
31
  /**
@@ -66,11 +68,6 @@ export declare class RunTree implements BaseRun {
66
68
  execution_order: number;
67
69
  child_execution_order: number;
68
70
  constructor(originalConfig: RunTreeConfig);
69
- static fromRunnableConfig(parentConfig: RunnableConfigLike, props: {
70
- name: string;
71
- tags?: string[];
72
- metadata?: KVMap;
73
- }): RunTree;
74
71
  private static getDefaultConfig;
75
72
  createChild(config: RunTreeConfig): RunTree;
76
73
  end(outputs?: KVMap, error?: string, endTime?: number): Promise<void>;
@@ -78,6 +75,13 @@ export declare class RunTree implements BaseRun {
78
75
  postRun(excludeChildRuns?: boolean): Promise<void>;
79
76
  patchRun(): Promise<void>;
80
77
  toJSON(): RunCreate;
78
+ static fromRunnableConfig(parentConfig: RunnableConfigLike, props: RunTreeConfig): RunTree;
79
+ static fromDottedOrder(dottedOrder: string): RunTree | undefined;
80
+ static fromHeaders(headers: Record<string, string>, inheritArgs?: RunTreeConfig): RunTree | undefined;
81
+ toHeaders(): {
82
+ "langsmith-trace": string;
83
+ baggage: string;
84
+ };
81
85
  }
82
86
  export declare function isRunTree(x?: unknown): x is RunTree;
83
87
  export declare function isRunnableConfigLike(x?: unknown): x is RunnableConfigLike;
package/dist/run_trees.js CHANGED
@@ -12,6 +12,53 @@ export function convertToDottedOrderFormat(epoch, runId, executionOrder = 1) {
12
12
  const paddedOrder = executionOrder.toFixed(0).slice(0, 3).padStart(3, "0");
13
13
  return (stripNonAlphanumeric(`${new Date(epoch).toISOString().slice(0, -1)}${paddedOrder}Z`) + runId);
14
14
  }
15
+ /**
16
+ * Baggage header information
17
+ */
18
+ class Baggage {
19
+ constructor(metadata, tags) {
20
+ Object.defineProperty(this, "metadata", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "tags", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ this.metadata = metadata;
33
+ this.tags = tags;
34
+ }
35
+ static fromHeader(value) {
36
+ const items = value.split(",");
37
+ let metadata = {};
38
+ let tags = [];
39
+ for (const item of items) {
40
+ const [key, uriValue] = item.split("=");
41
+ const value = decodeURIComponent(uriValue);
42
+ if (key === "langsmith-metadata") {
43
+ metadata = JSON.parse(value);
44
+ }
45
+ else if (key === "langsmith-tags") {
46
+ tags = value.split(",");
47
+ }
48
+ }
49
+ return new Baggage(metadata, tags);
50
+ }
51
+ toHeader() {
52
+ const items = [];
53
+ if (this.metadata && Object.keys(this.metadata).length > 0) {
54
+ items.push(`langsmith-metadata=${encodeURIComponent(JSON.stringify(this.metadata))}`);
55
+ }
56
+ if (this.tags && this.tags.length > 0) {
57
+ items.push(`langsmith-tags=${encodeURIComponent(this.tags.join(","))}`);
58
+ }
59
+ return items.join(",");
60
+ }
61
+ }
15
62
  export class RunTree {
16
63
  constructor(originalConfig) {
17
64
  Object.defineProperty(this, "id", {
@@ -176,53 +223,6 @@ export class RunTree {
176
223
  }
177
224
  }
178
225
  }
179
- static fromRunnableConfig(parentConfig, props) {
180
- // We only handle the callback manager case for now
181
- const callbackManager = parentConfig?.callbacks;
182
- let parentRun;
183
- let projectName;
184
- let client;
185
- let tracingEnabled = isTracingEnabled();
186
- if (callbackManager) {
187
- const parentRunId = callbackManager?.getParentRunId?.() ?? "";
188
- const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
189
- parentRun = langChainTracer?.getRun?.(parentRunId);
190
- projectName = langChainTracer?.projectName;
191
- client = langChainTracer?.client;
192
- tracingEnabled = tracingEnabled || !!langChainTracer;
193
- }
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
- ],
213
- extra: {
214
- metadata: {
215
- ...parentRun?.extra?.metadata,
216
- ...parentConfig?.metadata,
217
- },
218
- },
219
- });
220
- return parentRunTree.createChild({
221
- name: props.name,
222
- tags: props.tags,
223
- metadata: props.metadata,
224
- });
225
- }
226
226
  static getDefaultConfig() {
227
227
  return {
228
228
  id: uuid.v4(),
@@ -340,6 +340,82 @@ export class RunTree {
340
340
  toJSON() {
341
341
  return this._convertToCreate(this, undefined, false);
342
342
  }
343
+ static fromRunnableConfig(parentConfig, props) {
344
+ // We only handle the callback manager case for now
345
+ const callbackManager = parentConfig?.callbacks;
346
+ let parentRun;
347
+ let projectName;
348
+ let client;
349
+ let tracingEnabled = isTracingEnabled();
350
+ if (callbackManager) {
351
+ const parentRunId = callbackManager?.getParentRunId?.() ?? "";
352
+ const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
353
+ parentRun = langChainTracer?.getRun?.(parentRunId);
354
+ projectName = langChainTracer?.projectName;
355
+ client = langChainTracer?.client;
356
+ tracingEnabled = tracingEnabled || !!langChainTracer;
357
+ }
358
+ if (!parentRun) {
359
+ return new RunTree({
360
+ ...props,
361
+ client,
362
+ tracingEnabled,
363
+ project_name: projectName,
364
+ });
365
+ }
366
+ const parentRunTree = new RunTree({
367
+ name: parentRun.name,
368
+ id: parentRun.id,
369
+ client,
370
+ tracingEnabled,
371
+ project_name: projectName,
372
+ tags: [
373
+ ...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
374
+ ],
375
+ extra: {
376
+ metadata: {
377
+ ...parentRun?.extra?.metadata,
378
+ ...parentConfig?.metadata,
379
+ },
380
+ },
381
+ });
382
+ return parentRunTree.createChild(props);
383
+ }
384
+ static fromDottedOrder(dottedOrder) {
385
+ return this.fromHeaders({ "langsmith-trace": dottedOrder });
386
+ }
387
+ static fromHeaders(headers, inheritArgs) {
388
+ const headerTrace = headers["langsmith-trace"];
389
+ if (!headerTrace || typeof headerTrace !== "string")
390
+ return undefined;
391
+ const parentDottedOrder = headerTrace.trim();
392
+ const parsedDottedOrder = parentDottedOrder.split(".").map((part) => {
393
+ const [strTime, uuid] = part.split("Z");
394
+ return { strTime, time: Date.parse(strTime + "Z"), uuid };
395
+ });
396
+ const traceId = parsedDottedOrder[0].uuid;
397
+ const config = {
398
+ ...inheritArgs,
399
+ name: inheritArgs?.["name"] ?? "parent",
400
+ run_type: inheritArgs?.["run_type"] ?? "chain",
401
+ start_time: inheritArgs?.["start_time"] ?? Date.now(),
402
+ id: parsedDottedOrder.at(-1)?.uuid,
403
+ trace_id: traceId,
404
+ dotted_order: parentDottedOrder,
405
+ };
406
+ if (headers["baggage"]) {
407
+ const baggage = Baggage.fromHeader(headers["baggage"]);
408
+ config.metadata = baggage.metadata;
409
+ config.tags = baggage.tags;
410
+ }
411
+ return new RunTree(config);
412
+ }
413
+ toHeaders() {
414
+ return {
415
+ "langsmith-trace": this.dotted_order,
416
+ baggage: new Baggage(this.extra?.metadata, this.tags).toHeader(),
417
+ };
418
+ }
343
419
  }
344
420
  export function isRunTree(x) {
345
421
  return (x !== undefined &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.1.28",
3
+ "version": "0.1.30",
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": [