From 7e4a7a1d8311e052072b35ab23d6b952cd5d59c0 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 25 Dec 2025 19:12:30 +0500 Subject: [PATCH] . --- .../service/OpenAIImageGenerationService.java | 46 +++++++++++++++++-- src/main/resources/application.properties | 3 +- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/kz/konturai/parser/service/OpenAIImageGenerationService.java b/src/main/java/kz/konturai/parser/service/OpenAIImageGenerationService.java index 07d912d..1ac3d9d 100644 --- a/src/main/java/kz/konturai/parser/service/OpenAIImageGenerationService.java +++ b/src/main/java/kz/konturai/parser/service/OpenAIImageGenerationService.java @@ -40,6 +40,9 @@ public class OpenAIImageGenerationService { @Value("${openai.image.quality:standard}") private String imageQuality; + @Value("${openai.image.style:natural}") + private String imageStyle; + @Value("${openai.timeoutMs:90000}") private long timeoutMs; @@ -101,16 +104,20 @@ public class OpenAIImageGenerationService { } try { + // Обогащаем промпт для лучшего качества генерации + String enrichedPrompt = enrichPromptForDallE(prompt); + Map requestBody = new HashMap<>(); requestBody.put("model", model); - requestBody.put("prompt", prompt); + requestBody.put("prompt", enrichedPrompt); requestBody.put("n", 1); requestBody.put("size", size); - requestBody.put("quality", imageQuality); + requestBody.put("quality", "hd"); + requestBody.put("style", imageStyle); requestBody.put("response_format", "b64_json"); // Получаем изображение в base64 logger.info("Requesting image generation from DALL-E with prompt: {}", - prompt.substring(0, Math.min(100, prompt.length()))); + enrichedPrompt.substring(0, Math.min(100, enrichedPrompt.length()))); Map response = webClient.post() .header("Authorization", "Bearer " + apiKey) @@ -170,6 +177,39 @@ public class OpenAIImageGenerationService { } } + /** + * Обогащает промпт для DALL-E, добавляя инструкции для корректной генерации + * изображений + * и правильного отображения русского текста + * + * @param originalPrompt Исходный промпт от пользователя + * @return Обогащенный промпт с инструкциями для DALL-E + */ + private String enrichPromptForDallE(String originalPrompt) { + // Проверка на пустую строку + if (originalPrompt == null || originalPrompt.trim().isEmpty()) { + return originalPrompt; + } + + // Базовый шаблон обертки + String baseWrapper = "I will provide a description of an image. Generate the image strictly following this description without adding extra objects or changing the artistic style unless specified. "; + + // Проверка на текст в кавычках (русский текст) + boolean hasQuotedText = originalPrompt.contains("\"") || originalPrompt.contains("«") + || originalPrompt.contains("»"); + if (hasQuotedText) { + baseWrapper += "If the description contains text in quotes, write that text clearly and accurately in Russian. The text in quotes must be written exactly as provided, using the Russian alphabet (Cyrillic), with correct grammar and spelling. "; + } + + // Проверка на запрос фото и добавление профессиональных ключевых слов + String lowerPrompt = originalPrompt.toLowerCase(); + if (lowerPrompt.contains("фото") || lowerPrompt.contains("фотография") || lowerPrompt.contains("photo")) { + baseWrapper += "Use natural lighting, high resolution, editorial style. "; + } + + return baseWrapper + "Description: " + originalPrompt; + } + private RetryBackoffSpec createRetrySpec(String operation) { return Retry.backoff(maxRetryAttempts, Duration.ofMillis(initialRetryDelayMs)) .maxBackoff(Duration.ofMillis(maxRetryDelayMs)) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d9f182f..4b2d629 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -78,7 +78,8 @@ serper.timeoutMs=30000 # OpenAI DALL-E Image Generation Configuration openai.image.model=dall-e-3 openai.image.size=1024x1024 -openai.image.quality=standard +openai.image.quality=hd +openai.image.style=natural # Email Configuration spring.mail.host=smtp.gmail.com