Skip to content

生图模型调用教程 ​

本文介绍如何使用 OpenAI 协议和 Midjourney-Proxy 协议调用 VLX FLUX 的图像生成模型。

前置准备

  • 已注册 VLX FLUX 并获取 API Key
  • 已确认 API 地址为 https://api.vlxflux.io
  • 本地已安装所需开发环境

OpenAI 协议 ​

通过标准的 /v1/images/generations 端点,你可以调用 DALL-E、Midjourney 代理模型和其他图像生成能力。

安装依赖 ​

bash
pip install openai requests

Python 调用 ​

基础调用 ​

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://api.vlxflux.io/v1"
)

response = client.images.generate(
    model="dall-e-3",
    prompt="A cute cat drinking coffee in cyberpunk style",
    size="1024x1024",
    quality="standard",
    n=1,
)

image_url = response.data[0].url
print("Image URL:", image_url)

返回 Base64 ​

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://api.vlxflux.io/v1"
)

response = client.images.generate(
    model="dall-e-3",
    prompt="A cute cat drinking coffee in cyberpunk style",
    size="1024x1024",
    response_format="b64_json",
    n=1,
)

image_b64 = response.data[0].b64_json
print(image_b64[:50] + "...")

保存图片 ​

python
import base64
from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://api.vlxflux.io/v1"
)

response = client.images.generate(
    model="dall-e-3",
    prompt="A cute cat drinking coffee in cyberpunk style",
    size="1024x1024",
    response_format="b64_json",
    n=1,
)

image_data = response.data[0].b64_json
with open("cat.png", "wb") as f:
    f.write(base64.b64decode(image_data))

print("Saved as cat.png")

curl 调用 ​

bash
curl https://api.vlxflux.io/v1/images/generations   -H "Content-Type: application/json"   -H "Authorization: Bearer sk-xxxxxxxx"   -d '{
    "model": "dall-e-3",
    "prompt": "a cute cat drinking coffee, cyberpunk style",
    "size": "1024x1024",
    "n": 1
  }'

支持的模型 ​

模型类型说明
dall-e-2生图官方 DALL-E 2 模型
dall-e-3生图更强的细节能力
midjourney生图代理转换的 Midjourney 模型
stable-diffusion生图Stable Diffusion 模型

携带附加参数 ​

python
response = client.images.generate(
    model="midjourney",
    prompt="a cute cat drinking coffee --ar 16:9 --niji 6",
    n=1,
)

Midjourney-Proxy 协议 ​

如果你的项目原本就基于 midjourney-proxy 规范开发,可以直接切换 Base URL 和 API Secret。

请求鉴权 ​

text
mj-api-secret: sk-xxxxxxxx

Python 调用 ​

提交任务 ​

python
import requests

url = "https://api.vlxflux.io/mj/submit/imagine"
headers = {
    "mj-api-secret": "sk-xxxxxxxx",
    "Content-Type": "application/json"
}
data = {
    "prompt": "a futuristic city, neon lights --ar 16:9"
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

查询状态 ​

python
import requests

task_id = "123456789"
url = f"https://api.vlxflux.io/mj/task/{task_id}/fetch"
headers = {
    "mj-api-secret": "sk-xxxxxxxx"
}

response = requests.get(url, headers=headers)
print(response.json())

高频错误对照表 ​

状态码错误原因解决方案
400请求参数错误检查 prompt、size 等字段
401鉴权失败检查 API Key 或 Bearer 前缀
402余额不足充值后重试
403模型无权限检查模型权限
500上游服务拥堵稍后重试或切换模型