Skip to content

Commit a685395

Browse files
committed
Return true unconditionally if mode is only_on_device
1 parent 5fa83b3 commit a685395

File tree

2 files changed

+19
-77
lines changed

2 files changed

+19
-77
lines changed

Diff for: packages/vertexai/src/methods/chrome-adapter.test.ts

+7-59
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,8 @@ describe('ChromeAdapter', () => {
6161
})
6262
).to.be.false;
6363
});
64-
it('returns false if AI API is undefined', async () => {
65-
const adapter = new ChromeAdapter(undefined, 'prefer_on_device');
66-
expect(
67-
await adapter.isAvailable({
68-
contents: []
69-
})
70-
).to.be.false;
71-
});
7264
it('returns false if LanguageModel API is undefined', async () => {
73-
const adapter = new ChromeAdapter(
74-
{} as LanguageModel,
75-
'prefer_on_device'
76-
);
65+
const adapter = new ChromeAdapter(undefined, 'prefer_on_device');
7766
expect(
7867
await adapter.isAvailable({
7968
contents: []
@@ -82,7 +71,9 @@ describe('ChromeAdapter', () => {
8271
});
8372
it('returns false if request contents empty', async () => {
8473
const adapter = new ChromeAdapter(
85-
{} as LanguageModel,
74+
{
75+
availability: async () => Availability.available
76+
} as LanguageModel,
8677
'prefer_on_device'
8778
);
8879
expect(
@@ -93,7 +84,9 @@ describe('ChromeAdapter', () => {
9384
});
9485
it('returns false if request content has function role', async () => {
9586
const adapter = new ChromeAdapter(
96-
{} as LanguageModel,
87+
{
88+
availability: async () => Availability.available
89+
} as LanguageModel,
9790
'prefer_on_device'
9891
);
9992
expect(
@@ -107,51 +100,6 @@ describe('ChromeAdapter', () => {
107100
})
108101
).to.be.false;
109102
});
110-
it('returns false if request system instruction has function role', async () => {
111-
const adapter = new ChromeAdapter(
112-
{} as LanguageModel,
113-
'prefer_on_device'
114-
);
115-
expect(
116-
await adapter.isAvailable({
117-
contents: [],
118-
systemInstruction: {
119-
role: 'function',
120-
parts: []
121-
}
122-
})
123-
).to.be.false;
124-
});
125-
it('returns false if request system instruction has multiple parts', async () => {
126-
const adapter = new ChromeAdapter(
127-
{} as LanguageModel,
128-
'prefer_on_device'
129-
);
130-
expect(
131-
await adapter.isAvailable({
132-
contents: [],
133-
systemInstruction: {
134-
role: 'function',
135-
parts: [{ text: 'a' }, { text: 'b' }]
136-
}
137-
})
138-
).to.be.false;
139-
});
140-
it('returns false if request system instruction has non-text part', async () => {
141-
const adapter = new ChromeAdapter(
142-
{} as LanguageModel,
143-
'prefer_on_device'
144-
);
145-
expect(
146-
await adapter.isAvailable({
147-
contents: [],
148-
systemInstruction: {
149-
role: 'function',
150-
parts: [{ inlineData: { mimeType: 'a', data: 'b' } }]
151-
}
152-
})
153-
).to.be.false;
154-
});
155103
it('returns true if model is readily available', async () => {
156104
const languageModelProvider = {
157105
availability: () => Promise.resolve(Availability.available)

Diff for: packages/vertexai/src/methods/chrome-adapter.ts

+12-18
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,23 @@ export class ChromeAdapter {
6060
* separation of concerns.</p>
6161
*/
6262
async isAvailable(request: GenerateContentRequest): Promise<boolean> {
63-
// Returns false if we should only use in-cloud inference.
6463
if (this.mode === 'only_in_cloud') {
6564
return false;
6665
}
67-
// Returns false if the on-device inference API is undefined.;
68-
if (!this.languageModelProvider) {
69-
return false;
66+
console.log(this.languageModelProvider);
67+
const availability = await this.languageModelProvider?.availability();
68+
if (availability === Availability.downloadable) {
69+
// Triggers async model download.
70+
this.download();
7071
}
71-
// Returns false if the request can't be run on-device.
72-
if (!ChromeAdapter.isOnDeviceRequest(request)) {
73-
return false;
74-
}
75-
const availability = await this.languageModelProvider.availability();
76-
switch (availability) {
77-
case Availability.available:
78-
// Returns true only if a model is immediately available.
79-
return true;
80-
case Availability.downloadable:
81-
// Triggers async download if model is downloadable.
82-
this.download();
83-
default:
84-
return false;
72+
if (this.mode === 'only_on_device') {
73+
return true;
8574
}
75+
// Applies prefer_on_device logic.
76+
return (
77+
availability === Availability.available &&
78+
ChromeAdapter.isOnDeviceRequest(request)
79+
);
8680
}
8781

8882
/**

0 commit comments

Comments
 (0)