langsmith 0.1.25 → 0.1.26
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/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/schemas.d.ts +20 -0
- package/dist/traceable.cjs +13 -5
- package/dist/traceable.d.ts +10 -0
- package/dist/traceable.js +13 -5
- 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/package.json +2 -2
package/dist/client.cjs
CHANGED
|
@@ -1185,6 +1185,14 @@ class Client {
|
|
|
1185
1185
|
const tenantId = await this._getTenantId();
|
|
1186
1186
|
return `${this.getHostUrl()}/o/${tenantId}/projects/p/${project.id}`;
|
|
1187
1187
|
}
|
|
1188
|
+
async getDatasetUrl({ datasetId, datasetName, }) {
|
|
1189
|
+
if (datasetId === undefined && datasetName === undefined) {
|
|
1190
|
+
throw new Error("Must provide either datasetName or datasetId");
|
|
1191
|
+
}
|
|
1192
|
+
const dataset = await this.readDataset({ datasetId, datasetName });
|
|
1193
|
+
const tenantId = await this._getTenantId();
|
|
1194
|
+
return `${this.getHostUrl()}/o/${tenantId}/datasets/${dataset.id}`;
|
|
1195
|
+
}
|
|
1188
1196
|
async _getTenantId() {
|
|
1189
1197
|
if (this._tenantId !== null) {
|
|
1190
1198
|
return this._tenantId;
|
|
@@ -1443,7 +1451,7 @@ class Client {
|
|
|
1443
1451
|
}
|
|
1444
1452
|
await response.json();
|
|
1445
1453
|
}
|
|
1446
|
-
async createExample(inputs, outputs, { datasetId, datasetName, createdAt, exampleId, metadata, }) {
|
|
1454
|
+
async createExample(inputs, outputs, { datasetId, datasetName, createdAt, exampleId, metadata, split, }) {
|
|
1447
1455
|
let datasetId_ = datasetId;
|
|
1448
1456
|
if (datasetId_ === undefined && datasetName === undefined) {
|
|
1449
1457
|
throw new Error("Must provide either datasetName or datasetId");
|
|
@@ -1463,6 +1471,7 @@ class Client {
|
|
|
1463
1471
|
created_at: createdAt_?.toISOString(),
|
|
1464
1472
|
id: exampleId,
|
|
1465
1473
|
metadata,
|
|
1474
|
+
split,
|
|
1466
1475
|
};
|
|
1467
1476
|
const response = await this.caller.call(fetch, `${this.apiUrl}/examples`, {
|
|
1468
1477
|
method: "POST",
|
|
@@ -1496,6 +1505,7 @@ class Client {
|
|
|
1496
1505
|
inputs: input,
|
|
1497
1506
|
outputs: outputs ? outputs[idx] : undefined,
|
|
1498
1507
|
metadata: metadata ? metadata[idx] : undefined,
|
|
1508
|
+
split: props.splits ? props.splits[idx] : undefined,
|
|
1499
1509
|
id: exampleIds ? exampleIds[idx] : undefined,
|
|
1500
1510
|
source_run_id: sourceRunIds ? sourceRunIds[idx] : undefined,
|
|
1501
1511
|
};
|
|
@@ -1533,7 +1543,7 @@ class Client {
|
|
|
1533
1543
|
const path = `/examples/${exampleId}`;
|
|
1534
1544
|
return await this._get(path);
|
|
1535
1545
|
}
|
|
1536
|
-
async *listExamples({ datasetId, datasetName, exampleIds, asOf, inlineS3Urls, metadata, } = {}) {
|
|
1546
|
+
async *listExamples({ datasetId, datasetName, exampleIds, asOf, splits, inlineS3Urls, metadata, } = {}) {
|
|
1537
1547
|
let datasetId_;
|
|
1538
1548
|
if (datasetId !== undefined && datasetName !== undefined) {
|
|
1539
1549
|
throw new Error("Must provide either datasetName or datasetId, not both");
|
|
@@ -1564,6 +1574,11 @@ class Client {
|
|
|
1564
1574
|
params.append("id", id_);
|
|
1565
1575
|
}
|
|
1566
1576
|
}
|
|
1577
|
+
if (splits !== undefined) {
|
|
1578
|
+
for (const split of splits) {
|
|
1579
|
+
params.append("splits", split);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1567
1582
|
if (metadata !== undefined) {
|
|
1568
1583
|
const serializedMetadata = JSON.stringify(metadata);
|
|
1569
1584
|
params.append("metadata", serializedMetadata);
|
package/dist/client.d.ts
CHANGED
|
@@ -145,6 +145,7 @@ export type CreateExampleOptions = {
|
|
|
145
145
|
createdAt?: Date;
|
|
146
146
|
exampleId?: string;
|
|
147
147
|
metadata?: KVMap;
|
|
148
|
+
split?: string | string[];
|
|
148
149
|
};
|
|
149
150
|
export declare class Queue<T> {
|
|
150
151
|
items: [T, () => void][];
|
|
@@ -338,6 +339,10 @@ export declare class Client {
|
|
|
338
339
|
projectId?: string;
|
|
339
340
|
projectName?: string;
|
|
340
341
|
}): Promise<string>;
|
|
342
|
+
getDatasetUrl({ datasetId, datasetName, }: {
|
|
343
|
+
datasetId?: string;
|
|
344
|
+
datasetName?: string;
|
|
345
|
+
}): Promise<string>;
|
|
341
346
|
private _getTenantId;
|
|
342
347
|
listProjects({ projectIds, name, nameContains, referenceDatasetId, referenceDatasetName, referenceFree, }?: {
|
|
343
348
|
projectIds?: string[];
|
|
@@ -385,11 +390,12 @@ export declare class Client {
|
|
|
385
390
|
datasetId?: string;
|
|
386
391
|
datasetName?: string;
|
|
387
392
|
}): Promise<void>;
|
|
388
|
-
createExample(inputs: KVMap, outputs: KVMap, { datasetId, datasetName, createdAt, exampleId, metadata, }: CreateExampleOptions): Promise<Example>;
|
|
393
|
+
createExample(inputs: KVMap, outputs: KVMap, { datasetId, datasetName, createdAt, exampleId, metadata, split, }: CreateExampleOptions): Promise<Example>;
|
|
389
394
|
createExamples(props: {
|
|
390
395
|
inputs: Array<KVMap>;
|
|
391
396
|
outputs?: Array<KVMap>;
|
|
392
397
|
metadata?: Array<KVMap>;
|
|
398
|
+
splits?: Array<string | Array<string>>;
|
|
393
399
|
sourceRunIds?: Array<string>;
|
|
394
400
|
exampleIds?: Array<string>;
|
|
395
401
|
datasetId?: string;
|
|
@@ -398,11 +404,12 @@ export declare class Client {
|
|
|
398
404
|
createLLMExample(input: string, generation: string | undefined, options: CreateExampleOptions): Promise<Example>;
|
|
399
405
|
createChatExample(input: KVMap[] | LangChainBaseMessage[], generations: KVMap | LangChainBaseMessage | undefined, options: CreateExampleOptions): Promise<Example>;
|
|
400
406
|
readExample(exampleId: string): Promise<Example>;
|
|
401
|
-
listExamples({ datasetId, datasetName, exampleIds, asOf, inlineS3Urls, metadata, }?: {
|
|
407
|
+
listExamples({ datasetId, datasetName, exampleIds, asOf, splits, inlineS3Urls, metadata, }?: {
|
|
402
408
|
datasetId?: string;
|
|
403
409
|
datasetName?: string;
|
|
404
410
|
exampleIds?: string[];
|
|
405
411
|
asOf?: string | Date;
|
|
412
|
+
splits?: string[];
|
|
406
413
|
inlineS3Urls?: boolean;
|
|
407
414
|
metadata?: KVMap;
|
|
408
415
|
}): AsyncIterable<Example>;
|
package/dist/client.js
CHANGED
|
@@ -1158,6 +1158,14 @@ export class Client {
|
|
|
1158
1158
|
const tenantId = await this._getTenantId();
|
|
1159
1159
|
return `${this.getHostUrl()}/o/${tenantId}/projects/p/${project.id}`;
|
|
1160
1160
|
}
|
|
1161
|
+
async getDatasetUrl({ datasetId, datasetName, }) {
|
|
1162
|
+
if (datasetId === undefined && datasetName === undefined) {
|
|
1163
|
+
throw new Error("Must provide either datasetName or datasetId");
|
|
1164
|
+
}
|
|
1165
|
+
const dataset = await this.readDataset({ datasetId, datasetName });
|
|
1166
|
+
const tenantId = await this._getTenantId();
|
|
1167
|
+
return `${this.getHostUrl()}/o/${tenantId}/datasets/${dataset.id}`;
|
|
1168
|
+
}
|
|
1161
1169
|
async _getTenantId() {
|
|
1162
1170
|
if (this._tenantId !== null) {
|
|
1163
1171
|
return this._tenantId;
|
|
@@ -1416,7 +1424,7 @@ export class Client {
|
|
|
1416
1424
|
}
|
|
1417
1425
|
await response.json();
|
|
1418
1426
|
}
|
|
1419
|
-
async createExample(inputs, outputs, { datasetId, datasetName, createdAt, exampleId, metadata, }) {
|
|
1427
|
+
async createExample(inputs, outputs, { datasetId, datasetName, createdAt, exampleId, metadata, split, }) {
|
|
1420
1428
|
let datasetId_ = datasetId;
|
|
1421
1429
|
if (datasetId_ === undefined && datasetName === undefined) {
|
|
1422
1430
|
throw new Error("Must provide either datasetName or datasetId");
|
|
@@ -1436,6 +1444,7 @@ export class Client {
|
|
|
1436
1444
|
created_at: createdAt_?.toISOString(),
|
|
1437
1445
|
id: exampleId,
|
|
1438
1446
|
metadata,
|
|
1447
|
+
split,
|
|
1439
1448
|
};
|
|
1440
1449
|
const response = await this.caller.call(fetch, `${this.apiUrl}/examples`, {
|
|
1441
1450
|
method: "POST",
|
|
@@ -1469,6 +1478,7 @@ export class Client {
|
|
|
1469
1478
|
inputs: input,
|
|
1470
1479
|
outputs: outputs ? outputs[idx] : undefined,
|
|
1471
1480
|
metadata: metadata ? metadata[idx] : undefined,
|
|
1481
|
+
split: props.splits ? props.splits[idx] : undefined,
|
|
1472
1482
|
id: exampleIds ? exampleIds[idx] : undefined,
|
|
1473
1483
|
source_run_id: sourceRunIds ? sourceRunIds[idx] : undefined,
|
|
1474
1484
|
};
|
|
@@ -1506,7 +1516,7 @@ export class Client {
|
|
|
1506
1516
|
const path = `/examples/${exampleId}`;
|
|
1507
1517
|
return await this._get(path);
|
|
1508
1518
|
}
|
|
1509
|
-
async *listExamples({ datasetId, datasetName, exampleIds, asOf, inlineS3Urls, metadata, } = {}) {
|
|
1519
|
+
async *listExamples({ datasetId, datasetName, exampleIds, asOf, splits, inlineS3Urls, metadata, } = {}) {
|
|
1510
1520
|
let datasetId_;
|
|
1511
1521
|
if (datasetId !== undefined && datasetName !== undefined) {
|
|
1512
1522
|
throw new Error("Must provide either datasetName or datasetId, not both");
|
|
@@ -1537,6 +1547,11 @@ export class Client {
|
|
|
1537
1547
|
params.append("id", id_);
|
|
1538
1548
|
}
|
|
1539
1549
|
}
|
|
1550
|
+
if (splits !== undefined) {
|
|
1551
|
+
for (const split of splits) {
|
|
1552
|
+
params.append("splits", split);
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1540
1555
|
if (metadata !== undefined) {
|
|
1541
1556
|
const serializedMetadata = JSON.stringify(metadata);
|
|
1542
1557
|
params.append("metadata", serializedMetadata);
|
|
@@ -8,6 +8,7 @@ const _uuid_js_1 = require("../utils/_uuid.cjs");
|
|
|
8
8
|
const async_caller_js_1 = require("../utils/async_caller.cjs");
|
|
9
9
|
const atee_js_1 = require("../utils/atee.cjs");
|
|
10
10
|
const env_js_1 = require("../utils/env.cjs");
|
|
11
|
+
const error_js_1 = require("../utils/error.cjs");
|
|
11
12
|
const _random_name_js_1 = require("./_random_name.cjs");
|
|
12
13
|
const evaluator_js_1 = require("./evaluator.cjs");
|
|
13
14
|
const uuid_1 = require("uuid");
|
|
@@ -238,15 +239,21 @@ class _ExperimentManager {
|
|
|
238
239
|
}
|
|
239
240
|
return project;
|
|
240
241
|
}
|
|
241
|
-
_printExperimentStart() {
|
|
242
|
-
// @TODO log with experiment URL
|
|
242
|
+
async _printExperimentStart() {
|
|
243
243
|
console.log(`Starting evaluation of experiment: ${this.experimentName}`);
|
|
244
|
+
const firstExample = this._examples?.[0];
|
|
245
|
+
const datasetId = firstExample?.dataset_id;
|
|
246
|
+
if (!datasetId || !this._experiment)
|
|
247
|
+
return;
|
|
248
|
+
const datasetUrl = await this.client.getDatasetUrl({ datasetId });
|
|
249
|
+
const compareUrl = `${datasetUrl}/compare?selectedSessions=${this._experiment.id}`;
|
|
250
|
+
console.log(`View results at ${compareUrl}`);
|
|
244
251
|
}
|
|
245
252
|
async start() {
|
|
246
253
|
const examples = await this.getExamples();
|
|
247
254
|
const firstExample = examples[0];
|
|
248
255
|
const project = await this._getProject(firstExample);
|
|
249
|
-
this._printExperimentStart();
|
|
256
|
+
await this._printExperimentStart();
|
|
250
257
|
return new _ExperimentManager({
|
|
251
258
|
examples,
|
|
252
259
|
experiment: project,
|
|
@@ -390,6 +397,7 @@ class _ExperimentManager {
|
|
|
390
397
|
}
|
|
391
398
|
catch (e) {
|
|
392
399
|
console.error(`Error running evaluator ${evaluator.evaluateRun.name} on run ${run.id}: ${e}`);
|
|
400
|
+
(0, error_js_1.printErrorStackTrace)(e);
|
|
393
401
|
}
|
|
394
402
|
}
|
|
395
403
|
return {
|
|
@@ -458,6 +466,7 @@ class _ExperimentManager {
|
|
|
458
466
|
}
|
|
459
467
|
catch (e) {
|
|
460
468
|
console.error(`Error running summary evaluator ${evaluator.name}: ${JSON.stringify(e, null, 2)}`);
|
|
469
|
+
(0, error_js_1.printErrorStackTrace)(e);
|
|
461
470
|
}
|
|
462
471
|
}
|
|
463
472
|
yield {
|
|
@@ -493,6 +502,21 @@ class _ExperimentManager {
|
|
|
493
502
|
return undefined;
|
|
494
503
|
return modifiedAtTime.reduce((max, current) => (current.time > max.time ? current : max), modifiedAtTime[0]).date;
|
|
495
504
|
}
|
|
505
|
+
async _getDatasetSplits() {
|
|
506
|
+
const examples = await this.getExamples();
|
|
507
|
+
const allSplits = examples.reduce((acc, ex) => {
|
|
508
|
+
if (ex.metadata && ex.metadata.dataset_split) {
|
|
509
|
+
if (Array.isArray(ex.metadata.dataset_split)) {
|
|
510
|
+
ex.metadata.dataset_split.forEach((split) => acc.add(split));
|
|
511
|
+
}
|
|
512
|
+
else if (typeof ex.metadata.dataset_split === "string") {
|
|
513
|
+
acc.add(ex.metadata.dataset_split);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return acc;
|
|
517
|
+
}, new Set());
|
|
518
|
+
return allSplits.size ? Array.from(allSplits) : undefined;
|
|
519
|
+
}
|
|
496
520
|
async _end() {
|
|
497
521
|
const experiment = this._experiment;
|
|
498
522
|
if (!experiment) {
|
|
@@ -500,6 +524,7 @@ class _ExperimentManager {
|
|
|
500
524
|
}
|
|
501
525
|
const projectMetadata = await this._getExperimentMetadata();
|
|
502
526
|
projectMetadata["dataset_version"] = await this._getDatasetVersion();
|
|
527
|
+
projectMetadata["dataset_splits"] = await this._getDatasetSplits();
|
|
503
528
|
// Update revision_id if not already set
|
|
504
529
|
if (!projectMetadata["revision_id"]) {
|
|
505
530
|
projectMetadata["revision_id"] = await (0, _git_js_1.getDefaultRevisionId)();
|
|
@@ -631,6 +656,7 @@ async function _forward(fn, example, experimentName, metadata, client) {
|
|
|
631
656
|
}
|
|
632
657
|
catch (e) {
|
|
633
658
|
console.error(`Error running target function: ${e}`);
|
|
659
|
+
(0, error_js_1.printErrorStackTrace)(e);
|
|
634
660
|
}
|
|
635
661
|
if (!run) {
|
|
636
662
|
throw new Error(`Run not created by target function.
|
|
@@ -104,7 +104,7 @@ declare class _ExperimentManager {
|
|
|
104
104
|
_getExperiment(): TracerSession;
|
|
105
105
|
_getExperimentMetadata(): Promise<KVMap>;
|
|
106
106
|
_getProject(firstExample: Example): Promise<TracerSession>;
|
|
107
|
-
_printExperimentStart(): void
|
|
107
|
+
protected _printExperimentStart(): Promise<void>;
|
|
108
108
|
start(): Promise<_ExperimentManager>;
|
|
109
109
|
withPredictions(target: TargetNoInvoke, options?: {
|
|
110
110
|
maxConcurrency?: number;
|
|
@@ -139,6 +139,7 @@ declare class _ExperimentManager {
|
|
|
139
139
|
}): AsyncGenerator<ExperimentResultRow>;
|
|
140
140
|
_applySummaryEvaluators(summaryEvaluators: Array<SummaryEvaluatorT>): AsyncGenerator<(runsArray: Run[]) => AsyncGenerator<EvaluationResults>>;
|
|
141
141
|
_getDatasetVersion(): Promise<string | undefined>;
|
|
142
|
+
_getDatasetSplits(): Promise<string[] | undefined>;
|
|
142
143
|
_end(): Promise<void>;
|
|
143
144
|
}
|
|
144
145
|
/**
|
|
@@ -5,6 +5,7 @@ import { assertUuid } from "../utils/_uuid.js";
|
|
|
5
5
|
import { AsyncCaller } from "../utils/async_caller.js";
|
|
6
6
|
import { atee } from "../utils/atee.js";
|
|
7
7
|
import { getLangChainEnvVarsMetadata } from "../utils/env.js";
|
|
8
|
+
import { printErrorStackTrace } from "../utils/error.js";
|
|
8
9
|
import { randomName } from "./_random_name.js";
|
|
9
10
|
import { runEvaluator, } from "./evaluator.js";
|
|
10
11
|
import { v4 as uuidv4 } from "uuid";
|
|
@@ -234,15 +235,21 @@ class _ExperimentManager {
|
|
|
234
235
|
}
|
|
235
236
|
return project;
|
|
236
237
|
}
|
|
237
|
-
_printExperimentStart() {
|
|
238
|
-
// @TODO log with experiment URL
|
|
238
|
+
async _printExperimentStart() {
|
|
239
239
|
console.log(`Starting evaluation of experiment: ${this.experimentName}`);
|
|
240
|
+
const firstExample = this._examples?.[0];
|
|
241
|
+
const datasetId = firstExample?.dataset_id;
|
|
242
|
+
if (!datasetId || !this._experiment)
|
|
243
|
+
return;
|
|
244
|
+
const datasetUrl = await this.client.getDatasetUrl({ datasetId });
|
|
245
|
+
const compareUrl = `${datasetUrl}/compare?selectedSessions=${this._experiment.id}`;
|
|
246
|
+
console.log(`View results at ${compareUrl}`);
|
|
240
247
|
}
|
|
241
248
|
async start() {
|
|
242
249
|
const examples = await this.getExamples();
|
|
243
250
|
const firstExample = examples[0];
|
|
244
251
|
const project = await this._getProject(firstExample);
|
|
245
|
-
this._printExperimentStart();
|
|
252
|
+
await this._printExperimentStart();
|
|
246
253
|
return new _ExperimentManager({
|
|
247
254
|
examples,
|
|
248
255
|
experiment: project,
|
|
@@ -386,6 +393,7 @@ class _ExperimentManager {
|
|
|
386
393
|
}
|
|
387
394
|
catch (e) {
|
|
388
395
|
console.error(`Error running evaluator ${evaluator.evaluateRun.name} on run ${run.id}: ${e}`);
|
|
396
|
+
printErrorStackTrace(e);
|
|
389
397
|
}
|
|
390
398
|
}
|
|
391
399
|
return {
|
|
@@ -454,6 +462,7 @@ class _ExperimentManager {
|
|
|
454
462
|
}
|
|
455
463
|
catch (e) {
|
|
456
464
|
console.error(`Error running summary evaluator ${evaluator.name}: ${JSON.stringify(e, null, 2)}`);
|
|
465
|
+
printErrorStackTrace(e);
|
|
457
466
|
}
|
|
458
467
|
}
|
|
459
468
|
yield {
|
|
@@ -489,6 +498,21 @@ class _ExperimentManager {
|
|
|
489
498
|
return undefined;
|
|
490
499
|
return modifiedAtTime.reduce((max, current) => (current.time > max.time ? current : max), modifiedAtTime[0]).date;
|
|
491
500
|
}
|
|
501
|
+
async _getDatasetSplits() {
|
|
502
|
+
const examples = await this.getExamples();
|
|
503
|
+
const allSplits = examples.reduce((acc, ex) => {
|
|
504
|
+
if (ex.metadata && ex.metadata.dataset_split) {
|
|
505
|
+
if (Array.isArray(ex.metadata.dataset_split)) {
|
|
506
|
+
ex.metadata.dataset_split.forEach((split) => acc.add(split));
|
|
507
|
+
}
|
|
508
|
+
else if (typeof ex.metadata.dataset_split === "string") {
|
|
509
|
+
acc.add(ex.metadata.dataset_split);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return acc;
|
|
513
|
+
}, new Set());
|
|
514
|
+
return allSplits.size ? Array.from(allSplits) : undefined;
|
|
515
|
+
}
|
|
492
516
|
async _end() {
|
|
493
517
|
const experiment = this._experiment;
|
|
494
518
|
if (!experiment) {
|
|
@@ -496,6 +520,7 @@ class _ExperimentManager {
|
|
|
496
520
|
}
|
|
497
521
|
const projectMetadata = await this._getExperimentMetadata();
|
|
498
522
|
projectMetadata["dataset_version"] = await this._getDatasetVersion();
|
|
523
|
+
projectMetadata["dataset_splits"] = await this._getDatasetSplits();
|
|
499
524
|
// Update revision_id if not already set
|
|
500
525
|
if (!projectMetadata["revision_id"]) {
|
|
501
526
|
projectMetadata["revision_id"] = await getDefaultRevisionId();
|
|
@@ -627,6 +652,7 @@ async function _forward(fn, example, experimentName, metadata, client) {
|
|
|
627
652
|
}
|
|
628
653
|
catch (e) {
|
|
629
654
|
console.error(`Error running target function: ${e}`);
|
|
655
|
+
printErrorStackTrace(e);
|
|
630
656
|
}
|
|
631
657
|
if (!run) {
|
|
632
658
|
throw new Error(`Run not created by target function.
|
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.26";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Client } from "./client.js";
|
|
2
|
-
export type { Dataset, Example, TracerSession, Run, Feedback, } from "./schemas.js";
|
|
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.26";
|
package/dist/index.js
CHANGED
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 {};
|
package/dist/traceable.cjs
CHANGED
|
@@ -63,12 +63,20 @@ const handleRunOutputs = (rawOutputs) => {
|
|
|
63
63
|
}
|
|
64
64
|
return { outputs: rawOutputs };
|
|
65
65
|
};
|
|
66
|
-
const getTracingRunTree = (runTree, inputs) => {
|
|
66
|
+
const getTracingRunTree = (runTree, inputs, getInvocationParams) => {
|
|
67
67
|
const tracingEnabled_ = tracingIsEnabled(runTree.tracingEnabled);
|
|
68
68
|
if (!tracingEnabled_) {
|
|
69
69
|
return undefined;
|
|
70
70
|
}
|
|
71
71
|
runTree.inputs = handleRunInputs(inputs);
|
|
72
|
+
const invocationParams = getInvocationParams?.(...inputs);
|
|
73
|
+
if (invocationParams != null) {
|
|
74
|
+
runTree.extra ??= {};
|
|
75
|
+
runTree.extra.metadata = {
|
|
76
|
+
...runTree.extra.metadata,
|
|
77
|
+
...invocationParams,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
72
80
|
return runTree;
|
|
73
81
|
};
|
|
74
82
|
// idea: store the state of the promise outside
|
|
@@ -279,7 +287,7 @@ function traceable(wrappedFunc, config) {
|
|
|
279
287
|
// used for handoff between LangChain.JS and traceable functions
|
|
280
288
|
if ((0, run_trees_js_1.isRunnableConfigLike)(firstArg)) {
|
|
281
289
|
return [
|
|
282
|
-
getTracingRunTree(run_trees_js_1.RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs),
|
|
290
|
+
getTracingRunTree(run_trees_js_1.RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs, config?.getInvocationParams),
|
|
283
291
|
restArgs,
|
|
284
292
|
];
|
|
285
293
|
}
|
|
@@ -295,7 +303,7 @@ function traceable(wrappedFunc, config) {
|
|
|
295
303
|
if (firstArg === exports.ROOT || (0, run_trees_js_1.isRunTree)(firstArg)) {
|
|
296
304
|
const currentRunTree = getTracingRunTree(firstArg === exports.ROOT
|
|
297
305
|
? new run_trees_js_1.RunTree(ensuredConfig)
|
|
298
|
-
: firstArg.createChild(ensuredConfig), restArgs);
|
|
306
|
+
: firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams);
|
|
299
307
|
return [currentRunTree, [currentRunTree, ...restArgs]];
|
|
300
308
|
}
|
|
301
309
|
// Node.JS uses AsyncLocalStorage (ALS) and AsyncResource
|
|
@@ -303,11 +311,11 @@ function traceable(wrappedFunc, config) {
|
|
|
303
311
|
const prevRunFromStore = asyncLocalStorage.getStore();
|
|
304
312
|
if (prevRunFromStore) {
|
|
305
313
|
return [
|
|
306
|
-
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs),
|
|
314
|
+
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs, config?.getInvocationParams),
|
|
307
315
|
processedArgs,
|
|
308
316
|
];
|
|
309
317
|
}
|
|
310
|
-
const currentRunTree = getTracingRunTree(new run_trees_js_1.RunTree(ensuredConfig), processedArgs);
|
|
318
|
+
const currentRunTree = getTracingRunTree(new run_trees_js_1.RunTree(ensuredConfig), processedArgs, config?.getInvocationParams);
|
|
311
319
|
return [currentRunTree, processedArgs];
|
|
312
320
|
})();
|
|
313
321
|
return asyncLocalStorage.run(currentRunTree, () => {
|
package/dist/traceable.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RunTree, RunTreeConfig, RunnableConfigLike } from "./run_trees.js";
|
|
2
|
+
import { InvocationParamsSchema } from "./schemas.js";
|
|
2
3
|
export declare const ROOT: unique symbol;
|
|
3
4
|
export type RunTreeLike = RunTree;
|
|
4
5
|
type SmartPromise<T> = T extends AsyncGenerator ? T : T extends Promise<unknown> ? T : Promise<T>;
|
|
@@ -52,6 +53,15 @@ export type TraceableFunction<Func extends (...args: any[]) => any> = Func exten
|
|
|
52
53
|
export declare function traceable<Func extends (...args: any[]) => any>(wrappedFunc: Func, config?: Partial<RunTreeConfig> & {
|
|
53
54
|
aggregator?: (args: any[]) => any;
|
|
54
55
|
argsConfigPath?: [number] | [number, string];
|
|
56
|
+
/**
|
|
57
|
+
* Extract invocation parameters from the arguments of the traced function.
|
|
58
|
+
* This is useful for LangSmith to properly track common metadata like
|
|
59
|
+
* provider, model name and temperature.
|
|
60
|
+
*
|
|
61
|
+
* @param args Arguments of the traced function
|
|
62
|
+
* @returns Key-value map of the invocation parameters, which will be merged with the existing metadata
|
|
63
|
+
*/
|
|
64
|
+
getInvocationParams?: (...args: Parameters<Func>) => InvocationParamsSchema | undefined;
|
|
55
65
|
}): TraceableFunction<Func>;
|
|
56
66
|
/**
|
|
57
67
|
* Return the current run tree from within a traceable-wrapped function.
|
package/dist/traceable.js
CHANGED
|
@@ -60,12 +60,20 @@ const handleRunOutputs = (rawOutputs) => {
|
|
|
60
60
|
}
|
|
61
61
|
return { outputs: rawOutputs };
|
|
62
62
|
};
|
|
63
|
-
const getTracingRunTree = (runTree, inputs) => {
|
|
63
|
+
const getTracingRunTree = (runTree, inputs, getInvocationParams) => {
|
|
64
64
|
const tracingEnabled_ = tracingIsEnabled(runTree.tracingEnabled);
|
|
65
65
|
if (!tracingEnabled_) {
|
|
66
66
|
return undefined;
|
|
67
67
|
}
|
|
68
68
|
runTree.inputs = handleRunInputs(inputs);
|
|
69
|
+
const invocationParams = getInvocationParams?.(...inputs);
|
|
70
|
+
if (invocationParams != null) {
|
|
71
|
+
runTree.extra ??= {};
|
|
72
|
+
runTree.extra.metadata = {
|
|
73
|
+
...runTree.extra.metadata,
|
|
74
|
+
...invocationParams,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
69
77
|
return runTree;
|
|
70
78
|
};
|
|
71
79
|
// idea: store the state of the promise outside
|
|
@@ -276,7 +284,7 @@ export function traceable(wrappedFunc, config) {
|
|
|
276
284
|
// used for handoff between LangChain.JS and traceable functions
|
|
277
285
|
if (isRunnableConfigLike(firstArg)) {
|
|
278
286
|
return [
|
|
279
|
-
getTracingRunTree(RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs),
|
|
287
|
+
getTracingRunTree(RunTree.fromRunnableConfig(firstArg, ensuredConfig), restArgs, config?.getInvocationParams),
|
|
280
288
|
restArgs,
|
|
281
289
|
];
|
|
282
290
|
}
|
|
@@ -292,7 +300,7 @@ export function traceable(wrappedFunc, config) {
|
|
|
292
300
|
if (firstArg === ROOT || isRunTree(firstArg)) {
|
|
293
301
|
const currentRunTree = getTracingRunTree(firstArg === ROOT
|
|
294
302
|
? new RunTree(ensuredConfig)
|
|
295
|
-
: firstArg.createChild(ensuredConfig), restArgs);
|
|
303
|
+
: firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams);
|
|
296
304
|
return [currentRunTree, [currentRunTree, ...restArgs]];
|
|
297
305
|
}
|
|
298
306
|
// Node.JS uses AsyncLocalStorage (ALS) and AsyncResource
|
|
@@ -300,11 +308,11 @@ export function traceable(wrappedFunc, config) {
|
|
|
300
308
|
const prevRunFromStore = asyncLocalStorage.getStore();
|
|
301
309
|
if (prevRunFromStore) {
|
|
302
310
|
return [
|
|
303
|
-
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs),
|
|
311
|
+
getTracingRunTree(prevRunFromStore.createChild(ensuredConfig), processedArgs, config?.getInvocationParams),
|
|
304
312
|
processedArgs,
|
|
305
313
|
];
|
|
306
314
|
}
|
|
307
|
-
const currentRunTree = getTracingRunTree(new RunTree(ensuredConfig), processedArgs);
|
|
315
|
+
const currentRunTree = getTracingRunTree(new RunTree(ensuredConfig), processedArgs, config?.getInvocationParams);
|
|
308
316
|
return [currentRunTree, processedArgs];
|
|
309
317
|
})();
|
|
310
318
|
return asyncLocalStorage.run(currentRunTree, () => {
|
|
@@ -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
|
+
}
|
package/dist/wrappers/openai.cjs
CHANGED
|
@@ -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;
|
package/dist/wrappers/openai.js
CHANGED
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langsmith",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
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": [
|
|
@@ -197,4 +197,4 @@
|
|
|
197
197
|
},
|
|
198
198
|
"./package.json": "./package.json"
|
|
199
199
|
}
|
|
200
|
-
}
|
|
200
|
+
}
|