@langchain/core 0.1.14 → 0.1.16
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/callbacks/manager.cjs +2 -1
- package/dist/callbacks/manager.d.ts +3 -2
- package/dist/callbacks/manager.js +1 -1
- package/dist/language_models/base.d.ts +2 -2
- package/dist/retrievers.cjs +2 -1
- package/dist/retrievers.js +2 -1
- package/dist/runnables/base.cjs +76 -63
- package/dist/runnables/base.d.ts +6 -7
- package/dist/runnables/base.js +77 -64
- package/dist/runnables/branch.cjs +10 -3
- package/dist/runnables/branch.js +10 -3
- package/dist/runnables/config.cjs +67 -12
- package/dist/runnables/config.d.ts +11 -1
- package/dist/runnables/config.js +65 -12
- package/dist/runnables/index.cjs +2 -1
- package/dist/runnables/index.d.ts +1 -1
- package/dist/runnables/index.js +1 -1
- package/dist/runnables/router.cjs +2 -3
- package/dist/runnables/router.js +2 -3
- package/dist/tools.cjs +102 -1
- package/dist/tools.d.ts +49 -0
- package/dist/tools.js +99 -0
- package/dist/tracers/base.cjs +8 -1
- package/dist/tracers/base.js +8 -1
- package/dist/tracers/log_stream.cjs +34 -3
- package/dist/tracers/log_stream.d.ts +4 -0
- package/dist/tracers/log_stream.js +34 -3
- package/dist/utils/testing/index.cjs +105 -3
- package/dist/utils/testing/index.d.ts +55 -0
- package/dist/utils/testing/index.js +102 -2
- package/package.json +4 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
|
|
3
|
+
exports.SyntheticEmbeddings = exports.FakeEmbeddings = exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
|
|
4
4
|
const chat_history_js_1 = require("../../chat_history.cjs");
|
|
5
5
|
const document_js_1 = require("../../documents/document.cjs");
|
|
6
6
|
const chat_models_js_1 = require("../../language_models/chat_models.cjs");
|
|
@@ -12,6 +12,7 @@ const retrievers_js_1 = require("../../retrievers.cjs");
|
|
|
12
12
|
const base_js_2 = require("../../runnables/base.cjs");
|
|
13
13
|
const tools_js_1 = require("../../tools.cjs");
|
|
14
14
|
const base_js_3 = require("../../tracers/base.cjs");
|
|
15
|
+
const embeddings_js_1 = require("../../embeddings.cjs");
|
|
15
16
|
/**
|
|
16
17
|
* Parser for comma-separated values. It splits the input text by commas
|
|
17
18
|
* and trims the resulting values.
|
|
@@ -99,16 +100,27 @@ class FakeStreamingLLM extends llms_js_1.LLM {
|
|
|
99
100
|
writable: true,
|
|
100
101
|
value: 50
|
|
101
102
|
});
|
|
103
|
+
Object.defineProperty(this, "responses", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
102
109
|
this.sleep = fields.sleep ?? this.sleep;
|
|
110
|
+
this.responses = fields.responses;
|
|
103
111
|
}
|
|
104
112
|
_llmType() {
|
|
105
113
|
return "fake";
|
|
106
114
|
}
|
|
107
115
|
async _call(prompt) {
|
|
108
|
-
|
|
116
|
+
const response = this.responses?.[0];
|
|
117
|
+
this.responses = this.responses?.slice(1);
|
|
118
|
+
return response ?? prompt;
|
|
109
119
|
}
|
|
110
120
|
async *_streamResponseChunks(input) {
|
|
111
|
-
|
|
121
|
+
const response = this.responses?.[0];
|
|
122
|
+
this.responses = this.responses?.slice(1);
|
|
123
|
+
for (const c of response ?? input) {
|
|
112
124
|
await new Promise((resolve) => setTimeout(resolve, this.sleep));
|
|
113
125
|
yield { text: c, generationInfo: {} };
|
|
114
126
|
}
|
|
@@ -395,3 +407,93 @@ class FakeTool extends tools_js_1.StructuredTool {
|
|
|
395
407
|
}
|
|
396
408
|
}
|
|
397
409
|
exports.FakeTool = FakeTool;
|
|
410
|
+
/**
|
|
411
|
+
* A class that provides fake embeddings by overriding the embedDocuments
|
|
412
|
+
* and embedQuery methods to return fixed values.
|
|
413
|
+
*/
|
|
414
|
+
class FakeEmbeddings extends embeddings_js_1.Embeddings {
|
|
415
|
+
constructor(params) {
|
|
416
|
+
super(params ?? {});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Generates fixed embeddings for a list of documents.
|
|
420
|
+
* @param documents List of documents to generate embeddings for.
|
|
421
|
+
* @returns A promise that resolves with a list of fixed embeddings for each document.
|
|
422
|
+
*/
|
|
423
|
+
embedDocuments(documents) {
|
|
424
|
+
return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Generates a fixed embedding for a query.
|
|
428
|
+
* @param _ The query to generate an embedding for.
|
|
429
|
+
* @returns A promise that resolves with a fixed embedding for the query.
|
|
430
|
+
*/
|
|
431
|
+
embedQuery(_) {
|
|
432
|
+
return Promise.resolve([0.1, 0.2, 0.3, 0.4]);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
exports.FakeEmbeddings = FakeEmbeddings;
|
|
436
|
+
/**
|
|
437
|
+
* A class that provides synthetic embeddings by overriding the
|
|
438
|
+
* embedDocuments and embedQuery methods to generate embeddings based on
|
|
439
|
+
* the input documents. The embeddings are generated by converting each
|
|
440
|
+
* document into chunks, calculating a numerical value for each chunk, and
|
|
441
|
+
* returning an array of these values as the embedding.
|
|
442
|
+
*/
|
|
443
|
+
class SyntheticEmbeddings extends embeddings_js_1.Embeddings {
|
|
444
|
+
constructor(params) {
|
|
445
|
+
super(params ?? {});
|
|
446
|
+
Object.defineProperty(this, "vectorSize", {
|
|
447
|
+
enumerable: true,
|
|
448
|
+
configurable: true,
|
|
449
|
+
writable: true,
|
|
450
|
+
value: void 0
|
|
451
|
+
});
|
|
452
|
+
this.vectorSize = params?.vectorSize ?? 4;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Generates synthetic embeddings for a list of documents.
|
|
456
|
+
* @param documents List of documents to generate embeddings for.
|
|
457
|
+
* @returns A promise that resolves with a list of synthetic embeddings for each document.
|
|
458
|
+
*/
|
|
459
|
+
async embedDocuments(documents) {
|
|
460
|
+
return Promise.all(documents.map((doc) => this.embedQuery(doc)));
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Generates a synthetic embedding for a document. The document is
|
|
464
|
+
* converted into chunks, a numerical value is calculated for each chunk,
|
|
465
|
+
* and an array of these values is returned as the embedding.
|
|
466
|
+
* @param document The document to generate an embedding for.
|
|
467
|
+
* @returns A promise that resolves with a synthetic embedding for the document.
|
|
468
|
+
*/
|
|
469
|
+
async embedQuery(document) {
|
|
470
|
+
let doc = document;
|
|
471
|
+
// Only use the letters (and space) from the document, and make them lower case
|
|
472
|
+
doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
|
|
473
|
+
// Pad the document to make sure it has a divisible number of chunks
|
|
474
|
+
const padMod = doc.length % this.vectorSize;
|
|
475
|
+
const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
|
|
476
|
+
const padSize = doc.length + padGapSize;
|
|
477
|
+
doc = doc.padEnd(padSize, " ");
|
|
478
|
+
// Break it into chunks
|
|
479
|
+
const chunkSize = doc.length / this.vectorSize;
|
|
480
|
+
const docChunk = [];
|
|
481
|
+
for (let co = 0; co < doc.length; co += chunkSize) {
|
|
482
|
+
docChunk.push(doc.slice(co, co + chunkSize));
|
|
483
|
+
}
|
|
484
|
+
// Turn each chunk into a number
|
|
485
|
+
const ret = docChunk.map((s) => {
|
|
486
|
+
let sum = 0;
|
|
487
|
+
// Get a total value by adding the value of each character in the string
|
|
488
|
+
for (let co = 0; co < s.length; co += 1) {
|
|
489
|
+
sum += s === " " ? 0 : s.charCodeAt(co);
|
|
490
|
+
}
|
|
491
|
+
// Reduce this to a number between 0 and 25 inclusive
|
|
492
|
+
// Then get the fractional number by dividing it by 26
|
|
493
|
+
const ret = (sum % 26) / 26;
|
|
494
|
+
return ret;
|
|
495
|
+
});
|
|
496
|
+
return ret;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
exports.SyntheticEmbeddings = SyntheticEmbeddings;
|
|
@@ -11,6 +11,7 @@ import { BaseRetriever } from "../../retrievers.js";
|
|
|
11
11
|
import { Runnable } from "../../runnables/base.js";
|
|
12
12
|
import { StructuredTool, ToolParams } from "../../tools.js";
|
|
13
13
|
import { BaseTracer, Run } from "../../tracers/base.js";
|
|
14
|
+
import { Embeddings, EmbeddingsParams } from "../../embeddings.js";
|
|
14
15
|
/**
|
|
15
16
|
* Parser for comma-separated values. It splits the input text by commas
|
|
16
17
|
* and trims the resulting values.
|
|
@@ -40,8 +41,10 @@ export declare class FakeLLM extends LLM {
|
|
|
40
41
|
}
|
|
41
42
|
export declare class FakeStreamingLLM extends LLM {
|
|
42
43
|
sleep?: number;
|
|
44
|
+
responses?: string[];
|
|
43
45
|
constructor(fields: {
|
|
44
46
|
sleep?: number;
|
|
47
|
+
responses?: string[];
|
|
45
48
|
} & BaseLLMParams);
|
|
46
49
|
_llmType(): string;
|
|
47
50
|
_call(prompt: string): Promise<string>;
|
|
@@ -144,3 +147,55 @@ export declare class FakeTool<T extends z.ZodObject<any, any, any, any> = z.ZodO
|
|
|
144
147
|
constructor(fields: FakeToolParams<T>);
|
|
145
148
|
protected _call(arg: z.output<T>, _runManager?: CallbackManagerForToolRun): Promise<string>;
|
|
146
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* A class that provides fake embeddings by overriding the embedDocuments
|
|
152
|
+
* and embedQuery methods to return fixed values.
|
|
153
|
+
*/
|
|
154
|
+
export declare class FakeEmbeddings extends Embeddings {
|
|
155
|
+
constructor(params?: EmbeddingsParams);
|
|
156
|
+
/**
|
|
157
|
+
* Generates fixed embeddings for a list of documents.
|
|
158
|
+
* @param documents List of documents to generate embeddings for.
|
|
159
|
+
* @returns A promise that resolves with a list of fixed embeddings for each document.
|
|
160
|
+
*/
|
|
161
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
162
|
+
/**
|
|
163
|
+
* Generates a fixed embedding for a query.
|
|
164
|
+
* @param _ The query to generate an embedding for.
|
|
165
|
+
* @returns A promise that resolves with a fixed embedding for the query.
|
|
166
|
+
*/
|
|
167
|
+
embedQuery(_: string): Promise<number[]>;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* An interface that defines additional parameters specific to the
|
|
171
|
+
* SyntheticEmbeddings class.
|
|
172
|
+
*/
|
|
173
|
+
interface SyntheticEmbeddingsParams extends EmbeddingsParams {
|
|
174
|
+
vectorSize: number;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* A class that provides synthetic embeddings by overriding the
|
|
178
|
+
* embedDocuments and embedQuery methods to generate embeddings based on
|
|
179
|
+
* the input documents. The embeddings are generated by converting each
|
|
180
|
+
* document into chunks, calculating a numerical value for each chunk, and
|
|
181
|
+
* returning an array of these values as the embedding.
|
|
182
|
+
*/
|
|
183
|
+
export declare class SyntheticEmbeddings extends Embeddings implements SyntheticEmbeddingsParams {
|
|
184
|
+
vectorSize: number;
|
|
185
|
+
constructor(params?: SyntheticEmbeddingsParams);
|
|
186
|
+
/**
|
|
187
|
+
* Generates synthetic embeddings for a list of documents.
|
|
188
|
+
* @param documents List of documents to generate embeddings for.
|
|
189
|
+
* @returns A promise that resolves with a list of synthetic embeddings for each document.
|
|
190
|
+
*/
|
|
191
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
192
|
+
/**
|
|
193
|
+
* Generates a synthetic embedding for a document. The document is
|
|
194
|
+
* converted into chunks, a numerical value is calculated for each chunk,
|
|
195
|
+
* and an array of these values is returned as the embedding.
|
|
196
|
+
* @param document The document to generate an embedding for.
|
|
197
|
+
* @returns A promise that resolves with a synthetic embedding for the document.
|
|
198
|
+
*/
|
|
199
|
+
embedQuery(document: string): Promise<number[]>;
|
|
200
|
+
}
|
|
201
|
+
export {};
|
|
@@ -9,6 +9,7 @@ import { BaseRetriever } from "../../retrievers.js";
|
|
|
9
9
|
import { Runnable } from "../../runnables/base.js";
|
|
10
10
|
import { StructuredTool } from "../../tools.js";
|
|
11
11
|
import { BaseTracer } from "../../tracers/base.js";
|
|
12
|
+
import { Embeddings } from "../../embeddings.js";
|
|
12
13
|
/**
|
|
13
14
|
* Parser for comma-separated values. It splits the input text by commas
|
|
14
15
|
* and trims the resulting values.
|
|
@@ -93,16 +94,27 @@ export class FakeStreamingLLM extends LLM {
|
|
|
93
94
|
writable: true,
|
|
94
95
|
value: 50
|
|
95
96
|
});
|
|
97
|
+
Object.defineProperty(this, "responses", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
96
103
|
this.sleep = fields.sleep ?? this.sleep;
|
|
104
|
+
this.responses = fields.responses;
|
|
97
105
|
}
|
|
98
106
|
_llmType() {
|
|
99
107
|
return "fake";
|
|
100
108
|
}
|
|
101
109
|
async _call(prompt) {
|
|
102
|
-
|
|
110
|
+
const response = this.responses?.[0];
|
|
111
|
+
this.responses = this.responses?.slice(1);
|
|
112
|
+
return response ?? prompt;
|
|
103
113
|
}
|
|
104
114
|
async *_streamResponseChunks(input) {
|
|
105
|
-
|
|
115
|
+
const response = this.responses?.[0];
|
|
116
|
+
this.responses = this.responses?.slice(1);
|
|
117
|
+
for (const c of response ?? input) {
|
|
106
118
|
await new Promise((resolve) => setTimeout(resolve, this.sleep));
|
|
107
119
|
yield { text: c, generationInfo: {} };
|
|
108
120
|
}
|
|
@@ -381,3 +393,91 @@ export class FakeTool extends StructuredTool {
|
|
|
381
393
|
return JSON.stringify(arg);
|
|
382
394
|
}
|
|
383
395
|
}
|
|
396
|
+
/**
|
|
397
|
+
* A class that provides fake embeddings by overriding the embedDocuments
|
|
398
|
+
* and embedQuery methods to return fixed values.
|
|
399
|
+
*/
|
|
400
|
+
export class FakeEmbeddings extends Embeddings {
|
|
401
|
+
constructor(params) {
|
|
402
|
+
super(params ?? {});
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Generates fixed embeddings for a list of documents.
|
|
406
|
+
* @param documents List of documents to generate embeddings for.
|
|
407
|
+
* @returns A promise that resolves with a list of fixed embeddings for each document.
|
|
408
|
+
*/
|
|
409
|
+
embedDocuments(documents) {
|
|
410
|
+
return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Generates a fixed embedding for a query.
|
|
414
|
+
* @param _ The query to generate an embedding for.
|
|
415
|
+
* @returns A promise that resolves with a fixed embedding for the query.
|
|
416
|
+
*/
|
|
417
|
+
embedQuery(_) {
|
|
418
|
+
return Promise.resolve([0.1, 0.2, 0.3, 0.4]);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* A class that provides synthetic embeddings by overriding the
|
|
423
|
+
* embedDocuments and embedQuery methods to generate embeddings based on
|
|
424
|
+
* the input documents. The embeddings are generated by converting each
|
|
425
|
+
* document into chunks, calculating a numerical value for each chunk, and
|
|
426
|
+
* returning an array of these values as the embedding.
|
|
427
|
+
*/
|
|
428
|
+
export class SyntheticEmbeddings extends Embeddings {
|
|
429
|
+
constructor(params) {
|
|
430
|
+
super(params ?? {});
|
|
431
|
+
Object.defineProperty(this, "vectorSize", {
|
|
432
|
+
enumerable: true,
|
|
433
|
+
configurable: true,
|
|
434
|
+
writable: true,
|
|
435
|
+
value: void 0
|
|
436
|
+
});
|
|
437
|
+
this.vectorSize = params?.vectorSize ?? 4;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Generates synthetic embeddings for a list of documents.
|
|
441
|
+
* @param documents List of documents to generate embeddings for.
|
|
442
|
+
* @returns A promise that resolves with a list of synthetic embeddings for each document.
|
|
443
|
+
*/
|
|
444
|
+
async embedDocuments(documents) {
|
|
445
|
+
return Promise.all(documents.map((doc) => this.embedQuery(doc)));
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Generates a synthetic embedding for a document. The document is
|
|
449
|
+
* converted into chunks, a numerical value is calculated for each chunk,
|
|
450
|
+
* and an array of these values is returned as the embedding.
|
|
451
|
+
* @param document The document to generate an embedding for.
|
|
452
|
+
* @returns A promise that resolves with a synthetic embedding for the document.
|
|
453
|
+
*/
|
|
454
|
+
async embedQuery(document) {
|
|
455
|
+
let doc = document;
|
|
456
|
+
// Only use the letters (and space) from the document, and make them lower case
|
|
457
|
+
doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
|
|
458
|
+
// Pad the document to make sure it has a divisible number of chunks
|
|
459
|
+
const padMod = doc.length % this.vectorSize;
|
|
460
|
+
const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
|
|
461
|
+
const padSize = doc.length + padGapSize;
|
|
462
|
+
doc = doc.padEnd(padSize, " ");
|
|
463
|
+
// Break it into chunks
|
|
464
|
+
const chunkSize = doc.length / this.vectorSize;
|
|
465
|
+
const docChunk = [];
|
|
466
|
+
for (let co = 0; co < doc.length; co += chunkSize) {
|
|
467
|
+
docChunk.push(doc.slice(co, co + chunkSize));
|
|
468
|
+
}
|
|
469
|
+
// Turn each chunk into a number
|
|
470
|
+
const ret = docChunk.map((s) => {
|
|
471
|
+
let sum = 0;
|
|
472
|
+
// Get a total value by adding the value of each character in the string
|
|
473
|
+
for (let co = 0; co < s.length; co += 1) {
|
|
474
|
+
sum += s === " " ? 0 : s.charCodeAt(co);
|
|
475
|
+
}
|
|
476
|
+
// Reduce this to a number between 0 and 25 inclusive
|
|
477
|
+
// Then get the fractional number by dividing it by 26
|
|
478
|
+
const ret = (sum % 26) / 26;
|
|
479
|
+
return ret;
|
|
480
|
+
});
|
|
481
|
+
return ret;
|
|
482
|
+
}
|
|
483
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs",
|
|
19
19
|
"build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch",
|
|
20
20
|
"build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
|
|
21
|
-
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src",
|
|
21
|
+
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
|
|
22
22
|
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
23
23
|
"lint": "yarn lint:eslint && yarn lint:dpdm",
|
|
24
24
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"p-queue": "^6.6.2",
|
|
44
44
|
"p-retry": "4",
|
|
45
45
|
"uuid": "^9.0.0",
|
|
46
|
-
"zod": "^3.22.
|
|
47
|
-
"zod-to-json-schema": "3.
|
|
46
|
+
"zod": "^3.22.4",
|
|
47
|
+
"zod-to-json-schema": "^3.22.3"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@jest/globals": "^29.5.0",
|