langsmith 0.1.21 → 0.1.22

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/README.md CHANGED
@@ -51,7 +51,7 @@ yarn add langchain
51
51
  Tracing can be activated by setting the following environment variables or by manually specifying the LangChainTracer.
52
52
 
53
53
  ```typescript
54
- process.env["LANGCHAIN_TRACING_V2"] = "true";
54
+ process.env["LANGSMITH_TRACING"] = "true";
55
55
  process.env["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com";
56
56
  process.env["LANGCHAIN_API_KEY"] = "<YOUR-LANGSMITH-API-KEY>";
57
57
  // process.env["LANGCHAIN_PROJECT"] = "My Project Name"; // Optional: "default" is used if not set
package/dist/client.cjs CHANGED
@@ -29,6 +29,7 @@ const async_caller_js_1 = require("./utils/async_caller.cjs");
29
29
  const messages_js_1 = require("./utils/messages.cjs");
30
30
  const env_js_1 = require("./utils/env.cjs");
31
31
  const index_js_1 = require("./index.cjs");
32
+ const _uuid_js_1 = require("./utils/_uuid.cjs");
32
33
  async function mergeRuntimeEnvIntoRunCreates(runs) {
33
34
  const runtimeEnv = await (0, env_js_1.getRuntimeEnvironment)();
34
35
  const envVars = (0, env_js_1.getLangChainEnvVarsMetadata)();
@@ -93,11 +94,6 @@ function trimQuotes(str) {
93
94
  .replace(/^"(.*)"$/, "$1")
94
95
  .replace(/^'(.*)'$/, "$1");
95
96
  }
96
- function assertUuid(str) {
97
- if (!uuid.validate(str)) {
98
- throw new Error(`Invalid UUID: ${str}`);
99
- }
100
- }
101
97
  const handle429 = async (response) => {
102
98
  if (response?.status === 429) {
103
99
  const retryAfter = parseInt(response.headers.get("retry-after") ?? "30", 10) * 1000;
@@ -667,7 +663,7 @@ class Client {
667
663
  await raiseForStatus(response, "batch create run");
668
664
  }
669
665
  async updateRun(runId, run) {
670
- assertUuid(runId);
666
+ (0, _uuid_js_1.assertUuid)(runId);
671
667
  if (run.inputs) {
672
668
  run.inputs = this.processInputs(run.inputs);
673
669
  }
@@ -704,7 +700,7 @@ class Client {
704
700
  await raiseForStatus(response, "update run");
705
701
  }
706
702
  async readRun(runId, { loadChildRuns } = { loadChildRuns: false }) {
707
- assertUuid(runId);
703
+ (0, _uuid_js_1.assertUuid)(runId);
708
704
  let run = await this._get(`/runs/${runId}`);
709
705
  if (loadChildRuns && run.child_run_ids) {
710
706
  run = await this._loadChildRuns(run);
@@ -920,7 +916,7 @@ class Client {
920
916
  run_id: runId,
921
917
  share_token: shareId || uuid.v4(),
922
918
  };
923
- assertUuid(runId);
919
+ (0, _uuid_js_1.assertUuid)(runId);
924
920
  const response = await this.caller.call(fetch, `${this.apiUrl}/runs/${runId}/share`, {
925
921
  method: "PUT",
926
922
  headers: this.headers,
@@ -935,7 +931,7 @@ class Client {
935
931
  return `${this.getHostUrl()}/public/${result["share_token"]}/r`;
936
932
  }
937
933
  async unshareRun(runId) {
938
- assertUuid(runId);
934
+ (0, _uuid_js_1.assertUuid)(runId);
939
935
  const response = await this.caller.call(fetch, `${this.apiUrl}/runs/${runId}/share`, {
940
936
  method: "DELETE",
941
937
  headers: this.headers,
@@ -945,7 +941,7 @@ class Client {
945
941
  await raiseForStatus(response, "unshare run");
946
942
  }
947
943
  async readRunSharedLink(runId) {
948
- assertUuid(runId);
944
+ (0, _uuid_js_1.assertUuid)(runId);
949
945
  const response = await this.caller.call(fetch, `${this.apiUrl}/runs/${runId}/share`, {
950
946
  method: "GET",
951
947
  headers: this.headers,
@@ -967,7 +963,7 @@ class Client {
967
963
  queryParams.append("id", runId);
968
964
  }
969
965
  }
970
- assertUuid(shareToken);
966
+ (0, _uuid_js_1.assertUuid)(shareToken);
971
967
  const response = await this.caller.call(fetch, `${this.apiUrl}/public/${shareToken}/runs${queryParams}`, {
972
968
  method: "GET",
973
969
  headers: this.headers,
@@ -985,7 +981,7 @@ class Client {
985
981
  const dataset = await this.readDataset({ datasetName });
986
982
  datasetId = dataset.id;
987
983
  }
988
- assertUuid(datasetId);
984
+ (0, _uuid_js_1.assertUuid)(datasetId);
989
985
  const response = await this.caller.call(fetch, `${this.apiUrl}/datasets/${datasetId}/share`, {
990
986
  method: "GET",
991
987
  headers: this.headers,
@@ -1007,7 +1003,7 @@ class Client {
1007
1003
  const data = {
1008
1004
  dataset_id: datasetId,
1009
1005
  };
1010
- assertUuid(datasetId);
1006
+ (0, _uuid_js_1.assertUuid)(datasetId);
1011
1007
  const response = await this.caller.call(fetch, `${this.apiUrl}/datasets/${datasetId}/share`, {
1012
1008
  method: "PUT",
1013
1009
  headers: this.headers,
@@ -1020,7 +1016,7 @@ class Client {
1020
1016
  return shareSchema;
1021
1017
  }
1022
1018
  async unshareDataset(datasetId) {
1023
- assertUuid(datasetId);
1019
+ (0, _uuid_js_1.assertUuid)(datasetId);
1024
1020
  const response = await this.caller.call(fetch, `${this.apiUrl}/datasets/${datasetId}/share`, {
1025
1021
  method: "DELETE",
1026
1022
  headers: this.headers,
@@ -1030,7 +1026,7 @@ class Client {
1030
1026
  await raiseForStatus(response, "unshare dataset");
1031
1027
  }
1032
1028
  async readSharedDataset(shareToken) {
1033
- assertUuid(shareToken);
1029
+ (0, _uuid_js_1.assertUuid)(shareToken);
1034
1030
  const response = await this.caller.call(fetch, `${this.apiUrl}/public/${shareToken}/datasets`, {
1035
1031
  method: "GET",
1036
1032
  headers: this.headers,
@@ -1101,7 +1097,7 @@ class Client {
1101
1097
  throw new Error("Must provide either projectName or projectId, not both");
1102
1098
  }
1103
1099
  else if (projectId !== undefined) {
1104
- assertUuid(projectId);
1100
+ (0, _uuid_js_1.assertUuid)(projectId);
1105
1101
  path += `/${projectId}`;
1106
1102
  }
1107
1103
  else if (projectName !== undefined) {
@@ -1141,7 +1137,7 @@ class Client {
1141
1137
  throw new Error("Must provide either projectName or projectId, not both");
1142
1138
  }
1143
1139
  else if (projectId !== undefined) {
1144
- assertUuid(projectId);
1140
+ (0, _uuid_js_1.assertUuid)(projectId);
1145
1141
  path += `/${projectId}`;
1146
1142
  }
1147
1143
  else if (projectName !== undefined) {
@@ -1220,7 +1216,7 @@ class Client {
1220
1216
  else {
1221
1217
  projectId_ = projectId;
1222
1218
  }
1223
- assertUuid(projectId_);
1219
+ (0, _uuid_js_1.assertUuid)(projectId_);
1224
1220
  const response = await this.caller.call(fetch, `${this.apiUrl}/sessions/${projectId_}`, {
1225
1221
  method: "DELETE",
1226
1222
  headers: this.headers,
@@ -1298,7 +1294,7 @@ class Client {
1298
1294
  throw new Error("Must provide either datasetName or datasetId, not both");
1299
1295
  }
1300
1296
  else if (datasetId !== undefined) {
1301
- assertUuid(datasetId);
1297
+ (0, _uuid_js_1.assertUuid)(datasetId);
1302
1298
  path += `/${datasetId}`;
1303
1299
  }
1304
1300
  else if (datasetName !== undefined) {
@@ -1392,7 +1388,7 @@ class Client {
1392
1388
  datasetId_ = dataset.id;
1393
1389
  }
1394
1390
  if (datasetId_ !== undefined) {
1395
- assertUuid(datasetId_);
1391
+ (0, _uuid_js_1.assertUuid)(datasetId_);
1396
1392
  path += `/${datasetId_}`;
1397
1393
  }
1398
1394
  else {
@@ -1495,7 +1491,7 @@ class Client {
1495
1491
  return this.createExample({ input: finalInput }, { output: finalOutput }, options);
1496
1492
  }
1497
1493
  async readExample(exampleId) {
1498
- assertUuid(exampleId);
1494
+ (0, _uuid_js_1.assertUuid)(exampleId);
1499
1495
  const path = `/examples/${exampleId}`;
1500
1496
  return await this._get(path);
1501
1497
  }
@@ -1535,7 +1531,7 @@ class Client {
1535
1531
  }
1536
1532
  }
1537
1533
  async deleteExample(exampleId) {
1538
- assertUuid(exampleId);
1534
+ (0, _uuid_js_1.assertUuid)(exampleId);
1539
1535
  const path = `/examples/${exampleId}`;
1540
1536
  const response = await this.caller.call(fetch, this.apiUrl + path, {
1541
1537
  method: "DELETE",
@@ -1549,7 +1545,7 @@ class Client {
1549
1545
  await response.json();
1550
1546
  }
1551
1547
  async updateExample(exampleId, update) {
1552
- assertUuid(exampleId);
1548
+ (0, _uuid_js_1.assertUuid)(exampleId);
1553
1549
  const response = await this.caller.call(fetch, `${this.apiUrl}/examples/${exampleId}`, {
1554
1550
  method: "PATCH",
1555
1551
  headers: { ...this.headers, "Content-Type": "application/json" },
@@ -1594,7 +1590,13 @@ class Client {
1594
1590
  sourceRunId: feedbackResult?.sourceRunId,
1595
1591
  });
1596
1592
  }
1597
- async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, feedbackConfig, }) {
1593
+ async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, feedbackConfig, projectId, }) {
1594
+ if (!runId && !projectId) {
1595
+ throw new Error("One of runId or projectId must be provided");
1596
+ }
1597
+ if (runId && projectId) {
1598
+ throw new Error("Only one of runId or projectId can be provided");
1599
+ }
1598
1600
  const feedback_source = {
1599
1601
  type: feedbackSourceType ?? "api",
1600
1602
  metadata: sourceInfo ?? {},
@@ -1606,7 +1608,7 @@ class Client {
1606
1608
  }
1607
1609
  if (feedback_source?.metadata !== undefined &&
1608
1610
  feedback_source.metadata["__run"]?.run_id !== undefined) {
1609
- assertUuid(feedback_source.metadata["__run"].run_id);
1611
+ (0, _uuid_js_1.assertUuid)(feedback_source.metadata["__run"].run_id);
1610
1612
  }
1611
1613
  const feedback = {
1612
1614
  id: feedbackId ?? uuid.v4(),
@@ -1618,6 +1620,7 @@ class Client {
1618
1620
  comment,
1619
1621
  feedback_source: feedback_source,
1620
1622
  feedbackConfig,
1623
+ session_id: projectId,
1621
1624
  };
1622
1625
  const url = `${this.apiUrl}/feedback`;
1623
1626
  const response = await this.caller.call(fetch, url, {
@@ -1644,7 +1647,7 @@ class Client {
1644
1647
  if (comment !== undefined && comment !== null) {
1645
1648
  feedbackUpdate["comment"] = comment;
1646
1649
  }
1647
- assertUuid(feedbackId);
1650
+ (0, _uuid_js_1.assertUuid)(feedbackId);
1648
1651
  const response = await this.caller.call(fetch, `${this.apiUrl}/feedback/${feedbackId}`, {
1649
1652
  method: "PATCH",
1650
1653
  headers: { ...this.headers, "Content-Type": "application/json" },
@@ -1655,13 +1658,13 @@ class Client {
1655
1658
  await raiseForStatus(response, "update feedback");
1656
1659
  }
1657
1660
  async readFeedback(feedbackId) {
1658
- assertUuid(feedbackId);
1661
+ (0, _uuid_js_1.assertUuid)(feedbackId);
1659
1662
  const path = `/feedback/${feedbackId}`;
1660
1663
  const response = await this._get(path);
1661
1664
  return response;
1662
1665
  }
1663
1666
  async deleteFeedback(feedbackId) {
1664
- assertUuid(feedbackId);
1667
+ (0, _uuid_js_1.assertUuid)(feedbackId);
1665
1668
  const path = `/feedback/${feedbackId}`;
1666
1669
  const response = await this.caller.call(fetch, this.apiUrl + path, {
1667
1670
  method: "DELETE",
@@ -1743,11 +1746,48 @@ class Client {
1743
1746
  * @returns An async iterable of FeedbackIngestToken objects.
1744
1747
  */
1745
1748
  async *listPresignedFeedbackTokens(runId) {
1746
- assertUuid(runId);
1749
+ (0, _uuid_js_1.assertUuid)(runId);
1747
1750
  const params = new URLSearchParams({ run_id: runId });
1748
1751
  for await (const tokens of this._getPaginated("/feedback/tokens", params)) {
1749
1752
  yield* tokens;
1750
1753
  }
1751
1754
  }
1755
+ _selectEvalResults(results) {
1756
+ let results_;
1757
+ if ("results" in results) {
1758
+ results_ = results.results;
1759
+ }
1760
+ else {
1761
+ results_ = [results];
1762
+ }
1763
+ return results_;
1764
+ }
1765
+ async logEvaluationFeedback(evaluatorResponse, run, sourceInfo) {
1766
+ const results = this._selectEvalResults(evaluatorResponse);
1767
+ for (const res of results) {
1768
+ let sourceInfo_ = sourceInfo || {};
1769
+ if (res.evaluatorInfo) {
1770
+ sourceInfo_ = { ...res.evaluatorInfo, ...sourceInfo_ };
1771
+ }
1772
+ let runId_ = null;
1773
+ if (res.targetRunId) {
1774
+ runId_ = res.targetRunId;
1775
+ }
1776
+ else if (run) {
1777
+ runId_ = run.id;
1778
+ }
1779
+ await this.createFeedback(runId_, res.key, {
1780
+ score: res.score,
1781
+ value: res.value,
1782
+ comment: res.comment,
1783
+ correction: res.correction,
1784
+ sourceInfo: sourceInfo_,
1785
+ sourceRunId: res.sourceRunId,
1786
+ feedbackConfig: res.feedbackConfig,
1787
+ feedbackSourceType: "model",
1788
+ });
1789
+ }
1790
+ return results;
1791
+ }
1752
1792
  }
1753
1793
  exports.Client = Client;
package/dist/client.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AsyncCallerParams } from "./utils/async_caller.js";
2
2
  import { DataType, Dataset, DatasetDiffInfo, DatasetShareSchema, Example, ExampleUpdate, Feedback, FeedbackConfig, FeedbackIngestToken, KVMap, LangChainBaseMessage, Run, RunCreate, RunUpdate, ScoreType, TimeDelta, TracerSession, TracerSessionResult, ValueType } from "./schemas.js";
3
- import { RunEvaluator } from "./evaluation/evaluator.js";
3
+ import { EvaluationResult, EvaluationResults, RunEvaluator } from "./evaluation/evaluator.js";
4
4
  interface ClientConfig {
5
5
  apiUrl?: string;
6
6
  apiKey?: string;
@@ -102,7 +102,6 @@ interface ListRunsParams {
102
102
  treeFilter?: string;
103
103
  /**
104
104
  * The values to include in the response.
105
- *
106
105
  */
107
106
  select?: string[];
108
107
  }
@@ -405,7 +404,7 @@ export declare class Client {
405
404
  loadChildRuns: boolean;
406
405
  referenceExample?: Example;
407
406
  }): Promise<Feedback>;
408
- createFeedback(runId: string, key: string, { score, value, correction, comment, sourceInfo, feedbackSourceType, sourceRunId, feedbackId, feedbackConfig, }: {
407
+ createFeedback(runId: string | null, key: string, { score, value, correction, comment, sourceInfo, feedbackSourceType, sourceRunId, feedbackId, feedbackConfig, projectId, }: {
409
408
  score?: ScoreType;
410
409
  value?: ValueType;
411
410
  correction?: object;
@@ -416,6 +415,7 @@ export declare class Client {
416
415
  sourceRunId?: string;
417
416
  feedbackId?: string;
418
417
  eager?: boolean;
418
+ projectId?: string;
419
419
  }): Promise<Feedback>;
420
420
  updateFeedback(feedbackId: string, { score, value, correction, comment, }: {
421
421
  score?: number | boolean | null;
@@ -455,5 +455,9 @@ export declare class Client {
455
455
  * @returns An async iterable of FeedbackIngestToken objects.
456
456
  */
457
457
  listPresignedFeedbackTokens(runId: string): AsyncIterable<FeedbackIngestToken>;
458
+ _selectEvalResults(results: EvaluationResult | EvaluationResults): Array<EvaluationResult>;
459
+ logEvaluationFeedback(evaluatorResponse: EvaluationResult | EvaluationResults, run?: Run, sourceInfo?: {
460
+ [key: string]: any;
461
+ }): Promise<EvaluationResult[]>;
458
462
  }
459
463
  export {};
package/dist/client.js CHANGED
@@ -3,6 +3,7 @@ import { AsyncCaller } from "./utils/async_caller.js";
3
3
  import { convertLangChainMessageToExample, isLangChainMessage, } from "./utils/messages.js";
4
4
  import { getEnvironmentVariable, getLangChainEnvVarsMetadata, getRuntimeEnvironment, } from "./utils/env.js";
5
5
  import { __version__ } from "./index.js";
6
+ import { assertUuid } from "./utils/_uuid.js";
6
7
  async function mergeRuntimeEnvIntoRunCreates(runs) {
7
8
  const runtimeEnv = await getRuntimeEnvironment();
8
9
  const envVars = getLangChainEnvVarsMetadata();
@@ -67,11 +68,6 @@ function trimQuotes(str) {
67
68
  .replace(/^"(.*)"$/, "$1")
68
69
  .replace(/^'(.*)'$/, "$1");
69
70
  }
70
- function assertUuid(str) {
71
- if (!uuid.validate(str)) {
72
- throw new Error(`Invalid UUID: ${str}`);
73
- }
74
- }
75
71
  const handle429 = async (response) => {
76
72
  if (response?.status === 429) {
77
73
  const retryAfter = parseInt(response.headers.get("retry-after") ?? "30", 10) * 1000;
@@ -1567,7 +1563,13 @@ export class Client {
1567
1563
  sourceRunId: feedbackResult?.sourceRunId,
1568
1564
  });
1569
1565
  }
1570
- async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, feedbackConfig, }) {
1566
+ async createFeedback(runId, key, { score, value, correction, comment, sourceInfo, feedbackSourceType = "api", sourceRunId, feedbackId, feedbackConfig, projectId, }) {
1567
+ if (!runId && !projectId) {
1568
+ throw new Error("One of runId or projectId must be provided");
1569
+ }
1570
+ if (runId && projectId) {
1571
+ throw new Error("Only one of runId or projectId can be provided");
1572
+ }
1571
1573
  const feedback_source = {
1572
1574
  type: feedbackSourceType ?? "api",
1573
1575
  metadata: sourceInfo ?? {},
@@ -1591,6 +1593,7 @@ export class Client {
1591
1593
  comment,
1592
1594
  feedback_source: feedback_source,
1593
1595
  feedbackConfig,
1596
+ session_id: projectId,
1594
1597
  };
1595
1598
  const url = `${this.apiUrl}/feedback`;
1596
1599
  const response = await this.caller.call(fetch, url, {
@@ -1722,4 +1725,41 @@ export class Client {
1722
1725
  yield* tokens;
1723
1726
  }
1724
1727
  }
1728
+ _selectEvalResults(results) {
1729
+ let results_;
1730
+ if ("results" in results) {
1731
+ results_ = results.results;
1732
+ }
1733
+ else {
1734
+ results_ = [results];
1735
+ }
1736
+ return results_;
1737
+ }
1738
+ async logEvaluationFeedback(evaluatorResponse, run, sourceInfo) {
1739
+ const results = this._selectEvalResults(evaluatorResponse);
1740
+ for (const res of results) {
1741
+ let sourceInfo_ = sourceInfo || {};
1742
+ if (res.evaluatorInfo) {
1743
+ sourceInfo_ = { ...res.evaluatorInfo, ...sourceInfo_ };
1744
+ }
1745
+ let runId_ = null;
1746
+ if (res.targetRunId) {
1747
+ runId_ = res.targetRunId;
1748
+ }
1749
+ else if (run) {
1750
+ runId_ = run.id;
1751
+ }
1752
+ await this.createFeedback(runId_, res.key, {
1753
+ score: res.score,
1754
+ value: res.value,
1755
+ comment: res.comment,
1756
+ correction: res.correction,
1757
+ sourceInfo: sourceInfo_,
1758
+ sourceRunId: res.sourceRunId,
1759
+ feedbackConfig: res.feedbackConfig,
1760
+ feedbackSourceType: "model",
1761
+ });
1762
+ }
1763
+ return results;
1764
+ }
1725
1765
  }