Membuat teks menggunakan Vertex AI Gemini API

Dalam panduan ini, Anda akan mengirim permintaan perintah teks serta permintaan perintah dan gambar multimodal ke Vertex AI Gemini API dan melihat responsnya.

Prasyarat

Untuk menyelesaikan panduan ini, Anda harus memiliki project Google Cloud dengan Vertex AI API yang diaktifkan. Ikuti panduan penyiapan Vertex AI untuk menyelesaikan langkah-langkah ini.

Menambahkan library klien Vertex AI sebagai dependensi

Library klien Vertex AI mencakup banyak fitur. Untuk mempercepat kompilasi, aktifkan hanya fitur yang Anda perlukan:

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

Mengirim perintah ke Vertex AI Gemini API

  1. Lakukan inisialisasi klien menggunakan setelan default:
let client = PredictionService::builder().build().await?;
  1. Buat nama model. Contoh ini menerima project ID sebagai argumen, menggunakan lokasi tetap (global), dan menetapkan model ke versi Gemini tertentu:
const MODEL: &str = "gemini-3.8-flash";
let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

Jika Anda menjalankan fungsi ini dalam kode Anda sendiri, teruskan project ID (tanpa awalan projects/) dari project yang Anda pilih di prasyarat.

  1. Kirim permintaan:
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. Mencetak respons. Anda dapat menggunakan penentu format :#? untuk memformat objek respons bertingkat:
println!("RESPONSE = {response:#?}");

Perintah teks: kode lengkap

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

Mengirim perintah dan gambar ke Vertex AI Gemini API

  1. Lakukan inisialisasi klien menggunakan setelan default:
let client = PredictionService::builder().build().await?;
  1. Buat nama model:
const MODEL: &str = "gemini-3.8-flash";
let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");
  1. Buat bagian gambar dari permintaan:
Part::new().set_file_data(
    FileData::new()
        .set_mime_type("image/jpeg")
        .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
),
  1. Buat bagian perintah teks dari permintaan:
Part::new().set_text("Describe this picture."),
  1. Kirim permintaan lengkap:
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. Mencetak respons lengkap:
println!("RESPONSE = {response:#?}");

Perintah dan gambar: kode lengkap

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