langchain 0.1.14 → 0.1.16-rc.0

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.
@@ -143,9 +143,9 @@ class LLMStringEvaluator extends LLMEvalChain {
143
143
  * @param callOptions
144
144
  * @param config
145
145
  */
146
- evaluateStrings(args, callOptions, config) {
146
+ evaluateStrings(args, config) {
147
147
  this.checkEvaluationArgs(args.reference, args.input);
148
- return this._evaluateStrings(args, callOptions, config);
148
+ return this._evaluateStrings(args, config);
149
149
  }
150
150
  }
151
151
  exports.LLMStringEvaluator = LLMStringEvaluator;
@@ -114,7 +114,7 @@ export declare abstract class LLMStringEvaluator<T extends EvalOutputType = Eval
114
114
  * @param callOptions
115
115
  * @param config
116
116
  */
117
- abstract _evaluateStrings(args: StringEvaluatorArgs, callOptions?: ExtractLLMCallOptions<this["llm"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
117
+ abstract _evaluateStrings(args: StringEvaluatorArgs & ExtractLLMCallOptions<this["llm"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
118
118
  /**
119
119
  * Evaluate Chain or LLM output, based on optional input and label.
120
120
  * @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
@@ -125,7 +125,7 @@ export declare abstract class LLMStringEvaluator<T extends EvalOutputType = Eval
125
125
  * @param callOptions
126
126
  * @param config
127
127
  */
128
- evaluateStrings(args: StringEvaluatorArgs, callOptions?: ExtractLLMCallOptions<this["llm"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
128
+ evaluateStrings(args: StringEvaluatorArgs & ExtractLLMCallOptions<this["llm"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
129
129
  }
130
130
  /**
131
131
  * Grade, tag, or otherwise evaluate predictions relative to their inputs
@@ -137,9 +137,9 @@ export class LLMStringEvaluator extends LLMEvalChain {
137
137
  * @param callOptions
138
138
  * @param config
139
139
  */
140
- evaluateStrings(args, callOptions, config) {
140
+ evaluateStrings(args, config) {
141
141
  this.checkEvaluationArgs(args.reference, args.input);
142
- return this._evaluateStrings(args, callOptions, config);
142
+ return this._evaluateStrings(args, config);
143
143
  }
144
144
  }
145
145
  /**
@@ -217,8 +217,8 @@ class CriteriaEvalChain extends base_js_1.LLMStringEvaluator {
217
217
  }
218
218
  return parsed;
219
219
  }
220
- async _evaluateStrings(args, callOptions, config) {
221
- const result = await this.call({ ...this.getEvalInput(args), ...callOptions }, config);
220
+ async _evaluateStrings(args, config) {
221
+ const result = await this.call({ ...this.getEvalInput(args) }, config);
222
222
  return this._prepareOutput(result);
223
223
  }
224
224
  }
@@ -61,7 +61,7 @@ export declare class CriteriaEvalChain extends LLMStringEvaluator {
61
61
  * @param result
62
62
  */
63
63
  _prepareOutput(result: ChainValues): any;
64
- _evaluateStrings(args: StringEvaluatorArgs, callOptions: ExtractLLMCallOptions<this["llm"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
64
+ _evaluateStrings(args: StringEvaluatorArgs & ExtractLLMCallOptions<this["llm"]>, config?: Callbacks | BaseCallbackConfig): Promise<ChainValues>;
65
65
  }
66
66
  /**
67
67
  * Criteria evaluation chain that requires references.
@@ -213,8 +213,8 @@ export class CriteriaEvalChain extends LLMStringEvaluator {
213
213
  }
214
214
  return parsed;
215
215
  }
216
- async _evaluateStrings(args, callOptions, config) {
217
- const result = await this.call({ ...this.getEvalInput(args), ...callOptions }, config);
216
+ async _evaluateStrings(args, config) {
217
+ const result = await this.call({ ...this.getEvalInput(args) }, config);
218
218
  return this._prepareOutput(result);
219
219
  }
220
220
  }
@@ -171,7 +171,6 @@ exports.optionalImportEntrypoints = [
171
171
  "langchain/graphs/neo4j_graph",
172
172
  "langchain/hub",
173
173
  "langchain/util/convex",
174
- "langchain/util/migrations/0_1",
175
174
  "langchain/experimental/multimodal_embeddings/googlevertexai",
176
175
  "langchain/experimental/chat_models/anthropic_functions",
177
176
  "langchain/experimental/llms/bittensor",
@@ -168,7 +168,6 @@ export const optionalImportEntrypoints = [
168
168
  "langchain/graphs/neo4j_graph",
169
169
  "langchain/hub",
170
170
  "langchain/util/convex",
171
- "langchain/util/migrations/0_1",
172
171
  "langchain/experimental/multimodal_embeddings/googlevertexai",
173
172
  "langchain/experimental/chat_models/anthropic_functions",
174
173
  "langchain/experimental/llms/bittensor",
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runOnDataset = void 0;
4
4
  const messages_1 = require("@langchain/core/messages");
5
5
  const runnables_1 = require("@langchain/core/runnables");
6
- const run_collector_1 = require("@langchain/core/tracers/run_collector");
7
6
  const tracer_langchain_1 = require("@langchain/core/tracers/tracer_langchain");
7
+ const base_1 = require("@langchain/core/tracers/base");
8
8
  const langsmith_1 = require("langsmith");
9
9
  const loader_js_1 = require("../evaluation/loader.cjs");
10
10
  const name_generation_js_1 = require("./name_generation.cjs");
11
11
  const progress_js_1 = require("./progress.cjs");
12
- class RunIdExtractor {
12
+ class SingleRunIdExtractor {
13
13
  constructor() {
14
14
  Object.defineProperty(this, "runIdPromiseResolver", {
15
15
  enumerable: true,
@@ -39,6 +39,39 @@ class RunIdExtractor {
39
39
  return this.runIdPromise;
40
40
  }
41
41
  }
42
+ class SingleRunExtractor extends base_1.BaseTracer {
43
+ constructor() {
44
+ super();
45
+ Object.defineProperty(this, "runPromiseResolver", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "runPromise", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ /** The name of the callback handler. */
58
+ Object.defineProperty(this, "name", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: "single_run_extractor"
63
+ });
64
+ this.runPromise = new Promise((extract) => {
65
+ this.runPromiseResolver = extract;
66
+ });
67
+ }
68
+ async persistRun(run) {
69
+ this.runPromiseResolver(run);
70
+ }
71
+ async extract() {
72
+ return this.runPromise;
73
+ }
74
+ }
42
75
  /**
43
76
  * Wraps an evaluator function + implements the RunEvaluator interface.
44
77
  */
@@ -59,7 +92,7 @@ class DynamicRunEvaluator {
59
92
  * @returns A promise that extracts to the evaluation result.
60
93
  */
61
94
  async evaluateRun(run, example) {
62
- const extractor = new RunIdExtractor();
95
+ const extractor = new SingleRunIdExtractor();
63
96
  const tracer = new tracer_langchain_1.LangChainTracer({ projectName: "evaluators" });
64
97
  const result = await this.evaluator.invoke({
65
98
  run,
@@ -146,7 +179,7 @@ class PreparedRunEvaluator {
146
179
  rawReferenceOutput: example?.outputs,
147
180
  run,
148
181
  });
149
- const extractor = new RunIdExtractor();
182
+ const extractor = new SingleRunIdExtractor();
150
183
  const tracer = new tracer_langchain_1.LangChainTracer({ projectName: "evaluators" });
151
184
  if (this.isStringEvaluator) {
152
185
  const evalResult = await this.evaluator.evaluateStrings({
@@ -226,25 +259,23 @@ const createWrappedModel = async (modelOrFactory) => {
226
259
  const loadExamples = async ({ datasetName, client, projectName, }) => {
227
260
  const exampleIterator = client.listExamples({ datasetName });
228
261
  const configs = [];
229
- const runCollectors = [];
262
+ const runExtractors = [];
230
263
  const examples = [];
231
264
  for await (const example of exampleIterator) {
232
- const runCollector = new run_collector_1.RunCollectorCallbackHandler({
233
- exampleId: example.id,
234
- });
265
+ const runExtractor = new SingleRunExtractor();
235
266
  configs.push({
236
267
  callbacks: [
237
268
  new tracer_langchain_1.LangChainTracer({ exampleId: example.id, projectName }),
238
- runCollector,
269
+ runExtractor,
239
270
  ],
240
271
  });
241
272
  examples.push(example);
242
- runCollectors.push(runCollector);
273
+ runExtractors.push(runExtractor);
243
274
  }
244
275
  return {
245
276
  configs,
246
277
  examples,
247
- runCollectors,
278
+ runExtractors,
248
279
  };
249
280
  };
250
281
  const applyEvaluators = async ({ evaluation, runs, examples, client, }) => {
@@ -352,7 +383,7 @@ const runOnDataset = async (chainOrFactory, datasetName, { evaluationConfig, pro
352
383
  const dataset = await testClient.readDataset({ datasetName });
353
384
  const datasetId = dataset.id;
354
385
  const testConcurrency = maxConcurrency ?? 5;
355
- const { configs, examples, runCollectors } = await loadExamples({
386
+ const { configs, examples, runExtractors } = await loadExamples({
356
387
  datasetName,
357
388
  client: testClient,
358
389
  projectName: testProjectName,
@@ -384,7 +415,7 @@ const runOnDataset = async (chainOrFactory, datasetName, { evaluationConfig, pro
384
415
  progress.complete();
385
416
  const runs = [];
386
417
  for (let i = 0; i < examples.length; i += 1) {
387
- runs.push(runCollectors[i].tracedRuns[0]);
418
+ runs.push(await runExtractors[i].extract());
388
419
  }
389
420
  let evalResults = {};
390
421
  if (evaluationConfig) {
@@ -1,12 +1,12 @@
1
1
  import { mapStoredMessagesToChatMessages } from "@langchain/core/messages";
2
2
  import { Runnable, RunnableLambda, } from "@langchain/core/runnables";
3
- import { RunCollectorCallbackHandler } from "@langchain/core/tracers/run_collector";
4
3
  import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
4
+ import { BaseTracer } from "@langchain/core/tracers/base";
5
5
  import { Client } from "langsmith";
6
6
  import { loadEvaluator } from "../evaluation/loader.js";
7
7
  import { randomName } from "./name_generation.js";
8
8
  import { ProgressBar } from "./progress.js";
9
- class RunIdExtractor {
9
+ class SingleRunIdExtractor {
10
10
  constructor() {
11
11
  Object.defineProperty(this, "runIdPromiseResolver", {
12
12
  enumerable: true,
@@ -36,6 +36,39 @@ class RunIdExtractor {
36
36
  return this.runIdPromise;
37
37
  }
38
38
  }
39
+ class SingleRunExtractor extends BaseTracer {
40
+ constructor() {
41
+ super();
42
+ Object.defineProperty(this, "runPromiseResolver", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "runPromise", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ /** The name of the callback handler. */
55
+ Object.defineProperty(this, "name", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: "single_run_extractor"
60
+ });
61
+ this.runPromise = new Promise((extract) => {
62
+ this.runPromiseResolver = extract;
63
+ });
64
+ }
65
+ async persistRun(run) {
66
+ this.runPromiseResolver(run);
67
+ }
68
+ async extract() {
69
+ return this.runPromise;
70
+ }
71
+ }
39
72
  /**
40
73
  * Wraps an evaluator function + implements the RunEvaluator interface.
41
74
  */
@@ -56,7 +89,7 @@ class DynamicRunEvaluator {
56
89
  * @returns A promise that extracts to the evaluation result.
57
90
  */
58
91
  async evaluateRun(run, example) {
59
- const extractor = new RunIdExtractor();
92
+ const extractor = new SingleRunIdExtractor();
60
93
  const tracer = new LangChainTracer({ projectName: "evaluators" });
61
94
  const result = await this.evaluator.invoke({
62
95
  run,
@@ -143,7 +176,7 @@ class PreparedRunEvaluator {
143
176
  rawReferenceOutput: example?.outputs,
144
177
  run,
145
178
  });
146
- const extractor = new RunIdExtractor();
179
+ const extractor = new SingleRunIdExtractor();
147
180
  const tracer = new LangChainTracer({ projectName: "evaluators" });
148
181
  if (this.isStringEvaluator) {
149
182
  const evalResult = await this.evaluator.evaluateStrings({
@@ -223,25 +256,23 @@ const createWrappedModel = async (modelOrFactory) => {
223
256
  const loadExamples = async ({ datasetName, client, projectName, }) => {
224
257
  const exampleIterator = client.listExamples({ datasetName });
225
258
  const configs = [];
226
- const runCollectors = [];
259
+ const runExtractors = [];
227
260
  const examples = [];
228
261
  for await (const example of exampleIterator) {
229
- const runCollector = new RunCollectorCallbackHandler({
230
- exampleId: example.id,
231
- });
262
+ const runExtractor = new SingleRunExtractor();
232
263
  configs.push({
233
264
  callbacks: [
234
265
  new LangChainTracer({ exampleId: example.id, projectName }),
235
- runCollector,
266
+ runExtractor,
236
267
  ],
237
268
  });
238
269
  examples.push(example);
239
- runCollectors.push(runCollector);
270
+ runExtractors.push(runExtractor);
240
271
  }
241
272
  return {
242
273
  configs,
243
274
  examples,
244
- runCollectors,
275
+ runExtractors,
245
276
  };
246
277
  };
247
278
  const applyEvaluators = async ({ evaluation, runs, examples, client, }) => {
@@ -349,7 +380,7 @@ export const runOnDataset = async (chainOrFactory, datasetName, { evaluationConf
349
380
  const dataset = await testClient.readDataset({ datasetName });
350
381
  const datasetId = dataset.id;
351
382
  const testConcurrency = maxConcurrency ?? 5;
352
- const { configs, examples, runCollectors } = await loadExamples({
383
+ const { configs, examples, runExtractors } = await loadExamples({
353
384
  datasetName,
354
385
  client: testClient,
355
386
  projectName: testProjectName,
@@ -381,7 +412,7 @@ export const runOnDataset = async (chainOrFactory, datasetName, { evaluationConf
381
412
  progress.complete();
382
413
  const runs = [];
383
414
  for (let i = 0; i < examples.length; i += 1) {
384
- runs.push(runCollectors[i].tracedRuns[0]);
415
+ runs.push(await runExtractors[i].extract());
385
416
  }
386
417
  let evalResults = {};
387
418
  if (evaluationConfig) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.1.14",
3
+ "version": "0.1.16-rc.0",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -1082,10 +1082,6 @@
1082
1082
  "util/time.js",
1083
1083
  "util/time.d.ts",
1084
1084
  "util/time.d.cts",
1085
- "util/migrations/0_1.cjs",
1086
- "util/migrations/0_1.js",
1087
- "util/migrations/0_1.d.ts",
1088
- "util/migrations/0_1.d.cts",
1089
1085
  "experimental/autogpt.cjs",
1090
1086
  "experimental/autogpt.js",
1091
1087
  "experimental/autogpt.d.ts",
@@ -1259,7 +1255,6 @@
1259
1255
  "eslint-plugin-no-instanceof": "^1.0.1",
1260
1256
  "eslint-plugin-prettier": "^4.2.1",
1261
1257
  "fast-xml-parser": "^4.2.7",
1262
- "glob": "^10.3.10",
1263
1258
  "google-auth-library": "^8.9.0",
1264
1259
  "handlebars": "^4.7.8",
1265
1260
  "html-to-text": "^9.0.5",
@@ -1273,6 +1268,7 @@
1273
1268
  "node-llama-cpp": "2.7.3",
1274
1269
  "notion-to-md": "^3.1.0",
1275
1270
  "officeparser": "^4.0.4",
1271
+ "openai": "^4.26.1",
1276
1272
  "pdf-parse": "1.1.1",
1277
1273
  "peggy": "^3.0.2",
1278
1274
  "playwright": "^1.32.1",
@@ -1286,12 +1282,12 @@
1286
1282
  "sonix-speech-recognition": "^2.1.1",
1287
1283
  "srt-parser-2": "^1.2.3",
1288
1284
  "ts-jest": "^29.1.0",
1289
- "ts-morph": "^21.0.1",
1290
1285
  "typeorm": "^0.3.12",
1291
1286
  "typescript": "~5.1.6",
1292
1287
  "vectordb": "^0.1.4",
1293
1288
  "weaviate-ts-client": "^1.4.0",
1294
1289
  "web-auth-library": "^1.0.3",
1290
+ "wikipedia": "^2.1.2",
1295
1291
  "youtube-transcript": "^1.0.6",
1296
1292
  "youtubei.js": "^5.8.0"
1297
1293
  },
@@ -1320,7 +1316,6 @@
1320
1316
  "d3-dsv": "^2.0.0",
1321
1317
  "epub2": "^3.0.1",
1322
1318
  "fast-xml-parser": "^4.2.7",
1323
- "glob": "^10.3.10",
1324
1319
  "google-auth-library": "^8.9.0",
1325
1320
  "handlebars": "^4.7.8",
1326
1321
  "html-to-text": "^9.0.5",
@@ -1340,7 +1335,6 @@
1340
1335
  "redis": "^4.6.4",
1341
1336
  "sonix-speech-recognition": "^2.1.1",
1342
1337
  "srt-parser-2": "^1.2.3",
1343
- "ts-morph": "^21.0.1",
1344
1338
  "typeorm": "^0.3.12",
1345
1339
  "vectordb": "^0.1.4",
1346
1340
  "weaviate-ts-client": "^1.4.0",
@@ -1425,9 +1419,6 @@
1425
1419
  "fast-xml-parser": {
1426
1420
  "optional": true
1427
1421
  },
1428
- "glob": {
1429
- "optional": true
1430
- },
1431
1422
  "google-auth-library": {
1432
1423
  "optional": true
1433
1424
  },
@@ -1485,9 +1476,6 @@
1485
1476
  "srt-parser-2": {
1486
1477
  "optional": true
1487
1478
  },
1488
- "ts-morph": {
1489
- "optional": true
1490
- },
1491
1479
  "typeorm": {
1492
1480
  "optional": true
1493
1481
  },
@@ -1513,7 +1501,7 @@
1513
1501
  "dependencies": {
1514
1502
  "@anthropic-ai/sdk": "^0.9.1",
1515
1503
  "@langchain/community": "~0.0.20",
1516
- "@langchain/core": "~0.1.24",
1504
+ "@langchain/core": "~0.1.25",
1517
1505
  "@langchain/openai": "~0.0.12",
1518
1506
  "binary-extensions": "^2.2.0",
1519
1507
  "expr-eval": "^2.0.2",
@@ -3960,15 +3948,6 @@
3960
3948
  "import": "./util/time.js",
3961
3949
  "require": "./util/time.cjs"
3962
3950
  },
3963
- "./util/migrations/0_1": {
3964
- "types": {
3965
- "import": "./util/migrations/0_1.d.ts",
3966
- "require": "./util/migrations/0_1.d.cts",
3967
- "default": "./util/migrations/0_1.d.ts"
3968
- },
3969
- "import": "./util/migrations/0_1.js",
3970
- "require": "./util/migrations/0_1.cjs"
3971
- },
3972
3951
  "./experimental/autogpt": {
3973
3952
  "types": {
3974
3953
  "import": "./experimental/autogpt.d.ts",
@@ -1,208 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.updateEntrypointsFrom0_0_xTo0_1_x = void 0;
7
- const ts_morph_1 = require("ts-morph");
8
- const glob_1 = require("glob");
9
- const node_path_1 = __importDefault(require("node:path"));
10
- /**
11
- * @param {string} packagePath
12
- * @param {Project} project
13
- * @returns {Array<EntrypointAndSymbols> }
14
- */
15
- async function getEntrypointsFromFile(packagePath, project) {
16
- // @TODO replace any with LangChainConfig from `@langchain/scripts`
17
- const { config } = await import(node_path_1.default.join(packagePath, "langchain.config.js"));
18
- const { entrypoints, deprecatedNodeOnly } = config;
19
- const result = Object.entries(entrypoints).flatMap(([key, value]) => {
20
- if (deprecatedNodeOnly.includes(key)) {
21
- return [];
22
- }
23
- const newFile = project.addSourceFileAtPath(node_path_1.default.join(packagePath, "src", `${value}.ts`));
24
- const exportedSymbolsMap = newFile.getExportedDeclarations();
25
- const exportedSymbols = Array.from(exportedSymbolsMap.entries()).map(([symbol, declarations]) => ({
26
- kind: declarations[0].getKind(),
27
- symbol,
28
- }));
29
- return {
30
- entrypoint: key,
31
- exportedSymbols,
32
- };
33
- });
34
- return result;
35
- }
36
- /**
37
- * Finds a matching symbol in the array of exported symbols.
38
- * @param {{ symbol: string, kind: SyntaxKind }} target - The target symbol and its kind to find.
39
- * @param {Array<EntrypointAndSymbols>} exportedSymbols - The array of exported symbols to search.
40
- * @param {string} packageSuffix - The suffix of the package to import from. Eg, core
41
- * @returns {{ entrypoint: string, foundSymbol: string } | undefined} The matching symbol or undefined if not found.
42
- */
43
- function findMatchingSymbol(target, exportedSymbols, packageSuffix) {
44
- for (const entry of exportedSymbols) {
45
- const foundSymbol = entry.exportedSymbols.find(({ symbol, kind }) => symbol === target.symbol && kind === target.kind);
46
- if (foundSymbol) {
47
- return {
48
- entrypoint: entry.entrypoint,
49
- foundSymbol: foundSymbol.symbol,
50
- packageSuffix,
51
- }; // Return the matching entry object
52
- }
53
- }
54
- return undefined;
55
- }
56
- /**
57
- * @param {Array<EntrypointAndSymbols>} entrypoints
58
- * @returns {Array<EntrypointAndSymbols>}
59
- */
60
- function removeLoad(entrypoints) {
61
- return entrypoints.flatMap((entrypoint) => {
62
- const newEntrypoint = entrypoint.entrypoint === "index" ? "" : `/${entrypoint.entrypoint}`;
63
- const withoutLoadOrIndex = entrypoint.exportedSymbols.filter((item) => {
64
- if (item.symbol === "load" && newEntrypoint === "load") {
65
- return false;
66
- }
67
- return true;
68
- });
69
- return {
70
- entrypoint: newEntrypoint,
71
- exportedSymbols: withoutLoadOrIndex,
72
- };
73
- });
74
- }
75
- function updateImport({ matchingSymbols, namedImport, projectFile, namedImportText, }) {
76
- const firstMatchingSymbol = matchingSymbols.find((matchingSymbol) => matchingSymbol);
77
- if (firstMatchingSymbol) {
78
- console.debug(`Found matching symbol in the "@langchain/${firstMatchingSymbol.packageSuffix}" package.`, {
79
- matchingSymbol: firstMatchingSymbol,
80
- });
81
- namedImport.remove();
82
- projectFile.addImportDeclaration({
83
- moduleSpecifier: `@langchain/${firstMatchingSymbol.packageSuffix}${firstMatchingSymbol.entrypoint}`,
84
- namedImports: [namedImportText],
85
- });
86
- return true;
87
- }
88
- return false;
89
- }
90
- /**
91
- * Find imports from deprecated pre 0.1 LangChain modules and update them to import
92
- * from the new LangChain packages.
93
- */
94
- async function updateEntrypointsFrom0_0_xTo0_1_x({ localLangChainPath, codePath, customGlobPattern, customIgnorePattern, skipCheck, }) {
95
- const project = new ts_morph_1.Project();
96
- const langchainCorePackageEntrypoints = removeLoad(await getEntrypointsFromFile(node_path_1.default.join(localLangChainPath, "langchain-core"), project));
97
- const langchainCommunityPackageEntrypoints = removeLoad(await getEntrypointsFromFile(node_path_1.default.join(localLangChainPath, "libs", "langchain-community"), project));
98
- const langchainOpenAIPackageEntrypoints = removeLoad(await getEntrypointsFromFile(node_path_1.default.join(localLangChainPath, "libs", "langchain-openai"), project));
99
- const langchainCoherePackageEntrypoints = !skipCheck?.includes("cohere" /* UpgradingModule.COHERE */)
100
- ? removeLoad(await getEntrypointsFromFile(node_path_1.default.join(localLangChainPath, "libs", "langchain-cohere"), project))
101
- : null;
102
- const langchainPineconePackageEntrypoints = !skipCheck?.includes("pinecone" /* UpgradingModule.PINECONE */)
103
- ? removeLoad(await getEntrypointsFromFile(node_path_1.default.join(localLangChainPath, "libs", "langchain-pinecone"), project))
104
- : null;
105
- const globPattern = customGlobPattern || "/**/*.ts";
106
- const ignorePattern = customIgnorePattern;
107
- const allCodebaseFiles = (await (0, glob_1.glob)(node_path_1.default.join(codePath, globPattern), {
108
- ignore: ignorePattern,
109
- }))
110
- .map((filePath) => node_path_1.default.resolve(filePath))
111
- .filter((filePath) => !filePath.includes("node_modules/"));
112
- for await (const filePath of allCodebaseFiles) {
113
- let projectFile;
114
- try {
115
- projectFile = project.addSourceFileAtPath(filePath);
116
- if (!projectFile) {
117
- throw new Error(`Failed to add source file at path: ${filePath}`);
118
- }
119
- }
120
- catch (error) {
121
- console.error({
122
- filePath,
123
- error,
124
- }, "Error occurred while trying to add source file. Continuing");
125
- return;
126
- }
127
- try {
128
- const imports = projectFile.getImportDeclarations();
129
- imports.forEach((importItem) => {
130
- // Get all imports
131
- const module = importItem.getModuleSpecifierValue();
132
- // Get only the named imports. Eg: import { foo } from "langchain/util";
133
- const namedImports = importItem.getNamedImports();
134
- if (!module.startsWith("langchain/")) {
135
- return;
136
- }
137
- // look at each import and see if it exists in
138
- let didUpdate = false;
139
- namedImports.forEach((namedImport) => {
140
- const namedImportText = namedImport.getText();
141
- let namedImportKind = null;
142
- const symbol = namedImport.getSymbol();
143
- if (symbol) {
144
- // Resolve alias symbol to its original symbol
145
- const aliasedSymbol = symbol.getAliasedSymbol() || symbol;
146
- // Get the original declarations of the symbol
147
- const declarations = aliasedSymbol.getDeclarations();
148
- if (declarations.length > 0) {
149
- // Assuming the first declaration is the original one
150
- const originalDeclarationKind = declarations[0].getKind();
151
- namedImportKind = originalDeclarationKind;
152
- }
153
- }
154
- // If we couldn't find the kind of the named imports kind, skip it
155
- if (!namedImportKind) {
156
- return;
157
- }
158
- const matchingSymbolCore = findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainCorePackageEntrypoints, "core");
159
- const matchingSymbolCommunity = findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainCommunityPackageEntrypoints, "community");
160
- const matchingSymbolOpenAI = findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainOpenAIPackageEntrypoints, "openai");
161
- const matchingSymbolCohere = langchainCoherePackageEntrypoints
162
- ? findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainCoherePackageEntrypoints, "cohere")
163
- : undefined;
164
- const matchingSymbolPinecone = langchainPineconePackageEntrypoints
165
- ? findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainPineconePackageEntrypoints, "pinecone")
166
- : undefined;
167
- didUpdate = updateImport({
168
- matchingSymbols: [
169
- matchingSymbolCore,
170
- matchingSymbolOpenAI,
171
- matchingSymbolCohere,
172
- matchingSymbolPinecone,
173
- matchingSymbolCommunity,
174
- ],
175
- namedImport,
176
- projectFile,
177
- namedImportText,
178
- });
179
- });
180
- if (didUpdate) {
181
- projectFile.saveSync();
182
- // Check if all named imports were removed, and only a file import remains.
183
- // eg: import { foo } from "langchain/anthropic"; -> import "langchain/anthropic";
184
- // if so, remove the import entirely
185
- const importClause = importItem.getImportClause();
186
- if (!importClause ||
187
- (!importClause.getDefaultImport() &&
188
- importClause.getNamedImports().length === 0)) {
189
- importItem.remove();
190
- projectFile.saveSync();
191
- }
192
- }
193
- });
194
- }
195
- catch (error) {
196
- console.error({
197
- filePath,
198
- error,
199
- }, "Error occurred while trying to read file. Continuing");
200
- }
201
- // Remove source file from the project after we're done with it
202
- // to prevent OOM errors.
203
- if (projectFile) {
204
- project.removeSourceFile(projectFile);
205
- }
206
- }
207
- }
208
- exports.updateEntrypointsFrom0_0_xTo0_1_x = updateEntrypointsFrom0_0_xTo0_1_x;
@@ -1,44 +0,0 @@
1
- declare const enum UpgradingModule {
2
- COHERE = "cohere",
3
- PINECONE = "pinecone"
4
- }
5
- /**
6
- * Find imports from deprecated pre 0.1 LangChain modules and update them to import
7
- * from the new LangChain packages.
8
- */
9
- export declare function updateEntrypointsFrom0_0_xTo0_1_x({ localLangChainPath, codePath, customGlobPattern, customIgnorePattern, skipCheck, }: {
10
- /**
11
- * The absolute path to the locally cloned LangChain repo root.
12
- * @example "/Users/username/code/langchainjs"
13
- */
14
- localLangChainPath: string;
15
- /**
16
- * The absolute path to the source directory of the codebase to update.
17
- * @example "/Users/username/code/my-project/src"
18
- */
19
- codePath: string;
20
- /**
21
- * Optionally, pass in a custom glob pattern to match files.
22
- * The backslash included in the example and default is only for
23
- * JSDoc to escape the asterisk. Do not include unless intentionally.
24
- * @example "/*.d.ts"
25
- * @default "**\/*.ts"
26
- */
27
- customGlobPattern?: string;
28
- /**
29
- * A custom ignore pattern for ignoring files.
30
- * The backslash included in the example and default is only for
31
- * JSDoc to escape the asterisk. Do not include unless intentionally.
32
- * @example ["**\/node_modules/**", "**\/dist/**", "**\/*.d.ts"]
33
- * @default node_modules/**
34
- */
35
- customIgnorePattern?: string[] | string;
36
- /**
37
- * Optionally skip checking the passed modules for imports to
38
- * update.
39
- * @example [UpgradingModule.COHERE]
40
- * @default undefined
41
- */
42
- skipCheck?: Array<UpgradingModule>;
43
- }): Promise<void>;
44
- export {};
@@ -1,201 +0,0 @@
1
- import { Project } from "ts-morph";
2
- import { glob } from "glob";
3
- import path from "node:path";
4
- /**
5
- * @param {string} packagePath
6
- * @param {Project} project
7
- * @returns {Array<EntrypointAndSymbols> }
8
- */
9
- async function getEntrypointsFromFile(packagePath, project) {
10
- // @TODO replace any with LangChainConfig from `@langchain/scripts`
11
- const { config } = await import(path.join(packagePath, "langchain.config.js"));
12
- const { entrypoints, deprecatedNodeOnly } = config;
13
- const result = Object.entries(entrypoints).flatMap(([key, value]) => {
14
- if (deprecatedNodeOnly.includes(key)) {
15
- return [];
16
- }
17
- const newFile = project.addSourceFileAtPath(path.join(packagePath, "src", `${value}.ts`));
18
- const exportedSymbolsMap = newFile.getExportedDeclarations();
19
- const exportedSymbols = Array.from(exportedSymbolsMap.entries()).map(([symbol, declarations]) => ({
20
- kind: declarations[0].getKind(),
21
- symbol,
22
- }));
23
- return {
24
- entrypoint: key,
25
- exportedSymbols,
26
- };
27
- });
28
- return result;
29
- }
30
- /**
31
- * Finds a matching symbol in the array of exported symbols.
32
- * @param {{ symbol: string, kind: SyntaxKind }} target - The target symbol and its kind to find.
33
- * @param {Array<EntrypointAndSymbols>} exportedSymbols - The array of exported symbols to search.
34
- * @param {string} packageSuffix - The suffix of the package to import from. Eg, core
35
- * @returns {{ entrypoint: string, foundSymbol: string } | undefined} The matching symbol or undefined if not found.
36
- */
37
- function findMatchingSymbol(target, exportedSymbols, packageSuffix) {
38
- for (const entry of exportedSymbols) {
39
- const foundSymbol = entry.exportedSymbols.find(({ symbol, kind }) => symbol === target.symbol && kind === target.kind);
40
- if (foundSymbol) {
41
- return {
42
- entrypoint: entry.entrypoint,
43
- foundSymbol: foundSymbol.symbol,
44
- packageSuffix,
45
- }; // Return the matching entry object
46
- }
47
- }
48
- return undefined;
49
- }
50
- /**
51
- * @param {Array<EntrypointAndSymbols>} entrypoints
52
- * @returns {Array<EntrypointAndSymbols>}
53
- */
54
- function removeLoad(entrypoints) {
55
- return entrypoints.flatMap((entrypoint) => {
56
- const newEntrypoint = entrypoint.entrypoint === "index" ? "" : `/${entrypoint.entrypoint}`;
57
- const withoutLoadOrIndex = entrypoint.exportedSymbols.filter((item) => {
58
- if (item.symbol === "load" && newEntrypoint === "load") {
59
- return false;
60
- }
61
- return true;
62
- });
63
- return {
64
- entrypoint: newEntrypoint,
65
- exportedSymbols: withoutLoadOrIndex,
66
- };
67
- });
68
- }
69
- function updateImport({ matchingSymbols, namedImport, projectFile, namedImportText, }) {
70
- const firstMatchingSymbol = matchingSymbols.find((matchingSymbol) => matchingSymbol);
71
- if (firstMatchingSymbol) {
72
- console.debug(`Found matching symbol in the "@langchain/${firstMatchingSymbol.packageSuffix}" package.`, {
73
- matchingSymbol: firstMatchingSymbol,
74
- });
75
- namedImport.remove();
76
- projectFile.addImportDeclaration({
77
- moduleSpecifier: `@langchain/${firstMatchingSymbol.packageSuffix}${firstMatchingSymbol.entrypoint}`,
78
- namedImports: [namedImportText],
79
- });
80
- return true;
81
- }
82
- return false;
83
- }
84
- /**
85
- * Find imports from deprecated pre 0.1 LangChain modules and update them to import
86
- * from the new LangChain packages.
87
- */
88
- export async function updateEntrypointsFrom0_0_xTo0_1_x({ localLangChainPath, codePath, customGlobPattern, customIgnorePattern, skipCheck, }) {
89
- const project = new Project();
90
- const langchainCorePackageEntrypoints = removeLoad(await getEntrypointsFromFile(path.join(localLangChainPath, "langchain-core"), project));
91
- const langchainCommunityPackageEntrypoints = removeLoad(await getEntrypointsFromFile(path.join(localLangChainPath, "libs", "langchain-community"), project));
92
- const langchainOpenAIPackageEntrypoints = removeLoad(await getEntrypointsFromFile(path.join(localLangChainPath, "libs", "langchain-openai"), project));
93
- const langchainCoherePackageEntrypoints = !skipCheck?.includes("cohere" /* UpgradingModule.COHERE */)
94
- ? removeLoad(await getEntrypointsFromFile(path.join(localLangChainPath, "libs", "langchain-cohere"), project))
95
- : null;
96
- const langchainPineconePackageEntrypoints = !skipCheck?.includes("pinecone" /* UpgradingModule.PINECONE */)
97
- ? removeLoad(await getEntrypointsFromFile(path.join(localLangChainPath, "libs", "langchain-pinecone"), project))
98
- : null;
99
- const globPattern = customGlobPattern || "/**/*.ts";
100
- const ignorePattern = customIgnorePattern;
101
- const allCodebaseFiles = (await glob(path.join(codePath, globPattern), {
102
- ignore: ignorePattern,
103
- }))
104
- .map((filePath) => path.resolve(filePath))
105
- .filter((filePath) => !filePath.includes("node_modules/"));
106
- for await (const filePath of allCodebaseFiles) {
107
- let projectFile;
108
- try {
109
- projectFile = project.addSourceFileAtPath(filePath);
110
- if (!projectFile) {
111
- throw new Error(`Failed to add source file at path: ${filePath}`);
112
- }
113
- }
114
- catch (error) {
115
- console.error({
116
- filePath,
117
- error,
118
- }, "Error occurred while trying to add source file. Continuing");
119
- return;
120
- }
121
- try {
122
- const imports = projectFile.getImportDeclarations();
123
- imports.forEach((importItem) => {
124
- // Get all imports
125
- const module = importItem.getModuleSpecifierValue();
126
- // Get only the named imports. Eg: import { foo } from "langchain/util";
127
- const namedImports = importItem.getNamedImports();
128
- if (!module.startsWith("langchain/")) {
129
- return;
130
- }
131
- // look at each import and see if it exists in
132
- let didUpdate = false;
133
- namedImports.forEach((namedImport) => {
134
- const namedImportText = namedImport.getText();
135
- let namedImportKind = null;
136
- const symbol = namedImport.getSymbol();
137
- if (symbol) {
138
- // Resolve alias symbol to its original symbol
139
- const aliasedSymbol = symbol.getAliasedSymbol() || symbol;
140
- // Get the original declarations of the symbol
141
- const declarations = aliasedSymbol.getDeclarations();
142
- if (declarations.length > 0) {
143
- // Assuming the first declaration is the original one
144
- const originalDeclarationKind = declarations[0].getKind();
145
- namedImportKind = originalDeclarationKind;
146
- }
147
- }
148
- // If we couldn't find the kind of the named imports kind, skip it
149
- if (!namedImportKind) {
150
- return;
151
- }
152
- const matchingSymbolCore = findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainCorePackageEntrypoints, "core");
153
- const matchingSymbolCommunity = findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainCommunityPackageEntrypoints, "community");
154
- const matchingSymbolOpenAI = findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainOpenAIPackageEntrypoints, "openai");
155
- const matchingSymbolCohere = langchainCoherePackageEntrypoints
156
- ? findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainCoherePackageEntrypoints, "cohere")
157
- : undefined;
158
- const matchingSymbolPinecone = langchainPineconePackageEntrypoints
159
- ? findMatchingSymbol({ symbol: namedImportText, kind: namedImportKind }, langchainPineconePackageEntrypoints, "pinecone")
160
- : undefined;
161
- didUpdate = updateImport({
162
- matchingSymbols: [
163
- matchingSymbolCore,
164
- matchingSymbolOpenAI,
165
- matchingSymbolCohere,
166
- matchingSymbolPinecone,
167
- matchingSymbolCommunity,
168
- ],
169
- namedImport,
170
- projectFile,
171
- namedImportText,
172
- });
173
- });
174
- if (didUpdate) {
175
- projectFile.saveSync();
176
- // Check if all named imports were removed, and only a file import remains.
177
- // eg: import { foo } from "langchain/anthropic"; -> import "langchain/anthropic";
178
- // if so, remove the import entirely
179
- const importClause = importItem.getImportClause();
180
- if (!importClause ||
181
- (!importClause.getDefaultImport() &&
182
- importClause.getNamedImports().length === 0)) {
183
- importItem.remove();
184
- projectFile.saveSync();
185
- }
186
- }
187
- });
188
- }
189
- catch (error) {
190
- console.error({
191
- filePath,
192
- error,
193
- }, "Error occurred while trying to read file. Continuing");
194
- }
195
- // Remove source file from the project after we're done with it
196
- // to prevent OOM errors.
197
- if (projectFile) {
198
- project.removeSourceFile(projectFile);
199
- }
200
- }
201
- }
@@ -1 +0,0 @@
1
- module.exports = require('../../dist/util/migrations/0_0-0_1-migrate-imports.cjs');
@@ -1 +0,0 @@
1
- export * from '../../dist/util/migrations/0_0-0_1-migrate-imports.js'
@@ -1 +0,0 @@
1
- export * from '../../dist/util/migrations/0_0-0_1-migrate-imports.js'
@@ -1 +0,0 @@
1
- export * from '../../dist/util/migrations/0_0-0_1-migrate-imports.js'