Skip to content

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

Merged
dlarocque merged 27 commits intomainfrom
dl/genai
May 12, 2025
Merged

feat(vertexai): Migrate to AI and add GoogleAI support#8931
dlarocque merged 27 commits intomainfrom
dl/genai

Conversation

@dlarocque
Copy link
Copy Markdown
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());

Loading
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants