@langchain/google-common 0.2.18 → 1.0.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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +6 -6
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/auth.cjs +82 -116
- package/dist/auth.cjs.map +1 -0
- package/dist/auth.d.cts +46 -0
- package/dist/auth.d.cts.map +1 -0
- package/dist/auth.d.ts +41 -36
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +80 -110
- package/dist/auth.js.map +1 -0
- package/dist/chat_models.cjs +251 -466
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +98 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +87 -73
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +245 -457
- package/dist/chat_models.js.map +1 -0
- package/dist/connection.cjs +321 -466
- package/dist/connection.cjs.map +1 -0
- package/dist/connection.d.cts +109 -0
- package/dist/connection.d.cts.map +1 -0
- package/dist/connection.d.ts +98 -91
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +317 -459
- package/dist/connection.js.map +1 -0
- package/dist/embeddings.cjs +135 -186
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +44 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +38 -32
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +133 -181
- package/dist/embeddings.js.map +1 -0
- package/dist/experimental/media.cjs +380 -482
- package/dist/experimental/media.cjs.map +1 -0
- package/dist/experimental/media.d.cts +198 -0
- package/dist/experimental/media.d.cts.map +1 -0
- package/dist/experimental/media.d.ts +190 -202
- package/dist/experimental/media.d.ts.map +1 -0
- package/dist/experimental/media.js +369 -468
- package/dist/experimental/media.js.map +1 -0
- package/dist/experimental/utils/media_core.cjs +403 -517
- package/dist/experimental/utils/media_core.cjs.map +1 -0
- package/dist/experimental/utils/media_core.d.cts +215 -0
- package/dist/experimental/utils/media_core.d.cts.map +1 -0
- package/dist/experimental/utils/media_core.d.ts +171 -165
- package/dist/experimental/utils/media_core.d.ts.map +1 -0
- package/dist/experimental/utils/media_core.js +395 -506
- package/dist/experimental/utils/media_core.js.map +1 -0
- package/dist/index.cjs +58 -27
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -11
- package/dist/index.js +13 -11
- package/dist/llms.cjs +157 -244
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +72 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +64 -54
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +154 -238
- package/dist/llms.js.map +1 -0
- package/dist/output_parsers.cjs +148 -173
- package/dist/output_parsers.cjs.map +1 -0
- package/dist/output_parsers.d.cts +53 -0
- package/dist/output_parsers.d.cts.map +1 -0
- package/dist/output_parsers.d.ts +46 -42
- package/dist/output_parsers.d.ts.map +1 -0
- package/dist/output_parsers.js +146 -168
- package/dist/output_parsers.js.map +1 -0
- package/dist/types-anthropic.d.cts +229 -0
- package/dist/types-anthropic.d.cts.map +1 -0
- package/dist/types-anthropic.d.ts +221 -215
- package/dist/types-anthropic.d.ts.map +1 -0
- package/dist/types.cjs +51 -62
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +748 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +669 -656
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +46 -45
- package/dist/types.js.map +1 -0
- package/dist/utils/anthropic.cjs +598 -821
- package/dist/utils/anthropic.cjs.map +1 -0
- package/dist/utils/anthropic.js +597 -818
- package/dist/utils/anthropic.js.map +1 -0
- package/dist/utils/common.cjs +130 -211
- package/dist/utils/common.cjs.map +1 -0
- package/dist/utils/common.d.cts +13 -0
- package/dist/utils/common.d.cts.map +1 -0
- package/dist/utils/common.d.ts +12 -7
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.js +128 -207
- package/dist/utils/common.js.map +1 -0
- package/dist/utils/failed_handler.cjs +28 -30
- package/dist/utils/failed_handler.cjs.map +1 -0
- package/dist/utils/failed_handler.d.cts +9 -0
- package/dist/utils/failed_handler.d.cts.map +1 -0
- package/dist/utils/failed_handler.d.ts +8 -2
- package/dist/utils/failed_handler.d.ts.map +1 -0
- package/dist/utils/failed_handler.js +28 -28
- package/dist/utils/failed_handler.js.map +1 -0
- package/dist/utils/gemini.cjs +1020 -1488
- package/dist/utils/gemini.cjs.map +1 -0
- package/dist/utils/gemini.d.cts +51 -0
- package/dist/utils/gemini.d.cts.map +1 -0
- package/dist/utils/gemini.d.ts +51 -48
- package/dist/utils/gemini.d.ts.map +1 -0
- package/dist/utils/gemini.js +1015 -1479
- package/dist/utils/gemini.js.map +1 -0
- package/dist/utils/index.cjs +38 -23
- package/dist/utils/index.d.cts +8 -0
- package/dist/utils/index.d.ts +8 -7
- package/dist/utils/index.js +8 -7
- package/dist/utils/palm.d.cts +11 -0
- package/dist/utils/palm.d.cts.map +1 -0
- package/dist/utils/palm.d.ts +9 -4
- package/dist/utils/palm.d.ts.map +1 -0
- package/dist/utils/safety.cjs +13 -22
- package/dist/utils/safety.cjs.map +1 -0
- package/dist/utils/safety.d.cts +12 -0
- package/dist/utils/safety.d.cts.map +1 -0
- package/dist/utils/safety.d.ts +10 -4
- package/dist/utils/safety.d.ts.map +1 -0
- package/dist/utils/safety.js +13 -19
- package/dist/utils/safety.js.map +1 -0
- package/dist/utils/stream.cjs +296 -475
- package/dist/utils/stream.cjs.map +1 -0
- package/dist/utils/stream.d.cts +165 -0
- package/dist/utils/stream.d.cts.map +1 -0
- package/dist/utils/stream.d.ts +156 -131
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/stream.js +293 -469
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
- package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
- package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.js +40 -76
- package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
- package/package.json +69 -85
- package/dist/types-anthropic.cjs +0 -2
- package/dist/types-anthropic.js +0 -1
- package/dist/utils/anthropic.d.ts +0 -4
- package/dist/utils/palm.cjs +0 -2
- package/dist/utils/palm.js +0 -1
- package/experimental/media.cjs +0 -1
- package/experimental/media.d.cts +0 -1
- package/experimental/media.d.ts +0 -1
- package/experimental/media.js +0 -1
- package/experimental/utils/media_core.cjs +0 -1
- package/experimental/utils/media_core.d.cts +0 -1
- package/experimental/utils/media_core.d.ts +0 -1
- package/experimental/utils/media_core.js +0 -1
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/types.cjs +0 -1
- package/types.d.cts +0 -1
- package/types.d.ts +0 -1
- package/types.js +0 -1
- package/utils.cjs +0 -1
- package/utils.d.cts +0 -1
- package/utils.d.ts +0 -1
- package/utils.js +0 -1
package/dist/embeddings.js
CHANGED
|
@@ -1,184 +1,136 @@
|
|
|
1
|
-
import { Embeddings } from "@langchain/core/embeddings";
|
|
2
|
-
import { chunkArray } from "@langchain/core/utils/chunk_array";
|
|
3
|
-
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
4
1
|
import { GoogleAIConnection } from "./connection.js";
|
|
5
2
|
import { ApiKeyGoogleAuth } from "./auth.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
instances: input,
|
|
60
|
-
parameters,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
async formatData(input, parameters) {
|
|
64
|
-
switch (this.platform) {
|
|
65
|
-
case "gcp":
|
|
66
|
-
return this.formatDataVertex(input, parameters);
|
|
67
|
-
case "gai":
|
|
68
|
-
return this.formatDataAiStudio(input, parameters);
|
|
69
|
-
default:
|
|
70
|
-
throw new Error(`Unknown platform to format embeddings ${this.platform}`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
3
|
+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
4
|
+
import { Embeddings } from "@langchain/core/embeddings";
|
|
5
|
+
import { chunkArray } from "@langchain/core/utils/chunk_array";
|
|
6
|
+
|
|
7
|
+
//#region src/embeddings.ts
|
|
8
|
+
var EmbeddingsConnection = class extends GoogleAIConnection {
|
|
9
|
+
convertSystemMessageToHumanContent;
|
|
10
|
+
constructor(fields, caller, client, streaming) {
|
|
11
|
+
super(fields, caller, client, streaming);
|
|
12
|
+
}
|
|
13
|
+
buildUrlMethodAiStudio() {
|
|
14
|
+
return "embedContent";
|
|
15
|
+
}
|
|
16
|
+
buildUrlMethodVertex() {
|
|
17
|
+
return "predict";
|
|
18
|
+
}
|
|
19
|
+
async buildUrlMethod() {
|
|
20
|
+
switch (this.platform) {
|
|
21
|
+
case "gcp": return this.buildUrlMethodVertex();
|
|
22
|
+
case "gai": return this.buildUrlMethodAiStudio();
|
|
23
|
+
default: throw new Error(`Unknown platform when building method: ${this.platform}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
get modelPublisher() {
|
|
27
|
+
return "google";
|
|
28
|
+
}
|
|
29
|
+
formatDataAiStudio(input, parameters) {
|
|
30
|
+
const parts = input.map((instance) => ({ text: instance.content }));
|
|
31
|
+
const content = { parts };
|
|
32
|
+
const outputDimensionality = parameters?.outputDimensionality;
|
|
33
|
+
const ret = {
|
|
34
|
+
content,
|
|
35
|
+
outputDimensionality
|
|
36
|
+
};
|
|
37
|
+
let key;
|
|
38
|
+
for (key in ret) if (ret[key] === void 0) delete ret[key];
|
|
39
|
+
return ret;
|
|
40
|
+
}
|
|
41
|
+
formatDataVertex(input, parameters) {
|
|
42
|
+
return {
|
|
43
|
+
instances: input,
|
|
44
|
+
parameters
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async formatData(input, parameters) {
|
|
48
|
+
switch (this.platform) {
|
|
49
|
+
case "gcp": return this.formatDataVertex(input, parameters);
|
|
50
|
+
case "gai": return this.formatDataAiStudio(input, parameters);
|
|
51
|
+
default: throw new Error(`Unknown platform to format embeddings ${this.platform}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
74
55
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
*/
|
|
157
|
-
async embedDocuments(documents) {
|
|
158
|
-
// Vertex "text-" models could do up 5 documents at once,
|
|
159
|
-
// but the "gemini-embedding-001" can only do 1.
|
|
160
|
-
// AI Studio can only do a chunk size of 1.
|
|
161
|
-
// TODO: Make this configurable
|
|
162
|
-
const chunkSize = 1;
|
|
163
|
-
const instanceChunks = chunkArray(documents.map((document) => ({
|
|
164
|
-
content: document,
|
|
165
|
-
})), chunkSize);
|
|
166
|
-
const parameters = this.buildParameters();
|
|
167
|
-
const options = {};
|
|
168
|
-
const responses = await Promise.all(instanceChunks.map((instances) => this.connection.request(instances, parameters, options)));
|
|
169
|
-
const result = responses?.map((response) => this.responseToValues(response)).flat() ??
|
|
170
|
-
[];
|
|
171
|
-
return result;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Takes a document as input and returns a promise that resolves to an
|
|
175
|
-
* embedding for the document. It calls the embedDocuments method with the
|
|
176
|
-
* document as the input.
|
|
177
|
-
* @param document A document to be embedded.
|
|
178
|
-
* @returns A promise that resolves to an embedding for the document.
|
|
179
|
-
*/
|
|
180
|
-
async embedQuery(document) {
|
|
181
|
-
const data = await this.embedDocuments([document]);
|
|
182
|
-
return data[0];
|
|
183
|
-
}
|
|
184
|
-
}
|
|
56
|
+
* Enables calls to Google APIs for generating
|
|
57
|
+
* text embeddings.
|
|
58
|
+
*/
|
|
59
|
+
var BaseGoogleEmbeddings = class extends Embeddings {
|
|
60
|
+
model;
|
|
61
|
+
dimensions;
|
|
62
|
+
connection;
|
|
63
|
+
constructor(fields) {
|
|
64
|
+
super(fields);
|
|
65
|
+
this.model = fields.model;
|
|
66
|
+
this.dimensions = fields.dimensions ?? fields.outputDimensionality;
|
|
67
|
+
this.connection = new EmbeddingsConnection({
|
|
68
|
+
...fields,
|
|
69
|
+
...this
|
|
70
|
+
}, this.caller, this.buildClient(fields), false);
|
|
71
|
+
}
|
|
72
|
+
buildApiKeyClient(apiKey) {
|
|
73
|
+
return new ApiKeyGoogleAuth(apiKey);
|
|
74
|
+
}
|
|
75
|
+
buildApiKey(fields) {
|
|
76
|
+
return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
|
|
77
|
+
}
|
|
78
|
+
buildClient(fields) {
|
|
79
|
+
const apiKey = this.buildApiKey(fields);
|
|
80
|
+
if (apiKey) return this.buildApiKeyClient(apiKey);
|
|
81
|
+
else return this.buildAbstractedClient(fields);
|
|
82
|
+
}
|
|
83
|
+
buildParameters() {
|
|
84
|
+
const ret = { outputDimensionality: this.dimensions };
|
|
85
|
+
let key;
|
|
86
|
+
for (key in ret) if (ret[key] === void 0) delete ret[key];
|
|
87
|
+
return ret;
|
|
88
|
+
}
|
|
89
|
+
vertexResponseToValues(response) {
|
|
90
|
+
const predictions = response?.data?.predictions ?? [];
|
|
91
|
+
return predictions.map((prediction) => prediction.embeddings.values);
|
|
92
|
+
}
|
|
93
|
+
aiStudioResponseToValues(response) {
|
|
94
|
+
const value = response?.data?.embedding?.values ?? [];
|
|
95
|
+
return [value];
|
|
96
|
+
}
|
|
97
|
+
responseToValues(response) {
|
|
98
|
+
switch (this.connection.platform) {
|
|
99
|
+
case "gcp": return this.vertexResponseToValues(response);
|
|
100
|
+
case "gai": return this.aiStudioResponseToValues(response);
|
|
101
|
+
default: throw new Error(`Unknown response platform: ${this.connection.platform}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Takes an array of documents as input and returns a promise that
|
|
106
|
+
* resolves to a 2D array of embeddings for each document. It splits the
|
|
107
|
+
* documents into chunks and makes requests to the Google Vertex AI API to
|
|
108
|
+
* generate embeddings.
|
|
109
|
+
* @param documents An array of documents to be embedded.
|
|
110
|
+
* @returns A promise that resolves to a 2D array of embeddings for each document.
|
|
111
|
+
*/
|
|
112
|
+
async embedDocuments(documents) {
|
|
113
|
+
const chunkSize = 1;
|
|
114
|
+
const instanceChunks = chunkArray(documents.map((document) => ({ content: document })), chunkSize);
|
|
115
|
+
const parameters = this.buildParameters();
|
|
116
|
+
const options = {};
|
|
117
|
+
const responses = await Promise.all(instanceChunks.map((instances) => this.connection.request(instances, parameters, options)));
|
|
118
|
+
const result = responses?.map((response) => this.responseToValues(response)).flat() ?? [];
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Takes a document as input and returns a promise that resolves to an
|
|
123
|
+
* embedding for the document. It calls the embedDocuments method with the
|
|
124
|
+
* document as the input.
|
|
125
|
+
* @param document A document to be embedded.
|
|
126
|
+
* @returns A promise that resolves to an embedding for the document.
|
|
127
|
+
*/
|
|
128
|
+
async embedQuery(document) {
|
|
129
|
+
const data = await this.embedDocuments([document]);
|
|
130
|
+
return data[0];
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
export { BaseGoogleEmbeddings };
|
|
136
|
+
//# sourceMappingURL=embeddings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embeddings.js","names":["fields: BaseGoogleEmbeddingsParams<AuthOptions> | undefined","caller: AsyncCaller","client: GoogleAbstractedClient","streaming: boolean","input: VertexEmbeddingsInstance[]","parameters: VertexEmbeddingsParameters","parts: GeminiPartText[]","instance: VertexEmbeddingsInstance","ret: AIStudioEmbeddingsRequest","key: keyof AIStudioEmbeddingsRequest","fields: BaseGoogleEmbeddingsParams<AuthOptions>","apiKey: string","fields?: GoogleConnectionParams<AuthOptions>","ret: VertexEmbeddingsParameters","key: keyof VertexEmbeddingsParameters","response: VertexEmbeddingsResponse","predictions: VertexEmbeddingsResponsePrediction[]","prediction: VertexEmbeddingsResponsePrediction","response: AIStudioEmbeddingsResponse","value: number[]","response: GoogleEmbeddingsResponse","documents: string[]","instanceChunks: VertexEmbeddingsInstance[][]","result: number[][]","document: string"],"sources":["../src/embeddings.ts"],"sourcesContent":["import { Embeddings } from \"@langchain/core/embeddings\";\nimport {\n AsyncCaller,\n AsyncCallerCallOptions,\n} from \"@langchain/core/utils/async_caller\";\nimport { chunkArray } from \"@langchain/core/utils/chunk_array\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n\nimport { GoogleAIConnection } from \"./connection.js\";\nimport { ApiKeyGoogleAuth, GoogleAbstractedClient } from \"./auth.js\";\nimport {\n BaseGoogleEmbeddingsOptions,\n BaseGoogleEmbeddingsParams,\n GoogleConnectionParams,\n VertexEmbeddingsInstance,\n GoogleEmbeddingsResponse,\n VertexEmbeddingsParameters,\n GoogleEmbeddingsRequest,\n VertexEmbeddingsResponse,\n AIStudioEmbeddingsResponse,\n VertexEmbeddingsResponsePrediction,\n AIStudioEmbeddingsRequest,\n GeminiPartText,\n VertexEmbeddingsRequest,\n} from \"./types.js\";\n\nclass EmbeddingsConnection<\n CallOptions extends AsyncCallerCallOptions,\n AuthOptions\n> extends GoogleAIConnection<\n CallOptions,\n VertexEmbeddingsInstance[],\n AuthOptions,\n GoogleEmbeddingsResponse\n> {\n convertSystemMessageToHumanContent: boolean | undefined;\n\n constructor(\n fields: BaseGoogleEmbeddingsParams<AuthOptions> | undefined,\n caller: AsyncCaller,\n client: GoogleAbstractedClient,\n streaming: boolean\n ) {\n super(fields, caller, client, streaming);\n }\n\n buildUrlMethodAiStudio(): string {\n return \"embedContent\";\n }\n\n buildUrlMethodVertex(): string {\n return \"predict\";\n }\n\n async buildUrlMethod(): Promise<string> {\n switch (this.platform) {\n case \"gcp\":\n return this.buildUrlMethodVertex();\n case \"gai\":\n return this.buildUrlMethodAiStudio();\n default:\n throw new Error(\n `Unknown platform when building method: ${this.platform}`\n );\n }\n }\n\n get modelPublisher(): string {\n // All the embedding models are currently published by \"google\"\n return \"google\";\n }\n\n formatDataAiStudio(\n input: VertexEmbeddingsInstance[],\n parameters: VertexEmbeddingsParameters\n ): AIStudioEmbeddingsRequest {\n const parts: GeminiPartText[] = input.map(\n (instance: VertexEmbeddingsInstance) => ({\n text: instance.content,\n })\n );\n const content = {\n parts,\n };\n const outputDimensionality = parameters?.outputDimensionality;\n\n const ret: AIStudioEmbeddingsRequest = {\n content,\n outputDimensionality,\n };\n\n // Remove undefined attributes\n let key: keyof AIStudioEmbeddingsRequest;\n for (key in ret) {\n if (ret[key] === undefined) {\n delete ret[key];\n }\n }\n\n return ret;\n }\n\n formatDataVertex(\n input: VertexEmbeddingsInstance[],\n parameters: VertexEmbeddingsParameters\n ): VertexEmbeddingsRequest {\n return {\n instances: input,\n parameters,\n };\n }\n\n async formatData(\n input: VertexEmbeddingsInstance[],\n parameters: VertexEmbeddingsParameters\n ): Promise<GoogleEmbeddingsRequest> {\n switch (this.platform) {\n case \"gcp\":\n return this.formatDataVertex(input, parameters);\n case \"gai\":\n return this.formatDataAiStudio(input, parameters);\n default:\n throw new Error(\n `Unknown platform to format embeddings ${this.platform}`\n );\n }\n }\n}\n\n/**\n * Enables calls to Google APIs for generating\n * text embeddings.\n */\nexport abstract class BaseGoogleEmbeddings<AuthOptions>\n extends Embeddings\n implements BaseGoogleEmbeddingsParams<AuthOptions>\n{\n model: string;\n\n dimensions?: number;\n\n private connection: EmbeddingsConnection<\n BaseGoogleEmbeddingsOptions,\n AuthOptions\n >;\n\n constructor(fields: BaseGoogleEmbeddingsParams<AuthOptions>) {\n super(fields);\n\n this.model = fields.model;\n this.dimensions = fields.dimensions ?? fields.outputDimensionality;\n\n this.connection = new EmbeddingsConnection(\n { ...fields, ...this },\n this.caller,\n this.buildClient(fields),\n false\n );\n }\n\n abstract buildAbstractedClient(\n fields?: GoogleConnectionParams<AuthOptions>\n ): GoogleAbstractedClient;\n\n buildApiKeyClient(apiKey: string): GoogleAbstractedClient {\n return new ApiKeyGoogleAuth(apiKey);\n }\n\n buildApiKey(\n fields?: GoogleConnectionParams<AuthOptions>\n ): string | undefined {\n return fields?.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n }\n\n buildClient(\n fields?: GoogleConnectionParams<AuthOptions>\n ): GoogleAbstractedClient {\n const apiKey = this.buildApiKey(fields);\n if (apiKey) {\n return this.buildApiKeyClient(apiKey);\n } else {\n return this.buildAbstractedClient(fields);\n }\n }\n\n buildParameters(): VertexEmbeddingsParameters {\n const ret: VertexEmbeddingsParameters = {\n outputDimensionality: this.dimensions,\n };\n\n // Remove undefined attributes\n let key: keyof VertexEmbeddingsParameters;\n for (key in ret) {\n if (ret[key] === undefined) {\n delete ret[key];\n }\n }\n\n return ret;\n }\n\n vertexResponseToValues(response: VertexEmbeddingsResponse): number[][] {\n const predictions: VertexEmbeddingsResponsePrediction[] =\n response?.data?.predictions ?? [];\n return predictions.map(\n (prediction: VertexEmbeddingsResponsePrediction): number[] =>\n prediction.embeddings.values\n );\n }\n\n aiStudioResponseToValues(response: AIStudioEmbeddingsResponse): number[][] {\n const value: number[] = response?.data?.embedding?.values ?? [];\n return [value];\n }\n\n responseToValues(response: GoogleEmbeddingsResponse): number[][] {\n switch (this.connection.platform) {\n case \"gcp\":\n return this.vertexResponseToValues(\n response as VertexEmbeddingsResponse\n );\n case \"gai\":\n return this.aiStudioResponseToValues(\n response as AIStudioEmbeddingsResponse\n );\n default:\n throw new Error(\n `Unknown response platform: ${this.connection.platform}`\n );\n }\n }\n\n /**\n * Takes an array of documents as input and returns a promise that\n * resolves to a 2D array of embeddings for each document. It splits the\n * documents into chunks and makes requests to the Google Vertex AI API to\n * generate embeddings.\n * @param documents An array of documents to be embedded.\n * @returns A promise that resolves to a 2D array of embeddings for each document.\n */\n async embedDocuments(documents: string[]): Promise<number[][]> {\n // Vertex \"text-\" models could do up 5 documents at once,\n // but the \"gemini-embedding-001\" can only do 1.\n // AI Studio can only do a chunk size of 1.\n // TODO: Make this configurable\n const chunkSize = 1;\n const instanceChunks: VertexEmbeddingsInstance[][] = chunkArray(\n documents.map((document) => ({\n content: document,\n })),\n chunkSize\n );\n const parameters: VertexEmbeddingsParameters = this.buildParameters();\n const options = {};\n const responses = await Promise.all(\n instanceChunks.map((instances) =>\n this.connection.request(instances, parameters, options)\n )\n );\n const result: number[][] =\n responses?.map((response) => this.responseToValues(response)).flat() ??\n [];\n return result;\n }\n\n /**\n * Takes a document as input and returns a promise that resolves to an\n * embedding for the document. It calls the embedDocuments method with the\n * document as the input.\n * @param document A document to be embedded.\n * @returns A promise that resolves to an embedding for the document.\n */\n async embedQuery(document: string): Promise<number[]> {\n const data = await this.embedDocuments([document]);\n return data[0];\n }\n}\n"],"mappings":";;;;;;;AA0BA,IAAM,uBAAN,cAGU,mBAKR;CACA;CAEA,YACEA,QACAC,QACAC,QACAC,WACA;EACA,MAAM,QAAQ,QAAQ,QAAQ,UAAU;CACzC;CAED,yBAAiC;AAC/B,SAAO;CACR;CAED,uBAA+B;AAC7B,SAAO;CACR;CAED,MAAM,iBAAkC;AACtC,UAAQ,KAAK,UAAb;GACE,KAAK,MACH,QAAO,KAAK,sBAAsB;GACpC,KAAK,MACH,QAAO,KAAK,wBAAwB;GACtC,QACE,OAAM,IAAI,MACR,CAAC,uCAAuC,EAAE,K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