Vertex AI 中的 Gemini API 快速入门

本快速入门将向您介绍如何安装适用于您所选语言的 Google Gen AI SDK,然后发出您的第一个 API 请求。示例会因您是使用 API 密钥还是应用默认凭据 (ADC) 进行身份验证而略有不同。

选择身份验证方法:


准备工作

如果您尚未设置应用默认凭据,请参阅配置应用默认凭据

设置环境

Python

  1. 使用 Python 3.9 或更高版本安装 Google Gen AI SDK:

    pip install -q -U google-genai
  2. 设置环境变量:

    export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT_ID

REST

设置环境变量:

export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT_ID

提交第一个请求

使用 generateContent 方法向 Vertex AI 中的 Gemini API 发送请求:

Python

from google import genai

client = genai.Client(vertexai=True, project="${GOOGLE_CLOUD_PROJECT}", location="global")

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)
      

REST

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1/projects/${GOOGLE_CLOUD_PROJECT}/locations/global/publishers/google/models/gemini-2.0-flash-001:generateContent -d \
$'{
  "contents": {
    "role": "user",
    "parts": {
      "text": "Explain how AI works in a few words"
    }
  }
}'
      

生成图片

Gemini 可以通过对话方式生成和处理图片。您可以使用文本、图片或两者结合来提示 Gemini,以执行各种与图片相关的任务,例如图片生成和编辑。以下代码演示了如何根据描述性问题生成图片:

您必须在配置中添加 responseModalities: ["TEXT", "IMAGE"]。这些模型不支持仅输出图片。

Python

from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import base64

client = genai.Client(vertexai=True, project="${GOOGLE_CLOUD_PROJECT}", location="global")

contents = ('Hi, can you create an image of the Eiffel tower with fireworks in the background?')

response = client.models.generate_content(
    model="gemini-2.0-flash-preview-image-generation",
    contents=contents,
    config=types.GenerateContentConfig(
      response_modalities=['TEXT', 'IMAGE']
    )
)


for part in response.candidates[0].content.parts:
  if part.text is not None:
    print(part.text)
  elif part.inline_data is not None:
    image = Image.open(BytesIO((part.inline_data.data)))
    image.save('gemini-native-image.png')
    image.show()
      

REST

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1/projects/${GOOGLE_CLOUD_PROJECT}/locations/global/publishers/google/models/gemini-2.0-flash-preview-image-generation:generateContent -d \
$'{
  "contents": {
    "role": "user",
    "parts": {
      "text": "Can you create an image of the Eiffel tower with fireworks in the background?"
    }
  },
  "generationConfig": {
    "responseModalities":["TEXT","IMAGE"]
  }
}'
      

图片理解

Gemini 还可以理解图片。以下代码使用上一部分中生成的图片,并使用其他模型推断图片的相关信息:

Python

from google import genai
from google.genai import types


with open("gemini-native-image.png", "rb") as f:
    image = f.read()


client = genai.Client(vertexai=True, project="${GOOGLE_CLOUD_PROJECT}", location="global")


response = client.models.generate_content(
    model="gemini-flash-2.0",
    contents=[
        Part.from_bytes(data=image, mime_type="image/png"),
        "Write a short and engaging blog post based on this picture.",
    ],
)

print(response.text)
'''
Okay, here's a short and engaging blog post inspired by the image:

**Paris Aglow: A Night to Remember!**

WOW! Just look at this image. Fireworks exploding over Paris, the Eiffel Tower brilliantly lit – can you imagine a more magical sight? This is Paris at its most dazzling, a city transformed into a canvas of light and color.

The Eiffel Tower, the iconic symbol of Paris, stands proud amidst the spectacle.  It's illuminated in blue, adding to the magical atmosphere of the celebrations.

Whether it's a special occasion, a national holiday, or just a spontaneous celebration of life, Paris knows how to put on a show. This image is a reminder of the city's enduring beauty and its power to inspire awe. Have you ever witnessed the magic of Paris? Let me know in the comments!
'''
      

REST

  1. 为您使用的映像设置 shell 变量:

    export B64_BASE_IMAGE=YOUR_B64_ENCODED_IMAGE
  2. 运行以下命令:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    https://aiplatform.googleapis.com/v1/projects/${GOOGLE_CLOUD_PROJECT}/locations/global/publishers/google/models/gemini-2.0-flash:generateContent -d \
    $'{
      "contents": [
        {
          "role": "user",
          "parts": [
            {"text": "Write a short and engaging blog post based on this picture."},
            {"inlineData": {
                "data": "${B64_BASE_IMAGE}",
                "mimeType": "image/png",
              }
            }
          ]
        }
      ]
    }'
              

代码执行

Vertex AI 代码执行功能中的 Gemini API 可让模型生成和运行 Python 代码,并从结果中迭代学习,直到获得最终输出。Vertex AI 提供代码执行作为工具,类似于函数调用。您可以使用此代码执行功能来构建可从基于代码的推理中受益并生成文本输出的应用。例如:

Python

from google import genai
from google.genai import types

client = genai.Client(vertexai=True, project="${GOOGLE_CLOUD_PROJECT}", location="global")

response = client.models.generate_content(
  model="gemini-2.0-flash",
  contents="What is the sum of the first 50 prime numbers? "
  "Generate and run code for the calculation, and make sure you get all 50.",
  config=types.GenerateContentConfig(
      tools=[types.Tool(code_execution=types.ToolCodeExecution)]
  ),
)

for part in response.candidates[0].content.parts:
  if part.text is not None:
      print(part.text)
  if part.executable_code is not None:
      print(part.executable_code.code)
  if part.code_execution_result is not None:
      print(part.code_execution_result.output)
      

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/${GOOGLE_CLOUD_PROJECT}/locations/us-central1/publishers/google/models/gemini-2.0-flash-001:generateContent -d \
$'{
  "tools": [{'codeExecution': {}}],
  "contents": {
    "role": "user",
    "parts": {
      "text": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50."
    }
  },
}'
      

如需查看更多代码执行示例,请参阅代码执行文档

后续步骤

现在,您已发出第一个 API 请求,不妨查看以下指南,了解如何为生产代码设置更高级的 Vertex AI 功能: