Skip to content

Commit

Permalink
Allow for customizing the replacement of OpenAI request domain name
Browse files Browse the repository at this point in the history
  • Loading branch information
kylelin1998 committed Oct 13, 2023
1 parent 2b71aa9 commit 7b5a930
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/main/java/code/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/code/config/ConfigSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public class ConfigSettings {
@ConfigField(isNotNull = false)
private Boolean conciseReplies;

@JSONField(name = "openai_api_prefix")
@ConfigField(isNotNull = true)
private String openaiAPIPrefix;

}
1 change: 1 addition & 0 deletions src/main/java/code/config/I18nEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public enum I18nEnum {
Die("die"),
PleaseChooseConciseReplies("please_choose_concise_replies"),
SetConciseReplies("set_concise_replies"),
SetOpenAIAPIPrefix("set_openai_api_prefix"),

;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/code/handler/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/code/handler/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -116,7 +116,7 @@ private static boolean conciseReplies() {
}

public static void init() {
AdminCommandsHandler.init();
AdminCommands.init();

// Chat
StepsBuilder
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/code/util/GPTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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))
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/i18n/i18n_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
set_concise_replies=Set Concise Replies
set_openai_api_prefix=Set the OpenAI API request prefix
3 changes: 2 additions & 1 deletion src/main/resources/i18n/i18n_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
set_concise_replies=\u8BBE\u7F6E\u7CBE\u7B80\u56DE\u590D
set_openai_api_prefix=\u8BBE\u7F6EOpenAI API\u8BF7\u6C42\u524D\u7F00

0 comments on commit 7b5a930

Please sign in to comment.