跳至主要內容

Azure AI 圖像生成(Black Forest Labs - Flux)

Azure AI 使用來自 Black Forest Labs 的 FLUX 模型,提供強大的圖像生成能力,可根據文字描述建立高品質圖片。

總覽​

屬性詳細資訊
說明Azure AI Image Generation 使用 FLUX 模型,從文字描述生成高品質圖片。
LiteLLM 上的提供者路由azure_ai/
提供者文件Azure AI FLUX 模型 ↗
支援的操作/images/generations, /images/edits

設定​

API 金鑰與 Base URL​

# Set your Azure AI API credentials
import os
os.environ["AZURE_AI_API_KEY"] = "your-api-key-here"
os.environ["AZURE_AI_API_BASE"] = "your-azure-ai-endpoint" # e.g., https://your-endpoint.eastus2.inference.ai.azure.com/

請從 Azure AI Studio 取得您的 API 金鑰與端點。

支援的模型​

模型名稱說明每張圖片成本
azure_ai/FLUX-1.1-pro用於高品質圖片生成的最新 FLUX 1.1 Pro 模型$0.04
azure_ai/FLUX.1-Kontext-pro具備增強情境理解能力的 FLUX 1 Kontext Pro 模型$0.04
azure_ai/flux.2-pro用於下一代圖片生成的 FLUX 2 Pro 模型$0.04
azure_ai/FLUX.2-flex具有可調整 guidance 和 steps 的 FLUX 2 Flex 模型$0.05 每百萬像素(1024x1024 時為 $0.052)

FLUX 2 模型是從 Azure 的 Black Forest Labs 路由提供,而不是 Azure OpenAI 部署路由:flux.2-pro 請求會送往 /providers/blackforestlabs/v1/flux-2-pro,而 FLUX.2-flex 請求會送往 /providers/blackforestlabs/v1/flux-2-flex,兩者都位於您的 api_base 下。模型名稱比對不分大小寫,因此 azure_ai/flux.2-flex 也可以。FLUX 2 Flex 是依生成圖片的每像素計費,因此 LiteLLM 記錄的成本會依照您請求的 size(或 width 與 height);若未明確指定尺寸,則會以 1024x1024 記錄,這也是 Azure 的預設生成尺寸

圖像生成​

使用方式 - LiteLLM Python SDK​

Basic Image Generation
import litellm
import os

# Set your API credentials
os.environ["AZURE_AI_API_KEY"] = "your-api-key-here"
os.environ["AZURE_AI_API_BASE"] = "your-azure-ai-endpoint"

# Generate a single image
response = litellm.image_generation(
model="azure_ai/FLUX.1-Kontext-pro",
prompt="A cute baby sea otter swimming in crystal clear water",
api_base=os.environ["AZURE_AI_API_BASE"],
api_key=os.environ["AZURE_AI_API_KEY"]
)

print(response.data[0].url)

使用方式 - LiteLLM Proxy Server​

1. 設定您的 config.yaml​

Azure AI Image Generation Configuration
model_list:
- model_name: azure-flux-kontext
litellm_params:
model: azure_ai/FLUX.1-Kontext-pro
api_key: os.environ/AZURE_AI_API_KEY
api_base: os.environ/AZURE_AI_API_BASE
model_info:
mode: image_generation

- model_name: azure-flux-11-pro
litellm_params:
model: azure_ai/FLUX-1.1-pro
api_key: os.environ/AZURE_AI_API_KEY
api_base: os.environ/AZURE_AI_API_BASE
model_info:
mode: image_generation

- model_name: azure-flux-2-pro
litellm_params:
model: azure_ai/flux.2-pro
api_key: os.environ/AZURE_AI_API_KEY
api_base: os.environ/AZURE_AI_API_BASE
api_version: preview
model_info:
mode: image_generation

- model_name: azure-flux-2-flex
litellm_params:
model: azure_ai/FLUX.2-flex
api_key: os.environ/AZURE_AI_API_KEY
api_base: os.environ/AZURE_AI_API_BASE
api_version: preview
model_info:
mode: image_generation

general_settings:
master_key: os.environ/LITELLM_MASTER_KEY

2. 啟動 LiteLLM Proxy Server​

Start LiteLLM Proxy Server
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000

3. 使用 OpenAI Python SDK 發出請求​

