Vertex AI Gemini API を使用してテキストを生成する

このガイドでは、テキスト プロンプト リクエストとマルチモーダル プロンプトと画像リクエストを Vertex AI Gemini API に送信し、レスポンスを表示します。

前提条件

このガイドを完了するには、Vertex AI API が有効になっている Google Cloud プロジェクトが必要です。Vertex AI 設定ガイドに沿って、これらの手順を完了します。

Vertex AI クライアント ライブラリを依存関係として追加する

Vertex AI クライアント ライブラリには多くの機能が含まれています。コンパイルを高速化するには、必要な機能のみを有効にします。

cargo add google-cloud-aiplatform-v1 --no-default-features --features prediction-service

Vertex AI Gemini API にプロンプトを送信する

  1. デフォルト設定を使用してクライアントを初期化します。
let client = PredictionService::builder().build().await?;
  1. モデル名を構築します。この例では、プロジェクト ID を引数として受け取り、固定ロケーション(global)を使用し、モデルを特定のバージョンの Gemini に設定します。
const MODEL: &str = "gemini-3.8-flash";
let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

この関数を独自のコードで実行する場合は、前提条件で選択したプロジェクトのプロジェクト ID(projects/ 接頭辞なし)を渡します。

  1. リクエストを送信します。
let response = client
    .generate_content()
    .set_model(&model)
    .set_contents([Content::new()
        .set_role("user")
        .set_parts([Part::new().set_text(
            "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?",
        )])])
    .send()
    .await;
  1. レスポンスを出力します。:#? 形式指定子を使用して、ネストされたレスポンス オブジェクトをフォーマットできます。
println!("RESPONSE = {response:#?}");

テキスト プロンプト: コードを完成させる

use google_cloud_aiplatform_v1::client::PredictionService;
use google_cloud_aiplatform_v1::model::{Content, Part};

pub async fn sample(project_id: &str) -> anyhow::Result<()> {
    let client = PredictionService::builder().build().await?;

    const MODEL: &str = "gemini-3.8-flash";
    let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

    let response = client
        .generate_content()
        .set_model(&model)
        .set_contents([Content::new()
            .set_role("user")
            .set_parts([Part::new().set_text(
                "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?",
            )])])
        .send()
        .await;
    println!("RESPONSE = {response:#?}");

    Ok(())
}

プロンプトと画像を Vertex AI Gemini API に送信する

  1. デフォルト設定を使用してクライアントを初期化します。
let client = PredictionService::builder().build().await?;
  1. モデル名を構築します。
const MODEL: &str = "gemini-3.8-flash";
let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");
  1. リクエストの画像部分を作成します。
Part::new().set_file_data(
    FileData::new()
        .set_mime_type("image/jpeg")
        .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
),
  1. リクエストのテキスト プロンプト部分を作成します。
Part::new().set_text("Describe this picture."),
  1. 完全なリクエストを送信します。
let response = client
    .generate_content()
    .set_model(&model)
    .set_contents([Content::new().set_role("user").set_parts([
        Part::new().set_file_data(
            FileData::new()
                .set_mime_type("image/jpeg")
                .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
        ),
        Part::new().set_text("Describe this picture."),
    ])])
    .send()
    .await;
  1. レスポンス全体を出力します。
println!("RESPONSE = {response:#?}");

プロンプトと画像: 完全なコード

use google_cloud_aiplatform_v1::client::PredictionService;
use google_cloud_aiplatform_v1::model::{Content, FileData, Part};

pub async fn sample(project_id: &str) -> anyhow::Result<()> {
    let client = PredictionService::builder().build().await?;

    const MODEL: &str = "gemini-3.8-flash";
    let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

    let response = client
        .generate_content()
        .set_model(&model)
        .set_contents([Content::new().set_role("user").set_parts([
            Part::new().set_file_data(
                FileData::new()
                    .set_mime_type("image/jpeg")
                    .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
            ),
            Part::new().set_text("Describe this picture."),
        ])])
        .send()
        .await;
    println!("RESPONSE = {response:#?}");

    Ok(())
}