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