适配最新的ui Open Webui

This commit is contained in:
Yanyutin753
2024-04-26 11:45:11 +08:00
parent 404ed0e042
commit be35d070c2
21 changed files with 77 additions and 19 deletions

View File

@@ -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

View File

@@ -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",

View File

@@ -10,7 +10,7 @@
</parent>
<groupId>com.gpt4.copilot</groupId>
<artifactId>gpt-4-copilot</artifactId>
<version>0.2.4</version>
<version>0.2.5</version>
<name>native</name>
<description>Demo project for Spring Boot with GraalVM Native Image</description>
<properties>

View File

@@ -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);
}
}

View File

@@ -25,7 +25,7 @@ public class CustomErrorController implements ErrorController {
" <title>Document</title>\n" +
"</head>\n" +
"<body>\n" +
" <p>Thanks you use gpt4-copilot-java-0.2.4</p>\n" +
" <p>Thanks you use gpt4-copilot-java-0.2.5</p>\n" +
" <p><a href=\"https://apifox.com/apidoc/shared-4301e565-a8df-48a0-85a5-bda2c4c3965a\">详细使用文档</a></p>\n" +
" <p><a href=\"https://github.com/Yanyutin753/unofficial-gpt4-api\">项目地址</a></p>\n" +
"</body>\n" +

View File

@@ -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("======================================================");
}

View File

@@ -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;
}
}

Binary file not shown.

View File

@@ -1,3 +1,3 @@
artifactId=gpt-4-copilot
groupId=com.gpt4.copilot
version=0.2.4
version=0.2.5

View File

@@ -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

View File

@@ -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