|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import httpx |
| 4 | + |
| 5 | +import litellm |
| 6 | +from litellm.llms.azure_ai.common_utils import AzureFoundryModelInfo |
| 7 | +from litellm.llms.openai.image_edit.transformation import OpenAIImageEditConfig |
| 8 | +from litellm.secret_managers.main import get_secret_str |
| 9 | +from litellm.utils import _add_path_to_api_base |
| 10 | + |
| 11 | + |
| 12 | +class AzureFoundryFluxImageEditConfig(OpenAIImageEditConfig): |
| 13 | + """ |
| 14 | + Azure AI Foundry FLUX image edit config |
| 15 | +
|
| 16 | + Supports FLUX models including FLUX-1.1-pro and FLUX-1-kontext-pro for image editing. |
| 17 | +
|
| 18 | + Azure AI Foundry FLUX models handle image editing through the /images/edits endpoint, |
| 19 | + same as standard Azure OpenAI models. The request format uses multipart/form-data |
| 20 | + with image files and prompt. |
| 21 | + """ |
| 22 | + |
| 23 | + def validate_environment( |
| 24 | + self, |
| 25 | + headers: dict, |
| 26 | + model: str, |
| 27 | + api_key: Optional[str] = None, |
| 28 | + ) -> dict: |
| 29 | + """ |
| 30 | + Validate Azure AI Foundry environment and set up authentication |
| 31 | + Uses Api-Key header format like Azure OpenAI |
| 32 | +
|
| 33 | + Azure AI Foundry uses the same authentication format as Azure OpenAI: |
| 34 | + - Header: Api-Key (not Authorization Bearer) |
| 35 | + - Endpoint: /openai/deployments/{model}/images/edits (for image editing) |
| 36 | + """ |
| 37 | + api_key = AzureFoundryModelInfo.get_api_key(api_key) |
| 38 | + |
| 39 | + if not api_key: |
| 40 | + raise ValueError( |
| 41 | + f"Azure AI API key is required for model {model}. Set AZURE_AI_API_KEY environment variable or pass api_key parameter." |
| 42 | + ) |
| 43 | + |
| 44 | + headers.update( |
| 45 | + { |
| 46 | + "Api-Key": api_key, # Azure AI Foundry uses Api-Key header format |
| 47 | + } |
| 48 | + ) |
| 49 | + return headers |
| 50 | + |
| 51 | + def get_complete_url( |
| 52 | + self, |
| 53 | + model: str, |
| 54 | + api_base: Optional[str], |
| 55 | + litellm_params: dict, |
| 56 | + ) -> str: |
| 57 | + """ |
| 58 | + Constructs a complete URL for Azure AI Foundry image edits API request. |
| 59 | +
|
| 60 | + Azure AI Foundry FLUX models handle image editing through the /images/edits |
| 61 | + endpoint. |
| 62 | +
|
| 63 | + Args: |
| 64 | + - model: Model name (deployment name for Azure AI Foundry) |
| 65 | + - api_base: Base URL for Azure AI endpoint |
| 66 | + - litellm_params: Additional parameters including api_version |
| 67 | +
|
| 68 | + Returns: |
| 69 | + - Complete URL for the image edits endpoint |
| 70 | + """ |
| 71 | + api_base = AzureFoundryModelInfo.get_api_base(api_base) |
| 72 | + |
| 73 | + if api_base is None: |
| 74 | + raise ValueError( |
| 75 | + "Azure AI API base is required. Set AZURE_AI_API_BASE environment variable or pass api_base parameter." |
| 76 | + ) |
| 77 | + |
| 78 | + api_version = (litellm_params.get("api_version") or litellm.api_version |
| 79 | + or get_secret_str("AZURE_AI_API_VERSION") |
| 80 | + ) |
| 81 | + if api_version is None: |
| 82 | + # API version is mandatory for Azure AI Foundry |
| 83 | + raise ValueError( |
| 84 | + "Azure API version is required. Set AZURE_AI_API_VERSION environment variable or pass api_version parameter." |
| 85 | + ) |
| 86 | + |
| 87 | + # Add the path to the base URL using the model as deployment name |
| 88 | + # Azure AI Foundry FLUX models use /images/edits for editing |
| 89 | + if "/openai/deployments/" in api_base: |
| 90 | + new_url = _add_path_to_api_base( |
| 91 | + api_base=api_base, |
| 92 | + ending_path="/images/edits", |
| 93 | + ) |
| 94 | + else: |
| 95 | + new_url = _add_path_to_api_base( |
| 96 | + api_base=api_base, |
| 97 | + ending_path=f"/openai/deployments/{model}/images/edits", |
| 98 | + ) |
| 99 | + |
| 100 | + # Use the new query_params dictionary |
| 101 | + final_url = httpx.URL(new_url).copy_with(params={"api-version": api_version}) |
| 102 | + |
| 103 | + return str(final_url) |
0 commit comments