快速入门:使用 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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  7. Enable the Vertex AI API.

    Enable the API

设置 Google Cloud CLI

在本地机器上,使用 Google Cloud CLI 进行设置和身份验证。如果您熟悉 Google AI Studio 中的 Gemini API,请注意,Vertex AI Gemini API 使用 Identity and Access Management(身份和访问管理)功能,而不是 API 密钥来管理访问权限。

  1. 安装并初始化 Google Cloud CLI。

  2. 如果您之前安装了 gcloud CLI,请运行以下命令,确保您的 gcloud 组件已更新。

    gcloud components update
  3. 如需使用 gcloud CLI 进行身份验证,请运行以下命令,生成本地应用默认凭据 (ADC) 文件。该命令启动的 Web 流程用于提供您的用户凭据。

    gcloud auth application-default login

    如需了解详情,请参阅设置应用默认凭据

为您的编程语言设置 SDK

在本地机器上,点击以下任一标签页,以安装适用于您的编程语言的 SDK。

Python

运行以下命令,安装并更新 Python 版 Vertex AI SDK。

pip3 install --upgrade "google-cloud-aiplatform>=1.64"

Node.js

运行以下命令,安装或更新 Node.js 版 aiplatform SDK。

npm install @google-cloud/vertexai

Java

如需将 google-cloud-vertexai 添加为依赖项,请为您的环境添加适当的代码。

带有 BOM 的 Maven

将以下 HTML 添加到 pom.xml

<dependencyManagement>
<dependencies>
  <dependency>
    <artifactId>libraries-bom</artifactId>
    <groupId>com.google.cloud</groupId>
    <scope>import</scope>
    <type>pom</type>
    <version>26.34.0</version>
  </dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-vertexai</artifactId>
</dependency>
</dependencies>

不带 BOM 的 Maven

将以下内容添加到 pom.xml

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-vertexai</artifactId>
  <version>0.4.0</version>
</dependency>

不带 BOM 的 Gradle

将以下内容添加到 build.gradle 中:

implementation 'com.google.cloud:google-cloud-vertexai:0.4.0'

Go

查看可用的 Vertex AI API Go 软件包,以确定哪个软件包最符合您的项目需求。

  • 推荐cloud.google.com/go/vertexai

    vertexai 是人工编写的软件包,可通过其访问常用功能和特征。

    对于大多数使用 Vertex AI API 进行构建的开发者,建议将此软件包作为起点。如需访问此软件包尚未涵盖的功能和特性,请改用自动生成的 aiplatform 软件包。

    如需安装此软件包,请运行以下命令。

    go get cloud.google.com/go/vertexai
  • cloud.google.com/go/aiplatform

    aiplatform 是自动生成的软件包。

    此软件包适用于需要访问人工编写的 vertexai 软件包尚未提供的 Vertex AI API 功能和特性的项目。

    如需安装此软件包,请运行以下命令。

    go get cloud.google.com/go/aiplatform

C#

安装 NuGet 提供的 Google.Cloud.AIPlatform.V1 软件包。使用您首选的方法将软件包添加到项目中。例如,在 Visual Studio 中右键点击项目,然后选择管理 NuGet 软件包...

REST

  1. 输入以下命令来配置环境变量。将 PROJECT_ID 替换为您的 Google Cloud 项目的 ID。

    MODEL_ID="gemini-1.5-flash-002"
    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 或在应用中添加代码来运行代码。

Python

如需发送提示请求,请创建一个 Python 文件 (.py),并将以下代码复制到该文件中。将 PROJECT_ID 的值设置为您的 Google Cloud 项目的 ID。更新值后,运行代码。

import vertexai
from vertexai.generative_models import GenerativeModel

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-002")

response = model.generate_content(
    "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?"
)

print(response.text)
# Example response:
# **Emphasizing the Dried Aspect:**
# * Everlasting Blooms
# * Dried & Delightful
# * The Petal Preserve
# ...

Node.js

如需发送提示请求,请创建一个 Node.js 文件 (.js),并将以下代码复制到该文件中。将 PROJECT_ID 替换为您的 Google Cloud 项目的 ID。更新值后,运行代码。

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function generate_from_text_input(projectId = 'PROJECT_ID') {
  const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});

  const generativeModel = vertexAI.getGenerativeModel({
    model: 'gemini-1.5-flash-001',
  });

  const prompt =
    "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";

  const resp = await generativeModel.generateContent(prompt);
  const contentResponse = await resp.response;
  console.log(JSON.stringify(contentResponse));
}

Java

如需发送提示请求,请创建一个 Java 文件 (.java),并将以下代码复制到该文件中。将 your-google-cloud-project-id 设置为您的 Google Cloud 项目 ID。更新值后,运行代码。

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.ResponseHandler;
import java.io.IOException;

public class TextInput {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";
    String textPrompt =
        "What's a good name for a flower shop that specializes in selling bouquets of"
            + " dried flowers?";

