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

Merged
merged 27 commits into from
May 12, 2025
Merged

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: 07f68ad

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/app

    TypeBase (e99683b)Merge (6e66af7)Diff
    browser19.5 kB19.5 kB-3 B (-0.0%)
    main20.4 kB20.4 kB-3 B (-0.0%)
    module19.5 kB19.5 kB-3 B (-0.0%)
  • @firebase/app-check

    TypeBase (e99683b)Merge (6e66af7)Diff
    browser26.7 kB26.9 kB+210 B (+0.8%)
    main27.7 kB27.9 kB+210 B (+0.8%)
    module26.7 kB26.9 kB+210 B (+0.8%)
  • @firebase/database-compat/standalone

    TypeBase (e99683b)Merge (6e66af7)Diff
    main366 kB366 kB-3 B (-0.0%)
  • @firebase/firestore

    TypeBase (e99683b)Merge (6e66af7)Diff
    browser385 kB385 kB+596 B (+0.2%)
    main595 kB596 kB+1.31 kB (+0.2%)
    module385 kB385 kB+596 B (+0.2%)
    react-native385 kB385 kB+600 B (+0.2%)
  • @firebase/util

    TypeBase (e99683b)Merge (6e66af7)Diff
    browser23.5 kB23.7 kB+207 B (+0.9%)
    main29.7 kB30.0 kB+301 B (+1.0%)
    module23.5 kB23.7 kB+207 B (+0.9%)
  • @firebase/vertexai

    TypeBase (e99683b)Merge (6e66af7)Diff
    browser36.0 kB42.5 kB+6.51 kB (+18.1%)
    main37.0 kB43.7 kB+6.67 kB (+18.0%)
    module36.0 kB42.5 kB+6.51 kB (+18.1%)
  • bundle

    46 size changes

    TypeBase (e99683b)Merge (6e66af7)Diff
    analytics (logEvent)44.7 kB44.7 kB-1 B (-0.0%)
    app-check (CustomProvider)37.8 kB38.0 kB+203 B (+0.5%)
    app-check (ReCaptchaEnterpriseProvider)40.3 kB40.5 kB+203 B (+0.5%)
    app-check (ReCaptchaV3Provider)40.3 kB40.5 kB+203 B (+0.5%)
    auth (Anonymous)77.8 kB77.8 kB-1 B (-0.0%)
    auth (EmailAndPassword)87.9 kB87.9 kB-1 B (-0.0%)
    auth (GoogleFBTwitterGitHubPopup)105 kB105 kB-1 B (-0.0%)
    auth (GooglePopup)102 kB102 kB-1 B (-0.0%)
    auth (GoogleRedirect)102 kB102 kB-1 B (-0.0%)
    auth (Phone)95.3 kB95.3 kB-1 B (-0.0%)
    database (Append to a list of data)150 kB150 kB-1 B (-0.0%)
    database (Filtering data)149 kB149 kB-1 B (-0.0%)
    database (Listen for child events)165 kB165 kB-1 B (-0.0%)
    database (Listen for value events + Detach listeners)165 kB165 kB-1 B (-0.0%)
    database (Listen for value events)165 kB165 kB-1 B (-0.0%)
    database (Read data once)165 kB165 kB-1 B (-0.0%)
    database (Save data as transactions)167 kB167 kB-1 B (-0.0%)
    database (Sort data)151 kB151 kB-1 B (-0.0%)
    database (Write data)149 kB149 kB-1 B (-0.0%)
    firestore (CSI Auto Indexing Disable and Delete)275 kB275 kB+406 B (+0.1%)
    firestore (CSI Auto Indexing Enable)275 kB275 kB+406 B (+0.1%)
    firestore (Persistence)306 kB307 kB+406 B (+0.1%)
    firestore (Query Cursors)252 kB252 kB-1 B (-0.0%)
    firestore (Query)249 kB249 kB-1 B (-0.0%)
    firestore (Read data once)237 kB237 kB-1 B (-0.0%)
    firestore (Read Write w Persistence)331 kB331 kB+406 B (+0.1%)
    firestore (Realtime updates)239 kB239 kB-1 B (-0.0%)
    firestore (Transaction)216 kB216 kB-1 B (-0.0%)
    firestore (Write data)216 kB216 kB-1 B (-0.0%)
    firestore-lite (Query Cursors)105 kB105 kB-1 B (-0.0%)
    firestore-lite (Query)101 kB101 kB-1 B (-0.0%)
    firestore-lite (Read data once)76.3 kB76.3 kB-1 B (-0.0%)
    firestore-lite (Transaction)102 kB102 kB-1 B (-0.0%)
    firestore-lite (Write data)85.9 kB85.9 kB-1 B (-0.0%)
    functions (call)35.0 kB35.0 kB-1 B (-0.0%)
    messaging (send + receive)47.5 kB47.5 kB-1 B (-0.0%)
    performance (trace)62.4 kB62.4 kB-1 B (-0.0%)
    remote-config (getAndFetch)48.8 kB48.8 kB-1 B (-0.0%)
    storage (getBytes)42.8 kB42.8 kB-1 B (-0.0%)
    storage (getDownloadURL)44.9 kB44.9 kB-1 B (-0.0%)
    storage (getMetadata)44.3 kB44.3 kB-1 B (-0.0%)
    storage (list + listAll)43.8 kB43.8 kB-1 B (-0.0%)
    storage (updateMetadata)44.6 kB44.6 kB-1 B (-0.0%)
    storage (uploadBytes)49.5 kB49.5 kB-1 B (-0.0%)
    storage (uploadBytesResumable)59.4 kB59.4 kB-1 B (-0.0%)
    storage (uploadString)49.7 kB49.7 kB-1 B (-0.0%)

  • firebase

    TypeBase (e99683b)Merge (6e66af7)Diff
    firebase-ai.js?33.8 kB? (?)
    firebase-app-check-compat.js22.9 kB23.1 kB+197 B (+0.9%)
    firebase-app-check.js25.3 kB25.5 kB+194 B (+0.8%)
    firebase-app.js103 kB103 kB-3 B (-0.0%)
    firebase-compat.js797 kB798 kB+577 B (+0.1%)
    firebase-firestore-compat.js342 kB343 kB+388 B (+0.1%)
    firebase-firestore.js443 kB444 kB+767 B (+0.2%)
    firebase-vertexai.js29.3 kB33.8 kB+4.50 kB (+15.4%)

