langchain 0.1.13 → 0.1.14

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.
@@ -0,0 +1,201 @@
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
+ }
@@ -17,5 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  const entrypoint_deprecation_js_1 = require("../util/entrypoint_deprecation.cjs");
18
18
  /* #__PURE__ */ (0, entrypoint_deprecation_js_1.logVersion010MigrationWarning)({
19
19
  oldEntrypointName: "vectorstores/redis",
20
+ newEntrypointName: "",
21
+ newPackageName: "@langchain/redis",
20
22
  });
21
23
  __exportStar(require("@langchain/community/vectorstores/redis"), exports);
@@ -1,5 +1,7 @@
1
1
  import { logVersion010MigrationWarning } from "../util/entrypoint_deprecation.js";
2
2
  /* #__PURE__ */ logVersion010MigrationWarning({
3
3
  oldEntrypointName: "vectorstores/redis",
4
+ newEntrypointName: "",
5
+ newPackageName: "@langchain/redis",
4
6
  });
5
7
  export * from "@langchain/community/vectorstores/redis";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -1082,6 +1082,10 @@
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",
1085
1089
  "experimental/autogpt.cjs",
1086
1090
  "experimental/autogpt.js",
1087
1091
  "experimental/autogpt.d.ts",
@@ -1255,6 +1259,7 @@
1255
1259
  "eslint-plugin-no-instanceof": "^1.0.1",
1256
1260
  "eslint-plugin-prettier": "^4.2.1",
1257
1261
  "fast-xml-parser": "^4.2.7",
1262
+ "glob": "^10.3.10",
1258
1263
  "google-auth-library": "^8.9.0",
1259
1264
  "handlebars": "^4.7.8",
1260
1265
  "html-to-text": "^9.0.5",
@@ -1281,6 +1286,7 @@
1281
1286
  "sonix-speech-recognition": "^2.1.1",
1282
1287
  "srt-parser-2": "^1.2.3",
1283
1288
  "ts-jest": "^29.1.0",
1289
+ "ts-morph": "^21.0.1",
1284
1290
  "typeorm": "^0.3.12",
1285
1291
  "typescript": "~5.1.6",
1286
1292
  "vectordb": "^0.1.4",
@@ -1314,6 +1320,7 @@
1314
1320
  "d3-dsv": "^2.0.0",
1315
1321
  "epub2": "^3.0.1",
1316
1322
  "fast-xml-parser": "^4.2.7",
1323
+ "glob": "^10.3.10",
1317
1324
  "google-auth-library": "^8.9.0",
1318
1325
  "handlebars": "^4.7.8",
1319
1326
  "html-to-text": "^9.0.5",
@@ -1333,6 +1340,7 @@
1333
1340
  "redis": "^4.6.4",
1334
1341
  "sonix-speech-recognition": "^2.1.1",
1335
1342
  "srt-parser-2": "^1.2.3",
1343
+ "ts-morph": "^21.0.1",
1336
1344
  "typeorm": "^0.3.12",
1337
1345
  "vectordb": "^0.1.4",
1338
1346
  "weaviate-ts-client": "^1.4.0",
@@ -1417,6 +1425,9 @@
1417
1425
  "fast-xml-parser": {
1418
1426
  "optional": true
1419
1427
  },
1428
+ "glob": {
1429
+ "optional": true
1430
+ },
1420
1431
  "google-auth-library": {
1421
1432
  "optional": true
1422
1433
  },
@@ -1474,6 +1485,9 @@
1474
1485
  "srt-parser-2": {
1475
1486
  "optional": true
1476
1487
  },
1488
+ "ts-morph": {
1489
+ "optional": true
1490
+ },
1477
1491
  "typeorm": {
1478
1492
  "optional": true
1479
1493
  },
@@ -1499,7 +1513,7 @@
1499
1513
  "dependencies": {
1500
1514
  "@anthropic-ai/sdk": "^0.9.1",
1501
1515
  "@langchain/community": "~0.0.20",
1502
- "@langchain/core": "~0.1.16",
1516
+ "@langchain/core": "~0.1.24",
1503
1517
  "@langchain/openai": "~0.0.12",
1504
1518
  "binary-extensions": "^2.2.0",
1505
1519
  "expr-eval": "^2.0.2",
@@ -3946,6 +3960,15 @@
3946
3960
  "import": "./util/time.js",
3947
3961
  "require": "./util/time.cjs"
3948
3962
  },
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
+ },
3949
3972
  "./experimental/autogpt": {
3950
3973
  "types": {
3951
3974
  "import": "./experimental/autogpt.d.ts",
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/util/migrations/0_0-0_1-migrate-imports.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/util/migrations/0_0-0_1-migrate-imports.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/util/migrations/0_0-0_1-migrate-imports.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/util/migrations/0_0-0_1-migrate-imports.js'