.
This commit is contained in:
@@ -36,13 +36,15 @@ public class OpenAIAnalyticsService {
|
|||||||
@Value("${openai.api.key}")
|
@Value("${openai.api.key}")
|
||||||
private String apiKey;
|
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 ключ при инициализации
|
// Проверяем API ключ при инициализации
|
||||||
if (apiKey == null || apiKey.trim().isEmpty() || apiKey.equals("your-openai-api-key-here")) {
|
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");
|
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");
|
throw new IllegalStateException("OpenAI API key is not configured");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!apiKey.startsWith("sk-")) {
|
if (!apiKey.startsWith("sk-")) {
|
||||||
logger.warn("OpenAI API key does not start with 'sk-'. Please verify your API key is correct.");
|
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))
|
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
||||||
.defaultHeader("Authorization", "Bearer " + apiKey)
|
.defaultHeader("Authorization", "Bearer " + apiKey)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
logger.info("OpenAIAnalyticsService initialized with model: {}", modelName);
|
logger.info("OpenAIAnalyticsService initialized with model: {}", modelName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +129,7 @@ public class OpenAIAnalyticsService {
|
|||||||
|
|
||||||
private String generate(String text, String instructionPrefix) {
|
private String generate(String text, String instructionPrefix) {
|
||||||
String prompt = instructionPrefix + text;
|
String prompt = instructionPrefix + text;
|
||||||
|
|
||||||
Map<String, Object> message = new HashMap<>();
|
Map<String, Object> message = new HashMap<>();
|
||||||
message.put("role", "user");
|
message.put("role", "user");
|
||||||
message.put("content", prompt);
|
message.put("content", prompt);
|
||||||
@@ -147,9 +149,11 @@ public class OpenAIAnalyticsService {
|
|||||||
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
|
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
|
||||||
})
|
})
|
||||||
.onErrorResume(err -> {
|
.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")) {
|
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")) {
|
} else if (err.getMessage().contains("429")) {
|
||||||
logger.error("OpenAI API rate limit exceeded. Please try again later.");
|
logger.error("OpenAI API rate limit exceeded. Please try again later.");
|
||||||
} else if (err.getMessage().contains("500")) {
|
} else if (err.getMessage().contains("500")) {
|
||||||
@@ -186,7 +190,7 @@ public class OpenAIAnalyticsService {
|
|||||||
|
|
||||||
public String generateWithInstruction(String text, String instruction, String language) {
|
public String generateWithInstruction(String text, String instruction, String language) {
|
||||||
String prompt = instruction + "\n\n" + text;
|
String prompt = instruction + "\n\n" + text;
|
||||||
|
|
||||||
Map<String, Object> message = new HashMap<>();
|
Map<String, Object> message = new HashMap<>();
|
||||||
message.put("role", "user");
|
message.put("role", "user");
|
||||||
message.put("content", prompt);
|
message.put("content", prompt);
|
||||||
@@ -206,9 +210,11 @@ public class OpenAIAnalyticsService {
|
|||||||
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
|
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
|
||||||
})
|
})
|
||||||
.onErrorResume(err -> {
|
.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")) {
|
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")) {
|
} else if (err.getMessage().contains("429")) {
|
||||||
logger.error("OpenAI API rate limit exceeded. Please try again later.");
|
logger.error("OpenAI API rate limit exceeded. Please try again later.");
|
||||||
} else if (err.getMessage().contains("500")) {
|
} else if (err.getMessage().contains("500")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user