图片生成(OpenAI协议)
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": "gemini-2.5-flash-image",
"prompt": "A cute baby sea otter",
"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": "gemini-2.5-flash-image",
"prompt": "A cute baby sea otter",
"aspect_ratio": "1:1"
}'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: "gemini-2.5-flash-image",
Prompt: "A cute baby sea otter",
ResponseFormat: "b64_json",
},
option.WithJSONSet("aspect_ratio", "1:1"),
)
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)
}
#!/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="gemini-2.5-flash-image",
prompt="A cute baby sea otter",
response_format="b64_json",
extra_body={"aspect_ratio": "1:1"}
)
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()
{
"created": 1589478378,
"data": [
{
"b64_json": "..."
}
]
}