通过 Vertex AI for Firebase SDK 开始使用 Gemini API


本指南将为您介绍如何开始调用 使用 Vertex AI for Firebase SDK。

前提条件

本指南假定您熟悉使用 JavaScript 开发 Web 应用。本指南独立于框架。

  • 确保您的开发环境和 Web 应用符合以下要求 要求:

    • (可选)Node.js
    • 现代网络浏览器
  • (可选)查看示例应用。

    下载示例应用

    您可以快速试用该 SDK,并查看各种用法的完整实现。 或使用示例应用(如果您没有自己的 Web 应用)。 要使用示例应用,您需要: 将其关联到 Firebase 项目

第 1 步:设置一个 Firebase 项目,并将您的应用关联到 Firebase

如果您已有 Firebase 项目和已关联到 Firebase 的应用

  1. 在 Firebase 控制台中,前往 Build with Gemini 页面 然后点击第二张卡片启动工作流程 后续任务。如果您在控制台中看到 Vertex AI 标签页, 这些任务都已完成。

  2. 继续执行本指南中的下一步,将 SDK 添加到您的应用中。

如果您还没有将 Firebase 项目和应用与 Firebase 相关联


第 2 步:添加 SDK

设置好 Firebase 项目并将应用关联到 Firebase 后 (请参阅上一步),您现在可以将 Vertex AI for Firebase SDK 添加到您的应用。

借助 Vertex AI for Firebase 库, Vertex AI Gemini API 已纳入 适用于 Web 的 Firebase JavaScript SDK。

  1. 使用 npm 安装 Firebase JS SDK for Web:

    npm install firebase
    
  2. 在您的应用中初始化 Firebase:

    import { initializeApp } from "firebase/app";
    
    // TODO(developer) Replace the following with your app's Firebase configuration
    // See: https://firebase.google.com/docs/web/learn-more#config-object
    const firebaseConfig = {
      // ...
    };
    
    // Initialize FirebaseApp
    const firebaseApp = initializeApp(firebaseConfig);
    

第 3 步:初始化 Vertex AI 服务和生成模型

在进行任何 API 调用之前,您需要先初始化 Vertex AI 服务和生成模型。

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai-preview";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Initialize the generative model with a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
const model = getGenerativeModel(vertexAI, { model: "gemini-1.5-flash" });

看完入门指南后,您可以了解一下 Gemini 模型,以及(可选) 位置

第 4 步:调用 Vertex AI Gemini API

现在,您已将应用关联到 Firebase、添加了 SDK 并初始化了 Vertex AI 服务和生成模型, 就可以调用 Vertex AI Gemini API 了。

您可以使用 generateContent() 根据纯文本提示生成文本 请求:

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai-preview";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Initialize the generative model with a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
const model = getGenerativeModel(vertexAI, { model: "gemini-1.5-flash" });

// Wrap in an async function so you can use await
async function run() {
  // Provide a prompt that contains text
  const prompt = "Write a story about a magic backpack."

  // To generate text output, call generateContent with the text input
  const result = await model.generateContent(prompt);

  const response = result.response;
  const text = response.text();
  console.log(text);
}

run();

您还可以做些什么?

详细了解 Gemini 模型

详细了解 适用于各种应用场景的模型 及其 配额和定价

试用 Gemini API 的其他功能

了解如何控制内容生成

。 您还可以使用以下方法对提示和模型配置进行实验: Vertex AI Studio


提供反馈 Vertex AI for Firebase 使用体验