Google Imagen
1. 概述
2. 请求说明
3. 请求参数
3.1 Header 参数
参数名称
类型
必填
说明
示例值
3.2 Body 参数 (application/json)
参数名称
类型
必填
说明
示例(默认值)
4. 请求示例
5. 响应示例
最后更新于
最后更新于
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.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\"
}"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)
}{
"created": 1589478378,
"data": [
{
"b64_json": "..."
},
{
"b64_json": "..."
}
]
}