    String output = textInput(projectId, location, modelName, textPrompt);
    System.out.println(output);
  }

  // Passes the provided text input to the Gemini model and returns the text-only response.
  // For the specified textPrompt, the model returns a list of possible store names.
  public static String textInput(
      String projectId, String location, String modelName, String textPrompt) throws IOException {
    // 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 (VertexAI vertexAI = new VertexAI(projectId, location)) {
      GenerativeModel model = new GenerativeModel(modelName, vertexAI);

      GenerateContentResponse response = model.generateContent(textPrompt);
      String output = ResponseHandler.getText(response);
      return output;
    }
  }
}

Go

如需发送提示请求,请创建一个 Go 文件 (.go),并将以下代码复制到该文件中。将 projectID 替换为您的 Google Cloud 项目的 ID。更新值后,运行代码。

import (
	"context"
	"encoding/json"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
)

func generateContentFromText(w io.Writer, projectID string) error {
	location := "us-central1"
	modelName := "gemini-1.5-flash-001"

	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("error creating client: %w", err)
	}
	gemini := client.GenerativeModel(modelName)
	prompt := genai.Text(
		"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?")

	resp, err := gemini.GenerateContent(ctx, prompt)
	if err != nil {
		return fmt.Errorf("error generating content: %w", err)
	}
	// See the JSON response in
	// https://pkg.go.dev/cloud.google.com/go/vertexai/genai#GenerateContentResponse.
	rb, err := json.MarshalIndent(resp, "", "  ")
	if err != nil {
		return fmt.Errorf("json.MarshalIndent: %w", err)
	}
	fmt.Fprintln(w, string(rb))
	return nil
}

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-1.5-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://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/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 示例图片)。

Python

如需发送提示请求,请创建一个 Python 文件 (.py),并将以下代码复制到该文件中。将 PROJECT_ID 的值设置为您的 Google Cloud 项目的 ID。更新值后,运行代码。

import vertexai

from vertexai.generative_models import GenerativeModel, Part

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-002")

response = model.generate_content(
    [
        Part.from_uri(
            "gs://cloud-samples-data/generative-ai/image/scones.jpg",
            mime_type="image/jpeg",
        ),
        "What is shown in this image?",
    ]
)

print(response.text)
# That's a lovely overhead shot of a rustic-style breakfast or brunch spread.
# Here's what's in the image:
# * **Blueberry scones:** Several freshly baked blueberry scones are arranged on parchment paper.
# They look crumbly and delicious.
# ...

Node.js

如需发送提示请求,请创建一个 Node.js 文件 (.js),并将以下代码复制到该文件中。将 PROJECT_ID 替换为您的 Google Cloud 项目的 ID。更新值后,运行代码。

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function createNonStreamingMultipartContent(
  projectId = 'PROJECT_ID',
  location = 'us-central1',
  model = 'gemini-1.5-flash-001',
  image = 'gs://generativeai-downloads/images/scones.jpg',
  mimeType = 'image/jpeg'
) {
  // Initialize Vertex with your Cloud project and location
  const vertexAI = new VertexAI({project: projectId, location: location});

  // Instantiate the model
  const generativeVisionModel = vertexAI.getGenerativeModel({
    model: model,
  });

  // For images, the SDK supports both Google Cloud Storage URI and base64 strings
  const filePart = {
    fileData: {
      fileUri: image,
      mimeType: mimeType,
    },
  };

  const textPart = {
    text: 'what is shown in this image?',
  };

  const request = {
    contents: [{role: 'user', parts: [filePart, textPart]}],
  };

  console.log('Prompt Text:');
  console.log(request.contents[0].parts[1].text);

  console.log('Non-Streaming Response Text:');

  // Generate a response
  const response = await generativeVisionModel.generateContent(request);

  // Select the text from the response
  const fullTextResponse =
    response.response.candidates[0].content.parts[0].text;

  console.log(fullTextResponse);
}

Java

如需发送提示请求,请创建一个 Java 文件 (.java),并将以下代码复制到该文件中。将 your-google-cloud-project-id 设置为您的 Google Cloud 项目 ID。更新值后,运行代码。

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.ContentMaker;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.PartMaker;
import java.io.IOException;

public class Quickstart {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    String output = quickstart(projectId, location, modelName);
    System.out.println(output);
  }

  // Analyzes the provided Multimodal input.
  public static String quickstart(String projectId, String location, String modelName)
      throws IOException {
    // 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 (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String imageUri = "gs://generativeai-downloads/images/scones.jpg";

      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      GenerateContentResponse response = model.generateContent(ContentMaker.fromMultiModalData(
          PartMaker.fromMimeTypeAndData("image/png", imageUri),
          "What's in this photo"
      ));

      return response.toString();
    }
  }
}

Go

如需发送提示请求,请创建一个 Go 文件 (.go),并将以下代码复制到该文件中。将 projectID 替换为您的 Google Cloud 项目的 ID。更新值后,运行代码。

import (
	"context"
	"encoding/json"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
)

