跳转至

Google Imagen

1. 概述

google推出的文生图模型。

说明:

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

2. 请求说明

  • 请求方法:POST
  • 请求地址:

    https://gateway.serevixai.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)

参数名称类型必填说明示例(默认值)
modelstring要使用的模型 ID。详见模型列表列出的可用版本,如 imagen-3.0-generate-001imagen-3.0-generate-001
promptstring一段描述所需图像的文字。imagen-3.0 描述最大长度为 480 词元。 imagegeneration@005 imagegeneration@006 描述最大长度为 128 词元。 imagegeneration@002 描述最大长度为 128 词元。A cute baby sea otter
nnumber生成图像的数量,必须在 1 到 4 之间。1
aspect_ratiostring图片的宽高比。支持1:1 9:16 16:9 3:4 4:3。不同模型支持的略有差异。1:1
output_formatstring生成图像的格式。支持png jpegpng
stylestring生成图像的风格。支持photograph digital_art landscape sketch watercolor cyberpunk pop_art。此参数仅适用于imagegeneration@002photograph
response_formatstring生成的图像返回的格式。只支持b64_jsonb64_json

4. 请求示例

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"
}
curl https://gateway.serevixai.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\"
}"
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.serevixai.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)
}

5. 响应示例

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