1- """Image Content Agent - Generates marketing images via DALL-E 3, gpt-image-1, or gpt-image-1.5 .
1+ """Image Content Agent - Generates marketing images.
22
33Provides the generate_image function used by the orchestrator
4- to create marketing images using either DALL-E 3, gpt- image-1, or gpt-image-1.5 .
4+ to create marketing images using the image generation model .
55"""
66
77import logging
1414logger = logging .getLogger (__name__ )
1515
1616
17- def _truncate_for_dalle (product_description : str , max_chars : int = 1500 ) -> str :
17+ def _truncate_for_image (product_description : str , max_chars : int = 1500 ) -> str :
1818 """
19- Truncate product descriptions to fit DALL-E's 4000 character limit .
19+ Truncate product descriptions for image-generation prompt limits .
2020 Extracts the most visually relevant information (colors, hex codes, finishes).
2121
2222 Args:
@@ -59,12 +59,12 @@ def _truncate_for_dalle(product_description: str, max_chars: int = 1500) -> str:
5959
6060 # If still too long, just truncate with ellipsis
6161 if len (result ) > max_chars :
62- result = result [:max_chars - 50 ] + '\n \n [Additional details truncated for DALL-E ]'
62+ result = result [:max_chars - 50 ] + '\n \n [Additional details truncated for image generation ]'
6363
6464 return result
6565
6666
67- async def generate_dalle_image (
67+ async def generate_image (
6868 prompt : str ,
6969 product_description : str = "" ,
7070 scene_description : str = "" ,
@@ -95,10 +95,10 @@ async def generate_dalle_image(
9595 logger .info (f"Using image generation model: { image_model } " )
9696
9797 # Use appropriate generator based on model
98- if image_model in ["gpt-image-1" , "gpt-image-1.5" ]:
99- return await _generate_gpt_image (prompt , product_description , scene_description , size , quality )
100- else :
98+ if image_model .lower ().startswith ("dall-e" ):
10199 return await _generate_dalle_image (prompt , product_description , scene_description , size , quality )
100+ else :
101+ return await _generate_gpt_image (prompt , product_description , scene_description , size , quality )
102102
103103
104104async def _generate_dalle_image (
@@ -127,9 +127,23 @@ async def _generate_dalle_image(
127127 size = size or app_settings .azure_openai .image_size
128128 quality = quality or app_settings .azure_openai .image_quality
129129
130- # DALL-E 3 has a 4000 character limit for prompts
130+ # Map gpt-image values to DALL-E compatible values when needed
131+ quality_mapping = {
132+ "low" : "standard" ,
133+ "medium" : "standard" ,
134+ "high" : "hd" ,
135+ "auto" : "standard" ,
136+ }
137+ quality = quality_mapping .get (quality , quality )
138+
139+ size_mapping = {
140+ "1536x1024" : "1792x1024" ,
141+ "1024x1536" : "1024x1792" ,
142+ }
143+ size = size_mapping .get (size , size )
144+
131145 # Truncate product descriptions to essential visual info
132- truncated_product_desc = _truncate_for_dalle (product_description , max_chars = 1500 )
146+ truncated_product_desc = _truncate_for_image (product_description , max_chars = 1500 )
133147
134148 # Also truncate the main prompt if it's too long
135149 main_prompt = prompt [:1000 ] if len (prompt ) > 1000 else prompt
@@ -163,11 +177,11 @@ async def _generate_dalle_image(
163177✓ Professional, polished marketing image
164178"""
165179
166- # Final safety check - DALL-E 3 has 4000 char limit
180+ # Final safety check before sending to image generation - if prompt is too long, truncate further and warn
167181 if len (full_prompt ) > 3900 :
168182 logger .warning (f"Prompt too long ({ len (full_prompt )} chars), truncating..." )
169183 # Reduce product context further
170- truncated_product_desc = _truncate_for_dalle (product_description , max_chars = 800 )
184+ truncated_product_desc = _truncate_for_image (product_description , max_chars = 800 )
171185 full_prompt = f"""⚠️ ZERO TEXT IN IMAGE. NO WORDS. NO LETTERS. NO PRODUCT NAMES.
172186
173187Create a PURELY VISUAL marketing image with no text whatsoever.
@@ -194,19 +208,18 @@ async def _generate_dalle_image(
194208 # Get token for Azure OpenAI
195209 token = await credential .get_token ("https://cognitiveservices.azure.com/.default" )
196210
197- # Use the dedicated DALL-E endpoint if configured, otherwise fall back to main endpoint
198- dalle_endpoint = app_settings .azure_openai .dalle_endpoint or app_settings .azure_openai .endpoint
199- logger .info (f"Using DALL-E endpoint: { dalle_endpoint } " )
211+ image_endpoint = app_settings .azure_openai .image_endpoint or app_settings .azure_openai .endpoint
212+ logger .info (f"Using endpoint: { image_endpoint } " )
200213
201214 client = AsyncAzureOpenAI (
202- azure_endpoint = dalle_endpoint ,
215+ azure_endpoint = image_endpoint ,
203216 azure_ad_token = token .token ,
204217 api_version = app_settings .azure_openai .preview_api_version ,
205218 )
206219
207220 try :
208221 response = await client .images .generate (
209- model = app_settings .azure_openai .dalle_model ,
222+ model = app_settings .azure_openai .image_model ,
210223 prompt = full_prompt ,
211224 size = size ,
212225 quality = quality ,
@@ -247,7 +260,7 @@ async def _generate_gpt_image(
247260 """
248261 Generate a marketing image using gpt-image-1 or gpt-image-1.5.
249262
250- gpt-image models have different capabilities than DALL-E 3 :
263+ gpt-image models:
251264 - Supports larger prompt sizes
252265 - Different size options: 1024x1024, 1536x1024, 1024x1536, auto
253266 - Different quality options: low, medium, high, auto
@@ -265,27 +278,12 @@ async def _generate_gpt_image(
265278 """
266279 brand = app_settings .brand_guidelines
267280
268- # Use defaults from settings if not provided
269- # Map DALL-E quality settings to gpt-image-1 or gpt-image-1.5 equivalents if needed
281+ # Image settings
270282 size = size or app_settings .azure_openai .image_size
271283 quality = quality or app_settings .azure_openai .image_quality
272284
273- # Map DALL-E quality values to gpt-image-1 or gpt-image-1.5 equivalents
274- quality_mapping = {
275- "standard" : "medium" ,
276- "hd" : "high" ,
277- }
278- quality = quality_mapping .get (quality , quality )
279-
280- # Map DALL-E sizes to gpt-image-1 or gpt-image-1.5 equivalents if needed
281- size_mapping = {
282- "1024x1792" : "1024x1536" , # Closest equivalent
283- "1792x1024" : "1536x1024" , # Closest equivalent
284- }
285- size = size_mapping .get (size , size )
286-
287285 # gpt-image-1 can handle larger prompts, so we can include more context
288- truncated_product_desc = _truncate_for_dalle (product_description , max_chars = 3000 )
286+ truncated_product_desc = _truncate_for_image (product_description , max_chars = 3000 )
289287
290288 main_prompt = prompt [:2000 ] if len (prompt ) > 2000 else prompt
291289 scene_desc = scene_description [:1000 ] if scene_description and len (scene_description ) > 1000 else scene_description
@@ -330,9 +328,8 @@ async def _generate_gpt_image(
330328 # Get token for Azure OpenAI
331329 token = await credential .get_token ("https://cognitiveservices.azure.com/.default" )
332330
333- # Use gpt-image-1 specific endpoint if configured, otherwise DALL-E endpoint, otherwise main endpoint
331+ # Use gpt-image-1 specific endpoint if configured, otherwise main endpoint
334332 image_endpoint = (app_settings .azure_openai .gpt_image_endpoint
335- or app_settings .azure_openai .dalle_endpoint
336333 or app_settings .azure_openai .endpoint )
337334 logger .info (f"Using gpt-image-1 endpoint: { image_endpoint } " )
338335
@@ -398,5 +395,5 @@ async def _generate_gpt_image(
398395 }
399396
400397
401- # Alias for backwards compatibility
402- generate_image = generate_dalle_image
398+ # Backward-compatible alias
399+ generate_dalle_image = generate_image
0 commit comments