跳至主要內容

Black Forest Labs 圖像編輯

Black Forest Labs 使用其 FLUX 模型提供強大的圖像編輯功能,可根據文字描述修改既有圖片。

概觀

屬性詳細資訊
說明Black Forest Labs Image Editing 使用 FLUX Kontext 與其他模型,根據文字提示修改、修補與擴展圖像。
LiteLLM 上的提供者路由black_forest_labs/
提供者文件Black Forest Labs API ↗
支援的操作/images/edits

設定

API 金鑰

import os

# Set your Black Forest Labs API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

請從 Black Forest Labs 取得您的 API 金鑰。

支援的模型

模型名稱說明使用情境
black_forest_labs/flux-kontext-proFLUX Kontext Pro - 透過提示進行一般圖像編輯一般編輯、風格轉換
black_forest_labs/flux-kontext-maxFLUX Kontext Max - 高品質編輯高品質修改
black_forest_labs/flux-pro-1.0-fillFLUX Pro Fill - 使用遮罩進行修補移除/替換物件
black_forest_labs/flux-pro-1.0-expandFLUX Pro Expand - 外延繪製擴展圖像邊界

圖像編輯

用法 - LiteLLM Python SDK

Basic Image Editing
import os
import litellm

# Set your API key
os.environ["BFL_API_KEY"] = "your-api-key-here"

# Edit an image with a prompt
response = litellm.image_edit(
model="black_forest_labs/flux-kontext-pro",
image=open("path/to/your/image.png", "rb"),
prompt="Add a green leaf to the scene",
)

# BFL returns URLs
print(response.data[0].url)

用法 - LiteLLM Proxy Server

1. 設定您的 config.yaml

Black Forest Labs Image Editing Configuration
model_list:
- model_name: bfl-kontext-pro
litellm_params:
model: black_forest_labs/flux-kontext-pro
api_key: os.environ/BFL_API_KEY
model_info:
mode: image_edit

- model_name: bfl-kontext-max
litellm_params:
model: black_forest_labs/flux-kontext-max
api_key: os.environ/BFL_API_KEY
model_info:
mode: image_edit

- model_name: bfl-fill
litellm_params:
model: black_forest_labs/flux-pro-1.0-fill
api_key: os.environ/BFL_API_KEY
model_info:
mode: image_edit

- model_name: bfl-expand
litellm_params:
model: black_forest_labs/flux-pro-1.0-expand
api_key: os.environ/BFL_API_KEY
model_info:
mode: image_edit

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. 發出圖像編輯請求

Black Forest Labs via Proxy - OpenAI SDK
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="http://localhost:4000",
api_key="sk-1234"
)

# Edit image with FLUX Kontext Pro
response = client.images.edit(
model="bfl-kontext-pro",
image=open("path/to/your/image.png", "rb"),
prompt="Add magical sparkles and fairy dust",
)

print(response.data[0].url)

支援的參數

OpenAI 相容參數

參數類型說明預設值
image檔案要編輯的圖像檔案必填
promptstring所需變更的文字描述必填
modelstring要使用的 FLUX 模型必填
mask檔案用於修補的遮罩圖像(flux-pro-1.0-fill)選用
ninteger圖像數量(BFL 每次請求回傳 1 張)1
sizestring對應至 aspect_ratio選用
response_formatstringurlb64_jsonurl

Black Forest Labs 特定參數

參數類型說明預設值模型
seedinteger用於可重現結果的種子隨機全部
output_formatstring輸出格式:pngjpegpng全部
safety_toleranceinteger安全過濾器容忍度(0-6)2全部
aspect_ratiostring輸出長寬比(例如:16:91:1原始Kontext models
stepsinteger推理步數模型預設值Fill
guidancefloat引導比例模型預設值Fill
grow_maskinteger遮罩擴張像素數0Fill
topinteger頂部擴展像素數0展開
bottominteger底部擴展像素數0展開
leftinteger左側擴展像素數0展開
rightinteger右側擴展像素數0展開

運作方式

Black Forest Labs 使用以輪詢為基礎的 API:

  1. 提交請求:LiteLLM 將您的圖像與提示送出至 BFL
  2. 取得任務 ID:BFL 會回傳任務 ID 與輪詢 URL
  3. 輪詢結果:LiteLLM 會自動輪詢,直到圖像準備完成
  4. 回傳結果:回傳產生的圖像 URL

這個輪詢流程由 LiteLLM 自動處理 - 您只需呼叫 image_edit() 並取得結果。

快速開始

  1. Black Forest Labs 建立帳號
  2. 從儀表板取得您的 API 金鑰
  3. 設定您的 BFL_API_KEY 環境變數
  4. 搭配任何支援的模型使用 litellm.image_edit()

其他資源