diff --git a/Dockerfiles/Dockerfile.jar b/Dockerfiles/Dockerfile.jar index 836d9dd..02d696f 100644 --- a/Dockerfiles/Dockerfile.jar +++ b/Dockerfiles/Dockerfile.jar @@ -12,7 +12,7 @@ ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 复制JAR文件到容器的/app目录下 -COPY /target/gpt-4-copilot-0.2.4.jar app.jar +COPY /target/gpt-4-copilot-0.2.5.jar app.jar # 声明服务运行在8081端口 EXPOSE 8081 diff --git a/config.json b/config.json index 4adc63e..ff0bcbf 100644 --- a/config.json +++ b/config.json @@ -2,7 +2,7 @@ "password":"920052ec-14fc-4215-974d-d6fd3c89dd91", "gpt4_prompt":true, "gpt3_sleepTime":0, - "gpt4_sleepTime":100, + "gpt4_sleepTime":80, "maxPoolSize":300, "vscode_version":"1.88.0", "copilot_chat_version":"0.15.2024041001", diff --git a/pom.xml b/pom.xml index 9e7d18d..3a0954a 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.gpt4.copilot gpt-4-copilot - 0.2.4 + 0.2.5 native Demo project for Spring Boot with GraalVM Native Image diff --git a/src/main/java/com/gpt4/copilot/controller/ChatController.java b/src/main/java/com/gpt4/copilot/controller/ChatController.java index 19c3c3c..832f37f 100644 --- a/src/main/java/com/gpt4/copilot/controller/ChatController.java +++ b/src/main/java/com/gpt4/copilot/controller/ChatController.java @@ -11,6 +11,7 @@ import com.gpt4.copilot.copilotApplication; import com.gpt4.copilot.pojo.Conversation; import com.gpt4.copilot.pojo.Result; import com.gpt4.copilot.pojo.SystemSetting; +import com.gpt4.copilot.pojo.streamResponse; import com.unfbx.chatgpt.entity.chat.Message; import com.unfbx.chatgpt.utils.TikTokensUtil; import jakarta.servlet.http.HttpServletRequest; @@ -707,7 +708,6 @@ public class ChatController { String keyAndUrl = authorizationHeader.substring(7); if (!keyAndUrl.contains("|")) { apiKey = keyAndUrl; - ; } else { String[] parts = keyAndUrl.split("\\|"); requestUrl = parts[0]; @@ -728,7 +728,6 @@ public class ChatController { String keyAndUrl = authorizationHeader.substring(7); if (!keyAndUrl.contains("|")) { apiKey = keyAndUrl; - ; } else { String[] parts = keyAndUrl.split("\\|"); requestUrl = parts[0]; @@ -1389,10 +1388,12 @@ public class ChatController { int sleep_time, long requestTokens, String apiKey) { +// // 用于计算token的临时模型 String temModel = model == null || !model.startsWith("gpt-4") ? "gpt-3.5-turbo-0613" : "gpt-4-0613"; try (PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8), true); BufferedReader in = new BufferedReader(new InputStreamReader(resp.body().byteStream(), StandardCharsets.UTF_8))) { String line; + String resContent = ""; long tokens = 0; if (in.toString().length() < 0) { response.setContentType("application/json; charset=utf-8"); @@ -1400,33 +1401,47 @@ public class ChatController { return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR); } while ((line = in.readLine()) != null) { - out.println(line); - out.flush(); if (line.startsWith("data:")) { try { String res_text = line.replace("data: ", ""); if (!"[DONE]".equals(res_text.trim())) { JSONObject resJson = com.alibaba.fastjson2.JSON.parseObject(res_text); JSONArray choicesArray = resJson.getJSONArray("choices"); - if (choicesArray.size() > 0) { + if (resJson.size() > 0 && choicesArray.size() > 0) { JSONObject firstChoice = choicesArray.getJSONObject(0); String content = firstChoice.getJSONObject("delta").getString("content"); - tokens += TikTokensUtil.tokens(temModel, content); - } - if (sleep_time > 0) { - Thread.sleep(sleep_time); + if (content == null || content.length() == 0) { + continue; + } + String chat_message_id = resJson.getString("id"); + String timestamp = resJson.getString("created"); + streamResponse.Choice choice = new streamResponse.Choice(0, new streamResponse.Delta(content), null); + streamResponse streamResponse = new streamResponse(chat_message_id, "chat.completion.chunk", model, choice, timestamp); + String tmpRes = "data: " + com.alibaba.fastjson.JSONObject.toJSONString(streamResponse) + "\n\n"; + out.print(tmpRes); + out.flush(); + resContent += content; + if (sleep_time > 0) { + Thread.sleep(sleep_time); + } } + } else { + out.print(line + "\n\n"); + out.flush(); } } catch (InterruptedException e) { + log.error(" A error occur......", e); throw new RuntimeException(e); } } } + tokens += TikTokensUtil.tokens(temModel, resContent); log.info("请求密钥:" + apiKey + ",是否流式:true,使用模型:" + model + ",请求tokens:" + requestTokens + ",补全tokens:" + tokens + ",vscode_version:" + systemSetting.getVscode_version() + ",copilot_chat_version:" + systemSetting.getCopilot_chat_version() - + ",字符间隔时间:" + sleep_time + "ms,响应:" + resp); + + ",字符间隔时间:" + sleep_time + "ms,响应内容:" + resContent); return null; } catch (IOException e) { + log.error(" A error occur......", e); throw new RuntimeException(e); } } diff --git a/src/main/java/com/gpt4/copilot/controller/CustomErrorController.java b/src/main/java/com/gpt4/copilot/controller/CustomErrorController.java index b35f898..f648e0e 100644 --- a/src/main/java/com/gpt4/copilot/controller/CustomErrorController.java +++ b/src/main/java/com/gpt4/copilot/controller/CustomErrorController.java @@ -25,7 +25,7 @@ public class CustomErrorController implements ErrorController { " Document\n" + "\n" + "\n" + - "

