支持的模型:
- 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)
参数列表
参数 | |
---|---|
|
与模型当前对话的内容。 对于单轮查询,这是单个实例。对于多轮查询,这是重复字段,包含对话记录和最新请求。 |
|
可选: 用户为模型提供的系统说明。 注意:只有文本应在各部分中使用,各部分的内容将在单独的段落中使用。 |
|
可选。请参阅 Function Call API |
|
可选。请参阅 Function Call API |
|
可选: 根据用于屏蔽不安全内容的请求设置。 在 |
|
可选: 生成的配置 |
内容
该类包含两个主要属性:role
和 parts
。role
属性表示生成内容的个人,而 parts
属性包含多个元素,每个元素表示消息中的一段数据。
参数 | |
---|---|
|
可选: 创建消息的实体的身份,应为
对于非多轮对话,此字段可以留空或未设置。 |
|
构成单条消息的有序部分的列表。不同的部分可能具有不同的 IANA MIME 类型。 |
部分
参数 | |
---|---|
|
可选: 文本提示或代码段。 |
|
可选: 原始字节中的内嵌数据。 |
|
可选: 存储在文件中的数据。 |
|
可选: 它包含一个表示 请参阅 Function Call API。 |
function_response |
可选:
请参阅 Function Call API。 |
video_metadata |
可选: 视频元数据。仅当视频数据以 |
Blob
参数 | |
---|---|
|
数据的 IANA MIME 类型。 |
|
原始字节。 |
FileData
参数 | |
---|---|
mime_type |
数据的 IANA MIME 类型。 |
file_uri |
字符串 存储数据的文件的 Cloud Storage URI |
FunctionCall
参数 | |
---|---|
|
要调用的函数名称。 |
|
采用 JSON 对象格式的函数参数和值。 如需了解参数详情,请参阅 Function Calling API。 |
FunctionResponse
参数 | |
---|---|
|
要调用的函数名称。 |
|
JSON 对象格式的函数响应。 |
VideoMetadata
参数 | |
---|---|
|
可选: 视频的起始偏移量 |
|
可选: 视频的结束偏移量 |
SafetySetting
参数 | |
---|---|
|
可选: 有害类别。 |
|
可选: 有害屏蔽阈值 |
|
可选: 对安全得分贡献最大的影响字词的最大数量,可能导致潜在屏蔽。 |
|
可选: 指定阈值是否用于概率或严重性分数。如果未指定,则阈值用于概率分数。 |
HarmCategory
参数 | |
---|---|
|
未指定有害类别。 |
|
有害类别为仇恨言论。 |
|
有害类别属于危险内容。 |
|
有害类别为骚扰。 |
|
有害类别为露骨色情内容。 |
HarmBlockThreshold
参数 | |
---|---|
|
未指定的有害屏蔽阈值。 |
|
屏蔽低阈值及以上(即屏蔽更多)。 |
|
屏蔽中等阈值及以上。 |
|
仅屏蔽高阈值(即屏蔽较少)。 |
|
全部不屏蔽。 |
HarmBlockMethod
参数 | |
---|---|
|
未指定有害屏蔽方法。 |
|
有害屏蔽方法会同时使用概率和严重程度得分。 |
|
有害屏蔽方法会使用概率得分。 |
GenerationConfig
参数 | |
---|---|
|
可选: 控制预测的随机性。 |
|
可选: 如果指定,则将进行核采样。 |
|
可选: 如果指定,则使用 top-k 采样。 |
|
可选: 要生成的候选对象数量。 |
|
可选:int 每条消息可生成的输出词元数上限。 |
|
可选: 停止序列。 |
|
可选: Logit 概率。 |
|
可选: 正面处罚。 |
|
可选: 频率处罚。 |
|
可选: Logit 偏差 |
|
可选: echo |
|
可选: 种子 |
|
可选: 生成的候选文本的输出响应 mimetype。 支持的 mimetype:
这是预览版功能。 |
示例
- 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)
深入探索
如需了解详情,请参阅: