跳转到主要内容

支持的模型

目前仅以下模型支持在对话中进行图片生成与编辑:
  • gemini-2.5-flash-image
  • gemini-2.5-flash-image-preview
  • gemini-2.0-flash-preview-image-generation
  • gpt-4o-image
  • gpt-5-all
  • midjourney-chat
本教程只是基本示例,关于 Gemini 画图模型更详细的教程请参考 Gemini 2.5 Flash 画图模型教程

生成图片

以下是通过对话画图的请求示例,和普通文本对话并无不同:
curl --location --request POST 'https://geekai.co/api/v1/chat/completions' \
--header 'Authorization: Bearer $GEEKAI_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "model": "gemini-2.5-flash-image",
    "messages": [
        {
            "role": "user",
            "content": "画一只可爱的小猫在草丛中玩耍"
        }
    ]
}'
gpt-4o-imagegpt-5-allmidjourney-chat 支持流式输出,gemini-2.5-flash-image-previewgemini-2.5-flash-image 不支持流式输出。 在响应对象中,image.url 字段中会包含生成的图片 URL 地址,你可以获取进行显示,也可以获取 content 字段值直接在支持 Markdown 渲染的组件中显示:
{
    "id": "1061a101-5fe0-44bd-9281-6fb6565dbd0a",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "好的,这是一只在草丛中玩耍的可爱小猫: \n![](https://static.geekai.co/image/2025/09/22/81e1cc5fc52a70c738f99b9037957bb4.png)\n",
                "image": {
                    "url": "https://static.geekai.co/image/2025/09/22/81e1cc5fc52a70c738f99b9037957bb4.png"
                }
            },
            "finish_reason": "stop"
        }
    ],
    "model": "gemini-2.5-flash-image",
    "object": "chat.completion",
    "usage": {
        ...
    }
}

图片编辑

如果你想要以图生图,或者在已有的图片上进行修改,可以结合图片对话请求实现:
curl --location --request POST 'https://geekai.co/api/v1/chat/completions' \
--header 'Authorization: Bearer {YOUR_GEEKAI_API_KEY}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "model": "gemini-2.5-flash-image",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "将图片中的中文替换成英文GeekAI,字体风格大小保持不变"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://static.geekai.co/icon/geekai-logo-main-tr.png"
            }
          }
        ]
      }
    ]
}'
在上面的请求中,image_url 字段的 url 属性可以是任何有效的图片 URL 地址或者 Base64 编码的图片数据。

解决 Gemini 不出图问题

如果在使用 Gemini 画图模型进行图像生成时遇到不出图的问题,可以尝试以下解决方案:
  1. 设置系统提示:在请求中通过系统提示强化画图诉求,如 你是一个AI画图机器人,请根据用户需求进行画图
  2. 调整参数设置:在请求参数中将 image_generation 设置为 true,表示这条请求强制进行画图。
curl --location --request POST 'https://geekai.co/api/v1/chat/completions' \
--header 'Authorization: Bearer {YOUR_GEEKAI_API_KEY}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "model": "gemini-2.5-flash-image-preview",
    "messages": [
        {
            "role": "system",
            "content": "你是一个AI画图机器人,请根据用户需求进行画图"
        },
        {
            "role": "user",
            "content": "一只可爱的小猫在草丛中玩耍"
        }
    ],
    "image_generation": true
}'

转到 Response API

OpenAI 全新的 Response API 支持在对话中引入画图工具进行图像生成和编辑,并支持除 gpt-4o 以外的更多模型以及更丰富的参数设置,关于如何在 Response API 中进行图像生成和编辑,请参考Response API 画图文档