This commit is contained in:
root
2025-12-25 19:12:30 +05:00
parent 9691b52026
commit 7e4a7a1d83
2 changed files with 45 additions and 4 deletions
@@ -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<String, Object> 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<String, Object> 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))
+2 -1
View File
@@ -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