Vertex AI for Firebase SDK を使用して Gemini API を使ってみる


このガイドでは、Vertex AI for Firebase SDK を使用して、アプリから直接 Vertex AI Gemini API の呼び出しを開始する方法について説明します。

前提条件

このガイドでは、JavaScript を使用したウェブアプリの開発に精通していることを前提としています。このガイドはフレームワークに依存していません。

  • 開発環境とウェブアプリが次の要件を満たしていることを確認します。

    • (省略可)Node.js
    • 最新のウェブブラウザ
  • (省略可)サンプルアプリを確認します。

    サンプルアプリをダウンロード

    SDK をすぐに試したり、さまざまなユースケースの完全な実装を確認したりできます。独自のウェブアプリがない場合は、サンプルアプリを使用できます。サンプルアプリを使用するには、Firebase プロジェクトに接続する必要があります。

ステップ 1: Firebase プロジェクトを設定し、アプリを Firebase に接続する

Firebase プロジェクトと Firebase に接続されたアプリがすでにある場合

  1. Firebase コンソールで [Gemini で構築する] ページに移動し、2 番目のカードをクリックして、次のタスクに役立つワークフローを起動します。コンソールに Vertex AI のタブが表示されている場合、これらのタスクは完了しています。

  2. このガイドの次のステップに進んで、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 の一部として含まれています。

  1. npm を使用してウェブ用の Firebase JS SDK をインストールします。

    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-preview-0514" });

スタートガイドを完了したら、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-preview-0514" });

// 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();

Google アシスタントの機能

Gemini モデルの詳細

さまざまなユースケースで利用可能なモデルと、その割り当てと料金について学習する。

Gemini API のその他の機能を試す

コンテンツの生成を制御する方法

Vertex AI Studio を使用して、プロンプトとモデル構成を試すこともできます。


Vertex AI for Firebase の使用に関するフィードバックを送信する