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


本指南将为您介绍如何开始调用 Vertex AI Gemini API Vertex AI in 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 in Firebase SDK 添加到您的应用。

借助 Vertex AI in 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 服务和生成模型

您需要先初始化 Vertex AI,然后才能进行任何 API 调用。 服务和生成模型。

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 in Firebase的体验