修改相应的BUG

This commit is contained in:
Yanyutin753
2024-04-23 01:58:20 +08:00
parent b51766b725
commit 778f53c845
4 changed files with 30 additions and 44 deletions

View File

@@ -510,18 +510,13 @@ public class ChatController {
} else if (resp.code() == 400) { } else if (resp.code() == 400) {
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 {
if (resp.code() == 403) { String token = getCopilotToken(apiKey);
String token = getCopilotToken(apiKey); if (token == null) {
if (token == null) { 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);
log.info("token过期Github CopilotToken重置化成功");
return againConversation(response, conversation, token, apiKey, model);
} else {
log.info("接口返回" + resp.code() + ",已500返回报错提醒...............");
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
} }
copilotTokenList.put(apiKey, token);
log.info("token过期Github CopilotToken重置化成功");
return againConversation(response, conversation, token, apiKey, model);
} }
} else { } else {
// 流式和非流式输出 // 流式和非流式输出
@@ -625,18 +620,13 @@ public class ChatController {
} else if (resp.code() == 400) { } else if (resp.code() == 400) {
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 {
if (resp.code() == 403) { String token = getCoCoToken(apiKey);
String token = getCoCoToken(apiKey); if (token == null) {
if (token == null) { 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);
log.info("token过期coCopilotToken重置化成功");
return againConversation(response, conversation, token, apiKey, model);
} else {
log.info("接口返回" + resp.code() + "已500返回报错提醒...............");
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
} }
coCopilotTokenList.put(apiKey, token);
log.info("token过期coCopilotToken重置化成功");
return againConversation(response, conversation, token, apiKey, model);
} }
} else { } else {
// 流式和非流式输出 // 流式和非流式输出
@@ -793,18 +783,13 @@ public class ChatController {
} else if (resp.code() == 400) { } else if (resp.code() == 400) {
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 {
if (resp.code() == 403) { String token = getSelfToken(apiKey, requestUrl);
String token = getSelfToken(apiKey, requestUrl); if (token == null) {
if (token == null) { 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);
log.info("token过期自定义selfToken重置化成功");
return againConversation(response, conversation, token, apiKey, model);
} else {
log.info("接口返回" + resp.code() + "已500返回报错提醒...............");
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
} }
selfTokenList.put(apiKey, token);
log.info("token过期自定义selfToken重置化成功");
return againConversation(response, conversation, token, apiKey, model);
} }
} else { } else {
// 流式和非流式输出 // 流式和非流式输出
@@ -840,7 +825,7 @@ 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()) {
return new ResponseEntity<>(Result.error("APIKey is wrong!"), 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);
@@ -849,7 +834,6 @@ public class ChatController {
} catch (Exception e) { } catch (Exception e) {
log.info("Exception " + e.getMessage()); log.info("Exception " + 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);
} }
} }
@@ -1282,7 +1266,6 @@ public class ChatController {
boolean isStream = conversation.isStream(); boolean isStream = conversation.isStream();
int sleep_time = calculateSleepTime(model, isStream); int sleep_time = calculateSleepTime(model, isStream);
if (isStream) { if (isStream) {
response.setContentType("text/event-stream; charset=UTF-8");
return outIsStreamPutChat(response, resp, model, sleep_time); return outIsStreamPutChat(response, resp, model, sleep_time);
} else { } else {
response.setContentType("application/json; charset=utf-8"); response.setContentType("application/json; charset=utf-8");
@@ -1350,13 +1333,18 @@ public class ChatController {
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) {
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){
response.setContentType("application/json; charset=utf-8");
log.error("补全token为:" + tokens + ", A error occur......");
return new ResponseEntity<>(Result.error("HUm... A error occur......"), HttpStatus.INTERNAL_SERVER_ERROR);
}
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
out.println(line); out.println(line);
out.flush(); out.flush();
if (sleep_time > 0 && line.startsWith("data:")) { if (line.startsWith("data:")) {
try { try {
String res_text = line.replace("data: ", ""); String res_text = line.replace("data: ", "");
if (!"[DONE]".equals(res_text.trim())) { if (!"[DONE]".equals(res_text.trim())) {
@@ -1365,19 +1353,17 @@ 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-4-0613", content); tokens += TikTokensUtil.tokens("gpt-3.5-turbo", content);
}
if (sleep_time > 0) {
Thread.sleep(sleep_time);
} }
Thread.sleep(sleep_time);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
if (tokens <= 0) {
log.error("补全token为:" + tokens + ", A error occur......");
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("使用模型:" + model + "补全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);

Binary file not shown.