Azure AI 이미지 생성

Azure AI 이미지 생성 (Black Forest Labs - Flux)

Azure AI는 Black Forest Labs의 FLUX 모델을 이용해 텍스트 설명으로 고품질 이미지를 생성할 수 있는 강력한 기능을 제공해요. LiteLLM을 쓰면 azure_ai/ 라우트로 이 기능을 표준 인터페이스에서 바로 호출할 수 있어요.

출처: 문서

본문

개요 (Overview)

속성 설명
설명 Azure AI 이미지 생성은 FLUX 모델로 텍스트 설명에서 고품질 이미지를 생성해요
LiteLLM 라우트 azure_ai/
공급자 문서 Azure AI FLUX Models
지원 작업 /images/generations, /images/edits

설정 (Setup)

API Key & Base URL

Azure AI Studio에서 API 키와 엔드포인트를 가져와요.

# 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/

지원 모델 (Supported Models)

모델 이름 설명 이미지당 비용
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 OpenAI 배포 라우트가 아니라 Azure의 Black Forest Labs 라우트에서 서비스돼요. 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이 기록하는 비용은 요청한 크기(또는 width/height)를 따르고, 명시한 크기가 없으면 Azure가 기본으로 생성하는 1024x1024로 기록해요.

이미지 생성 (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)

FLUX 1.1 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"

# Generate image with FLUX 1.1 Pro
response = litellm.image_generation(
    model="azure_ai/FLUX-1.1-pro",
    prompt="A futuristic cityscape at night with neon lights and flying cars",
    api_base=os.environ["AZURE_AI_API_BASE"],
    api_key=os.environ["AZURE_AI_API_KEY"],
)

print(response.data[0].url)

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

# Generate image with FLUX 2 Pro
response = litellm.image_generation(
    model="azure_ai/flux.2-pro",
    prompt="A photograph of a red fox in an autumn forest",
    api_base=os.environ["AZURE_AI_API_BASE"],
    api_key=os.environ["AZURE_AI_API_KEY"],
    api_version="preview",
    size="1024x1024",
    n=1,
)

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

FLUX 2 Flex 이미지 생성 (guidance와 steps 조절)

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://your-resource.services.ai.azure.com

# Generate image with FLUX 2 Flex, tuning guidance and steps
response = litellm.image_generation(
    model="azure_ai/FLUX.2-flex",
    prompt="A photograph of a red fox in an autumn forest",
    api_base=os.environ["AZURE_AI_API_BASE"],
    api_key=os.environ["AZURE_AI_API_KEY"],
    api_version="preview",
    size="1536x1024",
    n=1,
    guidance=4.5,
    steps=32,
)

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

비동기 이미지 생성 (Async)

import litellm
import asyncio
import os

async def generate_image():
    # 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 image asynchronously
    response = await litellm.aimage_generation(
        model="azure_ai/FLUX.1-Kontext-pro",
        prompt="A beautiful sunset over mountains with vibrant colors",
        api_base=os.environ["AZURE_AI_API_BASE"],
        api_key=os.environ["AZURE_AI_API_KEY"],
        n=1,
    )
    print(response.data[0].url)
    return response

# Run the async function
asyncio.run(generate_image())

고급 파라미터 이미지 생성

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 image with additional parameters
response = litellm.image_generation(
    model="azure_ai/FLUX-1.1-pro",
    prompt="A majestic dragon soaring over a medieval castle at dawn",
    api_base=os.environ["AZURE_AI_API_BASE"],
    api_key=os.environ["AZURE_AI_API_KEY"],
    n=1,
    size="1024x1024",
    quality="standard",
)

for image in response.data:
    print(f"Generated image URL: {image.url}")

LiteLLM Proxy Server 사용법

1. config.yaml 설정

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. Proxy 서버 시작

litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000

3. OpenAI Python SDK로 요청하기

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)

LiteLLM SDK:

import litellm

