@langchain/core 0.1.15 → 0.1.17-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.
@@ -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.
@@ -99,18 +100,31 @@ export class FakeStreamingLLM extends LLM {
99
100
  writable: true,
100
101
  value: void 0
101
102
  });
103
+ Object.defineProperty(this, "thrownErrorString", {
104
+ enumerable: true,
105
+ configurable: true,
106
+ writable: true,
107
+ value: void 0
108
+ });
102
109
  this.sleep = fields.sleep ?? this.sleep;
103
110
  this.responses = fields.responses;
111
+ this.thrownErrorString = fields.thrownErrorString;
104
112
  }
105
113
  _llmType() {
106
114
  return "fake";
107
115
  }
108
116
  async _call(prompt) {
117
+ if (this.thrownErrorString) {
118
+ throw new Error(this.thrownErrorString);
119
+ }
109
120
  const response = this.responses?.[0];
110
121
  this.responses = this.responses?.slice(1);
111
122
  return response ?? prompt;
112
123
  }
113
124
  async *_streamResponseChunks(input) {
125
+ if (this.thrownErrorString) {
126
+ throw new Error(this.thrownErrorString);
127
+ }
114
128
  const response = this.responses?.[0];
115
129
  this.responses = this.responses?.slice(1);
116
130
  for (const c of response ?? input) {
@@ -392,3 +406,91 @@ export class FakeTool extends StructuredTool {
392
406
  return JSON.stringify(arg);
393
407
  }
394
408
  }
409
+ /**
410
+ * A class that provides fake embeddings by overriding the embedDocuments
411
+ * and embedQuery methods to return fixed values.
412
+ */
413
+ export class FakeEmbeddings extends Embeddings {
414
+ constructor(params) {
415
+ super(params ?? {});
416
+ }
417
+ /**
418
+ * Generates fixed embeddings for a list of documents.
419
+ * @param documents List of documents to generate embeddings for.
420
+ * @returns A promise that resolves with a list of fixed embeddings for each document.
421
+ */
422
+ embedDocuments(documents) {
423
+ return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));
424
+ }
425
+ /**
426
+ * Generates a fixed embedding for a query.
427
+ * @param _ The query to generate an embedding for.
428
+ * @returns A promise that resolves with a fixed embedding for the query.
429
+ */
430
+ embedQuery(_) {
431
+ return Promise.resolve([0.1, 0.2, 0.3, 0.4]);
432
+ }
433
+ }
434
+ /**
435
+ * A class that provides synthetic embeddings by overriding the
436
+ * embedDocuments and embedQuery methods to generate embeddings based on
437
+ * the input documents. The embeddings are generated by converting each
438
+ * document into chunks, calculating a numerical value for each chunk, and
439
+ * returning an array of these values as the embedding.
440
+ */
441
+ export class SyntheticEmbeddings extends Embeddings {
442
+ constructor(params) {
443
+ super(params ?? {});
444
+ Object.defineProperty(this, "vectorSize", {
445
+ enumerable: true,
446
+ configurable: true,
447
+ writable: true,
448
+ value: void 0
449
+ });
450
+ this.vectorSize = params?.vectorSize ?? 4;
451
+ }
452
+ /**
453
+ * Generates synthetic embeddings for a list of documents.
454
+ * @param documents List of documents to generate embeddings for.
455
+ * @returns A promise that resolves with a list of synthetic embeddings for each document.
456
+ */
457
+ async embedDocuments(documents) {
458
+ return Promise.all(documents.map((doc) => this.embedQuery(doc)));
459
+ }
460
+ /**
461
+ * Generates a synthetic embedding for a document. The document is
462
+ * converted into chunks, a numerical value is calculated for each chunk,
463
+ * and an array of these values is returned as the embedding.
464
+ * @param document The document to generate an embedding for.
465
+ * @returns A promise that resolves with a synthetic embedding for the document.
466
+ */
467
+ async embedQuery(document) {
468
+ let doc = document;
469
+ // Only use the letters (and space) from the document, and make them lower case
470
+ doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
471
+ // Pad the document to make sure it has a divisible number of chunks
472
+ const padMod = doc.length % this.vectorSize;
473
+ const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
474
+ const padSize = doc.length + padGapSize;
475
+ doc = doc.padEnd(padSize, " ");
476
+ // Break it into chunks
477
+ const chunkSize = doc.length / this.vectorSize;
478
+ const docChunk = [];
479
+ for (let co = 0; co < doc.length; co += chunkSize) {
480
+ docChunk.push(doc.slice(co, co + chunkSize));
481
+ }
482
+ // Turn each chunk into a number
483
+ const ret = docChunk.map((s) => {
484
+ let sum = 0;
485
+ // Get a total value by adding the value of each character in the string
486
+ for (let co = 0; co < s.length; co += 1) {
487
+ sum += s === " " ? 0 : s.charCodeAt(co);
488
+ }
489
+ // Reduce this to a number between 0 and 25 inclusive
490
+ // Then get the fractional number by dividing it by 26
491
+ const ret = (sum % 26) / 26;
492
+ return ret;
493
+ });
494
+ return ret;
495
+ }
496
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.15",
3
+ "version": "0.1.17-rc.0",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -28,8 +28,8 @@
28
28
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
29
29
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
30
30
  "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
31
- "format": "prettier --write \"src\"",
32
- "format:check": "prettier --check \"src\""
31
+ "format": "prettier --write \"src\" \"scripts\"",
32
+ "format:check": "prettier --check \"src\" \"scripts\""
33
33
  },
34
34
  "author": "LangChain",
35
35
  "license": "MIT",
@@ -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.3",
47
- "zod-to-json-schema": "3.20.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",