Skip to content

VinF Hybrid Inference: log debug messages in conditional logic #8992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions e2e/sample-apps/modular.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,22 @@ async function callVertexAI(app) {
console.log('[VERTEXAI] start');
const vertexAI = getVertexAI(app);
const model = getGenerativeModel(vertexAI, {
mode: 'only_on_device'
mode: 'prefer_on_device'
});
const singleResult = await model.generateContent([
{ text: 'describe the following:' },
{ text: 'the mojave desert' }
{ text: 'describe this 20 x 20 px image in two words' },
{
inlineData: {
mimeType: 'image/heic',
data: 'AAAAGGZ0eXBoZWljAAAAAGhlaWNtaWYxAAAB7G1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAHBpY3QAAAAAAAAAAAAAAAAAAAAAJGRpbmYAAAAcZHJlZgAAAAAAAAABAAAADHVybCAAAAABAAAADnBpdG0AAAAAAAEAAAA4aWluZgAAAAAAAgAAABVpbmZlAgAAAAABAABodmMxAAAAABVpbmZlAgAAAQACAABFeGlmAAAAABppcmVmAAAAAAAAAA5jZHNjAAIAAQABAAABD2lwcnAAAADtaXBjbwAAABNjb2xybmNseAACAAIABoAAAAAMY2xsaQDLAEAAAAAUaXNwZQAAAAAAAAAUAAAADgAAAChjbGFwAAAAFAAAAAEAAAANAAAAAQAAAAAAAAAB/8AAAACAAAAAAAAJaXJvdAAAAAAQcGl4aQAAAAADCAgIAAAAcWh2Y0MBA3AAAACwAAAAAAAe8AD8/fj4AAALA6AAAQAXQAEMAf//A3AAAAMAsAAAAwAAAwAecCShAAEAI0IBAQNwAAADALAAAAMAAAMAHqAUIEHAjw1iHuRZVNwICBgCogABAAlEAcBhcshAUyQAAAAaaXBtYQAAAAAAAAABAAEHgQIDhIUGhwAAACxpbG9jAAAAAEQAAAIAAQAAAAEAAAJsAAABDAACAAAAAQAAAhQAAABYAAAAAW1kYXQAAAAAAAABdAAAAAZFeGlmAABNTQAqAAAACAAEARIAAwAAAAEAAQAAARoABQAAAAEAAAA+ARsABQAAAAEAAABGASgAAwAAAAEAAgAAAAAAAAAAAEgAAAABAAAASAAAAAEAAAEIKAGvoR8wDimTiRYUbALiHkU3ZdZ8DXAcSrRB9GARtVQHvnCE0LEyBGAyb5P4eYr6JAK5UxNX10WNlARq3ZpcGeVD+Xom6LodYasuZKKtDHCz/xnswOtC/ksZzVKhtWQqGvkXcsJnLYqWevNkacnccQ95jbHJBg9nXub69jAAN3xhNOXxjGSxaG9QvES5R7sYICEojRjLF5OB5K3v+okQAwfgWpz/u21ayideOgOZQLAyBkKOv7ymLNCagiPWTlHAuy/3qR1Q7m2ERFaxKIAbLSkIVO/P8m8+anKxhzhC//L8NMAUoF+Sf3aEH9O41fwLc+PlcbrDrjgY2EboD3cn9DyN32Rum2Ym'
}
}
]);
console.log(`Generated text: ${singleResult.response.text()}`);
const chat = model.startChat();
let chatResult = await chat.sendMessage('describe red in two words');
chatResult = await chat.sendMessage('describe blue');
console.log('Chat history:', await chat.getHistory());
console.log(`[VERTEXAI] end`);
}

Expand All @@ -345,7 +354,7 @@ function callDataConnect(app) {
async function main() {
console.log('FIREBASE VERSION', SDK_VERSION);
const app = initializeApp(config);
setLogLevel('warn');
setLogLevel('debug');

// callAppCheck(app);
// await authLogin(app);
Expand Down
29 changes: 25 additions & 4 deletions packages/vertexai/src/methods/chrome-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import { AIError } from '../errors';
import { logger } from '../logger';
import {
CountTokensRequest,
GenerateContentRequest,
Expand Down Expand Up @@ -65,6 +66,9 @@ export class ChromeAdapter {
*/
async isAvailable(request: GenerateContentRequest): Promise<boolean> {
if (this.mode === 'only_in_cloud') {
logger.debug(
`On-device inference unavailable because mode is "only_in_cloud".`
);
return false;
}

Expand All @@ -76,10 +80,20 @@ export class ChromeAdapter {
}

// Applies prefer_on_device logic.
return (
availability === Availability.available &&
ChromeAdapter.isOnDeviceRequest(request)
);
if (availability !== Availability.available) {
logger.debug(
`On-device inference unavailable because availability is "${availability}".`
);
return false;
}
if (!ChromeAdapter.isOnDeviceRequest(request)) {
logger.debug(
`On-device inference unavailable because request is incompatible.`
);
return false;
}

return true;
}

/**
Expand Down Expand Up @@ -135,13 +149,17 @@ export class ChromeAdapter {
private static isOnDeviceRequest(request: GenerateContentRequest): boolean {
// Returns false if the prompt is empty.
if (request.contents.length === 0) {
logger.debug('Empty prompt rejected for on-device inference.');
return false;
}

for (const content of request.contents) {
// Returns false if the request contains multiple roles, eg a chat history.
// TODO: remove this guard once LanguageModelMessage is supported.
if (content.role !== 'user') {
logger.debug(
`Non-user role "${content.role}" rejected for on-device inference.`
);
return false;
}

Expand All @@ -153,6 +171,9 @@ export class ChromeAdapter {
part.inlineData.mimeType
) === -1
) {
logger.debug(
`Unsupported mime type "${part.inlineData.mimeType}" rejected for on-device inference.`
);
return false;
}
}
Expand Down
Loading