Azure AI Image Generation via Proxy - OpenAI SDK
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="http://localhost:4000", # Your proxy URL
api_key="sk-<your-litellm-api-key>" # Your proxy API key
)

# Generate image with FLUX Kontext Pro
response = client.images.generate(
model="azure-flux-kontext",
prompt="A serene Japanese garden with cherry blossoms and a peaceful pond",
n=1,
size="1024x1024"
)

print(response.data[0].url)

圖像編輯​

FLUX 2 Pro 支援圖像編輯,方法是傳入輸入圖片,以及描述所需修改內容的提示詞。

使用方式 - LiteLLM Python SDK​

Basic Image Editing with FLUX 2 Pro
import litellm
import os

# Set your API credentials
os.environ["AZURE_AI_API_KEY"] = "your-api-key-here"
os.environ["AZURE_AI_API_BASE"] = "your-azure-ai-endpoint" # e.g., https://litellm-ci-cd-prod.services.ai.azure.com

# Edit an existing image
response = litellm.image_edit(
model="azure_ai/flux.2-pro",
prompt="Add a red hat to the subject",
image=open("input_image.png", "rb"),
api_base=os.environ["AZURE_AI_API_BASE"],
api_key=os.environ["AZURE_AI_API_KEY"],
api_version="preview",
)

print(response.data[0].b64_json) # FLUX 2 returns base64 encoded images

使用方式 - LiteLLM Proxy Server​

Image Edit via Proxy - cURL
curl --location 'http://localhost:4000/v1/images/edits' \
--header "Authorization: Bearer $LITELLM_API_KEY" \
--form 'model="azure-flux-2-pro"' \
--form 'prompt="Add sunglasses to the person"' \
--form 'image=@"input_image.png"'

支援的參數​

Azure AI Image Generation 支援以下與 OpenAI 相容的參數:

參數類型說明預設值範例
promptstring要生成的圖片文字描述必填"A sunset over the ocean"
modelstring要用於生成的 FLUX 模型必填"azure_ai/FLUX.1-Kontext-pro"
ninteger要生成的圖片數量(1-4)12
sizestring圖片尺寸"1024x1024""512x512", "1024x1024"
api_basestring您的 Azure AI 端點 URL必填"https://your-endpoint.eastus2.inference.ai.azure.com/"
api_keystring您的 Azure AI API 金鑰必填環境變數或直接值

FLUX 2 參數​

FLUX 2 Pro 與 FLUX 2 Flex 接受 n、size、output_format、seed、safety_tolerance 和 aspect_ratio,以及 Black Forest Labs 的名稱 width、height、num_images、guidance 和 steps。size 會以 width 和 height 傳送("1536x1024" 會變成 width: 1536, height: 1024),n 會以 num_images 傳送,而 size: "auto" 不會傳送任何尺寸,因此 Azure 會選擇其預設值。不是 WxH 的 size,例如 "large",會以 400 被拒絕,並標明預期格式。僅 OpenAI 使用的欄位 user、quality、background、moderation 和 output_compression 會被接受並丟棄,因此為 gpt-image-1 建置的用戶端可在無需 drop_params 的情況下繼續運作。任何其他不支援的欄位都會被拒絕,除非已設定 drop_params。Azure 會針對 FLUX 2 模型每次請求回傳一張圖片,不受 n 影響

參數類型說明範例
sizestringWxH 尺寸,或使用 "auto" 讓 Azure 採用預設值"1536x1024"
width, heightinteger像素尺寸,為 size 的替代選項1536, 1024
output_formatstring圖片編碼"jpeg", "png"
seedinteger用於可重現輸出的種子42
safety_toleranceinteger內容審查嚴格度2
aspect_ratiostring生成圖片的長寬比"16:9"
guidancefloat提示詞遵循度(FLUX 2 Flex)4.5
stepsinteger擴散步數(FLUX 2 Flex)32

快速開始​

  1. 在 Azure AI Studio 建立帳戶
  2. 在您的 Azure AI Studio 工作區部署 FLUX 模型
  3. 從部署詳細資料取得您的 API 金鑰與端點
  4. 設定您的 AZURE_AI_API_KEY 與 AZURE_AI_API_BASE 環境變數
  5. 開始使用 LiteLLM 生成圖片

其他資源​