推理 API

Vertex AI Gemini API 支持将多模态提示作为输入和输出文本或代码。

支持的模型

  • Gemini 1.0 Pro
    • gemini-1.0-pro
    • gemini-1.0-pro-001
    • gemini-1.0-pro-002
  • Gemini 1.0 Pro Vision
    • gemini-1.0-pro-vision
    • gemini-1.0-pro-vision-001
  • Gemini 1.0 Ultra
    • gemini-1.0-ultra
    • gemini-1.0-ultra-001
  • Gemini 1.0 Ultra Vision
    • gemini-1.0-ultra-vision
    • gemini-1.0-ultra-vision-001
  • Gemini 1.5 Pro
    • gemini-1.5-pro-preview-0409
  • Gemini Experimental
    • gemini-experimental

限制

  • 提供过多图片时延迟时间较长

语法

  • PROJECT_ID = PROJECT_ID
  • REGION = us-central1
  • MODEL_ID = gemini-1.5-pro-preview-0409

非流式

curl

https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:generateContent \
  -d '{
    "contents": [{
      ...
    }],
    "generation_config": {
      ...
    },
    "safety_settings": {
      ...
    }
  }'

Python

gemini_model = GenerativeModel(MODEL_ID)
generation_config = GenerationConfig(...)

model_response = gemini_model.generate_content([...], generation_config, safety_settings={...})

流式处理

curl

https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:streamGenerateContent \
  -d '{
    "contents": [{
      ...
    }],
    "generation_config": {
      ...
    },
    "safety_settings": {
      ...
    }
  }'

Python

gemini_model = GenerativeModel(MODEL_ID)
model_response = gemini_model.generate_content([...], generation_config, safety_settings={...}, stream=True)

参数列表

参数

contents

Content

与模型当前对话的内容。

对于单轮查询,这是单个实例。对于多轮查询,这是重复字段,包含对话记录和最新请求。

system_instruction

可选:Content

用户为模型提供的系统说明。

注意:只有文本应在各部分中使用,各部分的内容将在单独的段落中使用。

tools

可选。请参阅 Function Call API

tool_config

可选。请参阅 Function Call API

safety_settings

可选:SafetySetting

根据用于屏蔽不安全内容的请求设置。

GenerateContentResponse.candidates 上强制执行。

generation_config

可选:GenerationConfig

生成的配置

内容

该类包含两个主要属性:rolepartsrole 属性表示生成内容的个人,而 parts 属性包含多个元素,每个元素表示消息中的一段数据。

参数

role

可选:string

创建消息的实体的身份,应为 usermodel

  • user:表示消息是由真人发送的,通常是用户生成的消息。
  • model:表示消息由模型生成。

model 值用于在多轮对话期间将模型中的消息插入对话。

对于非多轮对话,此字段可以留空或未设置。

parts

Part

构成单条消息的有序部分的列表。不同的部分可能具有不同的 IANA MIME 类型

部分

参数

text

可选:string

文本提示或代码段。

inline_data

可选:Blob

原始字节中的内嵌数据。

file_data

可选:FileData

存储在文件中的数据。

function_call

可选:FunctionCall

它包含一个表示 FunctionDeclaration.name 的字符串和一个结构化 JSON 对象,该对象包含模型预测的函数调用的所有参数。

请参阅 Function Call API

function_response

可选:FunctionResponse

FunctionCall 的结果输出,其中包含表示 FunctionDeclaration.name 的字符串以及一个包含函数调用的任何输出的结构化 JSON 对象。它用作模型的上下文。

请参阅 Function Call API

video_metadata

可选:VideoMetadata

视频元数据。仅当视频数据以 inline_datafile_data 呈现时,才应指定元数据。

Blob

参数

mime_type

string

数据的 IANA MIME 类型

data

bytes

原始字节。

FileData

参数

mime_type

string

数据的 IANA MIME 类型

file_uri

字符串

存储数据的文件的 Cloud Storage URI

FunctionCall

参数

name

string

要调用的函数名称。

args

Struct

采用 JSON 对象格式的函数参数和值。

如需了解参数详情,请参阅 Function Calling API

FunctionResponse

参数

name

string

要调用的函数名称。

response

Struct

JSON 对象格式的函数响应。

VideoMetadata

参数

start_offset

可选:google.protobuf.Duration

视频的起始偏移量

end_offset

可选:google.protobuf.Duration

视频的结束偏移量

SafetySetting

参数

category

可选:HarmCategory

有害类别。

threshold

可选:HarmBlockThreshold

有害屏蔽阈值

max_influential_terms

可选:int

对安全得分贡献最大的影响字词的最大数量,可能导致潜在屏蔽。

method

可选:HarmBlockMethod

指定阈值是否用于概率或严重性分数。如果未指定,则阈值用于概率分数。

HarmCategory

参数

HARM_CATEGORY_UNSPECIFIED

未指定有害类别。

HARM_CATEGORY_HATE_SPEECH

有害类别为仇恨言论。

HARM_CATEGORY_DANGEROUS_CONTENT

有害类别属于危险内容。

HARM_CATEGORY_HARASSMENT

有害类别为骚扰。

HARM_CATEGORY_SEXUALLY_EXPLICIT

有害类别为露骨色情内容。

HarmBlockThreshold

参数

HARM_BLOCK_THRESHOLD_UNSPECIFIED

未指定的有害屏蔽阈值。