Thanks you use gpt4-copilot-java-0.2.4

\n" + + "

Thanks you use gpt4-copilot-java-0.2.5

\n" + "

详细使用文档

\n" + "

项目地址

\n" + "\n" + diff --git a/src/main/java/com/gpt4/copilot/copilotApplication.java b/src/main/java/com/gpt4/copilot/copilotApplication.java index 50484a1..0d3ad84 100644 --- a/src/main/java/com/gpt4/copilot/copilotApplication.java +++ b/src/main/java/com/gpt4/copilot/copilotApplication.java @@ -298,11 +298,9 @@ public class copilotApplication { System.out.println("one_selfCopilot_limit:" + ChatController.getSystemSetting().getOne_selfCopilot_limit()); System.out.println("gpt4-copilot-java 初始化接口成功!"); System.out.println("======================================================"); - System.out.println("******原神gpt4-copilot-java v0.2.4启动成功******"); + System.out.println("******原神gpt4-copilot-java v0.2.5启动成功******"); System.out.println("* 由于本人略菜,graalvm依赖问题无法解决,之后代码将只通过jar和docker的形式运行"); - System.out.println("* 修复部分bug,优化读取config.json代码,提升稳定性"); - System.out.println("* 新增token计算,优化报错,支持one_api重试机制"); - System.out.println("* 优化部分报错日志,提高日志可读性"); + System.out.println("* 适配最新的ui Open Webui"); System.out.println("URL地址:http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + ""); System.out.println("======================================================"); } diff --git a/src/main/java/com/gpt4/copilot/pojo/streamResponse.java b/src/main/java/com/gpt4/copilot/pojo/streamResponse.java new file mode 100644 index 0000000..6edff20 --- /dev/null +++ b/src/main/java/com/gpt4/copilot/pojo/streamResponse.java @@ -0,0 +1,41 @@ +package com.gpt4.copilot.pojo; + +import com.alibaba.fastjson.annotation.JSONType; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author Yangyang + * @create 2024-04-26 11:17 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@JSONType(orders={"id","model","object","choices","created"}) +public class streamResponse { + private String id; + private String model; + private String object; + private Choice choice; + private String created; + + + @Data + @NoArgsConstructor + @AllArgsConstructor + @JSONType(orders={"index", "delta", "finish_reason"}) + public static class Choice { + private int index; + private Delta delta; + private String finish_reason; + } + + @Data + @NoArgsConstructor + @AllArgsConstructor + public static class Delta { + private String content; + + } +} diff --git a/target/classes/com/gpt4/copilot/controller/ChatController$1.class b/target/classes/com/gpt4/copilot/controller/ChatController$1.class index d930e21..c4c2113 100644 Binary files a/target/classes/com/gpt4/copilot/controller/ChatController$1.class and b/target/classes/com/gpt4/copilot/controller/ChatController$1.class differ diff --git a/target/classes/com/gpt4/copilot/controller/ChatController$2.class b/target/classes/com/gpt4/copilot/controller/ChatController$2.class index 145c0bb..63540d5 100644 Binary files a/target/classes/com/gpt4/copilot/controller/ChatController$2.class and b/target/classes/com/gpt4/copilot/controller/ChatController$2.class differ diff --git a/target/classes/com/gpt4/copilot/controller/ChatController.class b/target/classes/com/gpt4/copilot/controller/ChatController.class index 3e4cd68..c5f0aa9 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/controller/CustomErrorController.class b/target/classes/com/gpt4/copilot/controller/CustomErrorController.class index 3dc169a..712fc35 100644 Binary files a/target/classes/com/gpt4/copilot/controller/CustomErrorController.class and b/target/classes/com/gpt4/copilot/controller/CustomErrorController.class differ diff --git a/target/classes/com/gpt4/copilot/copilotApplication.class b/target/classes/com/gpt4/copilot/copilotApplication.class index 0c51a22..72f3460 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/streamResponse$Choice.class b/target/classes/com/gpt4/copilot/pojo/streamResponse$Choice.class new file mode 100644 index 0000000..39da9a0 Binary files /dev/null and b/target/classes/com/gpt4/copilot/pojo/streamResponse$Choice.class differ diff --git a/target/classes/com/gpt4/copilot/pojo/streamResponse$Delta.class b/target/classes/com/gpt4/copilot/pojo/streamResponse$Delta.class new file mode 100644 index 0000000..ac3bbf5 Binary files /dev/null and b/target/classes/com/gpt4/copilot/pojo/streamResponse$Delta.class differ diff --git a/target/classes/com/gpt4/copilot/pojo/streamResponse.class b/target/classes/com/gpt4/copilot/pojo/streamResponse.class new file mode 100644 index 0000000..1592b7c Binary files /dev/null and b/target/classes/com/gpt4/copilot/pojo/streamResponse.class differ diff --git a/target/gpt-4-copilot-0.2.4.jar.original b/target/gpt-4-copilot-0.2.4.jar.original deleted file mode 100644 index fc40652..0000000 Binary files a/target/gpt-4-copilot-0.2.4.jar.original and /dev/null differ diff --git a/target/gpt-4-copilot-0.2.4.jar b/target/gpt-4-copilot-0.2.5.jar similarity index 99% rename from target/gpt-4-copilot-0.2.4.jar rename to target/gpt-4-copilot-0.2.5.jar index 4378956..e86ffb6 100644 Binary files a/target/gpt-4-copilot-0.2.4.jar and b/target/gpt-4-copilot-0.2.5.jar differ diff --git a/target/gpt-4-copilot-0.2.5.jar.original b/target/gpt-4-copilot-0.2.5.jar.original new file mode 100644 index 0000000..7ecb434 Binary files /dev/null and b/target/gpt-4-copilot-0.2.5.jar.original differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties index 3f0cfc5..17e36ad 100644 --- a/target/maven-archiver/pom.properties +++ b/target/maven-archiver/pom.properties @@ -1,3 +1,3 @@ artifactId=gpt-4-copilot groupId=com.gpt4.copilot -version=0.2.4 +version=0.2.5 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index 36a64ac..9180cbc 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1,12 +1,15 @@ com\gpt4\copilot\controller\ChatController.class com\gpt4\copilot\config\CorsConfig.class com\gpt4\copilot\pojo\Conversation$ConversationBuilderImpl.class +com\gpt4\copilot\pojo\streamResponse.class com\gpt4\copilot\copilotApplication.class +com\gpt4\copilot\pojo\streamResponse$Delta.class com\gpt4\copilot\controller\ChatController$1.class com\gpt4\copilot\pojo\Conversation$ConversationBuilder.class com\gpt4\copilot\config\WebConfig.class com\gpt4\copilot\controller\CustomErrorController.class com\gpt4\copilot\pojo\Conversation.class +com\gpt4\copilot\pojo\streamResponse$Choice.class com\gpt4\copilot\pojo\SystemSetting.class com\gpt4\copilot\pojo\Result.class com\gpt4\copilot\controller\ChatController$2.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index c970c86..3be3273 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -4,5 +4,6 @@ F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\ F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\pojo\Conversation.java F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\pojo\SystemSetting.java F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\controller\ChatController.java +F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\pojo\streamResponse.java F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\config\WebConfig.java F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\copilotApplication.java