This commit is contained in:
root
2025-10-14 01:56:56 +05:00
parent f564ad30c4
commit 848856ea67
@@ -36,13 +36,15 @@ public class OpenAIAnalyticsService {
@Value("${openai.api.key}")
private String apiKey;
public OpenAIAnalyticsService(@Value("${openai.api.url:https://api.openai.com/v1/chat/completions}") String openaiUrl) {
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");
if (apiKey == null || apiKey.trim().isEmpty()) {
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.");
}
@@ -56,7 +58,7 @@ public class OpenAIAnalyticsService {
.clientConnector(new ReactorClientHttpConnector(httpClient))
.defaultHeader("Authorization", "Bearer " + apiKey)
.build();
logger.info("OpenAIAnalyticsService initialized with model: {}", modelName);
}
@@ -127,7 +129,7 @@ public class OpenAIAnalyticsService {
private String generate(String text, String instructionPrefix) {
String prompt = instructionPrefix + text;
Map<String, Object> message = new HashMap<>();
message.put("role", "user");
message.put("content", prompt);
@@ -147,9 +149,11 @@ public class OpenAIAnalyticsService {
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
})
.onErrorResume(err -> {
logger.error("OpenAI request failed: {} - {}", err.getMessage(), err.getClass().getSimpleName());
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.");
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")) {
@@ -186,7 +190,7 @@ public class OpenAIAnalyticsService {
public String generateWithInstruction(String text, String instruction, String language) {
String prompt = instruction + "\n\n" + text;
Map<String, Object> message = new HashMap<>();
message.put("role", "user");
message.put("content", prompt);
@@ -206,9 +210,11 @@ public class OpenAIAnalyticsService {
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
})
.onErrorResume(err -> {
logger.error("OpenAI request failed: {} - {}", err.getMessage(), err.getClass().getSimpleName());
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.");
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")) {