使用 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(())
}