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.7版本
Browse files Browse the repository at this point in the history
1.优化添加进one-api的代码,使得id不一直增加
2.优化copilot,cocopilot的chat接口
3.修复历史问题bug,优化代码,优化前端
  • Loading branch information
Yanyutin753 committed Jan 16, 2024
1 parent 8f0d1fb commit 3f95a3e
Show file tree
Hide file tree
Showing 60 changed files with 459 additions and 329 deletions.
8 changes: 0 additions & 8 deletions rearServer/rearServer.iml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public Result toUpdateAllToken() {
String res = apiService.autoUpdateToken("");
if (res.contains("生成Token成功")) {
try {
String s = poolService.refreshAllTokens();
String s1 = shareService.refreshAllToken();
String s = poolService.refreshAllPoolTokens();
String s1 = shareService.refreshAllShareTokens();
return Result.success(res + s + s1);
} catch (Exception e) {
return Result.success(res + "<br>但是自动更新pool_token和oneApi里的share_token失败");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tokensTool.pandoraNext.chat.Conversation;
import com.tokensTool.pandoraNext.pojo.Result;
Expand Down Expand Up @@ -37,6 +36,11 @@
@Slf4j
@RestController()
public class chatController {
private final OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.build();

/**
* 缓存cocopilotToken
Expand Down Expand Up @@ -117,7 +121,6 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
try {
Future<Object> future = executor.submit(() -> {

if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
}
Expand All @@ -138,7 +141,6 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
}
// 创建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);
Expand All @@ -161,17 +163,16 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
log.info("coCopilotTokenList重置化成功!");
againConversation(response, conversation, token);
return null;
} else {
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
return null;
});
return future.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand All @@ -181,31 +182,35 @@ public Object againConversation(HttpServletResponse response,
@org.springframework.web.bind.annotation.RequestBody Conversation conversation,
String token) {
try {
OkHttpClient client = productClient(5);
Map<String, String> headersMap = new HashMap<>();
//添加头部
addHeader(headersMap, 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()) {
return new ResponseEntity<>("copilot/cocopilot APIKey is wrong Or your network is wrong", HttpStatus.UNAUTHORIZED);
Future<Object> future = executor.submit(() -> {
Map<String, String> headersMap = new HashMap<>();
//添加头部
addHeader(headersMap, 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()) {
return new ResponseEntity<>("copilot/cocopilot APIKey is wrong Or your network is wrong", HttpStatus.UNAUTHORIZED);
} else {
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
return null;
} catch (IOException e) {
return null;
});
return future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}

}

/**
Expand All @@ -225,7 +230,6 @@ public Object againConversation(HttpServletResponse response,
public Object coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) throws ExecutionException, InterruptedException {
try {
Future<Object> future = executor.submit(() -> {

if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
}
Expand All @@ -246,7 +250,6 @@ public Object coCoPilotConversation(HttpServletResponse response, HttpServletReq
}
// 创建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);
Expand All @@ -269,10 +272,11 @@ public Object coCoPilotConversation(HttpServletResponse response, HttpServletReq
log.info("coCopilotTokenList重置化成功!");
againConversation(response, conversation, token);
return null;
} else {
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
// 流式和非流式输出
outPutChat(response, resp, conversation);
addModel(conversation);
}
return null;

Expand Down Expand Up @@ -322,7 +326,6 @@ public Object coPilotEmbeddings(HttpServletResponse response, HttpServletRequest
}
// 创建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);
Expand All @@ -345,19 +348,19 @@ public Object coPilotEmbeddings(HttpServletResponse response, HttpServletRequest
log.info("coCopilotTokenList重置化成功!");
coPilotEmbeddings(response, request, conversation);
return null;
}
// 非流式输出
outPutEmbeddings(response, resp);
com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(json);
String model = jsonObject.getString("model");
if (modelsUsage.containsKey(model)) {
modelsUsage.put(model, modelsUsage.get(model) + 1);
} else {
modelsUsage.put(model, 1);
// 非流式输出
outPutEmbeddings(response, resp);
com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(json);
String model = jsonObject.getString("model");
if (modelsUsage.containsKey(model)) {
modelsUsage.put(model, modelsUsage.get(model) + 1);
} else {
modelsUsage.put(model, 1);
}
}
}
return null;

});
return future.get();
} catch (InterruptedException e) {
Expand Down Expand Up @@ -392,7 +395,6 @@ public Object coCoPilotEmbeddings(HttpServletResponse response, HttpServletReque
}
// 创建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);
Expand All @@ -414,15 +416,16 @@ public Object coCoPilotEmbeddings(HttpServletResponse response, HttpServletReque
log.info("coCopilotTokenList重置化成功!");
coCoPilotEmbeddings(response, request, conversation);
return null;
}
// 非流式输出
outPutEmbeddings(response, resp);
com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(json);
String model = jsonObject.getString("model");
if (modelsUsage.containsKey(model)) {
modelsUsage.put(model, modelsUsage.get(model) + 1);
} else {
modelsUsage.put(model, 1);
// 非流式输出
outPutEmbeddings(response, resp);
com.alibaba.fastjson2.JSONObject jsonObject = com.alibaba.fastjson2.JSON.parseObject(json);
String model = jsonObject.getString("model");
if (modelsUsage.containsKey(model)) {
modelsUsage.put(model, modelsUsage.get(model) + 1);
} else {
modelsUsage.put(model, 1);
}
}
}
return null;
Expand Down Expand Up @@ -473,7 +476,6 @@ private Result getModelUsage() {
* @throws IOException
*/
private String getCopilotToken(String apiKey) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.github.com/copilot_internal/v2/token")
.addHeader("Host", "api.github.com")
Expand Down Expand Up @@ -503,7 +505,6 @@ private String getCopilotToken(String apiKey) throws IOException {
* @throws IOException
*/
private String getCoCoToken(String apiKey) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.cocopilot.org/copilot_internal/v2/token")
.addHeader("Host", "api.cocopilot.org")
Expand Down Expand Up @@ -533,9 +534,16 @@ private String getCoCoToken(String apiKey) throws IOException {
* @throws JsonProcessingException
*/
@GetMapping("/v1/models")
public JsonNode models() throws JsonProcessingException {
String jsonString = models;
return new ObjectMapper().readTree(jsonString);
public Object models() throws JsonProcessingException {
try {
Future<Object> future = executor.submit(() -> {
String jsonString = models;
return new ObjectMapper().readTree(jsonString);
});
return future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
Expand All @@ -545,9 +553,16 @@ public JsonNode models() throws JsonProcessingException {
* @throws JsonProcessingException
*/
@GetMapping("/cocopilot/v1/models")
public JsonNode cocoPilotModels() throws JsonProcessingException {
String jsonString = models;
return new ObjectMapper().readTree(jsonString);
public Object cocoPilotModels() {
try {
Future<Object> future = executor.submit(() -> {
String jsonString = models;
return new ObjectMapper().readTree(jsonString);
});
return future.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public Result selectPoolToken(@RequestParam("name") String name) {
@GetMapping("updateAllPoolToken")
public Result updateAllPoolToken() {
try {
String res = poolService.refreshAllTokens();
String res = poolService.refreshAllPoolTokens();
return Result.success(res);
} catch (Exception e) {
e.printStackTrace();
Expand Down
Loading

0 comments on commit 3f95a3e

Please sign in to comment.