BLOCK_LOW_AND_ABOVE

屏蔽低阈值及以上(即屏蔽更多)。

BLOCK_MEDIUM_AND_ABOVE

屏蔽中等阈值及以上。

BLOCK_ONLY_HIGH

仅屏蔽高阈值(即屏蔽较少)。

BLOCK_NONE

全部不屏蔽。

HarmBlockMethod

参数

HARM_BLOCK_METHOD_UNSPECIFIED

未指定有害屏蔽方法。

SEVERITY

有害屏蔽方法会同时使用概率和严重程度得分。

PROBABILITY

有害屏蔽方法会使用概率得分。

GenerationConfig

参数

temperature

可选:float

控制预测的随机性。

top_p

可选:float

如果指定,则将进行核采样。

top_k

可选:float

如果指定,则使用 top-k 采样。

candidate_count

可选:int

要生成的候选对象数量。

max_output_tokens

可选:int

每条消息可生成的输出词元数上限。

stop_sequences

可选:List[string]

停止序列。

logprobs

可选:int

Logit 概率。

presence_penalty

可选:float

正面处罚。

frequency_penalty

可选:float

频率处罚。

logit_bias

可选:Dict[string, float]

Logit 偏差

echo

可选:bool

echo

seed

可选:int

种子

response_mime_type

可选:string (enum)

生成的候选文本的输出响应 mimetype。

支持的 mimetype:

  • text/plain:(默认)文本输出。
  • application/json:候选项中的 JSON 响应。
  • 需要提示模型输出适当的回答类型,否则行为未定义。

这是预览版功能。

示例

  • PROJECT_ID = PROJECT_ID
  • REGION = us-central1
  • MODEL_ID = gemini-1.0-pro

文本推理

curl

curl -X POST \
 -H "Authorization: Bearer $(gcloud auth print-access-token)" \
 -H "Content-Type: application/json" \
 https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:generateContent \
 -d '{
   "contents": [{
     "role": "user",
     "parts": [{
       "text": "Write a story about a magic backpack."
     }]
   }]
 }'

Python

import vertexai
from vertexai.generative_models import GenerativeModel

vertexai.init(project=PROJECT_ID, location=REGION)

gemini_model = GenerativeModel(MODEL_ID)
model_response = gemini_model.generate_content("Write a story about a magic backpack.")

print(model_response)

多模态

curl

curl -X POST \
 -H "Authorization: Bearer $(gcloud auth print-access-token)" \
 -H "Content-Type: application/json" \
 https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:generateContent \
 -d '{
   "contents": [{
     "role": "user",
     "parts": [
      {
        "text": "Are following video and image correlated?"
      },
      {
        "file_data": {"file_uri": "gs://cloud-samples-data/video/animals.mp4", "mime_type":"video/mp4"}
      },
      {
        "file_data": {"file_uri": "gs://generativeai-downloads/images/character.jpg", "mime_type":"image/jpeg"}
      }
     ]
   }]
 }'

Python

import vertexai
from vertexai.generative_models import GenerativeModel,Part

vertexai.init(project=PROJECT_ID, location=REGION)

gemini_model = GenerativeModel(MODEL_ID)
model_response = gemini_model.generate_content([
   "Are following video and image correlated?",
   Part.from_uri("gs://cloud-samples-data/video/animals.mp4", "video/mp4"),
   Part.from_uri("gs://generativeai-downloads/images/character.jpg", "image/jpeg")
])

print(model_response)

流式传输文本

curl

curl -X POST \
 -H "Authorization: Bearer $(gcloud auth print-access-token)" \
 -H "Content-Type: application/json" \
 https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:streamGenerateContent \
 -d '{
   "contents": [{
     "role": "user",
     "parts": [{
       "text": "Write a story about a magic backpack."
     }]
   }]
 }'

Python

import vertexai
from vertexai.generative_models import GenerativeModel

vertexai.init(project=PROJECT_ID, location=REGION)

gemini_model = GenerativeModel(MODEL_ID)
model_response = gemini_model.generate_content("Write a story about a magic backpack.", stream=True)

for resp in model_response:
  print(resp)

流式传输多模态

curl

curl -X POST \
 -H "Authorization: Bearer $(gcloud auth print-access-token)" \
 -H "Content-Type: application/json" \
 https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/publishers/google/models/${MODEL_ID}:streamGenerateContent \
 -d '{
   "contents": [{
     "role": "user",
     "parts": [
      {
        "text": "Are following video and image correlated?"
      },
      {
        "file_data": {"file_uri": "gs://cloud-samples-data/video/animals.mp4", "mime_type":"video/mp4"}
      },
      {
        "file_data": {"file_uri": "gs://generativeai-downloads/images/character.jpg", "mime_type":"image/jpeg"}
      }
     ]
   }]
 }'

Python

import vertexai
from vertexai.generative_models import GenerativeModel,Part

vertexai.init(project=PROJECT_ID, location=REGION)

gemini_model = GenerativeModel(MODEL_ID)
model_response = gemini_model.generate_content([
   "Are following video and image correlated?",
   Part.from_uri("gs://cloud-samples-data/video/animals.mp4", "video/mp4"),
   Part.from_uri("gs://generativeai-downloads/images/character.jpg", "image/jpeg")
], stream=True)

for resp in model_response:
  print(resp)

深入探索

如需了解详情,请参阅: