-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
googlevertexai.ts
216 lines (192 loc) Β· 6.19 KB
/
googlevertexai.ts
1
2
3
4
5
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { GoogleAuth, GoogleAuthOptions } from "google-auth-library";
import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
import { AsyncCallerCallOptions } from "@langchain/core/utils/async_caller";
import {
GoogleVertexAIBaseLLMInput,
GoogleVertexAIBasePrediction,
GoogleVertexAILLMPredictions,
} from "../../types/googlevertexai-types.js";
import {
GoogleVertexAILLMConnection,
GoogleVertexAILLMResponse,
} from "../../util/googlevertexai-connection.js";
/**
* Parameters for the GoogleVertexAIMultimodalEmbeddings class, extending
* both EmbeddingsParams and GoogleVertexAIConnectionParams.
*/
export interface GoogleVertexAIMultimodalEmbeddingsParams
extends EmbeddingsParams,
GoogleVertexAIBaseLLMInput<GoogleAuthOptions> {}
/**
* Options for the GoogleVertexAIMultimodalEmbeddings class, extending
* AsyncCallerCallOptions.
*/
interface GoogleVertexAIMultimodalEmbeddingsOptions
extends AsyncCallerCallOptions {}
/**
* An instance of media (text or image) that can be used for generating
* embeddings.
*/
interface GoogleVertexAIMultimodalEmbeddingsInstance {
text?: string;
image?: {
bytesBase64Encoded: string;
};
}
/**
* The results of generating embeddings, extending
* GoogleVertexAIBasePrediction. It includes text and image embeddings.
*/
interface GoogleVertexAIMultimodalEmbeddingsResults
extends GoogleVertexAIBasePrediction {
textEmbedding?: number[];
imageEmbedding?: number[];
}
/**
* The media should have a text property, an image property, or both.
*/
export type GoogleVertexAIMedia =
| {
text: string;
image?: Buffer;
}
| {
text?: string;
image: Buffer;
};
export type MediaEmbeddings = {
text?: number[];
image?: number[];
};
/**
* Class for generating embeddings for text and images using Google's
* Vertex AI. It extends the Embeddings base class and implements the
* GoogleVertexAIMultimodalEmbeddingsParams interface.
*/
export class GoogleVertexAIMultimodalEmbeddings
extends Embeddings
implements GoogleVertexAIMultimodalEmbeddingsParams
{
model = "multimodalembedding@001";
private connection: GoogleVertexAILLMConnection<
GoogleVertexAIMultimodalEmbeddingsOptions,
GoogleVertexAIMultimodalEmbeddingsInstance,
GoogleVertexAIMultimodalEmbeddingsResults,
GoogleAuthOptions
>;
constructor(fields?: GoogleVertexAIMultimodalEmbeddingsParams) {
super(fields ?? {});
this.model = fields?.model ?? this.model;
this.connection = new GoogleVertexAILLMConnection(
{ ...fields, ...this },
this.caller,
new GoogleAuth({
scopes: "https://www.googleapis.com/auth/cloud-platform",
...fields?.authOptions,
})
);
}
/**
* Converts media (text or image) to an instance that can be used for
* generating embeddings.
* @param media The media (text or image) to be converted.
* @returns An instance of media that can be used for generating embeddings.
*/
mediaToInstance(
media: GoogleVertexAIMedia
): GoogleVertexAIMultimodalEmbeddingsInstance {
const ret: GoogleVertexAIMultimodalEmbeddingsInstance = {};
if (media?.text) {
ret.text = media.text;
}
if (media.image) {
ret.image = {
bytesBase64Encoded: media.image.toString("base64"),
};
}
return ret;
}
/**
* Converts the response from Google Vertex AI to embeddings.
* @param response The response from Google Vertex AI.
* @returns An array of media embeddings.
*/
responseToEmbeddings(
response: GoogleVertexAILLMResponse<GoogleVertexAIMultimodalEmbeddingsResults>
): MediaEmbeddings[] {
return (
response?.data as GoogleVertexAILLMPredictions<GoogleVertexAIMultimodalEmbeddingsResults>
).predictions.map((r) => ({
text: r.textEmbedding,
image: r.imageEmbedding,
}));
}
/**
* Generates embeddings for multiple media instances.
* @param media An array of media instances.
* @returns A promise that resolves to an array of media embeddings.
*/
async embedMedia(media: GoogleVertexAIMedia[]): Promise<MediaEmbeddings[]> {
// Only one media embedding request is allowed
return Promise.all(media.map((m) => this.embedMediaQuery(m)));
}
/**
* Generates embeddings for a single media instance.
* @param media A single media instance.
* @returns A promise that resolves to a media embedding.
*/
async embedMediaQuery(media: GoogleVertexAIMedia): Promise<MediaEmbeddings> {
const instance: GoogleVertexAIMultimodalEmbeddingsInstance =
this.mediaToInstance(media);
const instances = [instance];
const parameters = {};
const options = {};
const responses = await this.connection.request(
instances,
parameters,
options
);
const result = this.responseToEmbeddings(responses);
return result[0];
}
/**
* Generates embeddings for multiple images.
* @param images An array of images.
* @returns A promise that resolves to an array of image embeddings.
*/
async embedImage(images: Buffer[]): Promise<number[][]> {
return this.embedMedia(images.map((image) => ({ image }))).then(
(embeddings) => embeddings.map((e) => e.image ?? [])
);
}
/**
* Generates embeddings for a single image.
* @param image A single image.
* @returns A promise that resolves to an image embedding.
*/
async embedImageQuery(image: Buffer): Promise<number[]> {
return this.embedMediaQuery({
image,
}).then((embeddings) => embeddings.image ?? []);
}
/**
* Generates embeddings for multiple text documents.
* @param documents An array of text documents.
* @returns A promise that resolves to an array of text document embeddings.
*/
async embedDocuments(documents: string[]): Promise<number[][]> {
return this.embedMedia(documents.map((text) => ({ text }))).then(
(embeddings) => embeddings.map((e) => e.text ?? [])
);
}
/**
* Generates embeddings for a single text document.
* @param document A single text document.
* @returns A promise that resolves to a text document embedding.
*/
async embedQuery(document: string): Promise<number[]> {
return this.embedMediaQuery({
text: document,
}).then((embeddings) => embeddings.text ?? []);
}
}