# Google Imagen

## 1. 概述

google推出的文生图模型。

**模型列表：**

* `imagegeneration@002`
* `imagegeneration@005`
* `imagegeneration@006`
* `imagen-3.0-generate-001`
* `imagen-3.0-generate-002`
* `imagen-3.0-fast-generate-001`

> **说明：**
>
> 1. 该系列模型对中文支持不太友好，使用自然语言描述时尽量使用英文

## 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="114.59375">参数名称</th><th width="108.4765625">类型</th><th width="78.66796875">必填</th><th width="241.89453125">说明</th><th>示例（默认值）</th></tr></thead><tbody><tr><td><strong>model</strong></td><td>string</td><td>是</td><td>要使用的模型 ID。详见<a href="#1-概述">概述</a>列出的可用版本，如 <code>imagen-3.0-generate-001</code>。</td><td><code>imagen-3.0-generate-001</code></td></tr><tr><td><strong>prompt</strong></td><td>string</td><td>是</td><td>一段描述所需图像的文字。<code>imagen-3.0</code> 描述最大长度为 480 词元。 <code>imagegeneration@005</code> <code>imagegeneration@006</code> 描述最大长度为 128 词元。 <code>imagegeneration@002</code> 描述最大长度为 128 词元。</td><td><code>A cute baby sea otter</code></td></tr><tr><td>n</td><td>number</td><td>否</td><td>生成图像的数量，必须在 1 到 4 之间。</td><td><code>1</code></td></tr><tr><td>aspect_ratio</td><td>string</td><td>否</td><td>图片的宽高比。支持<code>1:1</code> <code>9:16</code> <code>16:9</code> <code>3:4</code> <code>4:3</code>。不同模型支持的略有差异。</td><td><code>1:1</code></td></tr><tr><td>output_format</td><td>string</td><td>否</td><td>生成图像的格式。支持<code>png</code> <code>jpeg</code>。</td><td><code>png</code></td></tr><tr><td>style</td><td>string</td><td>否</td><td>生成图像的风格。支持<code>photograph</code> <code>digital_art</code> <code>landscape</code> <code>sketch</code> <code>watercolor</code> <code>cyberpunk</code> <code>pop_art</code>。此参数仅适用于<code>imagegeneration@002</code>。</td><td><code>photograph</code></td></tr><tr><td>response_format</td><td>string</td><td>否</td><td>生成的图像返回的格式。只支持<code>b64_json</code>。</td><td><code>b64_json</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": "imagen-3.0-generate-001",
	"prompt": "A cute baby sea otter",
	"n": 1,
	"aspect_ratio": "1:1"
}
```

{% 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\": \"imagen-3.0-generate-001\",
	\"prompt\": \"A cute baby sea otter\",
	\"n\": 1,
	\"aspect_ratio\": \"1:1\"
}"
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
)

const (
	YOUR_API_KEY    = "sk-123456789012345678901234567890123456789012345678"
	REQUEST_PAYLOAD = `{
	"model": "imagen-3.0-generate-001",
	"prompt": "A cute baby sea otter",
	"n": 1,
	"aspect_ratio": "1:1"
}`
	EXAMPLE_FILE_PATH = "example.png"
)

type Data struct {
	B64Json string `json:"b64_json"`
}

type ResponseJSON struct {
	Data []Data `json:"data"`
}

func main() {

	requestURL := "https://gateway.theturbo.ai/v1/images/generations"
	requestMethod := "POST"
	requestPayload := strings.NewReader(REQUEST_PAYLOAD)

	req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
	if err != nil {
		fmt.Println("Create request failed, err: ", err)
		return
	}

	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Accept", "application/json")
	req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)

	client := &http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Do request failed, err: ", err)
		return
	}
	defer resp.Body.Close()

	respBodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Read response body failed, err: ", err)
		return
	}

	if resp.StatusCode != 200 {
		fmt.Printf("status code: %d\n", resp.StatusCode)
		fmt.Println(string(respBodyBytes))
		return
	}

	var responseJSON ResponseJSON
	if err := json.Unmarshal(respBodyBytes, &responseJSON); err != nil {
		fmt.Println("Parse response body failed, err: ", err)
		return
	}

	if len(responseJSON.Data) == 0 || responseJSON.Data[0].B64Json == "" {
		fmt.Println("Parse response body failed, err: empty b64_json")
		return
	}

	imageData, err := base64.StdEncoding.DecodeString(responseJSON.Data[0].B64Json)
	if err != nil {
		fmt.Println("Parse b64_json failed, err: ", err)
		return
	}

	imageFile, err := os.Create(EXAMPLE_FILE_PATH)
	if err != nil {
		fmt.Printf("Create file %s failed, err: %v\n", EXAMPLE_FILE_PATH, err)
		return
	}

	if _, err := imageFile.Write(imageData); err != nil {
		fmt.Printf("Write to file %s failed, err: %v\n", EXAMPLE_FILE_PATH, err)
		imageFile.Close()
		return
	}

	imageFile.Close()

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

{% endtab %}
{% endtabs %}

## 5. 响应示例

```json
{
	"created": 1589478378,
	"data": [
		{
			"b64_json": "..."
		},
		{
			"b64_json": "..."
		}
	]
}
```


---

# 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/test/hotfix/aigw/image-generation/google-imagen-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.
