Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
PandoraNext-tokensTool v 0.6.5版本
Browse files Browse the repository at this point in the history
 PandoraNext-tokensTool v 0.6.5版本
1.优化copilot接口的流式输出
2.接口使用多线程,提高并发能力
3.修复历史问题bug,优化代码,优化前端
  • Loading branch information
Yanyutin753 committed Jan 11, 2024
1 parent b5fb7c5 commit d2fed51
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

/**
* @author Yangyang
Expand Down Expand Up @@ -72,6 +69,18 @@ public class chatController {

@Value("${copilot_interface}")
private boolean copilot_interface;
/**
* 请求体不是json 会报Request body is missing or not in JSON format
* Authorization token缺失 会报Authorization header is missing
* 无法请求到chat_token 会报copilot APIKey is wrong
*
* @param response
* @param request
* @param conversation
* @return
* @throws JSONException
* @throws IOException
*/
private ExecutorService executor = new ThreadPoolExecutor(0, 1000,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
Expand Down Expand Up @@ -105,75 +114,67 @@ private void clearModelsUsage() {
log.info("重置modelsUsage成功!");
}

/**
* 请求体不是json 会报Request body is missing or not in JSON format
* Authorization token缺失 会报Authorization header is missing
* 无法请求到chat_token 会报copilot APIKey is wrong
*
* @param response
* @param request
* @param conversation
* @return
* @throws JSONException
* @throws IOException
*/
@PostMapping(value = "/v1/chat/completions")
public Object coPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
try {
if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
}
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
String apiKey;
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
apiKey = authorizationHeader.substring(7);
} else {
return new ResponseEntity<>("Authorization header is missing", HttpStatus.UNAUTHORIZED);
}
if (!copilotTokenList.containsKey(apiKey)) {
String token = getCopilotToken(apiKey);
if (token == null) {
return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED);
}
copilotTokenList.put(apiKey, token);
log.info("coCopilotTokenList初始化成功!");
}
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
String chat_token = copilotTokenList.get(apiKey);
OkHttpClient client = productClient(5);
Map<String, String> headersMap = new HashMap<>();
//添加头部
addHeader(headersMap, chat_token);
String json = JSON.toJSONString(conversation);
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder()
.url("https://api.githubcopilot.com/chat/completions")
.post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) {
public Object coPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) throws ExecutionException, InterruptedException {
if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
}
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
String apiKey;
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
apiKey = authorizationHeader.substring(7);
} else {
return new ResponseEntity<>("Authorization header is missing", HttpStatus.UNAUTHORIZED);
}
Future<Object> future = executor.submit(() -> {
try {
if (!copilotTokenList.containsKey(apiKey)) {
String token = getCopilotToken(apiKey);
if (token == null) {
return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED);
}
copilotTokenList.put(apiKey, token);
log.info("coCopilotTokenList重置化成功!");
coPilotConversation(response, request, conversation);
return null;
log.info("coCopilotTokenList初始化成功!");
}
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
String chat_token = copilotTokenList.get(apiKey);
OkHttpClient client = productClient(5);
Map<String, String> headersMap = new HashMap<>();
//添加头部
addHeader(headersMap, chat_token);
String json = JSON.toJSONString(conversation);
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder()
.url("https://api.githubcopilot.com/chat/completions")
.post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) {
String token = getCopilotToken(apiKey);
if (token == null) {
return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED);
}
copilotTokenList.put(apiKey, token);
log.info("coCopilotTokenList重置化成功!");
coPilotConversation(response, request, conversation);
return null;
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return future.get();
}


