# OpenAI

## 1. 概述

业界第一大语言模型。根据文字提示生成图片。

**模型列表：**

* `gpt-image-2`

## 2. 请求说明

* **请求方法**:`POST`
* **请求地址**:

  > `https://gateway.theturbo.ai/v1/images/generations`

***

## 3. 请求参数

### 3.1 Header 参数

| 参数名称            | 类型     | 必填 | 说明                                         | 示例值                    |
| --------------- | ------ | -- | ------------------------------------------ | ---------------------- |
| `Content-Type`  | string | 是  | 设置请求头类型，必须为 `application/json`             | `application/json`     |
| `Accept`        | string | 是  | 设置响应类型，建议统一为 `application/json`            | `application/json`     |
| `Authorization` | string | 是  | 身份验证所需的 API\_KEY，格式 `Bearer $YOUR_API_KEY` | `Bearer $YOUR_API_KEY` |

***

### 3.2 Body 参数 (application/json)

<table><thead><tr><th width="96.3828125">参数名称</th><th width="98.30078125">类型</th><th width="107.2578125">必填</th><th width="278.05078125">说明</th><th>示例（默认值）</th></tr></thead><tbody><tr><td><strong>model</strong></td><td>string</td><td>是</td><td>要使用的模型 ID。详见<a href="#1-概述">概述</a>列出的可用版本，如 <code>gpt-image-2</code>。</td><td><code>gpt-image-2</code></td></tr><tr><td><strong>prompt</strong></td><td>string</td><td>是</td><td>一段描述所需图像的文字<code>gpt-image-2</code> 描述最大长度为 32000 字符。</td><td><code>A cute baby sea otter</code></td></tr><tr><td>n</td><td>number</td><td>否</td><td>生成图像的数量，必须在 1 到 10 之间。<code>gpt-image-2</code> 仅支持 n=1。</td><td><code>1</code></td></tr><tr><td>size</td><td>string</td><td>否</td><td>生成图像的尺寸。<code>gpt-image-2</code>支持<code>1024x1024</code>、<code>1536x1024</code>、<code>1024x1536</code>。</td><td><code>1024x1024</code></td></tr><tr><td>quality</td><td>string</td><td>否</td><td>生成图像的质量选项。<code>gpt-image-2</code>支持<code>high</code> 、<code>medium</code> 、<code>low</code>。</td><td><code>high</code></td></tr></tbody></table>

***

## 4. 请求示例

{% tabs %}
{% tab title="HTTP" %}

```http
POST /v1/images/generations
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "gpt-image-2",
	"prompt": "A cute baby sea otter",
	"n": 1,
	"size": "1024x1024"
}
```

{% endtab %}

{% tab title="Shell" %}

```sh
curl https://gateway.theturbo.ai/v1/images/generations \
	-H "Content-Type: application/json" \
	-H "Accept: application/json" \
	-H "Authorization: Bearer $YOUR_API_KEY" \
	-d "{
	\"model\": \"gpt-image-2\",
	\"prompt\": \"A cute baby sea otter\",
	\"n\": 1,
	\"size\": \"1024x1024\"
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"os"

	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {

	apiKey := "sk-123456789012345678901234567890123456789012345678"
	exampleFilePath := "example.png"

	client := openai.NewClient(
		option.WithAPIKey(apiKey),
		option.WithBaseURL("https://gateway.theturbo.ai/v1"),
	)

	resp, err := client.Images.Generate(
		context.Background(),
		openai.ImageGenerateParams{
			Model:          "gpt-image-2",
			Prompt:         "A cute baby sea otter",
			Size:           "1024x1024",
		},
	)

	if err != nil {
		fmt.Println("error:", err)
		return
	}

	if len(resp.Data) == 0 || resp.Data[0].B64JSON == "" {
		fmt.Println("error: empty b64_json")
		return
	}

	imageData, err := base64.StdEncoding.DecodeString(resp.Data[0].B64JSON)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	if err := os.WriteFile(exampleFilePath, imageData, 0644); err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println("success to write to file", exampleFilePath)
}

```

{% endtab %}

{% tab title="Python" %}

```python
#!/usr/bin/env python3

import base64
from openai import OpenAI

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"
    example_file_path = "example.png"

    client = OpenAI(
        api_key=api_key,
        base_url="https://gateway.theturbo.ai/v1"
    )

    response = client.images.generate(
        model="gpt-image-2",
        prompt="A cute baby sea otter",
        size="1024x1024",
    )

    if not response.data or not response.data[0].b64_json:
        print("error: empty b64_json")
        return

    image_data = base64.b64decode(response.data[0].b64_json)

    with open(example_file_path, "wb") as f:
        f.write(image_data)

    print("success to write to file", example_file_path)

if __name__ == "__main__":
    main()
```

{% endtab %}
{% endtabs %}

## 5. 响应示例

{% tabs %}
{% tab title="b64\_json" %}

```json
{
	"created": 1589478378,
	"data": [{
			"b64_json": "..."
		},
		{
			"b64_json": "..."
		}
	],
	"usage": {
		"input_tokens": 0,
		"input_tokens_details": {
			"image_tokens": 0,
			"text_tokens": 0
		},
		"output_tokens": 0,
		"total_tokens": 0,
		"output_tokens_details": {
			"image_tokens": 0,
			"text_tokens": 0
		}
	}
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/api-reference/cn/compute/aig/image-generation/openai-image-generation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