Test Logs

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

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 16, 2025

Size Analysis Report 1

This report is too large (426,227 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/RcSyd3BftK.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
dlarocque and others added 3 commits April 29, 2025 15:06
* [vertexai] Use json of unary-success-citations

The test files have been updated to include `json` extension versions of the tests files. The old, `txt` extension files are deprecated and will be deleted eventually.

* Update also the version used in the update script
@dlarocque dlarocque requested a review from hsubox76 April 30, 2025 14:18
Comment on lines -601 to -606
- title: VertexAI
path: /docs/reference/js/vertexai.vertexai.md
- title: VertexAIError
path: /docs/reference/js/vertexai.vertexaierror.md
- title: VertexAIModel
path: /docs/reference/js/vertexai.vertexaimodel.md
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types still exist (can't remove them until a breaking change), but our doc generation tool doesn't pick them up since they're just aliases to the new types (e.g. VertexAIError is just an alias to AIError.

@dlarocque dlarocque requested a review from rachelsaunders May 2, 2025 20:21
Copy link
Contributor

github-actions bot commented May 6, 2025

Vertex AI Mock Responses Check ⚠️

A newer major version of the mock responses for Vertex AI unit tests is available. update_vertexai_responses.sh should be updated to clone the latest version of the responses: v13.0

'@firebase/vertexai': minor
---

Add support for the Gemini Developer API, enabling usage in a free tier, and add new `AI` API to accomodate new product naming.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this isn't the final changelog that will be in the release notes. This will be changed to the final changelog entry we decide on when we write the release notes.

@dlarocque dlarocque requested a review from rachelsaunders May 12, 2025 19:37
@dlarocque dlarocque merged commit d5082f9 into main May 12, 2025
33 checks passed
@dlarocque dlarocque deleted the dl/genai branch May 12, 2025 19:43
@dlarocque dlarocque restored the dl/genai branch May 12, 2025 19:50
@google-oss-bot google-oss-bot mentioned this pull request May 12, 2025
hsubox76 added a commit that referenced this pull request May 13, 2025
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.

5 participants