# Configure LiteLLM to use your proxy
response = litellm.image_generation(
    model="litellm_proxy/azure-flux-11-pro",
    prompt="A cyberpunk warrior in a neon-lit alleyway",
    api_base="http://localhost:4000",
    api_key="sk-<your-litellm-api-key>",
)

print(response.data[0].url)

cURL:

curl --location 'http://localhost:4000/v1/images/generations' \
  --header 'Content-Type: application/json' \
  --header "Authorization: Bearer ***" \
  --data '{
    "model": "azure-flux-kontext",
    "prompt": "A cozy coffee shop interior with warm lighting and rustic wooden furniture",
    "n": 1,
    "size": "1024x1024"
  }'

이미지 편집 (Image Editing)

FLUX 2 Pro는 입력 이미지와 함께 원하는 수정 사항을 설명하는 프롬프트를 전달해 이미지를 편집할 수 있어요.

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

비동기 이미지 편집

import litellm
import asyncio
import os

async def edit_image():
    os.environ["AZURE_AI_API_KEY"] = "your-api-key-here"
    os.environ["AZURE_AI_API_BASE"] = "your-azure-ai-endpoint"

    response = await litellm.aimage_edit(
        model="azure_ai/flux.2-pro",
        prompt="Change the background to a sunset beach",
        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",
    )
    return response

asyncio.run(edit_image())

Proxy를 통한 이미지 편집

cURL:

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

OpenAI SDK:

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:4000",
    api_key="sk-<your-litellm-api-key>",
)

response = client.images.edit(
    model="azure-flux-2-pro",
    prompt="Make the sky more dramatic with storm clouds",
    image=open("input_image.png", "rb"),
)

print(response.data[0].b64_json)

지원 파라미터 (Supported Parameters)

Azure AI 이미지 생성은 다음 OpenAI 호환 파라미터를 지원해요.

파라미터 타입 설명 기본값 예시
prompt string 생성할 이미지의 텍스트 설명 필수 "A sunset over the ocean"
model string 생성에 사용할 FLUX 모델 필수 "azure_ai/FLUX.1-Kontext-pro"
n integer 생성할 이미지 수 (1-4) 1 2
size string 이미지 크기 "1024x1024" "512x512", "1024x1024"
api_base string Azure AI 엔드포인트 URL 필수 "https://your-endpoint.eastus2.inference.ai.azure.com/"
api_key string 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가 됨), nnum_images로 전송되며, size: "auto"는 크기를 보내지 않아서 Azure가 기본값을 선택해요. WxH 형식이 아닌 크기(예: "large")는 기대 형식을 알려주는 400 에러로 거부돼요. OpenAI 전용 필드인 user, quality, background, moderation, output_compression은 받아서 버리므로 gpt-image-1용 클라이언트가 drop_params 없이도 계속 동작해요. 다른 미지원 필드는 drop_params가 설정되지 않으면 거부돼요. Azure는 FLUX 2 모델에서 n과 무관하게 요청당 이미지 하나를 반환해요.

파라미터 타입 설명 예시
size string WxH 크기, 또는 Azure 기본값용 "auto" "1536x1024"
width, height integer 픽셀 단위 크기, size의 대안 1536, 1024
output_format string 이미지 인코딩 "jpeg", "png"
seed integer 재현 가능한 출력용 시드 42
safety_tolerance integer 콘텐츠 심사 강도 2
aspect_ratio string 생성 이미지의 종횡비 "16:9"
guidance float 프롬프트 준수도 (FLUX 2 Flex) 4.5
steps integer 확산 단계 수 (FLUX 2 Flex) 32

시작하기 (Getting Started)

  1. Azure AI Studio에서 계정 만들기
  2. Azure AI Studio 워크스페이스에 FLUX 모델 배포하기
  3. 배포 세부 정보에서 API 키와 엔드포인트 가져오기
  4. AZURE_AI_API_KEYAZURE_AI_API_BASE 환경 변수 설정하기
  5. LiteLLM으로 이미지 생성 시작하기

더 알아보기 (Learn more)