This commit is contained in:
root
2025-10-13 18:25:37 +05:00
parent e55eecd069
commit 3998095717
3 changed files with 175 additions and 2 deletions
@@ -37,6 +37,16 @@ public class OpenAIAnalyticsService {
private String apiKey;
public OpenAIAnalyticsService(@Value("${openai.api.url:https://api.openai.com/v1/chat/completions}") String openaiUrl) {
// Проверяем API ключ при инициализации
if (apiKey == null || apiKey.trim().isEmpty() || apiKey.equals("your-openai-api-key-here")) {
logger.error("OpenAI API key is not configured properly. Please set OPENAI_API_KEY environment variable or update application.properties");
throw new IllegalStateException("OpenAI API key is not configured");
}
if (!apiKey.startsWith("sk-")) {
logger.warn("OpenAI API key does not start with 'sk-'. Please verify your API key is correct.");
}
HttpClient httpClient = HttpClient.create()
.compress(true)
.responseTimeout(Duration.ofMillis(timeoutMs));
@@ -46,6 +56,8 @@ public class OpenAIAnalyticsService {
.clientConnector(new ReactorClientHttpConnector(httpClient))
.defaultHeader("Authorization", "Bearer " + apiKey)
.build();
logger.info("OpenAIAnalyticsService initialized with model: {}", modelName);
}
public MarketItem.Analytics analyzeText(String text) {
@@ -135,7 +147,14 @@ public class OpenAIAnalyticsService {
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
})
.onErrorResume(err -> {
logger.warn("OpenAI request failed: {}", err.getMessage());
logger.error("OpenAI request failed: {} - {}", err.getMessage(), err.getClass().getSimpleName());
if (err.getMessage().contains("401")) {
logger.error("OpenAI API key is invalid or expired. Please check your openai.api.key configuration.");
} else if (err.getMessage().contains("429")) {
logger.error("OpenAI API rate limit exceeded. Please try again later.");
} else if (err.getMessage().contains("500")) {
logger.error("OpenAI API server error. Please try again later.");
}
return Mono.empty();
})
.block(Duration.ofMillis(timeoutMs));
@@ -187,7 +206,14 @@ public class OpenAIAnalyticsService {
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
})
.onErrorResume(err -> {
logger.warn("OpenAI request failed: {}", err.getMessage());
logger.error("OpenAI request failed: {} - {}", err.getMessage(), err.getClass().getSimpleName());
if (err.getMessage().contains("401")) {
logger.error("OpenAI API key is invalid or expired. Please check your openai.api.key configuration.");
} else if (err.getMessage().contains("429")) {
logger.error("OpenAI API rate limit exceeded. Please try again later.");
} else if (err.getMessage().contains("500")) {
logger.error("OpenAI API server error. Please try again later.");
}
return Mono.empty();
})
.block(Duration.ofMillis(timeoutMs));