图片生成 / Imagen 4
POST v1/chat/generationsGemini 绘图和视频生成指引
Imagen 绘图
Imagen 是 Google 推出的先进图像生成 AI 模型系列,能够根据文本提示创建高质量、逼真的图像。本指南将帮助您了解如何使用
Imagen 系列 API 生成图像,包括参数设置、模型选择和代码示例。
可用模型列表:
imagen-4.0-generate-preview-05-20:最新的正式预览版
imagen-4.0-ultra-generate-exp-05-20:更高级的 4.0 实验款
imagen-3.0-generate-002:3.0 正式版
目前 Imagen 仅支持英文提示词(prompt),集成时建议增加自动翻译,让用户能够无障碍使用
绘制大量文本的表现不稳定,建议只绘制重点关键词
抢先体验期间,Imagen 系列模型同价,后续可能会按官方正式价格调整。
模型参数
Imagen 目前仅支持英文提示词,并提供以下参数:
numberOfImages: 要生成的图像数量,范围从 1 到 4(含)。默认值为 4。另外注意 imagen-4.0-ultra-generate-exp-05-20 单次只能生成 1 张。
aspectRatio: 更改生成图像的宽高比。支持的值有 “1:1”、“3:4”、“4:3”、“9:16” 和 “16:9”。默认值为 “1:1”。
personGeneration: 允许模型生成人物图像。支持以下值:
“DONT_ALLOW”: 阻止生成人物图像。
“ALLOW_ADULT”: 生成成人图像,但不生成儿童图像。这是默认值。
请求参数
Authorization
在 Header 添加参数 Authorization,其值为在 Bearer 之后拼接 Token。
Authorization: Bearer ******************Header 参数
| 参数 | 类型 | 必填 | 说明 | 示例 |
|---|---|---|---|---|
| Content-Type | string | 可选 | 请求体的数据格式类型 | application/json |
| Accept | string | 可选 | 客户端期望接收的响应数据格式 | application/json |
Body 参数
Content-Type: application/json
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 必需 | 模型 ID。指定要使用的模型(例如 gpt-3.5-turbo, gpt-4 等)。 |
| messages | array [object] | 必需 | 聊天记录。包含对话历史的对象数组。 |
| ├─ role | string | 可选 | 角色类型,通常为 user、assistant 或 system。 |
| └─ content | string | 可选 | 具体的文本内容。 |
| prompt | string | 必需 | 提示词。注:在标准的 Chat API 中通常使用 messages,此字段多见于 Completion API 或特定平台的封装。 |
| response_format | string | 必需 | 响应格式。指定模型输出的格式(例如 JSON 模式)。 |
| stream | boolean | 必需 | 流式输出。若为 true,则通过服务器发送事件流式传输响应。 |
| extra_body | object | 必需 | 扩展请求体。用于传递特定平台的额外参数。 |
| object | 必需 | Google 配置。针对 Google 模型或平台的特定配置对象。 | |
| temperature | number | 可选 | 采样温度 (0-2)。值越高输出越随机,值越低输出越确定。建议只调整此参数或 top_p 其中之一。 |
| top_p | number | 可选 | 核采样。例如 0.1 意味着只考虑概率质量前 10% 的 token。 |
| n | integer | 必需 | 生成数量。为每个输入生成多少个选择。保持 n=1 可降低成本。 |
| stop | string | 可选 | 停止序列。API 遇到这些序列时停止生成(最多 4 个)。注:最新的推理模型可能不支持。 |
| max_completion_tokens | integer | 可选 | 最大生成长度。生成的 token 上限(包含可见输出和推理 token)。 |
| presence_penalty | number | 可选 | 存在惩罚 (-2.0 到 2.0)。正值增加模型讨论新话题的可能性。 |
| frequency_penalty | number | 可选 | 频率惩罚 (-2.0 到 2.0)。正值降低模型逐字重复同一行的可能性。 |
| logit_bias | string | 可选 | 对数偏差。修改特定标记出现的概率(映射 token ID 到 -100 到 100 的偏差值)。 |
| user | string | 可选 | 用户标识。代表最终用户的唯一 ID,用于监控滥用行为。 |
请求示例 (JSON)
{
"model": "imagen-4.0-generate-preview-06-06",
"prompt": "a portrait of a sheepadoodle wearing a cape",
"response_format": "b64_json",
"n": 1
}返回响应
200 成功
Content-Type: application/json
响应参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | string | 必需 | 聊天补全对象的唯一标识符。 |
| object | string | 必需 | 对象类型,通常为 "chat.completion"。 |
| created | integer | 必需 | 补全创建时的 Unix 时间戳(秒)。 |
| model | string | 必需 | 用于生成此补全的模型。 |
| choices | array [object] | 必需 | 模型生成的补全列表。 |
| └─index | integer | 可选 | 此补全在其列表中的索引。 |
| └─message | object | 可选 | 包含模型生成的响应消息的对象。 |
| └─logprobs | null | 可选 | 如果请求中设置了 logprobs,则包含标记的对数概率。 |
| └─finish_reason | string | 可选 | 模型停止生成补全的原因。可能是 "stop"、"length" 或 "tool_calls"。 |
| usage | object | 必需 | 此补全请求使用的令牌统计信息。 |
| └─prompt_tokens | integer | 必需 | 提示中使用的令牌数量。 |
| └─completion_tokens | integer | 必需 | 补全中生成的令牌数量。 |
| └─total_tokens | integer | 必需 | 使用的总令牌数(提示 + 补全)。 |
响应示例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}示例代码
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}