Skip to content

Commit

Permalink
Added extra env variable to control DALLE additional parameters using…
Browse files Browse the repository at this point in the history
… a JSON. If not defined, in the API call, API will use default values for this {"size": "1024x1024", "quality": "standard", "style": "vivid"}
  • Loading branch information
Miguel Martin committed Dec 11, 2024
1 parent 3977441 commit 0d5ce5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following variables cluster all deployments into the groups of deployments w
|---|---|---|
|DALLE3_DEPLOYMENTS|``|Comma-separated list of deployments that support DALL-E 3 API. Example: `dall-e-3,dalle3,dall-e`|
|DALLE3_AZURE_API_VERSION|2024-02-01|The version API for requests to Azure DALL-E-3 API|
|DALLE_EXTRA_PARAMETERS|`{}`|JSON formated list of extra parameters, example: '{"size": "1024x1024", "quality": "hd", "style": "vivid"}'|
|GPT4_VISION_DEPLOYMENTS|``|Comma-separated list of deployments that support GPT-4V API. Example: `gpt-4-vision-preview,gpt-4-vision`|
|GPT4_VISION_MAX_TOKENS|1024|Default value of `max_tokens` parameter for GPT-4V when it wasn't provided in the request|
|MISTRAL_DEPLOYMENTS|``|Comma-separated list of deployments that support Mistral Large Azure API. Example: `mistral-large-azure,mistral-large`|
Expand Down
18 changes: 17 additions & 1 deletion aidial_adapter_openai/dalle3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import os
from typing import Any, AsyncIterator, Optional

import aiohttp
Expand All @@ -19,10 +21,24 @@
async def generate_image(
api_url: str, creds: OpenAICreds, user_prompt: str
) -> JSONResponse | Any:

extra_params = {}
try:
extra_params = json.loads(os.getenv("DALLE_EXTRA_PARAMETERS", "{}"))
except json.JSONDecodeError:
# Log or handle the case where the environment variable is not valid JSON
extra_params = {}

payload = {
"prompt": user_prompt,
"response_format": "b64_json",
**extra_params, # Add extra parameters
}

async with aiohttp.ClientSession() as session:
async with session.post(
api_url,
json={"prompt": user_prompt, "response_format": "b64_json"},
json=payload,
headers=get_auth_headers(creds),
) as response:
status_code = response.status
Expand Down

0 comments on commit 0d5ce5e

Please sign in to comment.