diff --git a/config.json b/config.json index ff0bcbf..d0e7328 100644 --- a/config.json +++ b/config.json @@ -11,5 +11,6 @@ "one_coCopilot_limit":30, "one_selfCopilot_limit":30, "serverPort":8080, + "max_tokens":32, "prefix":"/" } \ No newline at end of file diff --git a/src/main/java/com/gpt4/copilot/controller/ChatController.java b/src/main/java/com/gpt4/copilot/controller/ChatController.java index 6afe65b..b697566 100644 --- a/src/main/java/com/gpt4/copilot/controller/ChatController.java +++ b/src/main/java/com/gpt4/copilot/controller/ChatController.java @@ -145,7 +145,7 @@ public class ChatController { machineIdList = new ConcurrentHashMap<>(); setSystemSetting(selectSetting()); setExecutor(systemSetting.getMaxPoolSize()); - log.info("服务启动了最大线程数为"+systemSetting.getMaxPoolSize()+"的线程池"); + log.info("服务启动了最大线程数为" + systemSetting.getMaxPoolSize() + "的线程池"); log.info(loadData()); } catch (Exception e) { throw new RuntimeException(e); @@ -277,7 +277,7 @@ public class ChatController { Integer oneCopilotLimit = getValueOrDefault(jsonObject, "one_copilot_limit", 30, "config.json没有新增one_copilot_limit参数,现已增加!"); Integer oneCoCopilotLimit = getValueOrDefault(jsonObject, "one_coCopilot_limit", 30, "config.json没有新增one_coCopilot_limit参数,现已增加!"); Integer oneSelfCopilotLimit = getValueOrDefault(jsonObject, "one_selfCopilot_limit", 30, "config.json没有新增one_selfCopilot_limit参数,现已增加!"); - + Integer max_tokens = getValueOrDefault(jsonObject, "max_tokens", 32, "config.json没有新增max_tokens参数,现已增加!"); // 将修改后的 JSONObject 转换为格式化的 JSON 字符串 String updatedJson = com.alibaba.fastjson.JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat); Files.write(Paths.get(parent), updatedJson.getBytes()); @@ -295,6 +295,7 @@ public class ChatController { config.setOne_coCopilot_limit(oneCoCopilotLimit); config.setOne_selfCopilot_limit(oneSelfCopilotLimit); config.setGpt4_prompt(gpt4Prompt); + config.setMax_tokens(max_tokens); return config; } catch (Exception e) { @@ -687,7 +688,7 @@ public class ChatController { throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Request body is missing or not in JSON format"); } long tokens = conversation.tokens(); - if (tokens > 32 * 1024) { + if (tokens > systemSetting.getMax_tokens() * 1024) { log.error("Thread ID: " + Thread.currentThread().getId() + "请求密钥:" + apiKey + ",本次请求tokens is too long and over 32K: " + tokens); throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Message is too long and over 32K"); } else if (tokens <= 0) { @@ -1401,11 +1402,9 @@ public class ChatController { // 使用stream形式适应List List choices = Stream.of(new StreamResponse.Choice(0, new StreamResponse.Delta(content), null)).collect(Collectors.toList()); StreamResponse streamResponse = new StreamResponse(chat_message_id, model, "chat.completion.chunk", choices, timestamp); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("data: "); - stringBuilder.append(com.alibaba.fastjson.JSONObject.toJSONString(streamResponse)); - stringBuilder.append("\n\n"); - String tmpRes = stringBuilder.toString(); + String tmpRes = "data: " + + com.alibaba.fastjson.JSONObject.toJSONString(streamResponse) + + "\n\n"; out.print(tmpRes); out.flush(); diff --git a/src/main/java/com/gpt4/copilot/copilotApplication.java b/src/main/java/com/gpt4/copilot/copilotApplication.java index 9b8a7c6..f506ccb 100644 --- a/src/main/java/com/gpt4/copilot/copilotApplication.java +++ b/src/main/java/com/gpt4/copilot/copilotApplication.java @@ -92,6 +92,7 @@ public class copilotApplication { getValueOrDefault(jsonObject, "one_copilot_limit", 30, "config.json没有新增one_copilot_limit参数,现已增加!"); getValueOrDefault(jsonObject, "one_coCopilot_limit", 30, "config.json没有新增one_coCopilot_limit参数,现已增加!"); getValueOrDefault(jsonObject, "one_selfCopilot_limit", 30, "config.json没有新增one_selfCopilot_limit参数,现已增加!"); + getValueOrDefault(jsonObject, "max_tokens", 32, "config.json没有新增max_tokens参数,现已增加!"); // 将修改后的 JSONObject 转换为格式化的 JSON 字符串 String updatedJson = com.alibaba.fastjson.JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat); @@ -296,6 +297,7 @@ public class copilotApplication { System.out.println("one_copilot_limit:" + ChatController.getSystemSetting().getOne_copilot_limit()); System.out.println("one_coCopilot_limit:" + ChatController.getSystemSetting().getOne_coCopilot_limit()); System.out.println("one_selfCopilot_limit:" + ChatController.getSystemSetting().getOne_selfCopilot_limit()); + System.out.println("max_tokens:" + ChatController.getSystemSetting().getMax_tokens()); System.out.println("gpt4-copilot-java 初始化接口成功!"); System.out.println("======================================================"); System.out.println("******原神gpt4-copilot-java v0.2.7启动成功******"); diff --git a/src/main/java/com/gpt4/copilot/pojo/Conversation.java b/src/main/java/com/gpt4/copilot/pojo/Conversation.java index 610c777..f4364c2 100644 --- a/src/main/java/com/gpt4/copilot/pojo/Conversation.java +++ b/src/main/java/com/gpt4/copilot/pojo/Conversation.java @@ -66,6 +66,7 @@ public class Conversation implements Serializable { */ @NonNull private List messages; + @Builder.Default private String model = "gpt-3.5-turbo"; diff --git a/src/main/java/com/gpt4/copilot/pojo/SystemSetting.java b/src/main/java/com/gpt4/copilot/pojo/SystemSetting.java index cd7ca7a..1704ebd 100644 --- a/src/main/java/com/gpt4/copilot/pojo/SystemSetting.java +++ b/src/main/java/com/gpt4/copilot/pojo/SystemSetting.java @@ -75,4 +75,9 @@ public class SystemSetting { */ private Integer one_selfCopilot_limit; + /** + * max_tokens + */ + private Integer max_tokens; + } \ No newline at end of file diff --git a/target/classes/com/gpt4/copilot/controller/ChatController.class b/target/classes/com/gpt4/copilot/controller/ChatController.class index 69c0e41..b2603d8 100644 Binary files a/target/classes/com/gpt4/copilot/controller/ChatController.class and b/target/classes/com/gpt4/copilot/controller/ChatController.class differ diff --git a/target/classes/com/gpt4/copilot/copilotApplication.class b/target/classes/com/gpt4/copilot/copilotApplication.class index fffefad..a5ae9e1 100644 Binary files a/target/classes/com/gpt4/copilot/copilotApplication.class and b/target/classes/com/gpt4/copilot/copilotApplication.class differ diff --git a/target/classes/com/gpt4/copilot/pojo/Conversation.class b/target/classes/com/gpt4/copilot/pojo/Conversation.class index 28dcf4f..8c76e03 100644 Binary files a/target/classes/com/gpt4/copilot/pojo/Conversation.class and b/target/classes/com/gpt4/copilot/pojo/Conversation.class differ diff --git a/target/classes/com/gpt4/copilot/pojo/SystemSetting.class b/target/classes/com/gpt4/copilot/pojo/SystemSetting.class index 41e1315..f657252 100644 Binary files a/target/classes/com/gpt4/copilot/pojo/SystemSetting.class and b/target/classes/com/gpt4/copilot/pojo/SystemSetting.class differ diff --git a/target/gpt-4-copilot-0.2.7.jar b/target/gpt-4-copilot-0.2.7.jar index 5d43bb7..f2334aa 100644 Binary files a/target/gpt-4-copilot-0.2.7.jar and b/target/gpt-4-copilot-0.2.7.jar differ diff --git a/target/gpt-4-copilot-0.2.7.jar.original b/target/gpt-4-copilot-0.2.7.jar.original index 2d0ab94..59ca4f6 100644 Binary files a/target/gpt-4-copilot-0.2.7.jar.original and b/target/gpt-4-copilot-0.2.7.jar.original differ