Skip to content

feat(vertexai): Migrate to AI and add GoogleAI support #8931

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

dlarocque
Copy link
Contributor

@dlarocque dlarocque commented Apr 16, 2025

API: http://goto.google.com/googleai-in-vinf-api (internal)

This PR integrates support for the Google AI API alongside the existing Vertex AI backend. To accommodate this, the SDK has been refactored to use the more generic AI naming convention instead of VertexAI.

Changes

Google AI Support

  • Adds a googleAIBackend() function to configure the SDK to use the Google AI API. This enables usage with models available in a free tier.
  • Introduces mappers (googleAIMappers.ts) to translate requests and responses between the SDK's unified format (prioritizing Vertex AI) and the specific format required by the Google AI API. This handles differences in features like safety settings (method unsupported), citation metadata (citationSources vs citations), and response properties (defaulting missing safety scores).
  • Adds checks and throws AIError with code UNSUPPORTED or logs warnings for features/properties not available or different in the Google AI backend (e.g., SafetySetting.method, rounding topK, Part.videoMetadata).

Firebase AI Migration

  • Renames the core service interface and related classes/types from VertexAI to AI (e.g., AI, AIError, AIModel, AIErrorCode, AIOptions).
  • Maintains backwards compatibility by exporting the old names as aliases to the new ones (e.g., export type VertexAI = AI;, export const VertexAIError = AIError;).
  • Introduces Backend (GoogleAIBackend | VertexAIBackend) and BackendType types to abstract the backend configuration.

Initialization

  • Introduces the primary getAI(app?, options?) function for initializing the SDK. It accepts AIOptions to specify the desired backend.
  • Keeps the existing getVertexAI(app?, options?) function for backward compatibility, initializing a Vertex AI backend instance.

Internal

  • Adds an internal encoding/decoding scheme (helpers.ts) for instance identifiers based on backend type and location, allowing the component system to manage different service instances.
  • Updates internal request logic (request.ts, stream-reader.ts) to handle backend differences and use the appropriate mappers.

Packaging

  • Added a ./ai export path in firebase/package.json to allow the package to be imported with firebase/ai.

Testing

  • Added new unit tests covering backend configuration (googleAIBackend, vertexAIBackend), Google AI request/response mappers, and instance identifier helpers.
  • Added specific tests for core methods (generateContent, countTokens) using the Google AI backend path to verify request mapping and error handling.
  • Included tests explicitly for backwards compatibility for types and functions.

Sample usage

import { getAI, getVertexAI getGenerativeModel, vertexAIBackend } from 'firebase/ai';

// Google AI
let ai = getAI(app); // Google AI is the default backend.
let model = getGenerativeModel(ai, { model: 'gemini-2.0-flash' });
const result = await model.generateContent('Why is the sky blue?');
console.log(result.response.text());

// Vertex AI
let ai = getAI(app, { backend: vertexAIBackend() });
let model = getGenerativeModel(ai, { model: 'gemini-2.0-flash' });
const result = await model.generateContent('Why is the sky blue?');
console.log(result.response.text());

// Backwards compatibility
let vertexAI = getVertexAI(app); // Vertex AI backend
let model = getGenerativeModel(vertexAI, { model: 'gemini-2.0-flash' });
const result = await model.generateContent('Why is the sky blue');
console.log(result.response.text());

@dlarocque dlarocque requested a review from hsubox76 April 16, 2025 15:21
Copy link

changeset-bot bot commented Apr 16, 2025

🦋 Changeset detected

Latest commit: 475c81a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
firebase Minor
@firebase/vertexai Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 16, 2025

Size Report 1

Affected Products

  • @firebase/vertexai

    TypeBase (1363ecc)Merge (79f6ae1)Diff
    browser34.7 kB41.3 kB+6.54 kB (+18.8%)
    main35.7 kB42.4 kB+6.71 kB (+18.8%)
    module34.7 kB41.3 kB+6.54 kB (+18.8%)
  • firebase

    TypeBase (1363ecc)Merge (79f6ae1)Diff
    firebase-vertexai.js28.3 kB32.7 kB+4.48 kB (+15.9%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/scoMiZxJiw.html

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 16, 2025

Size Analysis Report 1

This report is too large (116,370 characters) to be displayed here in a GitHub comment. Please use the below link to see the full report on Google Cloud Storage.

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/Y06hh4YpoW.html

@dlarocque dlarocque changed the title [WIP] feat(vertexai): Migrate to GenAI and add GoogleAI support [WIP] feat(vertexai): Migrate to AI and add GoogleAI support Apr 17, 2025
@dlarocque dlarocque changed the title [WIP] feat(vertexai): Migrate to AI and add GoogleAI support feat(vertexai): Migrate to AI and add GoogleAI support Apr 22, 2025
@dlarocque dlarocque marked this pull request as ready for review April 22, 2025 18:43
@dlarocque dlarocque requested review from a team as code owners April 22, 2025 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants