@langchain/google-common 0.2.17 → 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.
Files changed (169) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +6 -6
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/auth.cjs +82 -116
  5. package/dist/auth.cjs.map +1 -0
  6. package/dist/auth.d.cts +46 -0
  7. package/dist/auth.d.cts.map +1 -0
  8. package/dist/auth.d.ts +41 -36
  9. package/dist/auth.d.ts.map +1 -0
  10. package/dist/auth.js +80 -110
  11. package/dist/auth.js.map +1 -0
  12. package/dist/chat_models.cjs +251 -466
  13. package/dist/chat_models.cjs.map +1 -0
  14. package/dist/chat_models.d.cts +98 -0
  15. package/dist/chat_models.d.cts.map +1 -0
  16. package/dist/chat_models.d.ts +87 -73
  17. package/dist/chat_models.d.ts.map +1 -0
  18. package/dist/chat_models.js +245 -457
  19. package/dist/chat_models.js.map +1 -0
  20. package/dist/connection.cjs +321 -466
  21. package/dist/connection.cjs.map +1 -0
  22. package/dist/connection.d.cts +109 -0
  23. package/dist/connection.d.cts.map +1 -0
  24. package/dist/connection.d.ts +98 -91
  25. package/dist/connection.d.ts.map +1 -0
  26. package/dist/connection.js +317 -459
  27. package/dist/connection.js.map +1 -0
  28. package/dist/embeddings.cjs +135 -186
  29. package/dist/embeddings.cjs.map +1 -0
  30. package/dist/embeddings.d.cts +44 -0
  31. package/dist/embeddings.d.cts.map +1 -0
  32. package/dist/embeddings.d.ts +38 -32
  33. package/dist/embeddings.d.ts.map +1 -0
  34. package/dist/embeddings.js +133 -181
  35. package/dist/embeddings.js.map +1 -0
  36. package/dist/experimental/media.cjs +380 -482
  37. package/dist/experimental/media.cjs.map +1 -0
  38. package/dist/experimental/media.d.cts +198 -0
  39. package/dist/experimental/media.d.cts.map +1 -0
  40. package/dist/experimental/media.d.ts +190 -202
  41. package/dist/experimental/media.d.ts.map +1 -0
  42. package/dist/experimental/media.js +369 -468
  43. package/dist/experimental/media.js.map +1 -0
  44. package/dist/experimental/utils/media_core.cjs +403 -517
  45. package/dist/experimental/utils/media_core.cjs.map +1 -0
  46. package/dist/experimental/utils/media_core.d.cts +215 -0
  47. package/dist/experimental/utils/media_core.d.cts.map +1 -0
  48. package/dist/experimental/utils/media_core.d.ts +171 -165
  49. package/dist/experimental/utils/media_core.d.ts.map +1 -0
  50. package/dist/experimental/utils/media_core.js +395 -506
  51. package/dist/experimental/utils/media_core.js.map +1 -0
  52. package/dist/index.cjs +58 -27
  53. package/dist/index.d.cts +13 -0
  54. package/dist/index.d.ts +13 -11
  55. package/dist/index.js +13 -11
  56. package/dist/llms.cjs +157 -244
  57. package/dist/llms.cjs.map +1 -0
  58. package/dist/llms.d.cts +72 -0
  59. package/dist/llms.d.cts.map +1 -0
  60. package/dist/llms.d.ts +64 -54
  61. package/dist/llms.d.ts.map +1 -0
  62. package/dist/llms.js +154 -238
  63. package/dist/llms.js.map +1 -0
  64. package/dist/output_parsers.cjs +148 -173
  65. package/dist/output_parsers.cjs.map +1 -0
  66. package/dist/output_parsers.d.cts +53 -0
  67. package/dist/output_parsers.d.cts.map +1 -0
  68. package/dist/output_parsers.d.ts +46 -42
  69. package/dist/output_parsers.d.ts.map +1 -0
  70. package/dist/output_parsers.js +146 -168
  71. package/dist/output_parsers.js.map +1 -0
  72. package/dist/types-anthropic.d.cts +229 -0
  73. package/dist/types-anthropic.d.cts.map +1 -0
  74. package/dist/types-anthropic.d.ts +221 -215
  75. package/dist/types-anthropic.d.ts.map +1 -0
  76. package/dist/types.cjs +51 -62
  77. package/dist/types.cjs.map +1 -0
  78. package/dist/types.d.cts +748 -0
  79. package/dist/types.d.cts.map +1 -0
  80. package/dist/types.d.ts +669 -656
  81. package/dist/types.d.ts.map +1 -0
  82. package/dist/types.js +46 -45
  83. package/dist/types.js.map +1 -0
  84. package/dist/utils/anthropic.cjs +598 -821
  85. package/dist/utils/anthropic.cjs.map +1 -0
  86. package/dist/utils/anthropic.js +597 -818
  87. package/dist/utils/anthropic.js.map +1 -0
  88. package/dist/utils/common.cjs +130 -211
  89. package/dist/utils/common.cjs.map +1 -0
  90. package/dist/utils/common.d.cts +13 -0
  91. package/dist/utils/common.d.cts.map +1 -0
  92. package/dist/utils/common.d.ts +12 -7
  93. package/dist/utils/common.d.ts.map +1 -0
  94. package/dist/utils/common.js +128 -207
  95. package/dist/utils/common.js.map +1 -0
  96. package/dist/utils/failed_handler.cjs +28 -30
  97. package/dist/utils/failed_handler.cjs.map +1 -0
  98. package/dist/utils/failed_handler.d.cts +9 -0
  99. package/dist/utils/failed_handler.d.cts.map +1 -0
  100. package/dist/utils/failed_handler.d.ts +8 -2
  101. package/dist/utils/failed_handler.d.ts.map +1 -0
  102. package/dist/utils/failed_handler.js +28 -28
  103. package/dist/utils/failed_handler.js.map +1 -0
  104. package/dist/utils/gemini.cjs +1020 -1488
  105. package/dist/utils/gemini.cjs.map +1 -0
  106. package/dist/utils/gemini.d.cts +51 -0
  107. package/dist/utils/gemini.d.cts.map +1 -0
  108. package/dist/utils/gemini.d.ts +51 -48
  109. package/dist/utils/gemini.d.ts.map +1 -0
  110. package/dist/utils/gemini.js +1015 -1479
  111. package/dist/utils/gemini.js.map +1 -0
  112. package/dist/utils/index.cjs +38 -23
  113. package/dist/utils/index.d.cts +8 -0
  114. package/dist/utils/index.d.ts +8 -7
  115. package/dist/utils/index.js +8 -7
  116. package/dist/utils/palm.d.cts +11 -0
  117. package/dist/utils/palm.d.cts.map +1 -0
  118. package/dist/utils/palm.d.ts +9 -4
  119. package/dist/utils/palm.d.ts.map +1 -0
  120. package/dist/utils/safety.cjs +13 -22
  121. package/dist/utils/safety.cjs.map +1 -0
  122. package/dist/utils/safety.d.cts +12 -0
  123. package/dist/utils/safety.d.cts.map +1 -0
  124. package/dist/utils/safety.d.ts +10 -4
  125. package/dist/utils/safety.d.ts.map +1 -0
  126. package/dist/utils/safety.js +13 -19
  127. package/dist/utils/safety.js.map +1 -0
  128. package/dist/utils/stream.cjs +296 -475
  129. package/dist/utils/stream.cjs.map +1 -0
  130. package/dist/utils/stream.d.cts +165 -0
  131. package/dist/utils/stream.d.cts.map +1 -0
  132. package/dist/utils/stream.d.ts +156 -131
  133. package/dist/utils/stream.d.ts.map +1 -0
  134. package/dist/utils/stream.js +293 -469
  135. package/dist/utils/stream.js.map +1 -0
  136. package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
  137. package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
  138. package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
  139. package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
  140. package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
  141. package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
  142. package/dist/utils/zod_to_gemini_parameters.js +40 -76
  143. package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
  144. package/package.json +69 -85
  145. package/dist/types-anthropic.cjs +0 -2
  146. package/dist/types-anthropic.js +0 -1
  147. package/dist/utils/anthropic.d.ts +0 -4
  148. package/dist/utils/palm.cjs +0 -2
  149. package/dist/utils/palm.js +0 -1
  150. package/experimental/media.cjs +0 -1
  151. package/experimental/media.d.cts +0 -1
  152. package/experimental/media.d.ts +0 -1
  153. package/experimental/media.js +0 -1
  154. package/experimental/utils/media_core.cjs +0 -1
  155. package/experimental/utils/media_core.d.cts +0 -1
  156. package/experimental/utils/media_core.d.ts +0 -1
  157. package/experimental/utils/media_core.js +0 -1
  158. package/index.cjs +0 -1
  159. package/index.d.cts +0 -1
  160. package/index.d.ts +0 -1
  161. package/index.js +0 -1
  162. package/types.cjs +0 -1
  163. package/types.d.cts +0 -1
  164. package/types.d.ts +0 -1
  165. package/types.js +0 -1
  166. package/utils.cjs +0 -1
  167. package/utils.d.cts +0 -1
  168. package/utils.d.ts +0 -1
  169. package/utils.js +0 -1
