langchain 0.1.12 → 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.
- package/dist/cache/redis.cjs +1 -1
- package/dist/cache/redis.d.ts +1 -1
- package/dist/cache/redis.js +1 -1
- package/dist/document_loaders/web/confluence.cjs +7 -1
- package/dist/document_loaders/web/confluence.d.ts +9 -0
- package/dist/document_loaders/web/confluence.js +7 -1
- package/dist/embeddings/fake.cjs +21 -92
- package/dist/embeddings/fake.d.ts +1 -53
- package/dist/embeddings/fake.js +7 -89
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +35 -14
- package/dist/load/import_map.d.ts +22 -1
- package/dist/load/import_map.js +37 -16
- package/dist/runnables/remote.cjs +15 -339
- package/dist/runnables/remote.d.ts +1 -30
- package/dist/runnables/remote.js +1 -337
- package/dist/stores/message/redis.cjs +2 -0
- package/dist/stores/message/redis.js +2 -0
- package/dist/util/migrations/0_0-0_1-migrate-imports.cjs +208 -0
- package/dist/util/migrations/0_0-0_1-migrate-imports.d.ts +44 -0
- package/dist/util/migrations/0_0-0_1-migrate-imports.js +201 -0
- package/dist/vectorstores/redis.cjs +2 -0
- package/dist/vectorstores/redis.js +2 -0
- package/dist/vectorstores/weaviate.cjs +2 -0
- package/dist/vectorstores/weaviate.js +2 -0
- package/package.json +26 -7
- package/util/migrations/0_1.cjs +1 -0
- package/util/migrations/0_1.d.cts +1 -0
- package/util/migrations/0_1.d.ts +1 -0
- package/util/migrations/0_1.js +1 -0
package/dist/runnables/remote.js
CHANGED
|
@@ -1,337 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Document } from "@langchain/core/documents";
|
|
3
|
-
import { ChatPromptValue, StringPromptValue, } from "@langchain/core/prompt_values";
|
|
4
|
-
import { LogStreamCallbackHandler, } from "@langchain/core/tracers/log_stream";
|
|
5
|
-
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessage, FunctionMessageChunk, HumanMessage, HumanMessageChunk, SystemMessage, SystemMessageChunk, ToolMessage, ToolMessageChunk, } from "@langchain/core/messages";
|
|
6
|
-
import { GenerationChunk, ChatGenerationChunk, RUN_KEY, } from "@langchain/core/outputs";
|
|
7
|
-
import { getBytes, getLines, getMessages, convertEventStreamToIterableReadableDataStream, } from "@langchain/community/utils/event_source_parse";
|
|
8
|
-
import { IterableReadableStream } from "@langchain/core/utils/stream";
|
|
9
|
-
function isSuperset(set, subset) {
|
|
10
|
-
for (const elem of subset) {
|
|
11
|
-
if (!set.has(elem)) {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
function revive(obj) {
|
|
19
|
-
if (Array.isArray(obj))
|
|
20
|
-
return obj.map(revive);
|
|
21
|
-
if (typeof obj === "object") {
|
|
22
|
-
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
23
|
-
if (!obj || obj instanceof Date) {
|
|
24
|
-
return obj;
|
|
25
|
-
}
|
|
26
|
-
const keysArr = Object.keys(obj);
|
|
27
|
-
const keys = new Set(keysArr);
|
|
28
|
-
if (isSuperset(keys, new Set(["page_content", "metadata"]))) {
|
|
29
|
-
return new Document({
|
|
30
|
-
pageContent: obj.page_content,
|
|
31
|
-
metadata: obj.metadata,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
if (isSuperset(keys, new Set(["content", "type", "additional_kwargs"]))) {
|
|
35
|
-
if (obj.type === "HumanMessage" || obj.type === "human") {
|
|
36
|
-
return new HumanMessage({
|
|
37
|
-
content: obj.content,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
if (obj.type === "SystemMessage" || obj.type === "system") {
|
|
41
|
-
return new SystemMessage({
|
|
42
|
-
content: obj.content,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
if (obj.type === "ChatMessage" || obj.type === "chat") {
|
|
46
|
-
return new ChatMessage({
|
|
47
|
-
content: obj.content,
|
|
48
|
-
role: obj.role,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
if (obj.type === "FunctionMessage" || obj.type === "function") {
|
|
52
|
-
return new FunctionMessage({
|
|
53
|
-
content: obj.content,
|
|
54
|
-
name: obj.name,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
if (obj.type === "ToolMessage" || obj.type === "tool") {
|
|
58
|
-
return new ToolMessage({
|
|
59
|
-
content: obj.content,
|
|
60
|
-
tool_call_id: obj.tool_call_id,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
if (obj.type === "AIMessage" || obj.type === "ai") {
|
|
64
|
-
return new AIMessage({
|
|
65
|
-
content: obj.content,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
if (obj.type === "HumanMessageChunk") {
|
|
69
|
-
return new HumanMessageChunk({
|
|
70
|
-
content: obj.content,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
if (obj.type === "SystemMessageChunk") {
|
|
74
|
-
return new SystemMessageChunk({
|
|
75
|
-
content: obj.content,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
if (obj.type === "ChatMessageChunk") {
|
|
79
|
-
return new ChatMessageChunk({
|
|
80
|
-
content: obj.content,
|
|
81
|
-
role: obj.role,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (obj.type === "FunctionMessageChunk") {
|
|
85
|
-
return new FunctionMessageChunk({
|
|
86
|
-
content: obj.content,
|
|
87
|
-
name: obj.name,
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
if (obj.type === "ToolMessageChunk") {
|
|
91
|
-
return new ToolMessageChunk({
|
|
92
|
-
content: obj.content,
|
|
93
|
-
tool_call_id: obj.tool_call_id,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
if (obj.type === "AIMessageChunk") {
|
|
97
|
-
return new AIMessageChunk({
|
|
98
|
-
content: obj.content,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
if (isSuperset(keys, new Set(["text", "generation_info", "type"]))) {
|
|
103
|
-
if (obj.type === "ChatGenerationChunk") {
|
|
104
|
-
return new ChatGenerationChunk({
|
|
105
|
-
message: revive(obj.message),
|
|
106
|
-
text: obj.text,
|
|
107
|
-
generationInfo: obj.generation_info,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
else if (obj.type === "ChatGeneration") {
|
|
111
|
-
return {
|
|
112
|
-
message: revive(obj.message),
|
|
113
|
-
text: obj.text,
|
|
114
|
-
generationInfo: obj.generation_info,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
else if (obj.type === "GenerationChunk") {
|
|
118
|
-
return new GenerationChunk({
|
|
119
|
-
text: obj.text,
|
|
120
|
-
generationInfo: obj.generation_info,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
else if (obj.type === "Generation") {
|
|
124
|
-
return {
|
|
125
|
-
text: obj.text,
|
|
126
|
-
generationInfo: obj.generation_info,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (isSuperset(keys, new Set(["tool", "tool_input", "log", "type"]))) {
|
|
131
|
-
if (obj.type === "AgentAction") {
|
|
132
|
-
return {
|
|
133
|
-
tool: obj.tool,
|
|
134
|
-
toolInput: obj.tool_input,
|
|
135
|
-
log: obj.log,
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (isSuperset(keys, new Set(["return_values", "log", "type"]))) {
|
|
140
|
-
if (obj.type === "AgentFinish") {
|
|
141
|
-
return {
|
|
142
|
-
returnValues: obj.return_values,
|
|
143
|
-
log: obj.log,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if (isSuperset(keys, new Set(["generations", "run", "type"]))) {
|
|
148
|
-
if (obj.type === "LLMResult") {
|
|
149
|
-
return {
|
|
150
|
-
generations: revive(obj.generations),
|
|
151
|
-
llmOutput: obj.llm_output,
|
|
152
|
-
[RUN_KEY]: obj.run,
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (isSuperset(keys, new Set(["messages"]))) {
|
|
157
|
-
// TODO: Start checking for type: ChatPromptValue and ChatPromptValueConcrete
|
|
158
|
-
// when LangServe bug is fixed
|
|
159
|
-
return new ChatPromptValue({
|
|
160
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
|
-
messages: obj.messages.map((msg) => revive(msg)),
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
if (isSuperset(keys, new Set(["text"]))) {
|
|
165
|
-
// TODO: Start checking for type: StringPromptValue
|
|
166
|
-
// when LangServe bug is fixed
|
|
167
|
-
return new StringPromptValue(obj.text);
|
|
168
|
-
}
|
|
169
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
170
|
-
const innerRevive = (key) => [
|
|
171
|
-
key,
|
|
172
|
-
revive(obj[key]),
|
|
173
|
-
];
|
|
174
|
-
const rtn = Object.fromEntries(keysArr.map(innerRevive));
|
|
175
|
-
return rtn;
|
|
176
|
-
}
|
|
177
|
-
return obj;
|
|
178
|
-
}
|
|
179
|
-
function deserialize(str) {
|
|
180
|
-
const obj = JSON.parse(str);
|
|
181
|
-
return revive(obj);
|
|
182
|
-
}
|
|
183
|
-
function removeCallbacks(options) {
|
|
184
|
-
const rest = { ...options };
|
|
185
|
-
delete rest.callbacks;
|
|
186
|
-
return rest;
|
|
187
|
-
}
|
|
188
|
-
export class RemoteRunnable extends Runnable {
|
|
189
|
-
constructor(fields) {
|
|
190
|
-
super(fields);
|
|
191
|
-
Object.defineProperty(this, "url", {
|
|
192
|
-
enumerable: true,
|
|
193
|
-
configurable: true,
|
|
194
|
-
writable: true,
|
|
195
|
-
value: void 0
|
|
196
|
-
});
|
|
197
|
-
Object.defineProperty(this, "options", {
|
|
198
|
-
enumerable: true,
|
|
199
|
-
configurable: true,
|
|
200
|
-
writable: true,
|
|
201
|
-
value: void 0
|
|
202
|
-
});
|
|
203
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
204
|
-
enumerable: true,
|
|
205
|
-
configurable: true,
|
|
206
|
-
writable: true,
|
|
207
|
-
value: ["langchain", "schema", "runnable", "remote"]
|
|
208
|
-
});
|
|
209
|
-
const { url, options } = fields;
|
|
210
|
-
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
211
|
-
this.options = options;
|
|
212
|
-
}
|
|
213
|
-
async post(path, body) {
|
|
214
|
-
return await fetch(`${this.url}${path}`, {
|
|
215
|
-
method: "POST",
|
|
216
|
-
body: JSON.stringify(body),
|
|
217
|
-
headers: {
|
|
218
|
-
"Content-Type": "application/json",
|
|
219
|
-
...this.options?.headers,
|
|
220
|
-
},
|
|
221
|
-
signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
async invoke(input, options) {
|
|
225
|
-
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
226
|
-
const response = await this.post("/invoke", {
|
|
227
|
-
input,
|
|
228
|
-
config: removeCallbacks(config),
|
|
229
|
-
kwargs: kwargs ?? {},
|
|
230
|
-
});
|
|
231
|
-
return revive((await response.json()).output);
|
|
232
|
-
}
|
|
233
|
-
async _batch(inputs, options, _, batchOptions) {
|
|
234
|
-
if (batchOptions?.returnExceptions) {
|
|
235
|
-
throw new Error("returnExceptions is not supported for remote clients");
|
|
236
|
-
}
|
|
237
|
-
const configsAndKwargsArray = options?.map((opts) => this._separateRunnableConfigFromCallOptions(opts));
|
|
238
|
-
const [configs, kwargs] = configsAndKwargsArray?.reduce(([pc, pk], [c, k]) => [
|
|
239
|
-
[...pc, c],
|
|
240
|
-
[...pk, k],
|
|
241
|
-
], [[], []]) ?? [undefined, undefined];
|
|
242
|
-
const response = await this.post("/batch", {
|
|
243
|
-
inputs,
|
|
244
|
-
config: (configs ?? [])
|
|
245
|
-
.map(removeCallbacks)
|
|
246
|
-
.map((config) => ({ ...config, ...batchOptions })),
|
|
247
|
-
kwargs,
|
|
248
|
-
});
|
|
249
|
-
const body = await response.json();
|
|
250
|
-
if (!body.output)
|
|
251
|
-
throw new Error("Invalid response from remote runnable");
|
|
252
|
-
return revive(body.output);
|
|
253
|
-
}
|
|
254
|
-
async batch(inputs, options, batchOptions) {
|
|
255
|
-
if (batchOptions?.returnExceptions) {
|
|
256
|
-
throw Error("returnExceptions is not supported for remote clients");
|
|
257
|
-
}
|
|
258
|
-
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
259
|
-
}
|
|
260
|
-
async stream(input, options) {
|
|
261
|
-
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
262
|
-
const response = await this.post("/stream", {
|
|
263
|
-
input,
|
|
264
|
-
config,
|
|
265
|
-
kwargs,
|
|
266
|
-
});
|
|
267
|
-
if (!response.ok) {
|
|
268
|
-
const json = await response.json();
|
|
269
|
-
const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
|
|
270
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
271
|
-
error.response = response;
|
|
272
|
-
throw error;
|
|
273
|
-
}
|
|
274
|
-
const { body } = response;
|
|
275
|
-
if (!body) {
|
|
276
|
-
throw new Error("Could not begin remote stream. Please check the given URL and try again.");
|
|
277
|
-
}
|
|
278
|
-
const stream = new ReadableStream({
|
|
279
|
-
async start(controller) {
|
|
280
|
-
const enqueueLine = getMessages((msg) => {
|
|
281
|
-
if (msg.data)
|
|
282
|
-
controller.enqueue(deserialize(msg.data));
|
|
283
|
-
});
|
|
284
|
-
const onLine = (line, fieldLength, flush) => {
|
|
285
|
-
enqueueLine(line, fieldLength, flush);
|
|
286
|
-
if (flush)
|
|
287
|
-
controller.close();
|
|
288
|
-
};
|
|
289
|
-
await getBytes(body, getLines(onLine));
|
|
290
|
-
},
|
|
291
|
-
});
|
|
292
|
-
return IterableReadableStream.fromReadableStream(stream);
|
|
293
|
-
}
|
|
294
|
-
async *streamLog(input, options, streamOptions) {
|
|
295
|
-
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
296
|
-
const stream = new LogStreamCallbackHandler({
|
|
297
|
-
...streamOptions,
|
|
298
|
-
autoClose: false,
|
|
299
|
-
});
|
|
300
|
-
const { callbacks } = config;
|
|
301
|
-
if (callbacks === undefined) {
|
|
302
|
-
config.callbacks = [stream];
|
|
303
|
-
}
|
|
304
|
-
else if (Array.isArray(callbacks)) {
|
|
305
|
-
config.callbacks = callbacks.concat([stream]);
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
const copiedCallbacks = callbacks.copy();
|
|
309
|
-
copiedCallbacks.inheritableHandlers.push(stream);
|
|
310
|
-
config.callbacks = copiedCallbacks;
|
|
311
|
-
}
|
|
312
|
-
// The type is in camelCase but the API only accepts snake_case.
|
|
313
|
-
const camelCaseStreamOptions = {
|
|
314
|
-
include_names: streamOptions?.includeNames,
|
|
315
|
-
include_types: streamOptions?.includeTypes,
|
|
316
|
-
include_tags: streamOptions?.includeTags,
|
|
317
|
-
exclude_names: streamOptions?.excludeNames,
|
|
318
|
-
exclude_types: streamOptions?.excludeTypes,
|
|
319
|
-
exclude_tags: streamOptions?.excludeTags,
|
|
320
|
-
};
|
|
321
|
-
const response = await this.post("/stream_log", {
|
|
322
|
-
input,
|
|
323
|
-
config,
|
|
324
|
-
kwargs,
|
|
325
|
-
...camelCaseStreamOptions,
|
|
326
|
-
diff: false,
|
|
327
|
-
});
|
|
328
|
-
const { body } = response;
|
|
329
|
-
if (!body) {
|
|
330
|
-
throw new Error("Could not begin remote stream log. Please check the given URL and try again.");
|
|
331
|
-
}
|
|
332
|
-
const runnableStream = convertEventStreamToIterableReadableDataStream(body);
|
|
333
|
-
for await (const log of runnableStream) {
|
|
334
|
-
yield revive(JSON.parse(log));
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
1
|
+
export * from "@langchain/core/runnables/remote";
|
|
@@ -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: "stores/message/redis",
|
|
20
|
+
newEntrypointName: "",
|
|
21
|
+
newPackageName: "@langchain/redis",
|
|
20
22
|
});
|
|
21
23
|
__exportStar(require("@langchain/community/stores/message/redis"), exports);
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { logVersion010MigrationWarning } from "../../util/entrypoint_deprecation.js";
|
|
2
2
|
/* #__PURE__ */ logVersion010MigrationWarning({
|
|
3
3
|
oldEntrypointName: "stores/message/redis",
|
|
4
|
+
newEntrypointName: "",
|
|
5
|
+
newPackageName: "@langchain/redis",
|
|
4
6
|
});
|
|
5
7
|
export * from "@langchain/community/stores/message/redis";
|
|
@@ -0,0 +1,208 @@
|
|
|
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;
|
|
@@ -0,0 +1,44 @@
|
|
|
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 {};
|