本指南介绍了如何开始使用 Vertex AI for Firebase SDK 从您的应用中直接调用 Vertex AI Gemini API。
前提条件
本指南假定您熟悉使用 JavaScript 开发 Web 应用的方法。本指南独立于框架。
确保您的开发环境和 Web 应用满足以下要求:
- (可选)Node.js
- 现代网络浏览器
(可选)查看示例应用。
您可以快速试用 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Web 应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
第 1 步:设置一个 Firebase 项目,并将您的应用关联到 Firebase
如果您已有 Firebase 项目和已关联到 Firebase 的应用
在 Firebase 控制台中,前往“使用 Gemini 构建”页面,然后点击第二张卡片,启动可帮助您执行以下任务的工作流。如果您在控制台中看到 Vertex AI 的标签页,则表示这些任务已完成。
升级您的项目以使用 Blaze 随用随付定价方案。
为您的项目启用以下两个 API:
aiplatform.googleapis.com
和firebaseml.googleapis.com
。
继续执行本指南中的下一步,将 SDK 添加到您的应用中。
如果您还没有将 Firebase 项目和应用与 Firebase 相关联
第 2 步:添加 SDK
设置 Firebase 项目并将应用关联到 Firebase(请参阅上一步),您现在可以将 Vertex AI for Firebase SDK 添加到您的应用。
Vertex AI for Firebase 库提供对 Vertex AI Gemini API 的访问权限,并且包含在 Firebase JavaScript SDK for Web 中。
使用 npm 安装 Firebase JS SDK for Web:
npm install firebase
在您的应用中初始化 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 使用体验提供反馈