快速入門導覽課程:使用 Vertex AI 的 Gemini API 生成文字

在本快速入門導覽課程中,您會將下列多模態要求傳送至 Vertex AI 中的 Gemini API,並查看回覆:

  • 文字提示
  • 提示和圖片
  • 提示和影片檔案 (含音軌)

您可以使用本機環境中的程式設計語言 SDK 或 REST API,完成本快速入門導覽課程。

必要條件

如要完成本快速入門導覽課程,請務必符合以下條件:

  • 設定 Google Cloud 專案並啟用 Vertex AI API
  • 在本機上:
    • 安裝、初始化及驗證 Google Cloud CLI
    • 安裝適用於您所用語言的 SDK

設定 Google Cloud 專案

設定 Google Cloud 專案並啟用 Vertex AI API。

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Vertex AI Platform Express User

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Vertex AI API.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Vertex AI Platform Express User

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]

    安裝 Google Cloud CLI 並進行驗證

    如要在 Vertex AI 中使用 Gemini API,請在本機設定及驗證 Google Cloud CLI。與 Google AI Studio 中的 Gemini API (使用 API 金鑰) 不同,Vertex AI 中的 Gemini API 會透過 Identity and Access Management 管理存取權。

    1. 安裝並初始化 Google Cloud CLI。

    2. 如果您先前已安裝 gcloud CLI,請執行下列指令,確保 gcloud 元件已更新。

      gcloud components update
    3. 如要透過 gcloud CLI 進行驗證,請執行下列指令,產生本機應用程式預設憑證 (ADC) 檔案。指令啟動的網頁流程會用於提供使用者憑證。

      gcloud auth application-default login

      詳情請參閱「設定應用程式預設憑證」。

    為您的程式設計語言設定 SDK

    在本機上,按一下下列其中一個分頁標籤,安裝您所用程式語言的 SDK。

    Python 適用的 Gen AI SDK

    執行下列指令,安裝及更新 Gen AI SDK for Python。

    pip install --upgrade google-genai

    Go 適用的 Gen AI SDK

    執行下列指令,安裝及更新 Go 專用的 Gen AI SDK。

    go get google.golang.org/genai

    Node.js 適用的 Gen AI SDK

    執行下列指令,安裝及更新 Node.js 適用的 Gen AI SDK。

    npm install @google/genai

    Java 適用的 Gen AI SDK

    安裝及更新 Java 適用的 Gen AI SDK:

    Maven

    請將以下內容新增到 pom.xml

    <dependencies>
      <dependency>
        <groupId>com.google.genai</groupId>
        <artifactId>google-genai</artifactId>
        <version>0.7.0</version>
      </dependency>
    </dependencies>
    

    C#

    從 NuGet 安裝 Google.Cloud.AIPlatform.V1 套件。使用您偏好的方法將套件新增至專案。舉例來說,在 Visual Studio 中以滑鼠右鍵按一下專案,然後選擇「管理 NuGet 套件...」

    REST

    1. 輸入下列內容,設定環境變數。將 PROJECT_ID 替換為 Google Cloud 專案 ID。

      MODEL_ID="gemini-2.0-flash-001"
      PROJECT_ID="PROJECT_ID"
    2. 使用 Google Cloud CLI 執行下列指令,佈建端點。

      gcloud beta services identity create --service=aiplatform.googleapis.com --project=${PROJECT_ID}

    將提示傳送至 Vertex AI 的 Gemini API

    請使用下列程式碼,將提示傳送至 Vertex AI 中的 Gemini API。 這個範例會傳回可能名稱清單,適用於專賣花卉的商店。

    您可以透過指令列、IDE 執行程式碼,或在應用程式中加入程式碼。

    Gen AI SDK for Python

    安裝

    pip install --upgrade google-genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    from google import genai
    from google.genai.types import HttpOptions
    
    client = genai.Client(http_options=HttpOptions(api_version="v1"))
    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents="How does AI work?",
    )
    print(response.text)
    # Example response:
    # Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
    #
    # Here's a simplified overview:
    # ...

    Gen AI SDK for Go

    瞭解如何安裝或更新 Gen AI SDK for Go

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    import (
    	"context"
    	"fmt"
    	"io"
    
    	"google.golang.org/genai"
    )
    
    // generateWithText shows how to generate text using a text prompt.
    func generateWithText(w io.Writer) error {
    	ctx := context.Background()
    
    	client, err := genai.NewClient(ctx, &genai.ClientConfig{
    		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
    	})
    	if err != nil {
    		return fmt.Errorf("failed to create genai client: %w", err)
    	}
    
    	resp, err := client.Models.GenerateContent(ctx,
    		"gemini-2.0-flash-001",
    		genai.Text("How does AI work?"),
    		nil,
    	)
    	if err != nil {
    		return fmt.Errorf("failed to generate content: %w", err)
    	}
    
    	respText, err := resp.Text()
    	if err != nil {
    		return fmt.Errorf("failed to convert model response to text: %w", err)
    	}
    	fmt.Fprintln(w, respText)
    	// Example response:
    	// That's a great question! Understanding how AI works can feel like ...
    	// ...
    	// **1. The Foundation: Data and Algorithms**
    	// ...
    
    	return nil
    }
    

    Gen AI SDK for Node.js

    安裝

    npm install @google/genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    const {GoogleGenAI} = require('@google/genai');
    
    const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
    const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
    
    async function generateContent(
      projectId = GOOGLE_CLOUD_PROJECT,
      location = GOOGLE_CLOUD_LOCATION
    ) {
      const ai = new GoogleGenAI({
        vertexai: true,
        project: projectId,
        location: location,
      });
    
      const response = await ai.models.generateContent({
        model: 'gemini-2.0-flash',
        contents: 'How does AI work?',
      });
    
      console.log(response.text);
    
      return response.text;
    }

    Gen AI SDK for Java

    瞭解如何安裝或更新 Gen AI SDK for Java

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    
    import com.google.genai.Client;
    import com.google.genai.types.Content;
    import com.google.genai.types.GenerateContentResponse;
    import com.google.genai.types.HttpOptions;
    import com.google.genai.types.Part;
    
    public class GenerateContentWithText {
    
      public static void main(String[] args) {
        // TODO(developer): Replace these variables before running the sample.
        String modelId = "gemini-2.0-flash";
        generateContent(modelId);
      }
    
      public static String generateContent(String modelId) {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        try (Client client = Client.builder()
            .httpOptions(HttpOptions.builder().apiVersion("v1").build())
            .build()) {
    
          GenerateContentResponse response =
              client.models.generateContent(modelId, Content.fromParts(
                      Part.fromText("How does AI work?")),
                  null);
    
          System.out.print(response.text());
          // Example response:
          // Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
          //
          // Here's a simplified overview:
          // ...
          return response.text();
        }
      }
    }

    C#

    如要傳送提示要求,請建立 C# 檔案 (.cs),然後將下列程式碼複製到該檔案中。將 your-project-id 設為您的 Google Cloud 專案 ID。更新值後,請執行程式碼。

    
    using Google.Cloud.AIPlatform.V1;
    using System;
    using System.Threading.Tasks;
    
    public class TextInputSample
    {
        public async Task<string> TextInput(
            string projectId = "your-project-id",
            string location = "us-central1",
            string publisher = "google",
            string model = "gemini-2.0-flash-001")
        {
    
            var predictionServiceClient = new PredictionServiceClientBuilder
            {
                Endpoint = $"{location}-aiplatform.googleapis.com"
            }.Build();
            string prompt = @"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";
    
            var generateContentRequest = new GenerateContentRequest
            {
                Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
                Contents =
                {
                    new Content
                    {
                        Role = "USER",
                        Parts =
                        {
                            new Part { Text = prompt }
                        }
                    }
                }
            };
    
            GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);
    
            string responseText = response.Candidates[0].Content.Parts[0].Text;
            Console.WriteLine(responseText);
    
            return responseText;
        }
    }
    

    REST

    如要傳送這項提示要求,請從指令列執行 curl 指令,或在應用程式中加入 REST 呼叫。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    https://aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/publishers/google/models/${MODEL_ID}:generateContent -d \
    $'{
      "contents": {
        "role": "user",
        "parts": [
          {
            "text": "What\'s a good name for a flower shop that specializes in selling bouquets of dried flowers?"
          }
        ]
      }
    }'

    模型會傳回回覆。請注意,系統會分段生成回覆,並分別評估每個段落的安全性。

    將提示和圖片傳送至 Vertex AI 的 Gemini API

    請使用下列程式碼,將包含文字和圖片的提示傳送至 Vertex AI 中的 Gemini API。這個範例會傳回提供的圖片說明 (Java 範例圖片)。

    Gen AI SDK for Python

    安裝

    pip install --upgrade google-genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    from google import genai
    from google.genai.types import HttpOptions, Part
    
    client = genai.Client(http_options=HttpOptions(api_version="v1"))
    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=[
            "What is shown in this image?",
            Part.from_uri(
                file_uri="gs://cloud-samples-data/generative-ai/image/scones.jpg",
                mime_type="image/jpeg",
            ),
        ],
    )
    print(response.text)
    # Example response:
    # The image shows a flat lay of blueberry scones arranged on parchment paper. There are ...

    Gen AI SDK for Go

    瞭解如何安裝或更新 Gen AI SDK for Go

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    import (
    	"context"
    	"fmt"
    	"io"
    
    	genai "google.golang.org/genai"
    )
    
    // generateWithTextImage shows how to generate text using both text and image input
    func generateWithTextImage(w io.Writer) error {
    	ctx := context.Background()
    
    	client, err := genai.NewClient(ctx, &genai.ClientConfig{
    		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
    	})
    	if err != nil {
    		return fmt.Errorf("failed to create genai client: %w", err)
    	}
    
    	modelName := "gemini-2.0-flash-001"
    	contents := []*genai.Content{
    		{Parts: []*genai.Part{
    			{Text: "What is shown in this image?"},
    			{FileData: &genai.FileData{
    				// Image source: https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpg
    				FileURI:  "gs://cloud-samples-data/generative-ai/image/scones.jpg",
    				MIMEType: "image/jpeg",
    			}},
    		}},
    	}
    
    	resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)
    	if err != nil {
    		return fmt.Errorf("failed to generate content: %w", err)
    	}
    
    	respText, err := resp.Text()
    	if err != nil {
    		return fmt.Errorf("failed to convert model response to text: %w", err)
    	}
    	fmt.Fprintln(w, respText)
    
    	// Example response:
    	// The image shows an overhead shot of a rustic, artistic arrangement on a surface that ...
    
    	return nil
    }
    

    Gen AI SDK for Node.js

    安裝

    npm install @google/genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    const {GoogleGenAI} = require('@google/genai');
    
    const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
    const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
    
    async function generateContent(
      projectId = GOOGLE_CLOUD_PROJECT,
      location = GOOGLE_CLOUD_LOCATION
    ) {
      const ai = new GoogleGenAI({
        vertexai: true,
        project: projectId,
        location: location,
      });
    
      const image = {
        fileData: {
          fileUri: 'gs://cloud-samples-data/generative-ai/image/scones.jpg',
          mimeType: 'image/jpeg',
        },
      };
    
      const response = await ai.models.generateContent({
        model: 'gemini-2.0-flash',
        contents: [image, 'What is shown in this image?'],
      });
    
      console.log(response.text);
    
      return response.text;
    }

    Gen AI SDK for Java

    瞭解如何安裝或更新 Gen AI SDK for Java

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    
    import com.google.genai.Client;
    import com.google.genai.types.Content;
    import com.google.genai.types.GenerateContentResponse;
    import com.google.genai.types.HttpOptions;
    import com.google.genai.types.Part;
    
    public class GenerateContentWithTextAndImage {
    
      public static void main(String[] args) {
        // TODO(developer): Replace these variables before running the sample.
        String modelId = "gemini-2.0-flash";
        generateContent(modelId);
      }
    
      public static String generateContent(String modelId) {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        try (Client client = Client.builder()
            .httpOptions(HttpOptions.builder().apiVersion("v1").build())
            .build()) {
    
          GenerateContentResponse response =
              client.models.generateContent(modelId, Content.fromParts(
                      Part.fromText("What is shown in this image?"),
                      Part.fromUri("gs://cloud-samples-data/generative-ai/image/scones.jpg", "image/jpeg")),
                  null);
    
          System.out.print(response.text());
          // Example response:
          // The image shows a flat lay of blueberry scones arranged on parchment paper. There are ...
          return response.text();
        }
      }
    }

    C#

    如要傳送提示要求,請建立 C# 檔案 (.cs),然後將下列程式碼複製到該檔案中。將 your-project-id 設為您的 Google Cloud 專案 ID。更新值後,請執行程式碼。

    
    using Google.Api.Gax.Grpc;
    using Google.Cloud.AIPlatform.V1;
    using System.Text;
    using System.Threading.Tasks;
    
    public class GeminiQuickstart
    {
        public async Task<string> GenerateContent(
            string projectId = "your-project-id",
            string location = "us-central1",
            string publisher = "google",
            string model = "gemini-2.0-flash-001"
        )
        {
            // Create client
            var predictionServiceClient = new PredictionServiceClientBuilder
            {
                Endpoint = $"{location}-aiplatform.googleapis.com"
            }.Build();
    
            // Initialize content request
            var generateContentRequest = new GenerateContentRequest
            {
                Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
                GenerationConfig = new GenerationConfig
                {
                    Temperature = 0.4f,
                    TopP = 1,
                    TopK = 32,
                    MaxOutputTokens = 2048
                },
                Contents =
                {
                    new Content
                    {
                        Role = "USER",
                        Parts =
                        {
                            new Part { Text = "What's in this photo?" },
                            new Part { FileData = new() { MimeType = "image/png", FileUri = "gs://generativeai-downloads/images/scones.jpg" } }
                        }
                    }
                }
            };
    
            // Make the request, returning a streaming response
            using PredictionServiceClient.StreamGenerateContentStream response = predictionServiceClient.StreamGenerateContent(generateContentRequest);
    
            StringBuilder fullText = new();
    
            // Read streaming responses from server until complete
            AsyncResponseStream<GenerateContentResponse> responseStream = response.GetResponseStream();
            await foreach (GenerateContentResponse responseItem in responseStream)
            {
                fullText.Append(responseItem.Candidates[0].Content.Parts[0].Text);
            }
    
            return fullText.ToString();
        }
    }
    

    REST

    您可以從 IDE 傳送這項提示要求,也可以視需要將 REST 呼叫嵌入應用程式中。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    https://aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/publishers/google/models/${MODEL_ID}:generateContent -d \
    $'{
      "contents": {
        "role": "user",
        "parts": [
          {
          "fileData": {
            "mimeType": "image/jpeg",
            "fileUri": "gs://generativeai-downloads/images/scones.jpg"
            }
          },
          {
            "text": "Describe this picture."
          }
        ]
      }
    }'

    模型會傳回回覆。請注意,系統會分段生成回覆,並分別評估每個段落的安全性。

    將提示和影片傳送至 Vertex AI 的 Gemini API

    請使用下列程式碼,將包含文字、音訊和影片的提示傳送至 Vertex AI 中的 Gemini API。這個範例會傳回所提供影片的說明,包括音軌中的重要內容。

    您可以透過指令列、IDE 或在應用程式中加入 REST 呼叫,傳送這項提示要求。

    Gen AI SDK for Python

    安裝

    pip install --upgrade google-genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    from google import genai
    from google.genai.types import HttpOptions, Part
    
    client = genai.Client(http_options=HttpOptions(api_version="v1"))
    prompt = """
    Analyze the provided video file, including its audio.
    Summarize the main points of the video concisely.
    Create a chapter breakdown with timestamps for key sections or topics discussed.
    """
    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=[
            Part.from_uri(
                file_uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
                mime_type="video/mp4",
            ),
            prompt,
        ],
    )
    
    print(response.text)
    # Example response:
    # Here's a breakdown of the video:
    #
    # **Summary:**
    #
    # Saeka Shimada, a photographer in Tokyo, uses the Google Pixel 8 Pro's "Video Boost" feature to ...
    #
    # **Chapter Breakdown with Timestamps:**
    #
    # * **[00:00-00:12] Introduction & Tokyo at Night:** Saeka Shimada introduces herself ...
    # ...

    Gen AI SDK for Go

    瞭解如何安裝或更新 Gen AI SDK for Go

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    import (
    	"context"
    	"fmt"
    	"io"
    
    	genai "google.golang.org/genai"
    )
    
    // generateWithVideo shows how to generate text using a video input.
    func generateWithVideo(w io.Writer) error {
    	ctx := context.Background()
    
    	client, err := genai.NewClient(ctx, &genai.ClientConfig{
    		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
    	})
    	if err != nil {
    		return fmt.Errorf("failed to create genai client: %w", err)
    	}
    
    	modelName := "gemini-2.0-flash-001"
    	contents := []*genai.Content{
    		{Parts: []*genai.Part{
    			{Text: `Analyze the provided video file, including its audio.
    Summarize the main points of the video concisely.
    Create a chapter breakdown with timestamps for key sections or topics discussed.`},
    			{FileData: &genai.FileData{
    				FileURI:  "gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
    				MIMEType: "video/mp4",
    			}},
    		}},
    	}
    
    	resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)
    	if err != nil {
    		return fmt.Errorf("failed to generate content: %w", err)
    	}
    
    	respText, err := resp.Text()
    	if err != nil {
    		return fmt.Errorf("failed to convert model response to text: %w", err)
    	}
    	fmt.Fprintln(w, respText)
    
    	// Example response:
    	// Here's an analysis of the provided video file:
    	//
    	// **Summary**
    	//
    	// The video features Saeka Shimada, a photographer in Tokyo, who uses the new Pixel phone ...
    	//
    	// **Chapter Breakdown**
    	//
    	// *   **0:00-0:05**: Introduction to Saeka Shimada and her work as a photographer in Tokyo.
    	// ...
    
    	return nil
    }
    

    Gen AI SDK for Node.js

    安裝

    npm install @google/genai

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    const {GoogleGenAI} = require('@google/genai');
    
    const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
    const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
    
    async function generateContent(
      projectId = GOOGLE_CLOUD_PROJECT,
      location = GOOGLE_CLOUD_LOCATION
    ) {
      const ai = new GoogleGenAI({
        vertexai: true,
        project: projectId,
        location: location,
      });
    
      const prompt = `
      Analyze the provided video file, including its audio.
      Summarize the main points of the video concisely.
      Create a chapter breakdown with timestamps for key sections or topics discussed.
     `;
    
      const video = {
        fileData: {
          fileUri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
          mimeType: 'video/mp4',
        },
      };
    
      const response = await ai.models.generateContent({
        model: 'gemini-2.0-flash',
        contents: [video, prompt],
      });
    
      console.log(response.text);
    
      return response.text;
    }

    Gen AI SDK for Java

    瞭解如何安裝或更新 Gen AI SDK for Java

    詳情請參閱 SDK 參考說明文件

    設定環境變數,透過 Vertex AI 使用 Gen AI SDK:

    # Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
    # with appropriate values for your project.
    export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
    export GOOGLE_CLOUD_LOCATION=global
    export GOOGLE_GENAI_USE_VERTEXAI=True

    
    import com.google.genai.Client;
    import com.google.genai.types.Content;
    import com.google.genai.types.GenerateContentResponse;
    import com.google.genai.types.HttpOptions;
    import com.google.genai.types.Part;
    
    public class GenerateContentWithVideo {
    
      public static void main(String[] args) {
        // TODO(developer): Replace these variables before running the sample.
        String modelId = "gemini-2.0-flash";
        String prompt = " Analyze the provided video file, including its audio.\n"
            + "    Summarize the main points of the video concisely.\n"
            + "    Create a chapter breakdown with timestamps for key sections or topics discussed.";
        generateContent(modelId, prompt);
      }
    
      public static String generateContent(String modelId, String prompt) {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        try (Client client = Client.builder()
            .httpOptions(HttpOptions.builder().apiVersion("v1").build())
            .build()) {
    
          GenerateContentResponse response =
              client.models.generateContent(modelId, Content.fromParts(
                      Part.fromText(prompt),
                      Part.fromUri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4", "video/mp4")),
                  null);
    
          System.out.print(response.text());
          // Example response:
          // Here's a breakdown of the video:
          //
          // **Summary:**
          //
          // Saeka Shimada, a photographer in Tokyo, uses the Google Pixel 8 Pro's "Video Boost" feature
          // to ...
          //
          // **Chapter Breakdown with Timestamps:**
          //
          // * **[00:00-00:12] Introduction & Tokyo at Night:** Saeka Shimada introduces herself ...
          return response.text();
        }
      }
    }

    C#

    如要傳送提示要求,請建立 C# 檔案 (.cs),然後將下列程式碼複製到該檔案中。將 your-project-id 設為您的 Google Cloud 專案 ID。更新值後,請執行程式碼。

    
    using Google.Cloud.AIPlatform.V1;
    using System;
    using System.Threading.Tasks;
    
    public class VideoInputWithAudio
    {
        public async Task<string> DescribeVideo(
            string projectId = "your-project-id",
            string location = "us-central1",
            string publisher = "google",
            string model = "gemini-2.0-flash-001")
        {
    
            var predictionServiceClient = new PredictionServiceClientBuilder
            {
                Endpoint = $"{location}-aiplatform.googleapis.com"
            }.Build();
    
            string prompt = @"Provide a description of the video.
    The description should also contain anything important which people say in the video.";
    
            var generateContentRequest = new GenerateContentRequest
            {
                Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
                Contents =
                {
                    new Content
                    {
                        Role = "USER",
                        Parts =
                        {
                            new Part { Text = prompt },
                            new Part { FileData = new() { MimeType = "video/mp4", FileUri = "gs://cloud-samples-data/generative-ai/video/pixel8.mp4" }}
                        }
                    }
                }
            };
    
            GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);
    
            string responseText = response.Candidates[0].Content.Parts[0].Text;
            Console.WriteLine(responseText);
    
            return responseText;
        }
    }
    

    REST

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    https://aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/publishers/google/models/${MODEL_ID}:generateContent -d \
    $'{
      "contents": {
        "role": "user",
        "parts": [
          {
          "fileData": {
            "mimeType": "video/mp4",
            "fileUri": "gs://cloud-samples-data/generative-ai/video/pixel8.mp4"
            }
          },
          {
            "text": "Provide a description of the video. The description should also contain anything important which people say in the video."
          }
        ]
      }
    }'

    模型會傳回回覆。請注意,系統會分段生成回覆,並分別評估每個段落的安全性。

    後續步驟