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

# 文生图

> 使用文本提示词生成图片

# 文生图

使用 OpenAI Images 兼容格式生成图片。

<Info>
  支持在 `reference_image`、`reference_images`、`image`、`images`、`input_image`、`input_image_url`、`mask` 等字段中传入图片资源。图片资源可以是公网 URL、`data:image/<format>;base64,<data>` 或 `asset://asset_xxx`；需要在提示词中引用素材时，使用 `{ "uri": "...", "label": "参考图" }`，详见 [上传资源](/llm-gateway/api/assets-upload)。
</Info>

## 请求参数

<ParamField body="model" type="string" required>
  图片生成模型 ID
</ParamField>

<ParamField body="prompt" type="string" required>
  图片生成提示词
</ParamField>

<ParamField body="size" type="string">
  图片尺寸，例如 `1024x1024`、`2k`、`3k`。网关会自动适配上游模型的尺寸要求
</ParamField>

<ParamField body="n" type="integer">
  生成图片数量
</ParamField>

<ParamField body="response_format" type="string">
  返回格式，通常为 `url`
</ParamField>

<ParamField body="reference_image" type="string">
  参考图片。直接传资源字符串：公网 URL、data URL 或 `asset://asset_xxx`。需要在 `prompt` 中引用时，可传 `{ "uri": "...", "label": "参考图" }`
</ParamField>

<ParamField body="reference_images" type="array">
  多张参考图片。数组元素支持与 `reference_image` 相同的写法；每个对象可以设置不同 `label`
</ParamField>

## 响应参数

<ResponseField name="created" type="integer">
  创建时间戳
</ResponseField>

<ResponseField name="data" type="array">
  生成结果列表

  <Expandable title="data">
    <ResponseField name="url" type="string">
      图片访问地址
    </ResponseField>
  </Expandable>
</ResponseField>

## 代码示例

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.chenyu.cn/v1/images/generations"
  headers = {
      "Authorization": "Bearer your_api_key",
      "Content-Type": "application/json"
  }
  payload = {
      "model": "doubao-seedream-5-0-lite-260128",
      "prompt": "电商主图，白底，一瓶护肤品，柔和布光",
      "size": "1024x1024",
      "reference_image": "asset://asset_reference_image"
  }

  response = requests.post(url, headers=headers, json=payload)
  print(response.json()["data"][0]["url"])
  ```

  ```javascript JavaScript theme={null}
  const axios = require('axios');

  axios.post('https://api.chenyu.cn/v1/images/generations', {
    model: 'doubao-seedream-5-0-lite-260128',
    prompt: '电商主图，白底，一瓶护肤品，柔和布光',
    size: '1024x1024',
    reference_image: 'asset://asset_reference_image'
  }, {
    headers: {
      Authorization: 'Bearer your_api_key',
      'Content-Type': 'application/json'
    }
  }).then((response) => {
    console.log(response.data.data[0].url);
  });
  ```

  ```curl cURL theme={null}
  curl -X POST "https://api.chenyu.cn/v1/images/generations" \
    -H "Authorization: Bearer your_api_key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "doubao-seedream-5-0-lite-260128",
      "prompt": "电商主图，白底，一瓶护肤品，柔和布光",
      "size": "1024x1024",
      "reference_image": "asset://asset_reference_image"
    }'
  ```
</CodeGroup>

## 响应示例

```json theme={null}
{
  "created": 1780000000,
  "data": [
    {
      "url": "https://example.com/generated.png"
    }
  ]
}
```

## 提示词引用素材

在参考图对象中传 `label` 后，可以在 `prompt` 里用 `@label` 指向该图片。`label` 不需要带 `@`，同一请求内不能重复。

```json theme={null}
{
  "model": "doubao-seedream-5-0-lite-260128",
  "prompt": "参考 @产品图 的主体结构，生成一张白底电商主图",
  "size": "1024x1024",
  "reference_image": {
    "uri": "asset://asset_product_reference",
    "label": "产品图"
  }
}
```
