跳至主要內容

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

圖像生成

使用方式 - 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

general_settings:
master_key: sk-1234

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-1234" # 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 sk-1234' \
--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 金鑰必填環境變數或直接值

快速開始

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

其他資源