> ## Documentation Index
> Fetch the complete documentation index at: https://docs.geekai.co/llms.txt
> Use this file to discover all available pages before exploring further.

# 图像生成

### 支持的模型

目前仅以下模型支持在对话中进行图片生成与编辑：

* `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`

<Note>
  本教程只是基本示例，关于 Gemini 画图模型更详细的教程请参考 [Gemini 2.5 Flash 画图模型教程](https://docs.geekai.co/cn/docs/image/google/gemini-2.5-flash-image)。
</Note>

### 生成图片

以下是通过对话画图的请求示例，和普通文本对话并无不同：

<CodeGroup>
  ```bash curl theme={null}
  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": "画一只可爱的小猫在草丛中玩耍"
          }
      ]
  }'
  ```

  ```bash python theme={null}
  # 先安装 OpenAI SDK: `pip3 install openai`

  from openai import OpenAI

  client = OpenAI(api_key="$GEEKAI_API_KEY", base_url="https://geekai.co/api/v1")

  response = client.chat.completions.create(
      model="gemini-2.5-flash-image",
      messages=[
          {"role": "user", "content": "画一只可爱的小猫在草丛中玩耍"},
      ]
  )

  print(response.choices[0].message.content)
  ```

  ```bash javascript theme={null}
  // 先安装 OpenAI SDK: `npm install openai`

  import OpenAI from "openai";

  const openai = new OpenAI({
      baseURL: 'https://geekai.co/api/v1',
      apiKey: '$GEEKAI_API_KEY'
  });

  async function main() {
      const completion = await openai.chat.completions.create({
          messages: [{ role: "user", content: "画一只可爱的小猫在草丛中玩耍" }],
          model: "gemini-2.5-flash-image",
      });

      console.log(completion.choices[0].message.content);
  }

  main();
  ```

  ```bash go theme={null}
  package main

  import (
  	"bytes"
  	"encoding/json"
  	"fmt"
  	"io"
  	"net/http"
  )

  func main() {
  	requestBody := map[string]interface{}{
  		"model": "gemini-2.5-flash-image",
  		"messages": []interface{}{
              map[string]interface{}{
                  "role": "user", 
                  "content": "画一只可爱的小猫在草丛中玩耍",
              },
          }
  	}
  	
  	jsonData, err := json.Marshal(requestBody)
  	if err != nil {
  		panic(err)
  	}

  	client := &http.Client{}
  	req, err := http.NewRequest("POST", "https://geekai.co/api/v1/chat/completions", bytes.NewBuffer(jsonData))
  	if err != nil {
  		panic(err)
  	}

  	req.Header.Set("Authorization", "Bearer $GEEKAI_API_KEY")
  	req.Header.Set("Content-Type", "application/json")

  	resp, err := client.Do(req)
  	if err != nil {
  		panic(err)
  	}
  	defer resp.Body.Close()

  	body, err := io.ReadAll(resp.Body)
  	if err != nil {
  		panic(err)
  	}

  	fmt.Println(string(body))
  }
  ```
</CodeGroup>

`gpt-4o-image`、`gpt-5-all`、`midjourney-chat` 支持流式输出，`gemini-2.5-flash-image-preview`、`gemini-2.5-flash-image` 不支持流式输出。

在响应对象中，`image.url` 字段中会包含生成的图片 URL 地址，你可以获取进行显示，也可以获取 `content` 字段值直接在支持 Markdown 渲染的组件中显示：

```json theme={null}
{
    "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": {
        ...
    }
}
```

### 图片编辑

如果你想要以图生图，或者在已有的图片上进行修改，可以结合[图片对话](https://docs.geekai.co/cn/docs/chat/image)请求实现：

<CodeGroup>
  ```bash curl theme={null}
  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"
              }
            }
          ]
        }
      ]
  }'
  ```

  ```bash python theme={null}
  # 先安装 OpenAI SDK: `pip3 install openai`

  from openai import OpenAI

  client = OpenAI(api_key="$GEEKAI_API_KEY", base_url="https://geekai.co/api/v1")

  response = client.chat.completions.create(
      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"}}
          ]}
      ]
  )

  print(response.choices[0].message.content)
  ```

  ```bash javascript theme={null}
  // 先安装 OpenAI SDK: `npm install openai`

  import OpenAI from "openai";

  const openai = new OpenAI({
      baseURL: 'https://geekai.co/api/v1',
      apiKey: '$GEEKAI_API_KEY'
  });

  async function main() {
      const completion = await openai.chat.completions.create({
          messages: [{ role: "user", content: [
              { "type": "text", "text": "将图片中的中文替换成英文GeekAI，字体风格大小保持不变" },
              { "type": "image_url", "image_url": { "url": "https://static.geekai.co/icon/geekai-logo-main-tr.png" } }
          ]}],
          model: "gemini-2.5-flash-image",
      });

      console.log(completion.choices[0].message.content);
  }

  main();
  ```

  ```bash go theme={null}
  package main

  import (
  	"bytes"
  	"encoding/json"
  	"fmt"
  	"io"
  	"net/http"
  )

  func main() {
  	requestBody := map[string]interface{}{
  		"model": "gemini-2.5-flash-image",
  		"messages": []interface{}{
              map[string]interface{}{
                  "role": "user", 
                  "content": []interface{}{
                      map[string]interface{}{
                          "type": "text", 
                          "text": "将图片中的中文替换成英文GeekAI，字体风格大小保持不变",
                      },
                      map[string]interface{}{
                          "type": "image_url", 
                          "image_url": map[string]string{
                              "url": "https://static.geekai.co/icon/geekai-logo-main-tr.png",
                          },
                      },
                  },
              },
          }
  	}
  	
  	jsonData, err := json.Marshal(requestBody)
  	if err != nil {
  		panic(err)
  	}

  	client := &http.Client{}
  	req, err := http.NewRequest("POST", "https://geekai.co/api/v1/chat/completions", bytes.NewBuffer(jsonData))
  	if err != nil {
  		panic(err)
  	}

  	req.Header.Set("Authorization", "Bearer $GEEKAI_API_KEY")
  	req.Header.Set("Content-Type", "application/json")

  	resp, err := client.Do(req)
  	if err != nil {
  		panic(err)
  	}
  	defer resp.Body.Close()

  	body, err := io.ReadAll(resp.Body)
  	if err != nil {
  		panic(err)
  	}

  	fmt.Println(string(body))
  }
  ```
</CodeGroup>

在上面的请求中，`image_url` 字段的 `url` 属性可以是任何有效的图片 URL 地址或者 Base64 编码的图片数据。

### 解决 Gemini 不出图问题

如果在使用 Gemini 画图模型进行图像生成时遇到不出图的问题，可以尝试以下解决方案：

1. **设置系统提示**：在请求中通过系统提示强化画图诉求，如 `你是一个AI画图机器人，请根据用户需求进行画图`；
2. **调整参数设置**：在请求参数中将 `image_generation` 设置为 `true`，表示这条请求强制进行画图。

```bash theme={null}
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 画图文档](https://docs.geekai.co/cn/docs/response/image_generation)。