@@ -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
- class EmbeddingsConnection extends GoogleAIConnection {
7
- constructor(fields, caller, client, streaming) {
8
- super(fields, caller, client, streaming);
9
- Object.defineProperty(this, "convertSystemMessageToHumanContent", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- }
16
- buildUrlMethodAiStudio() {
17
- return "embedContent";
18
- }
19
- buildUrlMethodVertex() {
20
- return "predict";
21
- }
22
- async buildUrlMethod() {
23
- switch (this.platform) {
24
- case "gcp":
25
- return this.buildUrlMethodVertex();
26
- case "gai":
27
- return this.buildUrlMethodAiStudio();
28
- default:
29
- throw new Error(`Unknown platform when building method: ${this.platform}`);
30
- }
31
- }
32
- get modelPublisher() {
33
- // All the embedding models are currently published by "google"
34
- return "google";
35
- }
36
- formatDataAiStudio(input, parameters) {
37
- const parts = input.map((instance) => ({
38
- text: instance.content,
39
- }));
40
- const content = {
41
- parts,
42
- };
43
- const outputDimensionality = parameters?.outputDimensionality;
44
- const ret = {
45
- content,
46
- outputDimensionality,
47
- };
48
- // Remove undefined attributes
49
- let key;
50
- for (key in ret) {
51
- if (ret[key] === undefined) {
52
- delete ret[key];
53
- }
54
- }
55
- return ret;
56
- }
57
- formatDataVertex(input, parameters) {
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
- * Enables calls to Google APIs for generating
76
- * text embeddings.
77
- */
78
- export class BaseGoogleEmbeddings extends Embeddings {
79
- constructor(fields) {
80
- super(fields);
81
- Object.defineProperty(this, "model", {
82
- enumerable: true,
83
- configurable: true,
84
- writable: true,
85
- value: void 0
86
- });
87
- Object.defineProperty(this, "dimensions", {
88
- enumerable: true,
89
- configurable: true,
90
- writable: true,
91
- value: void 0
92
- });
93
- Object.defineProperty(this, "connection", {
94
- enumerable: true,
95
- configurable: true,
96
- writable: true,
97
- value: void 0
98
- });
99
- this.model = fields.model;
100
- this.dimensions = fields.dimensions ?? fields.outputDimensionality;
101
- this.connection = new EmbeddingsConnection({ ...fields, ...this }, this.caller, this.buildClient(fields), false);
102
- }
103
- buildApiKeyClient(apiKey) {
104
- return new ApiKeyGoogleAuth(apiKey);
105
- }
106
- buildApiKey(fields) {
107
- return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
108
- }
109
- buildClient(fields) {
110
- const apiKey = this.buildApiKey(fields);
111
- if (apiKey) {
112
- return this.buildApiKeyClient(apiKey);
113
- }
114
- else {
115
- return this.buildAbstractedClient(fields);
116
- }
117
- }
118
- buildParameters() {
119
- const ret = {
120
- outputDimensionality: this.dimensions,
121
- };
122
- // Remove undefined attributes
123
- let key;
124
- for (key in ret) {
125
- if (ret[key] === undefined) {
126
- delete ret[key];
127
- }
128
- }
129
- return ret;
130
- }
131
- vertexResponseToValues(response) {
132
- const predictions = response?.data?.predictions ?? [];
133
- return predictions.map((prediction) => prediction.embeddings.values);
134
- }
135
- aiStudioResponseToValues(response) {
136
- const value = response?.data?.embedding?.values ?? [];
137
- return [value];
138
- }
139
- responseToValues(response) {
140
- switch (this.connection.platform) {
141
- case "gcp":
142
- return this.vertexResponseToValues(response);
143
- case "gai":
144
- return this.aiStudioResponseToValues(response);
145
- default:
146
- throw new Error(`Unknown response platform: ${this.connection.platform}`);
147
- }
148
- }
149
- /**
150
- * Takes an array of documents as input and returns a promise that
151
- * resolves to a 2D array of embeddings for each document. It splits the
152
- * documents into chunks and makes requests to the Google Vertex AI API to
153
- * generate embeddings.
154
- * @param documents An array of documents to be embedded.
155
- * @returns A promise that resolves to a 2D array of embeddings for each document.
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,KAAK,UAAU;EAE9D;CACF;CAED,IAAI,iBAAyB;AAE3B,SAAO;CACR;CAED,mBACEC,OACAC,YAC2B;EAC3B,MAAMC,QAA0B,MAAM,IACpC,CAACC,cAAwC,EACvC,MAAM,SAAS,QAChB,GACF;EACD,MAAM,UAAU,EACd,MACD;EACD,MAAM,uBAAuB,YAAY;EAEzC,MAAMC,MAAiC;GACrC;GACA;EACD;EAGD,IAAIC;AACJ,OAAK,OAAO,IACV,KAAI,IAAI,SAAS,QACf,OAAO,IAAI;AAIf,SAAO;CACR;CAED,iBACEL,OACAC,YACyB;AACzB,SAAO;GACL,WAAW;GACX;EACD;CACF;CAED,MAAM,WACJD,OACAC,YACkC;AAClC,UAAQ,KAAK,UAAb;GACE,KAAK,MACH,QAAO,KAAK,iBAAiB,O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