このガイドでは、 Vertex AI Gemini API を、Vertex AI Gemini API を Vertex AI for Firebase SDK。
前提条件
このガイドは、読者が JavaScript を使用したコードの作成に精通していることを前提としています。 サポートします。このガイドはフレームワークに依存していません。
開発環境とウェブアプリが次の要件を満たしていることを確認してください。 要件:
- (省略可)Node.js
- 最新のウェブブラウザ
(省略可)サンプルアプリを確認します。
SDK はすぐに試して、さまざまな使い方の完全な実装を確認できます。 独自のウェブアプリがない場合は、サンプルアプリを使用してください。 サンプルアプリを使用するには、次のことを行う必要があります。 それを Firebase プロジェクトに接続します。
ステップ 1: Firebase プロジェクトを設定し、アプリを Firebase に接続する
Firebase プロジェクトと Firebase に接続されたアプリがすでにある場合
Firebase コンソールで、 Gemini を使用した構築ページ、 2 つ目のカードをクリックして ワークフローを起動させます 学習します。コンソールに Vertex AI のタブが表示されている場合は、 タスクは完了しています。
以下を使用するには、プロジェクトをアップグレードしてください。 Blaze 従量課金制の料金プラン。
プロジェクトで次の 2 つの 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。
npm を使用してウェブ用の Firebase JS SDK をインストールします。
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 モデルと(必要に応じて) location を使用します。
ステップ 4: Vertex AI Gemini API を呼び出す
アプリを Firebase に接続し、SDK を追加して初期化を終えたので、 Vertex AI サービスと生成モデルの Vertex AI Gemini API を呼び出す準備が整いました。
generateContent()
を使用すると、テキストのみのプロンプトからテキストを生成できます。
request:
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();
で確認できます。
で確認できます。
Google アシスタントの機能
Gemini モデルの詳細
詳しくは、 さまざまなユースケースで利用可能な および 割り当てと料金をご確認ください。
Gemini API のその他の機能を試す
- テキスト生成の詳細 テキストのみのプロンプト: レスポンスをストリーミングします
- テキストの生成元 マルチモーダル プロンプト (テキスト、画像、PDF、動画、音声を含む)。
- マルチターンの会話(チャット)を構築する。
- 関数呼び出しを使用して接続する 生成モデルを外部のシステムや情報にエクスポートできます。
コンテンツの生成を制御する方法
- 以下を含むプロンプト設計について理解する ベスト プラクティス、戦略、プロンプトの例。
- 次のようなモデル パラメータを構成する temperature と max output トークンです。
- 安全性設定を使用して、 害を及ぼすおそれのある回答が返される可能性が高まります。
フィードバックを送信 Vertex AI for Firebase のご経験についてお聞かせください。