diff --git a/src/main/java/code/config/Config.java b/src/main/java/code/config/Config.java index cca52ca..bd6400f 100644 --- a/src/main/java/code/config/Config.java +++ b/src/main/java/code/config/Config.java @@ -39,7 +39,7 @@ public class Config { private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(); public static class MetaData { - public final static String CurrentVersion = "1.0.52"; + public final static String CurrentVersion = "1.0.60"; public final static String GitOwner = "kylelin1998"; public final static String GitRepo = "ChatGPTForTelegram"; public final static String ProcessName = "ChatGPTForTelegram-universal.jar"; @@ -114,6 +114,10 @@ private static ConfigSettings handle(ConfigSettings configSettings) { if (StringUtils.isBlank(gptModel)) { configSettings.setGptModel(GPTModel.Gpt3_5Turbo.getModel()); } + String openaiAPIPrefix = configSettings.getOpenaiAPIPrefix(); + if (StringUtils.isBlank(openaiAPIPrefix)) { + configSettings.setOpenaiAPIPrefix("https://api.openai.com"); + } return configSettings; } diff --git a/src/main/java/code/config/ConfigSettings.java b/src/main/java/code/config/ConfigSettings.java index 2dcd407..404668a 100644 --- a/src/main/java/code/config/ConfigSettings.java +++ b/src/main/java/code/config/ConfigSettings.java @@ -53,4 +53,8 @@ public class ConfigSettings { @ConfigField(isNotNull = false) private Boolean conciseReplies; + @JSONField(name = "openai_api_prefix") + @ConfigField(isNotNull = true) + private String openaiAPIPrefix; + } diff --git a/src/main/java/code/config/I18nEnum.java b/src/main/java/code/config/I18nEnum.java index 146d660..5da06e3 100644 --- a/src/main/java/code/config/I18nEnum.java +++ b/src/main/java/code/config/I18nEnum.java @@ -102,6 +102,7 @@ public enum I18nEnum { Die("die"), PleaseChooseConciseReplies("please_choose_concise_replies"), SetConciseReplies("set_concise_replies"), + SetOpenAIAPIPrefix("set_openai_api_prefix"), ; diff --git a/src/main/java/code/handler/Command.java b/src/main/java/code/handler/Command.java index 428a4db..297bc80 100644 --- a/src/main/java/code/handler/Command.java +++ b/src/main/java/code/handler/Command.java @@ -32,6 +32,7 @@ public enum Command { SetOpenStatus("set_open_status", false), SetGptToken("set_gpt_token", false), SetConciseReplies("set_concise_replies", false), + SetOpenAIAPIPrefix("set_openai_api_prefix", false), UpdateConfig("uc", false), Language("language", false), Restart("restart", false), diff --git a/src/main/java/code/handler/Handler.java b/src/main/java/code/handler/Handler.java index f584063..ea96ecf 100644 --- a/src/main/java/code/handler/Handler.java +++ b/src/main/java/code/handler/Handler.java @@ -2,7 +2,7 @@ import code.config.*; import code.eneity.RecordTableEntity; -import code.handler.commandsteps.AdminCommandsHandler; +import code.handler.commands.AdminCommands; import code.handler.message.InlineKeyboardButtonBuilder; import code.handler.message.InlineKeyboardButtonListBuilder; import code.handler.message.MessageHandle; @@ -86,7 +86,7 @@ private static boolean voiceHandle(StepsChatSession session) { GPTTranscriptionsParameter parameter = new GPTTranscriptionsParameter(); parameter.setModel(GPTTranscriptionsModel.Whisper_1.getModel()); parameter.setFile(new java.io.File(mp3)); - GPTTranscriptionsResponse response = GPTUtil.transcriptions(RequestProxyConfig.create(), token, parameter); + GPTTranscriptionsResponse response = GPTUtil.transcriptions(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), parameter); if (!response.isOk()) { MessageHandle.editMessage(message, I18nHandle.getText(session.getFromId(), I18nEnum.UnknownError)); return false; @@ -116,7 +116,7 @@ private static boolean conciseReplies() { } public static void init() { - AdminCommandsHandler.init(); + AdminCommands.init(); // Chat StepsBuilder @@ -178,7 +178,7 @@ public static void init() { token = GptTokenStore.getRandomToken(); Message finalMessage = message; - response = GPTUtil.chat(RequestProxyConfig.create(), token, gptChatParameter, (content -> { + response = GPTUtil.chat(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), gptChatParameter, (content -> { if (GlobalConfig.getDebug()) { log.info(JSON.toJSONString(content)); } @@ -348,7 +348,7 @@ public static void init() { for (int i = 0; i < 3; i++) { token = GptTokenStore.getRandomToken(); - response = GPTUtil.chat(RequestProxyConfig.create(), token, gptChatParameter, (content -> { + response = GPTUtil.chat(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), gptChatParameter, (content -> { if (GlobalConfig.getDebug()) { log.info(JSON.toJSONString(content)); } @@ -470,7 +470,7 @@ public static void init() { for (int i = 0; i < 3; i++) { token = GptTokenStore.getRandomToken(); - response = GPTUtil.chat(RequestProxyConfig.create(), token, gptChatParameter, (content -> { + response = GPTUtil.chat(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), gptChatParameter, (content -> { if (GlobalConfig.getDebug()) { log.info(JSON.toJSONString(content)); } @@ -584,7 +584,7 @@ public static void init() { for (int i = 0; i < 3; i++) { token = GptTokenStore.getRandomToken(); - response = GPTUtil.chat(RequestProxyConfig.create(), token, gptChatParameter, (content -> { + response = GPTUtil.chat(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), gptChatParameter, (content -> { if (GlobalConfig.getDebug()) { log.info(JSON.toJSONString(content)); } @@ -696,7 +696,7 @@ public static void init() { for (int i = 0; i < 3; i++) { token = GptTokenStore.getRandomToken(); - response = GPTUtil.chat(RequestProxyConfig.create(), token, gptChatParameter, (content -> { + response = GPTUtil.chat(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), gptChatParameter, (content -> { if (GlobalConfig.getDebug()) { log.info(JSON.toJSONString(content)); } @@ -781,7 +781,7 @@ public static void init() { String token = GptTokenStore.getRandomToken(); - GPTCreateImageResponse image = GPTUtil.createImage(RequestProxyConfig.create(), token, parameter); + GPTCreateImageResponse image = GPTUtil.createImage(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), parameter); if (image.isOk()) { MessageHandle.editMessage(message, I18nHandle.getText(session.getFromId(), I18nEnum.Downloading)); InputStream inputStream = DownloadUtil.download(RequestProxyConfig.create(), image.getData().get(0).getUrl()); @@ -850,7 +850,7 @@ public static void init() { for (int i = 0; i < 3; i++) { token = GptTokenStore.getRandomToken(); - response = GPTUtil.chat(RequestProxyConfig.create(), token, gptChatParameter, (content -> { + response = GPTUtil.chat(RequestProxyConfig.create(), token, GlobalConfig.getOpenaiAPIPrefix(), gptChatParameter, (content -> { if (GlobalConfig.getDebug()) { log.info(JSON.toJSONString(content)); } diff --git a/src/main/java/code/handler/commandsteps/AdminCommandsHandler.java b/src/main/java/code/handler/commands/AdminCommands.java similarity index 99% rename from src/main/java/code/handler/commandsteps/AdminCommandsHandler.java rename to src/main/java/code/handler/commands/AdminCommands.java index dc2e21f..041319e 100644 --- a/src/main/java/code/handler/commandsteps/AdminCommandsHandler.java +++ b/src/main/java/code/handler/commands/AdminCommands.java @@ -1,8 +1,7 @@ -package code.handler.commandsteps; +package code.handler.commands; import code.config.*; import code.handler.Command; -import code.handler.Handler; import code.handler.I18nHandle; import code.handler.StepsCenter; import code.handler.message.InlineKeyboardButtonBuilder; @@ -29,7 +28,7 @@ import static code.Main.GlobalConfig; @Slf4j -public class AdminCommandsHandler { +public class AdminCommands { private static boolean isAdmin(String fromId) { return GlobalConfig.getBotAdminId().equals(fromId); diff --git a/src/main/java/code/util/GPTUtil.java b/src/main/java/code/util/GPTUtil.java index dce0f62..3a44b4c 100644 --- a/src/main/java/code/util/GPTUtil.java +++ b/src/main/java/code/util/GPTUtil.java @@ -29,12 +29,12 @@ @Slf4j public class GPTUtil { - public static GPTCreateImageResponse createImage(RequestProxyConfig requestProxyConfig, String token, GPTCreateImageParameter parameter) { + public static GPTCreateImageResponse createImage(RequestProxyConfig requestProxyConfig, String token, String apiPrefix, GPTCreateImageParameter parameter) { GPTCreateImageResponse gptCreateImageResponse = new GPTCreateImageResponse(); gptCreateImageResponse.setOk(false); try { Request request = Request - .post("https://api.openai.com/v1/images/generations") + .post(apiPrefix + "/v1/images/generations") .setHeader("Authorization", "Bearer " + token) .bodyString(JSON.toJSONString(parameter), ContentType.APPLICATION_JSON) .connectTimeout(Timeout.ofSeconds(30)) @@ -58,7 +58,7 @@ public static GPTCreateImageResponse createImage(RequestProxyConfig requestProxy return gptCreateImageResponse; } - public static GPTTranscriptionsResponse transcriptions(RequestProxyConfig requestProxyConfig, String token, GPTTranscriptionsParameter parameter) { + public static GPTTranscriptionsResponse transcriptions(RequestProxyConfig requestProxyConfig, String token, String apiPrefix, GPTTranscriptionsParameter parameter) { GPTTranscriptionsResponse gptTranscriptionsResponse = new GPTTranscriptionsResponse(); gptTranscriptionsResponse.setOk(false); try { @@ -69,7 +69,7 @@ public static GPTTranscriptionsResponse transcriptions(RequestProxyConfig reques HttpEntity entity = builder.build(); Request request = Request - .post("https://api.openai.com/v1/audio/transcriptions") + .post(apiPrefix + "/v1/audio/transcriptions") .body(entity) .setHeader("Authorization", "Bearer " + token) .connectTimeout(Timeout.ofSeconds(30)) @@ -93,13 +93,13 @@ public static GPTTranscriptionsResponse transcriptions(RequestProxyConfig reques return gptTranscriptionsResponse; } - public static GPTChatResponse chat(RequestProxyConfig requestProxyConfig, String token, GPTChatParameter parameter, GPTCallback callback) { + public static GPTChatResponse chat(RequestProxyConfig requestProxyConfig, String token, String apiPrefix, GPTChatParameter parameter, GPTCallback callback) { GPTChatResponse chatResponse = new GPTChatResponse(); chatResponse.setOk(false); try { Request request = Request - .post("https://api.openai.com/v1/chat/completions") + .post(apiPrefix + "/v1/chat/completions") .setHeader("Authorization", "Bearer " + token) .setHeader("accept", "text/event-stream") .bodyString(JSON.toJSONString(parameter), org.apache.hc.core5.http.ContentType.APPLICATION_JSON) diff --git a/src/main/resources/i18n/i18n_en.properties b/src/main/resources/i18n/i18n_en.properties index 9c0b73a..e4c7f86 100644 --- a/src/main/resources/i18n/i18n_en.properties +++ b/src/main/resources/i18n/i18n_en.properties @@ -113,4 +113,5 @@ please_send_me_gpt_token=Please send me the ChatGPT API key. If you have multipl alive=Alive die=Die please_choose_concise_replies=Please select whether to enable concise replies. If enabled, some commands will remove the display of the question and exit prompt, and only show the reply content -set_concise_replies=Set Concise Replies \ No newline at end of file +set_concise_replies=Set Concise Replies +set_openai_api_prefix=Set the OpenAI API request prefix \ No newline at end of file diff --git a/src/main/resources/i18n/i18n_zh_CN.properties b/src/main/resources/i18n/i18n_zh_CN.properties index 74197e8..796b5a5 100644 --- a/src/main/resources/i18n/i18n_zh_CN.properties +++ b/src/main/resources/i18n/i18n_zh_CN.properties @@ -111,4 +111,5 @@ please_send_me_gpt_token=\u8BF7\u53D1\u9001ChatGPT API Key\u7ED9\u6211\uFF0C \u5 alive=\u5B58\u6D3B die=\u6B7B\u4EA1 please_choose_concise_replies=\u8BF7\u9009\u62E9\u662F\u5426\u5F00\u542F\u7CBE\u7B80\u56DE\u590D\uFF1F \u5F00\u542F\u540E\u90E8\u5206\u547D\u4EE4\u4F1A\u53BB\u9664\u56DE\u590D\u7684\u95EE\u9898\u663E\u793A\u548C\u9000\u51FA\u63D0\u793A\uFF0C \u4EC5\u663E\u793A\u56DE\u590D\u5185\u5BB9 -set_concise_replies=\u8BBE\u7F6E\u7CBE\u7B80\u56DE\u590D \ No newline at end of file +set_concise_replies=\u8BBE\u7F6E\u7CBE\u7B80\u56DE\u590D +set_openai_api_prefix=\u8BBE\u7F6EOpenAI API\u8BF7\u6C42\u524D\u7F00 \ No newline at end of file