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