/**
* 请求体不是json 会报Request body is missing or not in JSON format
* Authorization token缺失 会报Authorization header is missing
Expand All @@ -187,62 +188,66 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
* @throws IOException
*/
@PostMapping(value = "/cocopilot/v1/chat/completions")
public Object coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
try {
if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
}
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
String apiKey;
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
apiKey = authorizationHeader.substring(7);
} else {
return new ResponseEntity<>("Authorization header is missing", HttpStatus.UNAUTHORIZED);
}
if (!coCopilotTokenList.containsKey(apiKey)) {
String token = getCoCoToken(apiKey);
if (token == null) {
return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED);
}
coCopilotTokenList.put(apiKey, token);
log.info("coCopilotTokenList初始化成功!");
}
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
String chat_token = coCopilotTokenList.get(apiKey);
OkHttpClient client = productClient(5);
Map<String, String> headersMap = new HashMap<>();
//添加头部
addHeader(headersMap, chat_token);
String json = JSON.toJSONString(conversation);
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder()
.url("https://api.githubcopilot.com/chat/completions")
.post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) {
public Object coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) throws ExecutionException, InterruptedException {
if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
}
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
String apiKey;
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
apiKey = authorizationHeader.substring(7);
} else {
return new ResponseEntity<>("Authorization header is missing", HttpStatus.UNAUTHORIZED);
}
Future<Object> future = executor.submit(() -> {
try {
if (!coCopilotTokenList.containsKey(apiKey)) {
String token = getCoCoToken(apiKey);
if (token == null) {
return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED);
}
coCopilotTokenList.put(apiKey, token);
log.info("coCopilotTokenList重置化成功!");
coCoPilotConversation(response, request, conversation);
return null;
log.info("coCopilotTokenList初始化成功!");
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
String chat_token = coCopilotTokenList.get(apiKey);
OkHttpClient client = productClient(5);
Map<String, String> headersMap = new HashMap<>();
//添加头部
addHeader(headersMap, chat_token);
String json = JSON.toJSONString(conversation);
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder()
.url("https://api.githubcopilot.com/chat/completions")
.post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) {
String token = getCoCoToken(apiKey);
if (token == null) {
return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED);
}
coCopilotTokenList.put(apiKey, token);
log.info("coCopilotTokenList重置化成功!");
coCoPilotConversation(response, request, conversation);
return null;
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return future.get();
}


private void addModel(Conversation conversation) {
String model = conversation.getModel();
if (modelsUsage.containsKey(model)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
@Slf4j
@Service
public class systemServiceImpl implements systemService {
private final String deploy = "default";
@Value("${deployPosition}")
private String deployPosition;
private final String deploy = "default";


public String selectFile() {
String projectRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static void main(String[] args) {
log.info("--------------------------------------------------------------\n" +
"PandoraNext-tokensTool v 0.6.5版本\n" +
"1.优化copilot接口的流式输出\n" +
"2.修复历史问题bug,优化代码,优化前端\n" +
"2.接口使用多线程,提高并发能力\n" +
"3.修复历史问题bug,优化代码,优化前端\n" +
"--------------------------------------------------------------\n");
Instant instant = Instant.now();
String key = String.valueOf(instant.toEpochMilli());
Expand Down
16 changes: 15 additions & 1 deletion rearServer/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Welcome to PandoraNext-TokensTool</title><script defer="defer" src="js/chunk-vendors.612ab552.js"></script><script defer="defer" src="js/app.9160034c.js"></script><link href="css/chunk-vendors.8a308144.css" rel="stylesheet"></head><body><div id="app"></div></body></html>
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width,initial-scale=1" name="viewport">
<title>Welcome to PandoraNext-TokensTool</title>
<script defer="defer" src="js/chunk-vendors.612ab552.js"></script>
<script defer="defer" src="js/app.9160034c.js"></script>
<link href="css/chunk-vendors.8a308144.css" rel="stylesheet">
</head>
<body>
<div id="app"></div>
</body>
</html>
Binary file not shown.
Binary file not shown.
16 changes: 15 additions & 1 deletion rearServer/target/classes/static/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Welcome to PandoraNext-TokensTool</title><script defer="defer" src="js/chunk-vendors.612ab552.js"></script><script defer="defer" src="js/app.9160034c.js"></script><link href="css/chunk-vendors.8a308144.css" rel="stylesheet"></head><body><div id="app"></div></body></html>
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width,initial-scale=1" name="viewport">
<title>Welcome to PandoraNext-TokensTool</title>
<script defer="defer" src="js/chunk-vendors.612ab552.js"></script>
<script defer="defer" src="js/app.9160034c.js"></script>
<link href="css/chunk-vendors.8a308144.css" rel="stylesheet">
</head>
<body>
<div id="app"></div>
</body>
</html>
Binary file modified rearServer/target/pandoraNext-0.6.5-SNAPSHOT.jar
Binary file not shown.
Binary file modified simplyDeploy/pandoraNext-0.6.5-SNAPSHOT.jar
Binary file not shown.

0 comments on commit d2fed51

Please sign in to comment.