@langchain/google-genai 0.0.9 → 0.0.10

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.
@@ -4,6 +4,7 @@ exports.GoogleGenerativeAIEmbeddings = void 0;
4
4
  const generative_ai_1 = require("@google/generative-ai");
5
5
  const env_1 = require("@langchain/core/utils/env");
6
6
  const embeddings_1 = require("@langchain/core/embeddings");
7
+ const chunk_array_1 = require("@langchain/core/utils/chunk_array");
7
8
  /**
8
9
  * Class that extends the Embeddings class and provides methods for
9
10
  * generating embeddings using the Google Palm API.
@@ -58,6 +59,12 @@ class GoogleGenerativeAIEmbeddings extends embeddings_1.Embeddings {
58
59
  writable: true,
59
60
  value: true
60
61
  });
62
+ Object.defineProperty(this, "maxBatchSize", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: 100
67
+ }); // Max batch size for embedDocuments set by GenerativeModel client's batchEmbedContents call
61
68
  Object.defineProperty(this, "client", {
62
69
  enumerable: true,
63
70
  configurable: true,
@@ -96,11 +103,20 @@ class GoogleGenerativeAIEmbeddings extends embeddings_1.Embeddings {
96
103
  return res.embedding.values ?? [];
97
104
  }
98
105
  async _embedDocumentsContent(documents) {
99
- const req = {
100
- requests: documents.map((doc) => this._convertToContent(doc)),
101
- };
102
- const res = await this.client.batchEmbedContents(req);
103
- return res.embeddings.map((e) => e.values || []) ?? [];
106
+ const batchEmbedChunks = (0, chunk_array_1.chunkArray)(documents, this.maxBatchSize);
107
+ const batchEmbedRequests = batchEmbedChunks.map((chunk) => ({
108
+ requests: chunk.map((doc) => this._convertToContent(doc)),
109
+ }));
110
+ const responses = await Promise.allSettled(batchEmbedRequests.map((req) => this.client.batchEmbedContents(req)));
111
+ const embeddings = responses.flatMap((res, idx) => {
112
+ if (res.status === "fulfilled") {
113
+ return res.value.embeddings.map((e) => e.values || []);
114
+ }
115
+ else {
116
+ return Array(batchEmbedChunks[idx].length).fill([]);
117
+ }
118
+ });
119
+ return embeddings;
104
120
  }
105
121
  /**
106
122
  * Method that takes a document as input and returns a promise that
@@ -60,6 +60,7 @@ export declare class GoogleGenerativeAIEmbeddings extends Embeddings implements
60
60
  taskType?: TaskType;
61
61
  title?: string;
62
62
  stripNewLines: boolean;
63
+ maxBatchSize: number;
63
64
  private client;
64
65
  constructor(fields?: GoogleGenerativeAIEmbeddingsParams);
65
66
  private _convertToContent;
@@ -1,6 +1,7 @@
1
1
  import { GoogleGenerativeAI } from "@google/generative-ai";
2
2
  import { getEnvironmentVariable } from "@langchain/core/utils/env";
3
3
  import { Embeddings } from "@langchain/core/embeddings";
4
+ import { chunkArray } from "@langchain/core/utils/chunk_array";
4
5
  /**
5
6
  * Class that extends the Embeddings class and provides methods for
6
7
  * generating embeddings using the Google Palm API.
@@ -55,6 +56,12 @@ export class GoogleGenerativeAIEmbeddings extends Embeddings {
55
56
  writable: true,
56
57
  value: true
57
58
  });
59
+ Object.defineProperty(this, "maxBatchSize", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: 100
64
+ }); // Max batch size for embedDocuments set by GenerativeModel client's batchEmbedContents call
58
65
  Object.defineProperty(this, "client", {
59
66
  enumerable: true,
60
67
  configurable: true,
@@ -93,11 +100,20 @@ export class GoogleGenerativeAIEmbeddings extends Embeddings {
93
100
  return res.embedding.values ?? [];
94
101
  }
95
102
  async _embedDocumentsContent(documents) {
96
- const req = {
97
- requests: documents.map((doc) => this._convertToContent(doc)),
98
- };
99
- const res = await this.client.batchEmbedContents(req);
100
- return res.embeddings.map((e) => e.values || []) ?? [];
103
+ const batchEmbedChunks = chunkArray(documents, this.maxBatchSize);
104
+ const batchEmbedRequests = batchEmbedChunks.map((chunk) => ({
105
+ requests: chunk.map((doc) => this._convertToContent(doc)),
106
+ }));
107
+ const responses = await Promise.allSettled(batchEmbedRequests.map((req) => this.client.batchEmbedContents(req)));
108
+ const embeddings = responses.flatMap((res, idx) => {
109
+ if (res.status === "fulfilled") {
110
+ return res.value.embeddings.map((e) => e.values || []);
111
+ }
112
+ else {
113
+ return Array(batchEmbedChunks[idx].length).fill([]);
114
+ }
115
+ });
116
+ return embeddings;
101
117
  }
102
118
  /**
103
119
  * Method that takes a document as input and returns a promise that
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-genai",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "Sample integration for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -24,7 +24,7 @@
24
24
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
25
25
  "lint": "yarn lint:eslint && yarn lint:dpdm",
26
26
  "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
27
- "clean": "rm -rf dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn create-entrypoints -- --pre",
27
+ "clean": "rm -rf dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre",
28
28
  "prepack": "yarn build",
29
29
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
30
30
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",