试用 Vertex AI Gemini API

本页介绍如何使用 Google Cloud 控制台、编程语言 SDK 或 REST API 快速开始向 Vertex AI Gemini API 发送请求。

Google Cloud 新手

在 Google Cloud 中进行设置

如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 300 美元赠金,用于运行、测试和部署工作负载。设置过程只需三个简短的步骤:

注册屏幕截图

使用以下按钮创建账号。操作完成后,请返回此页面完成此新手教程。如需使用此网站上提供的所有功能,请使用您的账号登录。

创建账号

如需详细了解如何在 Google Cloud 上进行设置,请参阅在 Google Cloud 上进行设置

向 Vertex AI Gemini API 发送请求

如需查看向 Vertex AI Gemini API 发送请求的说明,请选择以下标签页之一:

Python

  1. 执行以下操作,选择 Google Cloud 项目:
    1. 在 Google Cloud 控制台中,转到信息中心页面。

      转到“信息中心”页面

    2. 点击页面顶部的项目选择列表。 在随即显示的选择资源窗口中,选择一个项目。

    3. 记下项目信息部分中显示的项目 ID。您需要项目 ID 才能执行后续步骤。
  2. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  3. 在 Cloud Shell 中,通过运行以下命令安装或更新 Python 版 Vertex AI SDK:

    pip3 install "google-cloud-aiplatform>=1.38"
    
  4. 发送提示请求。将 PROJECT_ID 替换为您的 Google Cloud 项目的 ID。

    # TODO(developer): Vertex AI SDK - uncomment below & run
    # pip3 install --upgrade --user google-cloud-aiplatform
    # gcloud auth application-default login
    
    import vertexai
    from vertexai.generative_models import GenerativeModel, Part
    
    # Initialize Vertex AI
    vertexai.init(project=project_id, location=location)
    # Load the model
    multimodal_model = GenerativeModel(model_name="gemini-1.0-pro-vision-001")
    # Query the model
    response = multimodal_model.generate_content(
        [
            # Add an example image
            Part.from_uri(
                "gs://generativeai-downloads/images/scones.jpg", mime_type="image/jpeg"
            ),
            # Add an example query
            "what is shown in this image?",
        ]
    )
    print(response)
    return response.text
    
    

    如需详细了解如何安装、更新和使用 Python 版 Vertex AI SDK,请参阅安装 Python 版 Vertex AI SDKPython 版 Vertex AI SDK API 参考文档

Node.js

  1. 执行以下操作,选择 Google Cloud 项目:
    1. 在 Google Cloud 控制台中,转到信息中心页面。

      转到“信息中心”页面

    2. 点击页面顶部的项目选择列表。 在随即显示的选择资源窗口中,选择一个项目。

    3. 记下项目信息部分中显示的项目 ID。您需要项目 ID 才能执行后续步骤。
  2. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  3. 在 Cloud Shell 中,通过运行以下命令安装或更新 Node.js 版 Vertex AI SDK:

    npm install @google-cloud/vertexai
    
  4. 发送提示请求。将 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.0-pro-vision',
      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:');
      // Create the response stream
      const responseStream =
        await generativeVisionModel.generateContentStream(request);
    
      // Wait for the response stream to complete
      const aggregatedResponse = await responseStream.response;
    
      // Select the text from the response
      const fullTextResponse =
        aggregatedResponse.candidates[0].content.parts[0].text;
    
      console.log(fullTextResponse);
    }

    如需详细了解如何安装和使用 Vertex AI Node.js SDK,请参阅 Node.js 版 Vertex AI SDK 参考文档

Java

  1. 通过执行以下操作,选择 Google Cloud 项目:

    1. 在 Google Cloud 控制台中,转到信息中心页面。

      转到“信息中心”页面

    2. 点击页面顶部的项目选择列表。 在随即显示的选择资源窗口中,选择一个项目。

    3. 记下项目信息部分中显示的项目 ID。您需要项目 ID 才能执行后续步骤。

  2. 设置 Java 开发环境

  3. 运行以下命令来进行身份验证。将 PROJECT_ID 替换为您的 Google Cloud 项目 ID,并将 ACCOUNT 替换为您的 Google Cloud 用户名。

    gcloud config set project PROJECT_ID &&
    gcloud auth login ACCOUNT
    
  4. google-cloud-vertexai 添加为依赖项:

    <!--If you are using Maven with BOM, add the following in your pom.xml-->
    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>libraries-bom</artifactId>
          <version>26.32.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vertexai</artifactId>
      </dependency>
    </dependencies>
    
    <!--If you are using Maven without BOM, add the following to your pom.xml-->
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-vertexai</artifactId>
      <version>0.4.0</version>
    </dependency>
    
    <!--If you are using Gradle without BOM, add the following to your build.gradle-->
    implementation 'com.google.cloud:google-cloud-vertexai:0.4.0'
    
  5. 发送提示请求。将 projectID 设置为您的 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.0-pro-vision";
    
        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://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png";
    
          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();
        }
      }
    }

    如需详细了解如何安装和使用 Vertex AI Java 开发工具包 (JDK),请参阅 Vertex AI JDK 参考文档

