@langchain/google-genai 0.0.8 → 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.
package/README.md CHANGED
@@ -160,4 +160,4 @@ yarn lint && yarn format
160
160
 
161
161
  ### Adding new entrypoints
162
162
 
163
- If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to `scripts/create-entrypoints.js` and run `yarn build` to generate the new entrypoint.
163
+ If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to the `entrypoints` field in the `config` variable located inside `langchain.config.js` and run `yarn build` to generate the new entrypoint.
@@ -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/index.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/index.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-genai",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Sample integration for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -12,25 +12,29 @@
12
12
  "type": "git",
13
13
  "url": "git@github.com:langchain-ai/langchainjs.git"
14
14
  },
15
+ "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-google-genai/",
15
16
  "scripts": {
16
17
  "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts",
17
18
  "build:deps": "yarn run turbo:command build --filter=@langchain/core",
18
19
  "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rm -rf dist/tests dist/**/tests",
19
- "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rm -rf dist-cjs",
20
- "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch",
21
- "build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
20
+ "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rm -rf dist-cjs",
21
+ "build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch",
22
+ "build:scripts": "yarn create-entrypoints && yarn check-tree-shaking",
22
23
  "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
23
24
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
24
25
  "lint": "yarn lint:eslint && yarn lint:dpdm",
25
26
  "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
26
- "clean": "rm -rf dist/ && NODE_OPTIONS=--max-old-space-size=4096 node scripts/create-entrypoints.js pre",
27
+ "clean": "rm -rf dist/ && NODE_OPTIONS=--max-old-space-size=4096 yarn lc-build --config ./langchain.config.js --create-entrypoints --pre",
27
28
  "prepack": "yarn build",
28
29
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
29
30
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
30
31
  "test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
31
32
  "test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
32
- "format": "prettier --config .prettierrc --write \"src\" \"scripts\"",
33
- "format:check": "prettier --config .prettierrc --check \"src\" \"scripts\""
33
+ "format": "prettier --config .prettierrc --write \"src\"",
34
+ "format:check": "prettier --config .prettierrc --check \"src\"",
35
+ "move-cjs-to-dist": "yarn lc-build --config ./langchain.config.js --move-cjs-dist",
36
+ "create-entrypoints": "yarn lc-build --config ./langchain.config.js --create-entrypoints",
37
+ "check-tree-shaking": "yarn lc-build --config ./langchain.config.js --tree-shaking"
34
38
  },
35
39
  "author": "LangChain",
36
40
  "license": "MIT",
@@ -40,7 +44,7 @@
40
44
  },
41
45
  "devDependencies": {
42
46
  "@jest/globals": "^29.5.0",
43
- "@langchain/community": "workspace:*",
47
+ "@langchain/scripts": "~0.0",
44
48
  "@swc/core": "^1.3.90",
45
49
  "@swc/jest": "^0.2.29",
46
50
  "@tsconfig/recommended": "^1.0.3",
@@ -68,7 +72,11 @@
68
72
  },
69
73
  "exports": {
70
74
  ".": {
71
- "types": "./index.d.ts",
75
+ "types": {
76
+ "import": "./index.d.ts",
77
+ "require": "./index.d.cts",
78
+ "default": "./index.d.ts"
79
+ },
72
80
  "import": "./index.js",
73
81
  "require": "./index.cjs"
74
82
  },
@@ -78,6 +86,7 @@
78
86
  "dist/",
79
87
  "index.cjs",
80
88
  "index.js",
81
- "index.d.ts"
89
+ "index.d.ts",
90
+ "index.d.cts"
82
91
  ]
83
92
  }