Skip to content

Commit 47f00b2

Browse files
committed
Document ChromeAdapter methods
1 parent dc0628f commit 47f00b2

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

packages/vertexai/src/methods/chrome-adapter.ts

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ export class ChromeAdapter {
4545
private mode?: InferenceMode,
4646
private onDeviceParams?: LanguageModelCreateOptions
4747
) {}
48+
4849
/**
49-
* Convenience method to check if a given request can be made on-device.
50-
* Encapsulates a few concerns: 1) the mode, 2) API existence, 3) prompt formatting, and
51-
* 4) model availability, including triggering download if necessary.
52-
* Pros: caller needn't be concerned with details of on-device availability. Cons: this method
53-
* spans a few concerns and splits request validation from usage. If instance variables weren't
54-
* already part of the API, we could consider a better separation of concerns.
50+
* Checks if a given request can be made on-device.
51+
*
52+
* <ol>Encapsulates a few concerns:
53+
* <li>the mode</li>
54+
* <li>API existence</li>
55+
* <li>prompt formatting</li>
56+
* <li>model availability, including triggering download if necessary</li>
57+
* </ol>
58+
*
59+
* <p>Pros: callers needn't be concerned with details of on-device availability.</p>
60+
* <p>Cons: this method spans a few concerns and splits request validation from usage.
61+
* If instance variables weren't already part of the API, we could consider a better
62+
* separation of concerns.</p>
5563
*/
5664
async isAvailable(request: GenerateContentRequest): Promise<boolean> {
5765
// Returns false if we should only use in-cloud inference.
@@ -82,10 +90,19 @@ export class ChromeAdapter {
8290
return false;
8391
}
8492
}
93+
94+
/**
95+
* Generates content on device.
96+
*
97+
* <p>This is comparable to {@link GenerativeModel.generateContent} for generating content in
98+
* Cloud.</p>
99+
* @param request a standard Vertex {@link GenerateContentRequest}
100+
* @returns {@link Response}, so we can reuse common response formatting.
101+
*/
85102
async generateContentOnDevice(
86103
request: GenerateContentRequest
87104
): Promise<Response> {
88-
const session = await this.session(
105+
const session = await this.createSession(
89106
// TODO: normalize on-device params during construction.
90107
this.onDeviceParams || {}
91108
);
@@ -104,6 +121,10 @@ export class ChromeAdapter {
104121
})
105122
} as Response;
106123
}
124+
125+
/**
126+
* Asserts inference for the given request can be performed by an on-device model.
127+
*/
107128
private static isOnDeviceRequest(request: GenerateContentRequest): boolean {
108129
// Returns false if the prompt is empty.
109130
if (request.contents.length === 0) {
@@ -127,6 +148,16 @@ export class ChromeAdapter {
127148

128149
return true;
129150
}
151+
152+
/**
153+
* Triggers the download of an on-device model.
154+
*
155+
* <p>Chrome only downloads models as needed. Chrome knows a model is needed when code calls
156+
* LanguageModel.create.</p>
157+
*
158+
* <p>Since Chrome manages the download, the SDK can only avoid redundant download requests by
159+
* tracking if a download has previously been requested.</p>
160+
*/
130161
private download(): void {
131162
if (this.isDownloading) {
132163
return;
@@ -138,9 +169,17 @@ export class ChromeAdapter {
138169
this.isDownloading = false;
139170
});
140171
}
172+
173+
/**
174+
* Converts a Vertex role string to a Chrome role string.
175+
*/
141176
private static toOnDeviceRole(role: Role): LanguageModelMessageRole {
142177
return role === 'model' ? 'assistant' : 'user';
143178
}
179+
180+
/**
181+
* Converts a Vertex Content object to a Chrome LanguageModelMessage object.
182+
*/
144183
private static toLanguageModelMessages(
145184
contents: Content[]
146185
): LanguageModelMessage[] {
@@ -149,6 +188,10 @@ export class ChromeAdapter {
149188
content: c.parts.map(ChromeAdapter.toLanguageModelMessageContent)
150189
}));
151190
}
191+
192+
/**
193+
* Converts a Vertex Part object to a Chrome LanguageModelMessageContent object.
194+
*/
152195
private static toLanguageModelMessageContent(
153196
part: Part
154197
): LanguageModelMessageContent {
@@ -162,10 +205,23 @@ export class ChromeAdapter {
162205
// TODO: support other input types
163206
throw new Error('Not yet implemented');
164207
}
165-
private async session(
166-
opts: LanguageModelCreateOptions
208+
209+
/**
210+
* Abstracts Chrome session creation.
211+
*
212+
* <p>Chrome uses a multi-turn session for all inference. Vertex uses single-turn for all
213+
* inference. To map the Vertex API to Chrome's API, the SDK creates a new session for all
214+
* inference.</p>
215+
*
216+
* <p>Chrome will remove a model from memory if it's no longer in use, so this method ensures a
217+
* new session is created before an old session is destroyed.</p>
218+
*/
219+
private async createSession(
220+
// TODO: define a default value, since these are optional.
221+
options: LanguageModelCreateOptions
167222
): Promise<LanguageModel> {
168-
const newSession = await this.languageModelProvider!.create(opts);
223+
// TODO: could we use this.onDeviceParams instead of passing in options?
224+
const newSession = await this.languageModelProvider!.create(options);
169225
if (this.oldSession) {
170226
this.oldSession.destroy();
171227
}

0 commit comments

Comments
 (0)