diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42a2157 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +e -i 66fe458e9ff3b7072252b455b08ac5c3076591bc^ diff --git a/src/main/java/com/gpt4/copilot/controller/ChatController.java b/src/main/java/com/gpt4/copilot/controller/ChatController.java index cb70201..514eb97 100644 --- a/src/main/java/com/gpt4/copilot/controller/ChatController.java +++ b/src/main/java/com/gpt4/copilot/controller/ChatController.java @@ -506,7 +506,7 @@ public class ChatController { Map headersMap = new HashMap<>(); //添加头部 addHeader(headersMap, chat_token, apiKey); - String model = (conversation.getModel() != null) ? conversation.getModel() : "gpt-3.5-turbo"; + String model = modelAdjust(conversation); Request streamRequest = getPrompt(conversation, model, headersMap); try (Response resp = client.newCall(streamRequest).execute()) { if (!resp.isSuccessful()) { @@ -535,6 +535,20 @@ public class ChatController { return getObjectResponseEntity(response, future); } + private String modelAdjust(Conversation conversation) { + String model = conversation.getModel(); + if(model == null){ + conversation.setModel("gpt-3.5-turbo"); + return "gpt-3.5-turbo"; + } + else{ + if(model.startsWith("gpt-4")) { + conversation.setModel("gpt-4"); + } + return model; + } + } + /** * 请求体不是json 会报Request body is missing or not in JSON format * Authorization token缺失 会报Authorization header is missing @@ -587,7 +601,7 @@ public class ChatController { Map headersMap = new HashMap<>(); //添加头部 addHeader(headersMap, chat_token, apiKey); - String model = (conversation.getModel() != null) ? conversation.getModel() : "gpt-3.5-turbo"; + String model = modelAdjust(conversation); Request streamRequest = getPrompt(conversation, model, headersMap); try (Response resp = client.newCall(streamRequest).execute()) { if (!resp.isSuccessful()) { @@ -645,7 +659,7 @@ public class ChatController { * @param conversation * @throws IOException */ - private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Conversation conversation) throws IllegalArgumentException { + private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Object conversation) throws IllegalArgumentException { if (conversation == null) { throw new IllegalArgumentException("Request body is missing or not in JSON format"); } @@ -712,7 +726,7 @@ public class ChatController { Map headersMap = new HashMap<>(); //添加头部 addHeader(headersMap, chat_token, apiKey); - String model = (conversation.getModel() != null) ? conversation.getModel() : "gpt-3.5-turbo"; + String model = modelAdjust(conversation); Request streamRequest = getPrompt(conversation, model, headersMap); try (Response resp = client.newCall(streamRequest).execute()) { if (!resp.isSuccessful()) { @@ -792,6 +806,22 @@ public class ChatController { } } + private Request getEmdPrompt(Object conversation, + Map headersMap) { + try { + String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); + RequestBody requestBody = RequestBody.create(json, JSON); + Request.Builder requestBuilder = new Request.Builder().url(github_embaddings).post(requestBody); + headersMap.forEach(requestBuilder::addHeader); + Request streamRequest = requestBuilder.build(); + return streamRequest; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + /** * ghu/gho 请求 * 请求体不是json 会报Request body is missing or not in JSON format @@ -808,7 +838,7 @@ public class ChatController { @PostMapping(value = "/v1/embeddings") public ResponseEntity coPilotEmbeddings(HttpServletResponse response, HttpServletRequest request, - @org.springframework.web.bind.annotation.RequestBody Conversation conversation) { + @org.springframework.web.bind.annotation.RequestBody Object conversation) { String header = request.getHeader("Authorization"); String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null; // 异步处理 @@ -844,11 +874,7 @@ public class ChatController { Map headersMap = new HashMap<>(); //添加头部 addHeader(headersMap, chat_token, apiKey); - String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); - RequestBody requestBody = RequestBody.create(json, JSON); - Request.Builder requestBuilder = new Request.Builder().url(github_embaddings).post(requestBody); - headersMap.forEach(requestBuilder::addHeader); - Request streamRequest = requestBuilder.build(); + Request streamRequest = getEmdPrompt(conversation, headersMap); try (Response resp = client.newCall(streamRequest).execute()) { if (!resp.isSuccessful()) { if (resp.code() == 429) { @@ -908,7 +934,7 @@ public class ChatController { @PostMapping(value = "/cocopilot/v1/embeddings") public ResponseEntity coCoPilotEmbeddings(HttpServletResponse response, HttpServletRequest request, - @org.springframework.web.bind.annotation.RequestBody Conversation conversation) { + @org.springframework.web.bind.annotation.RequestBody Object conversation) { String header = request.getHeader("Authorization"); String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null; // 异步处理 @@ -944,11 +970,7 @@ public class ChatController { Map headersMap = new HashMap<>(); //添加头部 addHeader(headersMap, chat_token, apiKey); - String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); - RequestBody requestBody = RequestBody.create(json, JSON); - Request.Builder requestBuilder = new Request.Builder().url(github_embaddings).post(requestBody); - headersMap.forEach(requestBuilder::addHeader); - Request streamRequest = requestBuilder.build(); + Request streamRequest = getEmdPrompt(conversation, headersMap); try (Response resp = client.newCall(streamRequest).execute()) { if (!resp.isSuccessful()) { if (resp.code() == 429) { @@ -993,7 +1015,7 @@ public class ChatController { @PostMapping(value = "/self/v1/embeddings") public ResponseEntity selfEmbeddings(HttpServletResponse response, HttpServletRequest request, - @org.springframework.web.bind.annotation.RequestBody Conversation conversation) { + @org.springframework.web.bind.annotation.RequestBody Object conversation) { String header = request.getHeader("Authorization"); String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null; // 异步处理 @@ -1022,11 +1044,7 @@ public class ChatController { Map headersMap = new HashMap<>(); //添加头部 addHeader(headersMap, chat_token, apiKey); - String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); - RequestBody requestBody = RequestBody.create(json, JSON); - Request.Builder requestBuilder = new Request.Builder().url(github_embaddings).post(requestBody); - headersMap.forEach(requestBuilder::addHeader); - Request streamRequest = requestBuilder.build(); + Request streamRequest = getEmdPrompt(conversation, headersMap); try (Response resp = client.newCall(streamRequest).execute()) { if (!resp.isSuccessful()) { if (resp.code() == 429) { @@ -1054,7 +1072,7 @@ public class ChatController { } public Object againEmbeddings(HttpServletResponse response, - @org.springframework.web.bind.annotation.RequestBody Conversation conversation, + @org.springframework.web.bind.annotation.RequestBody Object conversation, String token, String apiKey) { try { @@ -1283,6 +1301,11 @@ public class ChatController { */ private String saveMadchineId(String apiKey) { try { + if(machineIdList.containsKey(apiKey)){ + String machineId = machineIdList.get(apiKey); + log.info("机械码读取成功!对应的机械码为:" + machineId); + return machineId; + } String machineId = generateMachineId(); log.info("机械码生成成功!对应的机械码为:" + machineId); machineIdList.put(apiKey, machineId); diff --git a/src/main/java/com/gpt4/copilot/copilotApplication.java b/src/main/java/com/gpt4/copilot/copilotApplication.java index e3a6fa7..f06d3c3 100644 --- a/src/main/java/com/gpt4/copilot/copilotApplication.java +++ b/src/main/java/com/gpt4/copilot/copilotApplication.java @@ -51,7 +51,7 @@ public class copilotApplication { if (!jsonFile.exists() || jsonFile.length() == 0){ try { if (!jsonFile.exists()) { - // 创建文件machineIdList.json + // 创建文件config.json Files.createFile(jsonFilePath); } // 往 config.json 文件中添加一个空数组,防止重启报错 @@ -298,6 +298,7 @@ public class copilotApplication { System.out.println("gpt4-copilot-java 初始化接口成功!"); System.out.println("======================================================"); System.out.println("******原神gpt4-copilot-java-native v0.1.3启动成功******"); + System.out.println("* 对chat接口的模型进行重定向,减少潜在的风险"); System.out.println("* 使用ConcurrentHashMap,粗略的对于每个密钥按每分钟进行限速"); System.out.println("* 新增环境变量用于对gpt-4*等模型进行系统prompt提示"); System.out.println("* 新增url|apikey形式传入/self/*接口,用于自定义地址和密钥"); diff --git a/target/classes/com/gpt4/copilot/controller/ChatController.class b/target/classes/com/gpt4/copilot/controller/ChatController.class index 1618f56..5dc2139 100644 Binary files a/target/classes/com/gpt4/copilot/controller/ChatController.class and b/target/classes/com/gpt4/copilot/controller/ChatController.class differ diff --git a/target/classes/com/gpt4/copilot/copilotApplication.class b/target/classes/com/gpt4/copilot/copilotApplication.class index d9f1efc..c4bcc05 100644 Binary files a/target/classes/com/gpt4/copilot/copilotApplication.class and b/target/classes/com/gpt4/copilot/copilotApplication.class differ diff --git a/target/gpt-4-copilot-0.1.3.jar b/target/gpt-4-copilot-0.1.3.jar index 83e511f..0e55b36 100644 Binary files a/target/gpt-4-copilot-0.1.3.jar and b/target/gpt-4-copilot-0.1.3.jar differ diff --git a/target/gpt-4-copilot-0.1.3.jar.original b/target/gpt-4-copilot-0.1.3.jar.original index 8e6fdf9..4819255 100644 Binary files a/target/gpt-4-copilot-0.1.3.jar.original and b/target/gpt-4-copilot-0.1.3.jar.original differ