diff --git a/rearServer/src/main/java/com/tokensTool/pandoraNext/config/WebConfig.java b/rearServer/src/main/java/com/tokensTool/pandoraNext/config/WebConfig.java index 81103d8..18cb551 100644 --- a/rearServer/src/main/java/com/tokensTool/pandoraNext/config/WebConfig.java +++ b/rearServer/src/main/java/com/tokensTool/pandoraNext/config/WebConfig.java @@ -4,15 +4,23 @@ import com.tokensTool.pandoraNext.interceptor.LoginCheckInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.util.concurrent.TimeUnit; + @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private LoginCheckInterceptor loginCheckInterceptor; + @Override + public void configureAsyncSupport(AsyncSupportConfigurer configurer) { + // 设置默认异步请求超时时间,例如设置为6分钟 + configurer.setDefaultTimeout(TimeUnit.MINUTES.toMillis(6)); + } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/api/**"); diff --git a/rearServer/src/main/java/com/tokensTool/pandoraNext/controller/chatController.java b/rearServer/src/main/java/com/tokensTool/pandoraNext/controller/chatController.java index 1ea1f22..e6e07f1 100644 --- a/rearServer/src/main/java/com/tokensTool/pandoraNext/controller/chatController.java +++ b/rearServer/src/main/java/com/tokensTool/pandoraNext/controller/chatController.java @@ -67,13 +67,12 @@ public class chatController { .readTimeout(5, TimeUnit.MINUTES) .writeTimeout(5, TimeUnit.MINUTES) .build(); + private final ExecutorService executor = new ThreadPoolExecutor(0, 300, + 60L, TimeUnit.SECONDS, + new SynchronousQueue<>()); @Value("${copilot_interface}") private boolean copilot_interface; - private ExecutorService executor = new ThreadPoolExecutor(0, 100, - 60L, TimeUnit.SECONDS, - new SynchronousQueue()); - private static String generateMachineId() { try { UUID uuid = UUID.randomUUID(); @@ -116,66 +115,75 @@ private void clearModelsUsage() { * @throws IOException */ @PostMapping(value = "/v1/chat/completions") - public CompletableFuture> coPilotConversation(HttpServletResponse response, HttpServletRequest request, - @org.springframework.web.bind.annotation.RequestBody Conversation conversation) { + public CompletableFuture> coPilotConversation(HttpServletResponse response, HttpServletRequest request, + @org.springframework.web.bind.annotation.RequestBody Conversation conversation) { return CompletableFuture.supplyAsync(() -> { - 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); - Map headersMap = new HashMap<>(); - //添加头部 - addHeader(headersMap, chat_token); - String json = com.alibaba.fastjson2.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()) { - if (resp.code() == 429) { - return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + 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("token过期,coCopilotTokenList重置化成功!"); - againConversation(response, conversation, token); + log.info("coCopilotTokenList初始化成功!"); } + // 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions + String chat_token = copilotTokenList.get(apiKey); + Map headersMap = new HashMap<>(); + //添加头部 + addHeader(headersMap, chat_token); + String json = com.alibaba.fastjson2.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()) { + if (resp.code() == 429) { + return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + } else { + String token = getCopilotToken(apiKey); + if (token == null) { + return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED); + } + copilotTokenList.put(apiKey, token); + log.info("token过期,coCopilotTokenList重置化成功!"); + againConversation(response, conversation, token); + } + } else { + // 流式和非流式输出 + outPutChat(response, resp, conversation); + addModel(conversation); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + return null; + }, executor) + .orTimeout(6, TimeUnit.MINUTES) + .exceptionally(ex -> { + // 处理超时或其他异常 + if (ex instanceof TimeoutException) { + return new ResponseEntity<>("Request timed out", HttpStatus.REQUEST_TIMEOUT); } else { - // 流式和非流式输出 - outPutChat(response, resp, conversation); - addModel(conversation); + return new ResponseEntity<>("An error occurred", HttpStatus.INTERNAL_SERVER_ERROR); } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return null; - }, executor); + }); } @@ -193,65 +201,74 @@ public CompletableFuture> coPilotConversation(HttpServletRespo */ @PostMapping(value = "/cocopilot/v1/chat/completions") - public CompletableFuture> coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) throws ExecutionException, InterruptedException { + public CompletableFuture> coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) throws ExecutionException, InterruptedException { return CompletableFuture.supplyAsync(() -> { - 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<>("cocopilot 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); - Map headersMap = new HashMap<>(); - //添加头部 - addHeader(headersMap, chat_token); - String json = com.alibaba.fastjson2.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()) { - if (resp.code() == 429) { - return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + 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<>("cocopilot APIKey is wrong", HttpStatus.UNAUTHORIZED); } coCopilotTokenList.put(apiKey, token); - log.info("token过期,coCopilotTokenList重置化成功!"); - againConversation(response, conversation, token); + log.info("coCopilotTokenList初始化成功!"); + } + // 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions + String chat_token = coCopilotTokenList.get(apiKey); + Map headersMap = new HashMap<>(); + //添加头部 + addHeader(headersMap, chat_token); + String json = com.alibaba.fastjson2.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()) { + if (resp.code() == 429) { + return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + } else { + String token = getCoCoToken(apiKey); + if (token == null) { + return new ResponseEntity<>("cocopilot APIKey is wrong", HttpStatus.UNAUTHORIZED); + } + coCopilotTokenList.put(apiKey, token); + log.info("token过期,coCopilotTokenList重置化成功!"); + againConversation(response, conversation, token); + } + } else { + // 流式和非流式输出 + outPutChat(response, resp, conversation); + addModel(conversation); + } } + } catch (IOException e) { + throw new RuntimeException(e); + } + return null; + }, executor) + .orTimeout(6, TimeUnit.MINUTES) + .exceptionally(ex -> { + // 处理超时或其他异常 + if (ex instanceof TimeoutException) { + return new ResponseEntity<>("Request timed out", HttpStatus.REQUEST_TIMEOUT); } else { - // 流式和非流式输出 - outPutChat(response, resp, conversation); - addModel(conversation); + return new ResponseEntity<>("An error occurred", HttpStatus.INTERNAL_SERVER_ERROR); } - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return null; - }, executor); + }); } @@ -310,141 +327,194 @@ public Object againConversation(HttpServletResponse response, @PostMapping(value = "/v1/embeddings") public Object coPilotEmbeddings(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) { return CompletableFuture.supplyAsync(() -> { - 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); - Map headersMap = new HashMap<>(); - //添加头部 - addHeader(headersMap, chat_token); - String json = com.alibaba.fastjson2.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/embeddings") - .post(requestBody); - headersMap.forEach(requestBuilder::addHeader); - Request streamRequest = requestBuilder.build(); - try (Response resp = client.newCall(streamRequest).execute()) { - if (!resp.isSuccessful()) { - if (resp.code() == 429) { - return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + 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("token过期,coCopilotTokenList重置化成功!"); - coPilotEmbeddings(response, request, conversation); + log.info("coCopilotTokenList初始化成功!"); } - } else { - // 非流式输出 - 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); + // 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions + String chat_token = copilotTokenList.get(apiKey); + Map headersMap = new HashMap<>(); + //添加头部 + addHeader(headersMap, chat_token); + String json = com.alibaba.fastjson2.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/embeddings") + .post(requestBody); + headersMap.forEach(requestBuilder::addHeader); + Request streamRequest = requestBuilder.build(); + try (Response resp = client.newCall(streamRequest).execute()) { + if (!resp.isSuccessful()) { + if (resp.code() == 429) { + return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + } else { + String token = getCopilotToken(apiKey); + if (token == null) { + return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED); + } + copilotTokenList.put(apiKey, token); + log.info("token过期,coCopilotTokenList重置化成功!"); + againEmbeddings(response, conversation, token); + } + } else { + // 非流式输出 + 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; + } catch (IOException e) { + throw new RuntimeException(e); } - } - return null; - } catch (IOException e) { - throw new RuntimeException(e); - } - }, executor); + }, executor) + .orTimeout(6, TimeUnit.MINUTES) + .exceptionally(ex -> { + // 处理超时或其他异常 + if (ex instanceof TimeoutException) { + return new ResponseEntity<>("Request timed out", HttpStatus.REQUEST_TIMEOUT); + } else { + return new ResponseEntity<>("An error occurred", HttpStatus.INTERNAL_SERVER_ERROR); + } + }); } @PostMapping(value = "/cocopilot/v1/embeddings") public Object coCoPilotEmbeddings(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) { return CompletableFuture.supplyAsync(() -> { - 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); - Map headersMap = new HashMap<>(); - //添加头部 - addHeader(headersMap, chat_token); - String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); - 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/embeddings") - .post(requestBody); - headersMap.forEach(requestBuilder::addHeader); - Request streamRequest = requestBuilder.build(); - try (Response resp = client.newCall(streamRequest).execute()) { - if (!resp.isSuccessful()) { - if (resp.code() == 429) { - return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + 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("token过期,coCopilotTokenList重置化成功!"); - coCoPilotEmbeddings(response, request, conversation); + log.info("coCopilotTokenList初始化成功!"); } - } else { - // 非流式输出 - 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); + // 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions + String chat_token = coCopilotTokenList.get(apiKey); + Map headersMap = new HashMap<>(); + //添加头部 + addHeader(headersMap, chat_token); + String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); + 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/embeddings") + .post(requestBody); + headersMap.forEach(requestBuilder::addHeader); + Request streamRequest = requestBuilder.build(); + try (Response resp = client.newCall(streamRequest).execute()) { + if (!resp.isSuccessful()) { + if (resp.code() == 429) { + return new ResponseEntity<>("rate limit exceeded", HttpStatus.TOO_MANY_REQUESTS); + } else { + String token = getCoCoToken(apiKey); + if (token == null) { + return new ResponseEntity<>("copilot APIKey is wrong", HttpStatus.UNAUTHORIZED); + } + coCopilotTokenList.put(apiKey, token); + log.info("token过期,coCopilotTokenList重置化成功!"); + againEmbeddings(response, conversation, token); + } + } else { + // 非流式输出 + 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; + } catch (IOException e) { + throw new RuntimeException(e); + } + }, executor) + .orTimeout(6, TimeUnit.MINUTES) + .exceptionally(ex -> { + // 处理超时或其他异常 + if (ex instanceof TimeoutException) { + return new ResponseEntity<>("Request timed out", HttpStatus.REQUEST_TIMEOUT); + } else { + return new ResponseEntity<>("An error occurred", HttpStatus.INTERNAL_SERVER_ERROR); + } + }); + } + + public Object againEmbeddings(HttpServletResponse response, + @org.springframework.web.bind.annotation.RequestBody Object conversation, + String token) { + try { + Map 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/embeddings") + .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 { + // 非流式输出 + 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; - } catch (IOException e) { - throw new RuntimeException(e); } - - }, executor); + return null; + } catch (Exception e) { + throw new RuntimeException(e); + } } - /** * 添加模型,用于监控 * diff --git a/rearServer/src/main/java/com/tokensTool/pandoraNext/tokensToolApplication.java b/rearServer/src/main/java/com/tokensTool/pandoraNext/tokensToolApplication.java index f1779e5..c63ac19 100644 --- a/rearServer/src/main/java/com/tokensTool/pandoraNext/tokensToolApplication.java +++ b/rearServer/src/main/java/com/tokensTool/pandoraNext/tokensToolApplication.java @@ -46,7 +46,7 @@ public static void main(String[] args) { log.info("\n--------------------------------------------------------------\n" + "PandoraNext-tokensTool v 0.6.7版本\n" + "1.优化添加进one-api的代码,使得id不一直增加\n" + - "2.优化copilot,cocopilot的chat接口\n" + + "2.优化copilot,cocopilot的chat,embeddings接口,接受高并发\n" + "3.修复历史问题bug,优化代码,优化前端\n" + "--------------------------------------------------------------\n"); // 启动 diff --git a/rearServer/target/classes/com/tokensTool/pandoraNext/config/WebConfig.class b/rearServer/target/classes/com/tokensTool/pandoraNext/config/WebConfig.class index eb6ebb1..41398fc 100644 Binary files a/rearServer/target/classes/com/tokensTool/pandoraNext/config/WebConfig.class and b/rearServer/target/classes/com/tokensTool/pandoraNext/config/WebConfig.class differ diff --git a/rearServer/target/classes/com/tokensTool/pandoraNext/controller/chatController.class b/rearServer/target/classes/com/tokensTool/pandoraNext/controller/chatController.class index 821549e..b947793 100644 Binary files a/rearServer/target/classes/com/tokensTool/pandoraNext/controller/chatController.class and b/rearServer/target/classes/com/tokensTool/pandoraNext/controller/chatController.class differ diff --git a/rearServer/target/classes/com/tokensTool/pandoraNext/tokensToolApplication.class b/rearServer/target/classes/com/tokensTool/pandoraNext/tokensToolApplication.class index 845fb1a..0ee2cc7 100644 Binary files a/rearServer/target/classes/com/tokensTool/pandoraNext/tokensToolApplication.class and b/rearServer/target/classes/com/tokensTool/pandoraNext/tokensToolApplication.class differ diff --git a/rearServer/target/pandoraNext-0.6.7-SNAPSHOT.jar b/rearServer/target/pandoraNext-0.6.7-SNAPSHOT.jar index f00f3b1..6bbff32 100644 Binary files a/rearServer/target/pandoraNext-0.6.7-SNAPSHOT.jar and b/rearServer/target/pandoraNext-0.6.7-SNAPSHOT.jar differ diff --git a/simplyDeploy/pandoraNext-0.6.7-SNAPSHOT.jar b/simplyDeploy/pandoraNext-0.6.7-SNAPSHOT.jar index f00f3b1..6bbff32 100644 Binary files a/simplyDeploy/pandoraNext-0.6.7-SNAPSHOT.jar and b/simplyDeploy/pandoraNext-0.6.7-SNAPSHOT.jar differ