Go

  1. 执行以下操作,选择 Google Cloud 项目:
    1. 在 Google Cloud 控制台中,转到信息中心页面。

      转到“信息中心”页面

    2. 点击页面顶部的项目选择列表。 在随即显示的选择资源窗口中,选择一个项目。

    3. 记下项目信息部分中显示的项目 ID。您需要项目 ID 才能执行后续步骤。
  2. 为 Go 开发准备好环境
  3. 查看可用的 Vertex AI API Go 软件包,以确定哪个软件包最符合您的项目需求:

    • 软件包 cloud.google.com/go/vertexai推荐

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

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

    • 软件包 cloud.google.com/go/aiplatform

      aiplatform 是自动生成的软件包。

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

  4. 通过运行以下命令之一,根据项目需求安装所需的 Go 软件包:

    # Human authored package. Recommended for most developers.
    go get cloud.google.com/go/vertexai
    
    # Auto-generated package. go get cloud.google.com/go/aiplatform
  5. 发送提示请求。将 PROJECT_ID 替换为您的 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.0-pro-vision-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
    }
    

    如需详细了解如何安装和使用 Go 版 Vertex AI SDK,请参阅 Go 版 Vertex AI SDK 参考文档

C#

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 C# 设置说明执行操作。如需了解详情,请参阅 Vertex AI C# API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


using Google.Api.Gax.Grpc;
using Google.Cloud.AIPlatform.V1;
using System.Collections.Generic;
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.0-pro-vision"
    )
    {
        // Create client
        var predictionServiceClient = new PredictionServiceClientBuilder
        {
            Endpoint = $"{location}-aiplatform.googleapis.com"
        }.Build();

        // Prompt
        string prompt = "What's in this photo";
        string imageUri = "gs://generativeai-downloads/images/scones.jpg";

        // Initialize request argument(s)
        var content = new Content
        {
            Role = "USER"
        };
        content.Parts.AddRange(new List<Part>()
        {
            new() {
                Text = prompt
            },
            new() {
                FileData = new() {
                    MimeType = "image/png",
                    FileUri = imageUri
                }
            }
        });

        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
            }
        };
        generateContentRequest.Contents.Add(content);

        // 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

  1. 通过执行以下操作,选择 Google Cloud 项目:

    1. 在 Google Cloud 控制台中,转到信息中心页面。

      转到“信息中心”页面

    2. 点击页面顶部的项目选择列表。 在随即显示的选择资源窗口中,选择一个项目。

    3. 记下项目信息部分中显示的项目 ID。您需要项目 ID 才能执行后续步骤。

  2. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

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

    MODEL_ID="gemini-1.0-pro-vision"
    PROJECT_ID="PROJECT_ID"
    
  4. 预配端点:

    gcloud beta services identity create --service=aiplatform.googleapis.com --project=PROJECT_ID
    
  5. 输入以下 curl 命令以发送提示请求:

    curl \
    -X POST \
    -H "Authorization: Bearer $(gcloud auth application-default 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}:streamGenerateContent -d \
    $'{
      "contents": {
        "role": "user",
        "parts": [
          {
          "fileData": {
            "mimeType": "image/jpeg",
            "fileUri": "gs://generativeai-downloads/images/scones.jpg"
            }
          },
          {
            "text": "Describe this picture."
          }
        ]
      }
    }'
    
  6. 如果系统要求您授权 Cloud Shell,请点击授权

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

控制台

使用 Vertex AI Studio 快速设计并迭代您的提示。提示准备就绪后,您可以使用任何受支持的编程语言获取提示的代码。

  1. 执行以下操作,选择 Google Cloud 项目:
    1. 在 Google Cloud 控制台中,转到信息中心页面。

      转到“信息中心”页面

    2. 点击页面顶部的项目选择列表。 在随即显示的选择资源窗口中,选择一个项目。

  2. 在 Google Cloud 控制台中,转到 Vertex AI Studio 页面。

    Vertex AI Studio

  3. 点击多模态

  4. 示例提示下,找到标题为从图片中提取文本的提示,然后点击打开

    系统随即会打开提示页面,并在提示字段中填充提示。

  5. 点击提交以提交提示。

    模型会返回回复。

  6. 点击 获取代码以查看此提示请求的代码等效项。

后续步骤