mirror of
https://github.com/YuWanTingbb/unofficial-gpt4.git
synced 2025-10-16 07:03:17 +00:00
gpt4-copilot-java v0.2.4
1. 优化部分报错日志,提高日志可读性 2. 打包加入背景时间,方便查看日志
This commit is contained in:
@@ -7,8 +7,12 @@ LABEL maintainer="Yanyutin753"
|
|||||||
# 切换到 root 用户
|
# 切换到 root 用户
|
||||||
USER root
|
USER root
|
||||||
|
|
||||||
|
# 设置系统时区为Asia/Shanghai(北京时间)
|
||||||
|
ENV TZ=Asia/Shanghai
|
||||||
|
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||||||
|
|
||||||
# 复制JAR文件到容器的/app目录下
|
# 复制JAR文件到容器的/app目录下
|
||||||
COPY /target/gpt-4-copilot-0.2.3.jar app.jar
|
COPY /target/gpt-4-copilot-0.2.4.jar app.jar
|
||||||
|
|
||||||
# 声明服务运行在8081端口
|
# 声明服务运行在8081端口
|
||||||
EXPOSE 8081
|
EXPOSE 8081
|
||||||
|
12
pom.xml
12
pom.xml
@@ -10,7 +10,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
<groupId>com.gpt4.copilot</groupId>
|
<groupId>com.gpt4.copilot</groupId>
|
||||||
<artifactId>gpt-4-copilot</artifactId>
|
<artifactId>gpt-4-copilot</artifactId>
|
||||||
<version>0.2.3</version>
|
<version>0.2.4</version>
|
||||||
<name>native</name>
|
<name>native</name>
|
||||||
<description>Demo project for Spring Boot with GraalVM Native Image</description>
|
<description>Demo project for Spring Boot with GraalVM Native Image</description>
|
||||||
<properties>
|
<properties>
|
||||||
@@ -43,22 +43,12 @@
|
|||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>2.0.32</version>
|
<version>2.0.32</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- okhttp依赖-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
|
||||||
<artifactId>okhttp</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- 统计token-->
|
<!-- 统计token-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.unfbx</groupId>
|
<groupId>com.unfbx</groupId>
|
||||||
<artifactId>chatgpt-java</artifactId>
|
<artifactId>chatgpt-java</artifactId>
|
||||||
<version>1.1.5</version>
|
<version>1.1.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.knuddels</groupId>
|
|
||||||
<artifactId>jtokkit</artifactId>
|
|
||||||
<version>0.6.1</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@@ -425,6 +425,7 @@ public class ChatController {
|
|||||||
.addHeader("Accept", "*/*").build();
|
.addHeader("Accept", "*/*").build();
|
||||||
try (Response res = client.newCall(request_token).execute()) {
|
try (Response res = client.newCall(request_token).execute()) {
|
||||||
if (!res.isSuccessful()) {
|
if (!res.isSuccessful()) {
|
||||||
|
log.error("Unsuccessful response: " + res.message());
|
||||||
return new ResponseEntity<>(Result.error("Unsuccessful response: " + res.message()), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("Unsuccessful response: " + res.message()), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
response.setContentType("application/json; charset=utf-8");
|
response.setContentType("application/json; charset=utf-8");
|
||||||
@@ -491,10 +492,12 @@ public class ChatController {
|
|||||||
} else {
|
} else {
|
||||||
int requestNum = copilotTokenLimitList.get(apiKey).incrementAndGet();
|
int requestNum = copilotTokenLimitList.get(apiKey).incrementAndGet();
|
||||||
if (requestNum > systemSetting.getOne_copilot_limit()) {
|
if (requestNum > systemSetting.getOne_copilot_limit()) {
|
||||||
log.info(apiKey + " requests is " + requestNum + " rate limit exceeded");
|
log.error("请求密钥:" + apiKey + " requests is " + requestNum + " rate limit exceeded");
|
||||||
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 检查请求结构体
|
||||||
|
long requestTokens = checkConversation(conversation, apiKey);
|
||||||
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
||||||
String chat_token = copilotTokenList.get(apiKey);
|
String chat_token = copilotTokenList.get(apiKey);
|
||||||
Map<String, String> headersMap = new HashMap<>();
|
Map<String, String> headersMap = new HashMap<>();
|
||||||
@@ -503,24 +506,26 @@ public class ChatController {
|
|||||||
String model = modelAdjust(conversation);
|
String model = modelAdjust(conversation);
|
||||||
Request streamRequest = getPrompt(conversation, model, headersMap);
|
Request streamRequest = getPrompt(conversation, model, headersMap);
|
||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
log.info(resp.toString());
|
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
if (resp.code() == 429) {
|
if (resp.code() == 429) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
} else if (resp.code() == 400) {
|
} else if (resp.code() == 400) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
String token = getCopilotToken(apiKey);
|
String token = getCopilotToken(apiKey);
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
|
log.error("无效请求密钥:" + apiKey + ",请求token响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
copilotTokenList.put(apiKey, token);
|
copilotTokenList.put(apiKey, token);
|
||||||
log.info("token过期,Github CopilotToken重置化成功!");
|
log.info("token过期,Github CopilotToken重置化成功!");
|
||||||
return againConversation(response, conversation, token, apiKey, model);
|
return againConversation(response, conversation, token, apiKey, model, requestTokens);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 流式和非流式输出
|
// 流式和非流式输出
|
||||||
return outPutChat(response, resp, conversation, model);
|
return outPutChat(response, resp, conversation, model, requestTokens, apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -532,7 +537,6 @@ public class ChatController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRequestApikey(String authorizationHeader, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
private String getRequestApikey(String authorizationHeader, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||||
checkConversation(conversation);
|
|
||||||
String apiKey;
|
String apiKey;
|
||||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||||
apiKey = authorizationHeader.substring(7);
|
apiKey = authorizationHeader.substring(7);
|
||||||
@@ -602,10 +606,12 @@ public class ChatController {
|
|||||||
} else {
|
} else {
|
||||||
int requestNum = coCopilotTokenLimitList.get(apiKey).incrementAndGet();
|
int requestNum = coCopilotTokenLimitList.get(apiKey).incrementAndGet();
|
||||||
if (requestNum > systemSetting.getOne_coCopilot_limit()) {
|
if (requestNum > systemSetting.getOne_coCopilot_limit()) {
|
||||||
log.info(apiKey + " requests is " + requestNum + " rate limit exceeded");
|
log.error("请求密钥:" + apiKey + " requests is " + requestNum + " rate limit exceeded");
|
||||||
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 检查请求结构体
|
||||||
|
long requestTokens = checkConversation(conversation, apiKey);
|
||||||
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
||||||
String chat_token = coCopilotTokenList.get(apiKey);
|
String chat_token = coCopilotTokenList.get(apiKey);
|
||||||
Map<String, String> headersMap = new HashMap<>();
|
Map<String, String> headersMap = new HashMap<>();
|
||||||
@@ -616,21 +622,24 @@ public class ChatController {
|
|||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
if (resp.code() == 429) {
|
if (resp.code() == 429) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
} else if (resp.code() == 400) {
|
} else if (resp.code() == 400) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
String token = getCoCoToken(apiKey);
|
String token = getCoCoToken(apiKey);
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
|
log.error("无效请求密钥:" + apiKey + ",请求token响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("cocopilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("cocopilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
coCopilotTokenList.put(apiKey, token);
|
coCopilotTokenList.put(apiKey, token);
|
||||||
log.info("token过期,coCopilotToken重置化成功!");
|
log.info("token过期,coCopilotToken重置化成功!");
|
||||||
return againConversation(response, conversation, token, apiKey, model);
|
return againConversation(response, conversation, token, apiKey, model, requestTokens);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 流式和非流式输出
|
// 流式和非流式输出
|
||||||
return outPutChat(response, resp, conversation, model);
|
return outPutChat(response, resp, conversation, model, requestTokens, apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -672,28 +681,47 @@ public class ChatController {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Conversation conversation) throws IllegalArgumentException {
|
private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Conversation conversation) throws IllegalArgumentException {
|
||||||
checkConversation(conversation);
|
return getApiKeyAndRequestUrl(authorizationHeader, conversation);
|
||||||
return getApiKeyAndRequestUrl(authorizationHeader);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkConversation(Conversation conversation) {
|
private long checkConversation(Conversation conversation, String apiKey) {
|
||||||
if (conversation == null) {
|
if (conversation == null) {
|
||||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Request body is missing or not in JSON format");
|
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Request body is missing or not in JSON format");
|
||||||
}
|
}
|
||||||
long tokens = conversation.tokens();
|
long tokens = conversation.tokens();
|
||||||
if (tokens > 32 * 1024) {
|
if (tokens > 32 * 1024) {
|
||||||
log.error("本次请求tokens is too long and over 32K: " + tokens);
|
log.error("请求密钥:" + apiKey + ",本次请求tokens is too long and over 32K: " + tokens);
|
||||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Message is too long and over 32K");
|
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Message is too long and over 32K");
|
||||||
} else if (tokens <= 0) {
|
} else if (tokens <= 0) {
|
||||||
log.error("本次请求tokens is none: " + tokens);
|
log.error("请求密钥:" + apiKey + ",本次请求tokens is none: " + tokens);
|
||||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Message is none");
|
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Message is none");
|
||||||
} else {
|
|
||||||
log.info("本次请求tokens: " + tokens);
|
|
||||||
}
|
}
|
||||||
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private String[] getApiKeyAndRequestUrl(String authorizationHeader) {
|
private String[] getApiKeyAndRequestUrl(String authorizationHeader, Conversation conversation) {
|
||||||
|
String apiKey = null;
|
||||||
|
String requestUrl = null;
|
||||||
|
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||||
|
String keyAndUrl = authorizationHeader.substring(7);
|
||||||
|
if (!keyAndUrl.contains("|")) {
|
||||||
|
apiKey = keyAndUrl;
|
||||||
|
;
|
||||||
|
} else {
|
||||||
|
String[] parts = keyAndUrl.split("\\|");
|
||||||
|
requestUrl = parts[0];
|
||||||
|
apiKey = parts[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (apiKey == null) {
|
||||||
|
throw new IllegalArgumentException("Authorization ApiKey is missing");
|
||||||
|
}
|
||||||
|
return new String[]{requestUrl, apiKey};
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String[] getEmbApiKeyAndRequestUrl(String authorizationHeader) {
|
||||||
String apiKey = null;
|
String apiKey = null;
|
||||||
String requestUrl = null;
|
String requestUrl = null;
|
||||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||||
@@ -725,7 +753,7 @@ public class ChatController {
|
|||||||
if (conversation == null) {
|
if (conversation == null) {
|
||||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Request body is missing or not in JSON format");
|
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Request body is missing or not in JSON format");
|
||||||
}
|
}
|
||||||
return getApiKeyAndRequestUrl(authorizationHeader);
|
return getEmbApiKeyAndRequestUrl(authorizationHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -764,10 +792,12 @@ public class ChatController {
|
|||||||
} else {
|
} else {
|
||||||
int requestNum = selfTokenLimitList.get(apiKey).incrementAndGet();
|
int requestNum = selfTokenLimitList.get(apiKey).incrementAndGet();
|
||||||
if (requestNum > systemSetting.getOne_selfCopilot_limit()) {
|
if (requestNum > systemSetting.getOne_selfCopilot_limit()) {
|
||||||
log.info(apiKey + " requests is " + requestNum + " rate limit exceeded");
|
log.error("请求密钥:" + apiKey + " requests is " + requestNum + " rate limit exceeded");
|
||||||
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 检查请求结构体
|
||||||
|
long requestTokens = checkConversation(conversation, apiKey);
|
||||||
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
||||||
String chat_token = selfTokenList.get(apiKey);
|
String chat_token = selfTokenList.get(apiKey);
|
||||||
Map<String, String> headersMap = new HashMap<>();
|
Map<String, String> headersMap = new HashMap<>();
|
||||||
@@ -776,24 +806,26 @@ public class ChatController {
|
|||||||
String model = modelAdjust(conversation);
|
String model = modelAdjust(conversation);
|
||||||
Request streamRequest = getPrompt(conversation, model, headersMap);
|
Request streamRequest = getPrompt(conversation, model, headersMap);
|
||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
log.info("response code: " + resp.body());
|
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
if (resp.code() == 429) {
|
if (resp.code() == 429) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
} else if (resp.code() == 400) {
|
} else if (resp.code() == 400) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("messages is none or too long and over 32K"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
String token = getSelfToken(apiKey, requestUrl);
|
String token = getSelfToken(apiKey, requestUrl);
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
|
log.error("无效请求密钥:" + apiKey + ",请求token响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("自定义self APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("自定义self APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
selfTokenList.put(apiKey, token);
|
selfTokenList.put(apiKey, token);
|
||||||
log.info("token过期,自定义selfToken重置化成功!");
|
log.info("token过期,自定义selfToken重置化成功!");
|
||||||
return againConversation(response, conversation, token, apiKey, model);
|
return againConversation(response, conversation, token, apiKey, model, requestTokens);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 流式和非流式输出
|
// 流式和非流式输出
|
||||||
return outPutChat(response, resp, conversation, model);
|
return outPutChat(response, resp, conversation, model, requestTokens, apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -817,7 +849,8 @@ public class ChatController {
|
|||||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation,
|
@org.springframework.web.bind.annotation.RequestBody Conversation conversation,
|
||||||
String token,
|
String token,
|
||||||
String apiKey,
|
String apiKey,
|
||||||
String model) {
|
String model,
|
||||||
|
long requestTokens) {
|
||||||
try {
|
try {
|
||||||
Map<String, String> headersMap = new HashMap<>();
|
Map<String, String> headersMap = new HashMap<>();
|
||||||
//添加头部
|
//添加头部
|
||||||
@@ -825,14 +858,15 @@ public class ChatController {
|
|||||||
Request streamRequest = getPrompt(conversation, model, headersMap);
|
Request streamRequest = getPrompt(conversation, model, headersMap);
|
||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
|
log.error("无效密钥:" + apiKey + "第二次尝试失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("Please check your APIKey !"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("Please check your APIKey !"), HttpStatus.UNAUTHORIZED);
|
||||||
} else {
|
} else {
|
||||||
// 流式和非流式输出
|
// 流式和非流式输出
|
||||||
return outPutChat(response, resp, conversation, model);
|
return outPutChat(response, resp, conversation, model, requestTokens, apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("Exception " + e.getMessage());
|
log.error("HUm...A error occur:" + e.getMessage());
|
||||||
return new ResponseEntity<>(Result.error("HUm...A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("HUm...A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -842,7 +876,6 @@ public class ChatController {
|
|||||||
if (model.startsWith("gpt-4") && systemSetting.getGpt4_prompt()) {
|
if (model.startsWith("gpt-4") && systemSetting.getGpt4_prompt()) {
|
||||||
Message newMessage = getStringStringMap(model);
|
Message newMessage = getStringStringMap(model);
|
||||||
conversation.getMessages().add(0, newMessage);
|
conversation.getMessages().add(0, newMessage);
|
||||||
log.info(model + "模型,添加系统消息注入!");
|
|
||||||
}
|
}
|
||||||
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation);
|
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation);
|
||||||
RequestBody requestBody = RequestBody.create(json, JSON);
|
RequestBody requestBody = RequestBody.create(json, JSON);
|
||||||
@@ -905,7 +938,7 @@ public class ChatController {
|
|||||||
} else {
|
} else {
|
||||||
int requestNum = copilotTokenLimitList.get(apiKey).incrementAndGet();
|
int requestNum = copilotTokenLimitList.get(apiKey).incrementAndGet();
|
||||||
if (requestNum > systemSetting.getOne_copilot_limit()) {
|
if (requestNum > systemSetting.getOne_copilot_limit()) {
|
||||||
log.info(apiKey + " requests is " + requestNum + " rate limit exceeded");
|
log.error("请求密钥:" + apiKey + " requests is " + requestNum + " rate limit exceeded");
|
||||||
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -918,12 +951,15 @@ public class ChatController {
|
|||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
if (resp.code() == 429) {
|
if (resp.code() == 429) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
} else if (resp.code() == 400) {
|
} else if (resp.code() == 400) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("Model is not accessible"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("Model is not accessible"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
String token = getCopilotToken(apiKey);
|
String token = getCopilotToken(apiKey);
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
|
log.error("无效请求密钥:" + apiKey + ",请求token响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("Github Copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("Github Copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
copilotTokenList.put(apiKey, token);
|
copilotTokenList.put(apiKey, token);
|
||||||
@@ -995,11 +1031,11 @@ public class ChatController {
|
|||||||
} else {
|
} else {
|
||||||
int requestNum = coCopilotTokenLimitList.get(apiKey).incrementAndGet();
|
int requestNum = coCopilotTokenLimitList.get(apiKey).incrementAndGet();
|
||||||
if (requestNum > systemSetting.getOne_coCopilot_limit()) {
|
if (requestNum > systemSetting.getOne_coCopilot_limit()) {
|
||||||
log.info(apiKey + " requests is " + requestNum + " rate limit exceeded");
|
log.error("请求密钥:" + apiKey + " requests is " + requestNum + " rate limit exceeded");
|
||||||
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/embeddings
|
||||||
String chat_token = coCopilotTokenList.get(apiKey);
|
String chat_token = coCopilotTokenList.get(apiKey);
|
||||||
Map<String, String> headersMap = new HashMap<>();
|
Map<String, String> headersMap = new HashMap<>();
|
||||||
//添加头部
|
//添加头部
|
||||||
@@ -1008,12 +1044,15 @@ public class ChatController {
|
|||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
if (resp.code() == 429) {
|
if (resp.code() == 429) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
} else if (resp.code() == 400) {
|
} else if (resp.code() == 400) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("Model is not accessible"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("Model is not accessible"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
String token = getCoCoToken(apiKey);
|
String token = getCoCoToken(apiKey);
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
|
log.error("无效请求密钥:" + apiKey + ",请求token响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
coCopilotTokenList.put(apiKey, token);
|
coCopilotTokenList.put(apiKey, token);
|
||||||
@@ -1072,7 +1111,7 @@ public class ChatController {
|
|||||||
} else {
|
} else {
|
||||||
int requestNum = selfTokenLimitList.get(apiKey).incrementAndGet();
|
int requestNum = selfTokenLimitList.get(apiKey).incrementAndGet();
|
||||||
if (requestNum > systemSetting.getOne_selfCopilot_limit()) {
|
if (requestNum > systemSetting.getOne_selfCopilot_limit()) {
|
||||||
log.info(apiKey + " requests is " + requestNum + " rate limit exceeded");
|
log.error("请求密钥:" + apiKey + " requests is " + requestNum + " rate limit exceeded");
|
||||||
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("current requests is " + requestNum + " rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1084,12 +1123,15 @@ public class ChatController {
|
|||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
if (resp.code() == 429) {
|
if (resp.code() == 429) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
return new ResponseEntity<>(Result.error("rate limit exceeded"), HttpStatus.TOO_MANY_REQUESTS);
|
||||||
} else if (resp.code() == 400) {
|
} else if (resp.code() == 400) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("Model is not accessible"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("Model is not accessible"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
String token = getSelfToken(apiKey, requestUrl);
|
String token = getSelfToken(apiKey, requestUrl);
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
|
log.error("无效请求密钥:" + apiKey + ",请求token响应失败:" + resp);
|
||||||
return new ResponseEntity<>(Result.error("自定义 APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(Result.error("自定义 APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
selfTokenList.put(apiKey, token);
|
selfTokenList.put(apiKey, token);
|
||||||
@@ -1124,7 +1166,8 @@ public class ChatController {
|
|||||||
Request streamRequest = requestBuilder.build();
|
Request streamRequest = requestBuilder.build();
|
||||||
try (Response resp = client.newCall(streamRequest).execute()) {
|
try (Response resp = client.newCall(streamRequest).execute()) {
|
||||||
if (!resp.isSuccessful()) {
|
if (!resp.isSuccessful()) {
|
||||||
return new ResponseEntity<>(Result.error("copilot/cocopilot/自定义 APIKey is wrong Or your network is wrong"), HttpStatus.UNAUTHORIZED);
|
log.error("无效密钥:" + apiKey + "第二次尝试失败:" + resp);
|
||||||
|
return new ResponseEntity<>(Result.error("APIKey is wrong Or your network is wrong"), HttpStatus.UNAUTHORIZED);
|
||||||
} else {
|
} else {
|
||||||
// 非流式输出
|
// 非流式输出
|
||||||
outPutEmbeddings(response, resp);
|
outPutEmbeddings(response, resp);
|
||||||
@@ -1151,13 +1194,13 @@ public class ChatController {
|
|||||||
.addHeader("Editor-Plugin-Version", "copilot-chat/" + systemSetting.getCopilot_chat_version())
|
.addHeader("Editor-Plugin-Version", "copilot-chat/" + systemSetting.getCopilot_chat_version())
|
||||||
.addHeader("User-Agent", "GitHubCopilotChat/" + systemSetting.getCopilot_chat_version())
|
.addHeader("User-Agent", "GitHubCopilotChat/" + systemSetting.getCopilot_chat_version())
|
||||||
.addHeader("Accept", "*/*").build();
|
.addHeader("Accept", "*/*").build();
|
||||||
return getToken(request);
|
return getToken(request, apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getToken(Request request) throws IOException {
|
private String getToken(Request request, String apiKey) throws IOException {
|
||||||
try (Response response = client.newCall(request).execute()) {
|
try (Response response = client.newCall(request).execute()) {
|
||||||
log.info(response.toString());
|
|
||||||
if (!response.isSuccessful()) {
|
if (!response.isSuccessful()) {
|
||||||
|
log.error("请求密钥:" + apiKey + ",请求获取token出现问题:" + response);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String responseBody = response.body().string();
|
String responseBody = response.body().string();
|
||||||
@@ -1182,7 +1225,7 @@ public class ChatController {
|
|||||||
.addHeader("Editor-Plugin-Version", "copilot-chat/" + systemSetting.getCopilot_chat_version())
|
.addHeader("Editor-Plugin-Version", "copilot-chat/" + systemSetting.getCopilot_chat_version())
|
||||||
.addHeader("User-Agent", "GitHubCopilotChat/" + systemSetting.getCopilot_chat_version())
|
.addHeader("User-Agent", "GitHubCopilotChat/" + systemSetting.getCopilot_chat_version())
|
||||||
.addHeader("Accept", "*/*").build();
|
.addHeader("Accept", "*/*").build();
|
||||||
return getToken(request);
|
return getToken(request, apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1201,7 +1244,7 @@ public class ChatController {
|
|||||||
.addHeader("Editor-Plugin-Version", "copilot-chat/" + systemSetting.getCopilot_chat_version())
|
.addHeader("Editor-Plugin-Version", "copilot-chat/" + systemSetting.getCopilot_chat_version())
|
||||||
.addHeader("User-Agent", "GitHubCopilotChat/" + systemSetting.getCopilot_chat_version())
|
.addHeader("User-Agent", "GitHubCopilotChat/" + systemSetting.getCopilot_chat_version())
|
||||||
.addHeader("Accept", "*/*").build();
|
.addHeader("Accept", "*/*").build();
|
||||||
return getToken(request);
|
return getToken(request, apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1262,14 +1305,20 @@ public class ChatController {
|
|||||||
* @param resp
|
* @param resp
|
||||||
* @param conversation
|
* @param conversation
|
||||||
*/
|
*/
|
||||||
private ResponseEntity<Object> outPutChat(HttpServletResponse response, Response resp, Conversation conversation, String model) {
|
private ResponseEntity<Object> outPutChat(HttpServletResponse response,
|
||||||
|
Response resp,
|
||||||
|
Conversation conversation,
|
||||||
|
String model,
|
||||||
|
long requestTokens,
|
||||||
|
String apiKey) {
|
||||||
boolean isStream = conversation.isStream();
|
boolean isStream = conversation.isStream();
|
||||||
int sleep_time = calculateSleepTime(model, isStream);
|
int sleep_time = calculateSleepTime(model, isStream);
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
return outIsStreamPutChat(response, resp, model, sleep_time);
|
response.setContentType("text/event-stream; charset=UTF-8");
|
||||||
|
return outIsStreamPutChat(response, resp, model, sleep_time, requestTokens, apiKey);
|
||||||
} else {
|
} else {
|
||||||
response.setContentType("application/json; charset=utf-8");
|
response.setContentType("application/json; charset=utf-8");
|
||||||
return outNoStreamPutChat(response, resp, model);
|
return outNoStreamPutChat(response, resp, model, requestTokens, apiKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1281,7 +1330,11 @@ public class ChatController {
|
|||||||
* @param model
|
* @param model
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private ResponseEntity<Object> outNoStreamPutChat(HttpServletResponse response, Response resp, String model) {
|
private ResponseEntity<Object> outNoStreamPutChat(HttpServletResponse response,
|
||||||
|
Response resp,
|
||||||
|
String model,
|
||||||
|
long requestTokens,
|
||||||
|
String apiKey) {
|
||||||
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8), true);
|
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))) {
|
BufferedReader in = new BufferedReader(new InputStreamReader(resp.body().byteStream(), StandardCharsets.UTF_8))) {
|
||||||
|
|
||||||
@@ -1313,7 +1366,7 @@ public class ChatController {
|
|||||||
log.error("补全token为:" + tokens + ", A error occur......");
|
log.error("补全token为:" + tokens + ", A error occur......");
|
||||||
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
log.info("使用模型:" + model + ",补全tokens:" + tokens + ",vscode_version:" + systemSetting.getVscode_version() +
|
log.info("请求密钥:" + apiKey + ",是否流式:false,使用模型:" + model + ",请求tokens:" + requestTokens + ",补全tokens:" + tokens + ",vscode_version:" + systemSetting.getVscode_version() +
|
||||||
",copilot_chat_version:" + systemSetting.getCopilot_chat_version() + ",响应:" + resp);
|
",copilot_chat_version:" + systemSetting.getCopilot_chat_version() + ",响应:" + resp);
|
||||||
return null;
|
return null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -1330,13 +1383,18 @@ public class ChatController {
|
|||||||
* @param sleep_time
|
* @param sleep_time
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private ResponseEntity<Object> outIsStreamPutChat(HttpServletResponse response, Response resp, String model, int sleep_time) {
|
private ResponseEntity<Object> outIsStreamPutChat(HttpServletResponse response,
|
||||||
|
Response resp,
|
||||||
|
String model,
|
||||||
|
int sleep_time,
|
||||||
|
long requestTokens,
|
||||||
|
String apiKey) {
|
||||||
|
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);
|
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))) {
|
BufferedReader in = new BufferedReader(new InputStreamReader(resp.body().byteStream(), StandardCharsets.UTF_8))) {
|
||||||
String line;
|
String line;
|
||||||
long tokens = 0;
|
long tokens = 0;
|
||||||
response.setContentType("text/event-stream; charset=UTF-8");
|
if (in.toString().length() < 0) {
|
||||||
if(in.toString().length() < 0){
|
|
||||||
response.setContentType("application/json; charset=utf-8");
|
response.setContentType("application/json; charset=utf-8");
|
||||||
log.error("补全token为:" + tokens + ", A error occur......");
|
log.error("补全token为:" + tokens + ", A error occur......");
|
||||||
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
@@ -1353,7 +1411,7 @@ public class ChatController {
|
|||||||
if (choicesArray.size() > 0) {
|
if (choicesArray.size() > 0) {
|
||||||
JSONObject firstChoice = choicesArray.getJSONObject(0);
|
JSONObject firstChoice = choicesArray.getJSONObject(0);
|
||||||
String content = firstChoice.getJSONObject("delta").getString("content");
|
String content = firstChoice.getJSONObject("delta").getString("content");
|
||||||
tokens += TikTokensUtil.tokens("gpt-3.5-turbo", content);
|
tokens += TikTokensUtil.tokens(temModel, content);
|
||||||
}
|
}
|
||||||
if (sleep_time > 0) {
|
if (sleep_time > 0) {
|
||||||
Thread.sleep(sleep_time);
|
Thread.sleep(sleep_time);
|
||||||
@@ -1364,7 +1422,7 @@ public class ChatController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("使用模型:" + model + ",补全tokens:" + tokens + ",vscode_version:" + systemSetting.getVscode_version() +
|
log.info("请求密钥:" + apiKey + ",是否流式:true,使用模型:" + model + ",请求tokens:" + requestTokens + ",补全tokens:" + tokens + ",vscode_version:" + systemSetting.getVscode_version() +
|
||||||
",copilot_chat_version:" + systemSetting.getCopilot_chat_version()
|
",copilot_chat_version:" + systemSetting.getCopilot_chat_version()
|
||||||
+ ",字符间隔时间:" + sleep_time + "ms,响应:" + resp);
|
+ ",字符间隔时间:" + sleep_time + "ms,响应:" + resp);
|
||||||
return null;
|
return null;
|
||||||
@@ -1419,7 +1477,7 @@ public class ChatController {
|
|||||||
try {
|
try {
|
||||||
if (machineIdList.containsKey(apiKey)) {
|
if (machineIdList.containsKey(apiKey)) {
|
||||||
String machineId = machineIdList.get(apiKey);
|
String machineId = machineIdList.get(apiKey);
|
||||||
log.info("机械码读取成功!对应的机械码为:" + machineId);
|
// log.info("机械码读取成功!对应的机械码为:" + machineId);
|
||||||
return machineId;
|
return machineId;
|
||||||
}
|
}
|
||||||
String machineId = generateMachineId();
|
String machineId = generateMachineId();
|
||||||
|
@@ -25,7 +25,7 @@ public class CustomErrorController implements ErrorController {
|
|||||||
" <title>Document</title>\n" +
|
" <title>Document</title>\n" +
|
||||||
"</head>\n" +
|
"</head>\n" +
|
||||||
"<body>\n" +
|
"<body>\n" +
|
||||||
" <p>Thanks you use gpt4-copilot-java-0.2.3</p>\n" +
|
" <p>Thanks you use gpt4-copilot-java-0.2.4</p>\n" +
|
||||||
" <p><a href=\"https://apifox.com/apidoc/shared-4301e565-a8df-48a0-85a5-bda2c4c3965a\">详细使用文档</a></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" +
|
" <p><a href=\"https://github.com/Yanyutin753/unofficial-gpt4-api\">项目地址</a></p>\n" +
|
||||||
"</body>\n" +
|
"</body>\n" +
|
||||||
|
@@ -298,10 +298,11 @@ public class copilotApplication {
|
|||||||
System.out.println("one_selfCopilot_limit:" + ChatController.getSystemSetting().getOne_selfCopilot_limit());
|
System.out.println("one_selfCopilot_limit:" + ChatController.getSystemSetting().getOne_selfCopilot_limit());
|
||||||
System.out.println("gpt4-copilot-java 初始化接口成功!");
|
System.out.println("gpt4-copilot-java 初始化接口成功!");
|
||||||
System.out.println("======================================================");
|
System.out.println("======================================================");
|
||||||
System.out.println("******原神gpt4-copilot-java v0.2.3启动成功******");
|
System.out.println("******原神gpt4-copilot-java v0.2.4启动成功******");
|
||||||
System.out.println("* 由于本人略菜,graalvm依赖问题无法解决,之后代码将只通过jar和docker的形式运行");
|
System.out.println("* 由于本人略菜,graalvm依赖问题无法解决,之后代码将只通过jar和docker的形式运行");
|
||||||
System.out.println("* 修复部分bug,优化读取config.json代码,提升稳定性");
|
System.out.println("* 修复部分bug,优化读取config.json代码,提升稳定性");
|
||||||
System.out.println("* 新增token计算,优化报错,支持one_api重试机制");
|
System.out.println("* 新增token计算,优化报错,支持one_api重试机制");
|
||||||
|
System.out.println("* 优化部分报错日志,提高日志可读性");
|
||||||
System.out.println("URL地址:http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + "");
|
System.out.println("URL地址:http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + "");
|
||||||
System.out.println("======================================================");
|
System.out.println("======================================================");
|
||||||
}
|
}
|
||||||
|
@@ -30,6 +30,29 @@ public class Conversation implements Serializable {
|
|||||||
private static final Map<String, Encoding> modelMap = new HashMap();
|
private static final Map<String, Encoding> modelMap = new HashMap();
|
||||||
private static final EncodingRegistry registry = Encodings.newDefaultEncodingRegistry();
|
private static final EncodingRegistry registry = Encodings.newDefaultEncodingRegistry();
|
||||||
|
|
||||||
|
static {
|
||||||
|
ModelType[] var0 = ModelType.values();
|
||||||
|
int var1 = var0.length;
|
||||||
|
|
||||||
|
for (int var2 = 0; var2 < var1; ++var2) {
|
||||||
|
ModelType modelType = var0[var2];
|
||||||
|
modelMap.put(modelType.getName(), registry.getEncodingForModel(modelType));
|
||||||
|
}
|
||||||
|
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_0301.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_0613.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_16K.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_16K_0613.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_1106.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_32K.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_32K_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_0613.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_32K_0613.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_1106_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
modelMap.put(BaseChatCompletion.Model.GPT_4_VISION_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否流式输出.
|
* 是否流式输出.
|
||||||
* default:false
|
* default:false
|
||||||
@@ -38,25 +61,11 @@ public class Conversation implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
private boolean stream = false;
|
private boolean stream = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 问题描述
|
* 问题描述
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
private List<Message> messages;
|
private List<Message> messages;
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前参数的tokens数
|
|
||||||
*/
|
|
||||||
public long tokens() {
|
|
||||||
if (CollectionUtil.isEmpty(this.messages) || StrUtil.isBlank(this.getModel())) {
|
|
||||||
log.warn("参数异常model:{},prompt:{}", this.getModel(), this.messages);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
String temModel = this.getModel() == null || !model.startsWith("gpt-4") ? "gpt-3.5-turbo" :"gpt-4-0613";
|
|
||||||
return tokens(temModel, this.messages);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
private String model = "gpt-3.5-turbo";
|
private String model = "gpt-3.5-turbo";
|
||||||
|
|
||||||
@@ -100,8 +109,8 @@ public class Conversation implements Serializable {
|
|||||||
int sum = 0;
|
int sum = 0;
|
||||||
Iterator var6 = messages.iterator();
|
Iterator var6 = messages.iterator();
|
||||||
|
|
||||||
while(var6.hasNext()) {
|
while (var6.hasNext()) {
|
||||||
Message msg = (Message)var6.next();
|
Message msg = (Message) var6.next();
|
||||||
sum += tokensPerMessage;
|
sum += tokensPerMessage;
|
||||||
sum += tokens(encoding, msg.getContent());
|
sum += tokens(encoding, msg.getContent());
|
||||||
sum += tokens(encoding, msg.getRole());
|
sum += tokens(encoding, msg.getRole());
|
||||||
@@ -115,27 +124,15 @@ public class Conversation implements Serializable {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
static {
|
* 获取当前参数的tokens数
|
||||||
ModelType[] var0 = ModelType.values();
|
*/
|
||||||
int var1 = var0.length;
|
public long tokens() {
|
||||||
|
if (CollectionUtil.isEmpty(this.messages) || StrUtil.isBlank(this.getModel())) {
|
||||||
for(int var2 = 0; var2 < var1; ++var2) {
|
log.warn("参数异常model:{},prompt:{}", this.getModel(), this.messages);
|
||||||
ModelType modelType = var0[var2];
|
return 0;
|
||||||
modelMap.put(modelType.getName(), registry.getEncodingForModel(modelType));
|
|
||||||
}
|
}
|
||||||
|
String temModel = this.getModel() == null || !model.startsWith("gpt-4") ? "gpt-3.5-turbo-0613" : "gpt-4-0613";
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_0301.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
return tokens(temModel, this.messages);
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_0613.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_16K.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_16K_0613.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_3_5_TURBO_1106.getName(), registry.getEncodingForModel(ModelType.GPT_3_5_TURBO));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_32K.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_32K_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_0613.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_32K_0613.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_1106_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
modelMap.put(BaseChatCompletion.Model.GPT_4_VISION_PREVIEW.getName(), registry.getEncodingForModel(ModelType.GPT_4));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
target/gpt-4-copilot-0.2.4.jar.original
Normal file
BIN
target/gpt-4-copilot-0.2.4.jar.original
Normal file
Binary file not shown.
@@ -1,3 +1,3 @@
|
|||||||
artifactId=gpt-4-copilot
|
artifactId=gpt-4-copilot
|
||||||
groupId=com.gpt4.copilot
|
groupId=com.gpt4.copilot
|
||||||
version=0.2.3
|
version=0.2.4
|
||||||
|
Reference in New Issue
Block a user