func tryGemini(w io.Writer, projectID string, location string, modelName string) error {
	// location := "us-central1"
	// modelName := "gemini-1.5-flash-001"

	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("error creating client: %w", err)
	}
	gemini := client.GenerativeModel(modelName)

	img := genai.FileData{
		MIMEType: "image/jpeg",
		FileURI:  "gs://generativeai-downloads/images/scones.jpg",
	}
	prompt := genai.Text("What is in this image?")

	resp, err := gemini.GenerateContent(ctx, img, prompt)
	if err != nil {
		return fmt.Errorf("error generating content: %w", err)
	}
	rb, err := json.MarshalIndent(resp, "", "  ")
	if err != nil {
		return fmt.Errorf("json.MarshalIndent: %w", err)
	}
	fmt.Fprintln(w, string(rb))
	return nil
}

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-1.5-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://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/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 调用来发送此提示请求。

Python

如需发送提示请求,请创建一个 Python 文件 (.py),并将以下代码复制到该文件中。将 PROJECT_ID 的值设置为您的 Google Cloud 项目的 ID。更新值后,运行代码。


import vertexai
from vertexai.generative_models import GenerativeModel, Part

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-002")

prompt = """
Provide a description of the video.
The description should also contain anything important which people say in the video.
"""

video_file = Part.from_uri(
    uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
    mime_type="video/mp4",
)

contents = [video_file, prompt]

response = model.generate_content(contents)
print(response.text)
# Example response:
# Here is a description of the video.
# ... Then, the scene changes to a woman named Saeko Shimada..
# She says, "Tokyo has many faces. The city at night is totally different
# from what you see during the day."
# ...

Node.js

如需发送提示请求,请创建一个 Node.js 文件 (.js),并将以下代码复制到该文件中。将 PROJECT_ID 替换为您的 Google Cloud 项目的 ID。更新值后,运行代码。

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function analyze_video_with_audio(projectId = 'PROJECT_ID') {
  const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});

  const generativeModel = vertexAI.getGenerativeModel({
    model: 'gemini-1.5-flash-001',
  });

  const filePart = {
    file_data: {
      file_uri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
      mime_type: 'video/mp4',
    },
  };
  const textPart = {
    text: `
    Provide a description of the video.
    The description should also contain anything important which people say in the video.`,
  };

  const request = {
    contents: [{role: 'user', parts: [filePart, textPart]}],
  };

  const resp = await generativeModel.generateContent(request);
  const contentResponse = await resp.response;
  console.log(JSON.stringify(contentResponse));
}

Java

如需发送提示请求,请创建一个 Java 文件 (.java),并将以下代码复制到该文件中。将 your-google-cloud-project-id 设置为您的 Google Cloud 项目 ID。更新值后,运行代码。


import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.ContentMaker;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.PartMaker;
import com.google.cloud.vertexai.generativeai.ResponseHandler;
import java.io.IOException;

public class VideoInputWithAudio {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    videoAudioInput(projectId, location, modelName);
  }

  // Analyzes the given video input, including its audio track.
  public static String videoAudioInput(String projectId, String location, String modelName)
      throws IOException {
    // 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 (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String videoUri = "gs://cloud-samples-data/generative-ai/video/pixel8.mp4";

      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      GenerateContentResponse response = model.generateContent(
          ContentMaker.fromMultiModalData(
              "Provide a description of the video.\n The description should also "
                  + "contain anything important which people say in the video.",
              PartMaker.fromMimeTypeAndData("video/mp4", videoUri)
          ));

      String output = ResponseHandler.getText(response);
      System.out.println(output);

      return output;
    }
  }
}

Go

如需发送提示请求,请创建一个 Go 文件 (.go),并将以下代码复制到该文件中。将 projectID 替换为您的 Google Cloud 项目的 ID。更新值后,运行代码。

import (
	"context"
	"errors"
	"fmt"
	"io"
	"mime"
	"path/filepath"

	"cloud.google.com/go/vertexai/genai"
)

// generateMultimodalContent shows how to send video and text prompts to a model, writing the response to
// the provided io.Writer.
func generateMultimodalContent(w io.Writer, projectID, location, modelName string) error {
	// location := "us-central1"
	// modelName := "gemini-1.5-flash-001"
	ctx := context.Background()

	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("unable to create client: %w", err)
	}
	defer client.Close()

	model := client.GenerativeModel(modelName)

	// Given a video file URL, prepare video file as genai.Part
	part := genai.FileData{
		MIMEType: mime.TypeByExtension(filepath.Ext("pixel8.mp4")),
		FileURI:  "gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
	}

	res, err := model.GenerateContent(ctx, part, genai.Text(`
			Provide a description of the video.
			The description should also contain anything important which people say in the video.
	`))
	if err != nil {
		return fmt.Errorf("unable to generate contents: %w", err)
	}

	if len(res.Candidates) == 0 ||
		len(res.Candidates[0].Content.Parts) == 0 {
		return errors.New("empty response from model")
	}

	fmt.Fprintf(w, "generated response: %s\n", res.Candidates[0].Content.Parts[0])
	return nil
}

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-1.5-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://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/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."
      }
    ]
  }
}'

模型会返回回复。 请注意,系统分多个部分生成回复,其中每个部分会分别评估安全性。

后续步骤