mirror of
https://github.com/YuWanTingbb/unofficial-gpt4.git
synced 2025-10-14 14:11:23 +00:00
适配官方requestBody,减小被查询异常
This commit is contained in:
5
.idea/jarRepositories.xml
generated
5
.idea/jarRepositories.xml
generated
@@ -1,6 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
|
@@ -2,9 +2,10 @@
|
||||
"gpt3_sleepTime": 0,
|
||||
"password": "",
|
||||
"get_token_url": "",
|
||||
"copilot_chat_version": "0.14.2024031401",
|
||||
"prefix": "",
|
||||
"vscode_version": "vscode\/1.85.2",
|
||||
"serverPort": 8080,
|
||||
"maxPoolSize": 300,
|
||||
"gpt4_sleepTime": 100,
|
||||
"vscode_version": "vscode\/1.85.2"
|
||||
}
|
||||
"gpt4_sleepTime": 100
|
||||
}
|
2
pom.xml
2
pom.xml
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<groupId>com.gpt4.copilot</groupId>
|
||||
<artifactId>gpt-4-copilot</artifactId>
|
||||
<version>0.0.4</version>
|
||||
<version>0.0.6</version>
|
||||
<name>native</name>
|
||||
<description>Demo project for Spring Boot with GraalVM Native Image</description>
|
||||
<properties>
|
||||
|
@@ -4,10 +4,12 @@ import com.alibaba.fastjson2.JSON;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.gpt4.copilot.copilotApplication;
|
||||
import com.gpt4.copilot.pojo.Conversation;
|
||||
import com.gpt4.copilot.pojo.Result;
|
||||
import com.gpt4.copilot.pojo.systemSetting;
|
||||
import com.gpt4.copilot.pojo.SystemSetting;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@@ -39,8 +41,9 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Data
|
||||
@RestController()
|
||||
public class chatController {
|
||||
public class ChatController {
|
||||
/**
|
||||
* 缓存cocopilotToken
|
||||
*/
|
||||
@@ -57,10 +60,25 @@ public class chatController {
|
||||
* 模型
|
||||
*/
|
||||
private static final String models = "{\"data\":[{\"id\":\"text-search-babbage-doc-001\",\"object\":\"model\",\"created\":1651172509,\"owned_by\":\"openai-dev\"},{\"id\":\"gpt-4\",\"object\":\"model\",\"created\":1687882411,\"owned_by\":\"openai\"},{\"id\":\"babbage\",\"object\":\"model\",\"created\":1649358449,\"owned_by\":\"openai\"},{\"id\":\"gpt-3.5-turbo-0613\",\"object\":\"model\",\"created\":1686587434,\"owned_by\":\"openai\"},{\"id\":\"text-babbage-001\",\"object\":\"model\",\"created\":1649364043,\"owned_by\":\"openai\"},{\"id\":\"gpt-3.5-turbo\",\"object\":\"model\",\"created\":1677610602,\"owned_by\":\"openai\"},{\"id\":\"gpt-3.5-turbo-1106\",\"object\":\"model\",\"created\":1698959748,\"owned_by\":\"system\"},{\"id\":\"curie-instruct-beta\",\"object\":\"model\",\"created\":1649364042,\"owned_by\":\"openai\"},{\"id\":\"gpt-3.5-turbo-0301\",\"object\":\"model\",\"created\":1677649963,\"owned_by\":\"openai\"},{\"id\":\"gpt-3.5-turbo-16k-0613\",\"object\":\"model\",\"created\":1685474247,\"owned_by\":\"openai\"},{\"id\":\"text-embedding-ada-002\",\"object\":\"model\",\"created\":1671217299,\"owned_by\":\"openai-internal\"},{\"id\":\"davinci-similarity\",\"object\":\"model\",\"created\":1651172509,\"owned_by\":\"openai-dev\"},{\"id\":\"curie-similarity\",\"object\":\"model\",\"created\":1651172510,\"owned_by\":\"openai-dev\"},{\"id\":\"babbage-search-document\",\"object\":\"model\",\"created\":1651172510,\"owned_by\":\"openai-dev\"},{\"id\":\"curie-search-document\",\"object\":\"model\",\"created\":1651172508,\"owned_by\":\"openai-dev\"},{\"id\":\"babbage-code-search-code\",\"object\":\"model\",\"created\":1651172509,\"owned_by\":\"openai-dev\"},{\"id\":\"ada-code-search-text\",\"object\":\"model\",\"created\":1651172510,\"owned_by\":\"openai-dev\"},{\"id\":\"text-search-curie-query-001\",\"object\":\"model\",\"created\":1651172509,\"owned_by\":\"openai-dev\"},{\"id\":\"text-davinci-002\",\"object\":\"model\",\"created\":1649880484,\"owned_by\":\"openai\"},{\"id\":\"ada\",\"object\":\"model\",\"created\":1649357491,\"owned_by\":\"openai\"},{\"id\":\"text-ada-001\",\"object\":\"model\",\"created\":1649364042,\"owned_by\":\"openai\"},{\"id\":\"ada-similarity\",\"object\":\"model\",\"created\":1651172507,\"owned_by\":\"openai-dev\"},{\"id\":\"code-search-ada-code-001\",\"object\":\"model\",\"created\":1651172507,\"owned_by\":\"openai-dev\"},{\"id\":\"text-similarity-ada-001\",\"object\":\"model\",\"created\":1651172505,\"owned_by\":\"openai-dev\"},{\"id\":\"text-davinci-edit-001\",\"object\":\"model\",\"created\":1649809179,\"owned_by\":\"openai\"},{\"id\":\"code-davinci-edit-001\",\"object\":\"model\",\"created\":1649880484,\"owned_by\":\"openai\"},{\"id\":\"text-search-curie-doc-001\",\"object\":\"model\",\"created\":1651172509,\"owned_by\":\"openai-dev\"},{\"id\":\"text-curie-001\",\"object\":\"model\",\"created\":1649364043,\"owned_by\":\"openai\"},{\"id\":\"curie\",\"object\":\"model\",\"created\":1649359874,\"owned_by\":\"openai\"},{\"id\":\"davinci\",\"object\":\"model\",\"created\":1649359874,\"owned_by\":\"openai\"}]}";
|
||||
/**
|
||||
* 机器码
|
||||
*/
|
||||
private static final String machineId;
|
||||
/**
|
||||
* CoCopilot Token Url
|
||||
*/
|
||||
private final static String get_cocopilotToken_url = "https://api.cocopilot.org/copilot_internal/v2/token";
|
||||
/**
|
||||
* Copilot Token Url
|
||||
*/
|
||||
private final static String github_get_token_url = "https://api.github.com/copilot_internal/v2/token";
|
||||
/**
|
||||
* github Chat Url
|
||||
*/
|
||||
private final static String github_chat_url = "https://api.githubcopilot.com/chat/completions";
|
||||
/**
|
||||
* github Embedding Url
|
||||
*/
|
||||
private final static String github_embaddings = "https://api.githubcopilot.com/embeddings";
|
||||
/**
|
||||
* gpt4单字符睡眠时间
|
||||
@@ -90,7 +108,16 @@ public class chatController {
|
||||
* 自定义maxPoolSize
|
||||
*/
|
||||
private static Integer maxPoolSize;
|
||||
private static OkHttpClient client = new OkHttpClient.Builder().connectTimeout(3, TimeUnit.MINUTES).readTimeout(5, TimeUnit.MINUTES).writeTimeout(5, TimeUnit.MINUTES).build();
|
||||
/**
|
||||
* 定义okhttp库
|
||||
*/
|
||||
private static OkHttpClient client = new OkHttpClient.Builder().connectTimeout(3, TimeUnit.MINUTES)
|
||||
.readTimeout(5, TimeUnit.MINUTES)
|
||||
.writeTimeout(5, TimeUnit.MINUTES)
|
||||
.build();
|
||||
/**
|
||||
* 定义线程池里的线程名字
|
||||
*/
|
||||
private static ThreadFactory threadFactory = new ThreadFactory() {
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
@@ -99,14 +126,21 @@ public class chatController {
|
||||
return new Thread(r, "chatThreadPool-" + counter.getAndIncrement());
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 定义线程池
|
||||
*/
|
||||
private static ExecutorService executor;
|
||||
|
||||
|
||||
/**
|
||||
* 初始化ChatController类
|
||||
*/
|
||||
static {
|
||||
selfTokenList = new HashMap<>();
|
||||
copilotTokenList = new HashMap<>();
|
||||
coCopilotTokenList = new HashMap<>();
|
||||
machineId = generateMachineId();
|
||||
systemSetting systemSetting = selectSetting();
|
||||
SystemSetting systemSetting = selectSetting();
|
||||
setGpt4_sleepTime(systemSetting.getGpt4_sleepTime());
|
||||
setGpt3_sleepTime(systemSetting.getGpt3_sleepTime());
|
||||
setPassword(systemSetting.getPassword());
|
||||
@@ -122,7 +156,7 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setCopilot_chat_version(String copilot_chat_version) {
|
||||
chatController.copilot_chat_version = copilot_chat_version;
|
||||
ChatController.copilot_chat_version = copilot_chat_version;
|
||||
}
|
||||
|
||||
public static String getVscode_version() {
|
||||
@@ -130,7 +164,7 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setVscode_version(String vscode_version) {
|
||||
chatController.vscode_version = vscode_version;
|
||||
ChatController.vscode_version = vscode_version;
|
||||
}
|
||||
|
||||
public static Integer getMaxPoolSize() {
|
||||
@@ -138,11 +172,11 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setMaxPoolSize(Integer maxPoolSize) {
|
||||
chatController.maxPoolSize = maxPoolSize;
|
||||
ChatController.maxPoolSize = maxPoolSize;
|
||||
}
|
||||
|
||||
public static void setExecutor(Integer maxPoolSize) {
|
||||
chatController.executor = new ThreadPoolExecutor(0, maxPoolSize, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory);
|
||||
ChatController.executor = new ThreadPoolExecutor(0, maxPoolSize, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory);
|
||||
}
|
||||
|
||||
public static Integer getGpt4_sleepTime() {
|
||||
@@ -150,7 +184,7 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setGpt4_sleepTime(Integer gpt4_sleepTime) {
|
||||
chatController.gpt4_sleepTime = gpt4_sleepTime;
|
||||
ChatController.gpt4_sleepTime = gpt4_sleepTime;
|
||||
}
|
||||
|
||||
public static Integer getGpt3_sleepTime() {
|
||||
@@ -158,7 +192,7 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setGpt3_sleepTime(Integer gpt3_sleepTime) {
|
||||
chatController.gpt3_sleepTime = gpt3_sleepTime;
|
||||
ChatController.gpt3_sleepTime = gpt3_sleepTime;
|
||||
}
|
||||
|
||||
public static String getPassword() {
|
||||
@@ -166,7 +200,7 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setPassword(String password) {
|
||||
chatController.password = password;
|
||||
ChatController.password = password;
|
||||
}
|
||||
|
||||
public static String getGet_token_url() {
|
||||
@@ -174,9 +208,12 @@ public class chatController {
|
||||
}
|
||||
|
||||
public static void setGet_token_url(String get_token_url) {
|
||||
chatController.get_token_url = get_token_url;
|
||||
ChatController.get_token_url = get_token_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化获取环境变量
|
||||
*/
|
||||
public static String selectFile() {
|
||||
String projectRoot = System.getProperty("user.dir");
|
||||
String parent = projectRoot + File.separator + "config.json";
|
||||
@@ -203,7 +240,7 @@ public class chatController {
|
||||
*
|
||||
* @return systemSettings类
|
||||
*/
|
||||
public static systemSetting selectSetting() {
|
||||
public static SystemSetting selectSetting() {
|
||||
boolean exist = true;
|
||||
String parent = selectFile();
|
||||
try {
|
||||
@@ -270,7 +307,7 @@ public class chatController {
|
||||
exist = false;
|
||||
}
|
||||
// 将 JSONObject 转换为 Config 类的实例
|
||||
systemSetting config = new systemSetting();
|
||||
SystemSetting config = new SystemSetting();
|
||||
config.setPassword(jsonObject.optString("password"));
|
||||
config.setMaxPoolSize(jsonObject.optInt("maxPoolSize"));
|
||||
config.setGpt3_sleepTime(jsonObject.optInt("gpt3_sleepTime"));
|
||||
@@ -338,7 +375,8 @@ public class chatController {
|
||||
}
|
||||
// 将修改后的 JSONObject 转换为格式化的 JSON 字符串
|
||||
String updatedJson = jsonObject.toString(2);
|
||||
Files.write(Paths.get(parent), updatedJson.getBytes());
|
||||
Path path = Paths.get(parent);
|
||||
Files.write(path, updatedJson.getBytes());
|
||||
return Result.success("修改成功!");
|
||||
} else {
|
||||
return Result.error("管理员密码不对,请重新再试!");
|
||||
@@ -378,7 +416,6 @@ public class chatController {
|
||||
.addHeader("User-Agent", "GitHubCopilotChat/0.11.1")
|
||||
.addHeader("Accept", "*/*").build();
|
||||
try (Response res = client.newCall(request_token).execute()) {
|
||||
log.info(res.toString());
|
||||
if (!res.isSuccessful()) {
|
||||
return new ResponseEntity<>(Result.error("Unsuccessful response: " + res.message()), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
@@ -426,7 +463,9 @@ public class chatController {
|
||||
* @throws IOException
|
||||
*/
|
||||
@PostMapping(value = "/v1/chat/completions")
|
||||
public ResponseEntity<Object> coPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
public ResponseEntity<Object> coPilotConversation(HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
@@ -513,7 +552,9 @@ public class chatController {
|
||||
*/
|
||||
|
||||
@PostMapping(value = "/cocopilot/v1/chat/completions")
|
||||
public ResponseEntity<Object> coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
public ResponseEntity<Object> coCoPilotConversation(HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
@@ -530,7 +571,7 @@ public class chatController {
|
||||
if (!coCopilotTokenList.containsKey(apiKey)) {
|
||||
String token = getCoCoToken(apiKey);
|
||||
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("coCopilotToken初始化成功!");
|
||||
@@ -557,7 +598,7 @@ public class chatController {
|
||||
return new ResponseEntity<>(Result.error("cocopilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
coCopilotTokenList.put(apiKey, token);
|
||||
log.info("token过期,始皇coCopilotToken重置化成功!");
|
||||
log.info("token过期,coCopilotToken重置化成功!");
|
||||
againConversation(response, conversation, token);
|
||||
}
|
||||
} else {
|
||||
@@ -571,22 +612,13 @@ public class chatController {
|
||||
return null;
|
||||
}, executor);
|
||||
|
||||
ResponseEntity<Object> responseEntity;
|
||||
|
||||
try {
|
||||
responseEntity = future.get(6, TimeUnit.MINUTES);
|
||||
} catch (TimeoutException ex) {
|
||||
future.cancel(true);
|
||||
responseEntity = new ResponseEntity<>(Result.error("The chat timed out"), HttpStatus.REQUEST_TIMEOUT);
|
||||
} catch (Exception ex) {
|
||||
log.error(ex.getMessage());
|
||||
responseEntity = new ResponseEntity<>(Result.error("An error occurred"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return responseEntity;
|
||||
return getObjectResponseEntity(future);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/self/v1/chat/completions")
|
||||
public ResponseEntity<Object> selfConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
public ResponseEntity<Object> selfConversation(HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
@@ -614,6 +646,7 @@ public class chatController {
|
||||
//添加头部
|
||||
addHeader(headersMap, chat_token);
|
||||
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation);
|
||||
log.info(conversation.toString());
|
||||
// 创建一个 RequestBody 对象
|
||||
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
RequestBody requestBody = RequestBody.create(json, JSON);
|
||||
@@ -644,18 +677,7 @@ public class chatController {
|
||||
return null;
|
||||
}, executor);
|
||||
|
||||
ResponseEntity<Object> responseEntity;
|
||||
|
||||
try {
|
||||
responseEntity = future.get(6, TimeUnit.MINUTES);
|
||||
} catch (TimeoutException ex) {
|
||||
future.cancel(true);
|
||||
responseEntity = new ResponseEntity<>(Result.error("The chat timed out"), HttpStatus.REQUEST_TIMEOUT);
|
||||
} catch (Exception ex) {
|
||||
log.error(ex.getMessage());
|
||||
responseEntity = new ResponseEntity<>(Result.error("An error occurred"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return responseEntity;
|
||||
return getObjectResponseEntity(future);
|
||||
}
|
||||
|
||||
|
||||
@@ -668,7 +690,9 @@ public class chatController {
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public Object againConversation(HttpServletResponse response, @org.springframework.web.bind.annotation.RequestBody Object conversation, String token) {
|
||||
public Object againConversation(HttpServletResponse response,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation,
|
||||
String token) {
|
||||
try {
|
||||
Map<String, String> headersMap = new HashMap<>();
|
||||
//添加头部
|
||||
@@ -708,7 +732,9 @@ public class chatController {
|
||||
* @throws IOException
|
||||
*/
|
||||
@PostMapping(value = "/v1/embeddings")
|
||||
public ResponseEntity<Object> coPilotEmbeddings(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
public ResponseEntity<Object> coPilotEmbeddings(HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
@@ -766,6 +792,10 @@ public class chatController {
|
||||
}
|
||||
}, executor);
|
||||
|
||||
return getObjectResponseEntity(future);
|
||||
}
|
||||
|
||||
private ResponseEntity<Object> getObjectResponseEntity(CompletableFuture<ResponseEntity<Object>> future) {
|
||||
ResponseEntity<Object> responseEntity;
|
||||
|
||||
try {
|
||||
@@ -795,7 +825,9 @@ public class chatController {
|
||||
* @throws IOException
|
||||
*/
|
||||
@PostMapping(value = "/cocopilot/v1/embeddings")
|
||||
public ResponseEntity<Object> coCoPilotEmbeddings(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
public ResponseEntity<Object> coCoPilotEmbeddings(HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
@@ -815,7 +847,7 @@ public class chatController {
|
||||
return new ResponseEntity<>(Result.error("copilot APIKey is wrong"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
coCopilotTokenList.put(apiKey, token);
|
||||
log.info("始皇coCopilotToken初始化成功!");
|
||||
log.info("coCopilotToken初始化成功!");
|
||||
}
|
||||
// 创建OkHttpClient请求 请求https://api.githubcopilot.com/chat/completions
|
||||
String chat_token = coCopilotTokenList.get(apiKey);
|
||||
@@ -852,18 +884,7 @@ public class chatController {
|
||||
}
|
||||
}, executor);
|
||||
|
||||
ResponseEntity<Object> responseEntity;
|
||||
|
||||
try {
|
||||
responseEntity = future.get(6, TimeUnit.MINUTES);
|
||||
} catch (TimeoutException ex) {
|
||||
future.cancel(true);
|
||||
responseEntity = new ResponseEntity<>(Result.error("The chat timed out"), HttpStatus.REQUEST_TIMEOUT);
|
||||
} catch (Exception ex) {
|
||||
log.error(ex.getMessage());
|
||||
responseEntity = new ResponseEntity<>(Result.error("An error occurred"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return responseEntity;
|
||||
return getObjectResponseEntity(future);
|
||||
}
|
||||
|
||||
|
||||
@@ -881,7 +902,9 @@ public class chatController {
|
||||
* @throws IOException
|
||||
*/
|
||||
@PostMapping(value = "/self/v1/embeddings")
|
||||
public ResponseEntity<Object> selfEmbeddings(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Object conversation) {
|
||||
public ResponseEntity<Object> selfEmbeddings(HttpServletResponse response,
|
||||
HttpServletRequest request,
|
||||
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
|
||||
String authorizationHeader = StringUtils.trimToNull(request.getHeader("Authorization"));
|
||||
// 异步处理
|
||||
CompletableFuture<ResponseEntity<Object>> future = CompletableFuture.supplyAsync(() -> {
|
||||
@@ -937,21 +960,10 @@ public class chatController {
|
||||
}
|
||||
}, executor);
|
||||
|
||||
ResponseEntity<Object> responseEntity;
|
||||
|
||||
try {
|
||||
responseEntity = future.get(6, TimeUnit.MINUTES);
|
||||
} catch (TimeoutException ex) {
|
||||
future.cancel(true);
|
||||
responseEntity = new ResponseEntity<>(Result.error("The chat timed out"), HttpStatus.REQUEST_TIMEOUT);
|
||||
} catch (Exception ex) {
|
||||
log.error(ex.getMessage());
|
||||
responseEntity = new ResponseEntity<>(Result.error("An error occurred"), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return responseEntity;
|
||||
return getObjectResponseEntity(future);
|
||||
}
|
||||
|
||||
public Object againEmbeddings(HttpServletResponse response, @org.springframework.web.bind.annotation.RequestBody Object conversation, String token) {
|
||||
public Object againEmbeddings(HttpServletResponse response, @org.springframework.web.bind.annotation.RequestBody Conversation conversation, String token) {
|
||||
try {
|
||||
Map<String, String> headersMap = new HashMap<>();
|
||||
//添加头部
|
||||
@@ -1059,14 +1071,8 @@ public class chatController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* copilot的模型
|
||||
*
|
||||
* @return
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
@GetMapping("/v1/models")
|
||||
public Object models() throws JsonProcessingException {
|
||||
|
||||
private Object getModels() {
|
||||
try {
|
||||
Future<Object> future = executor.submit(() -> {
|
||||
String jsonString = models;
|
||||
@@ -1078,6 +1084,17 @@ public class chatController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* copilot的模型
|
||||
*
|
||||
* @return
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
@GetMapping("/v1/models")
|
||||
public Object models() {
|
||||
return getModels();
|
||||
}
|
||||
|
||||
/**
|
||||
* cocopilot的模型
|
||||
*
|
||||
@@ -1086,17 +1103,19 @@ public class chatController {
|
||||
*/
|
||||
@GetMapping("/cocopilot/v1/models")
|
||||
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);
|
||||
}
|
||||
return getModels();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义的模型
|
||||
*
|
||||
* @return
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
@GetMapping("/self/v1/models")
|
||||
public Object selfPilotModels() {
|
||||
return getModels();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提问请求头
|
||||
@@ -1128,11 +1147,10 @@ public class chatController {
|
||||
* @param resp
|
||||
* @param conversation
|
||||
*/
|
||||
private void outPutChat(HttpServletResponse response, Response resp, Object conversation) {
|
||||
private void outPutChat(HttpServletResponse response, Response resp, Conversation conversation) {
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(com.alibaba.fastjson2.JSON.toJSONString(conversation));
|
||||
boolean isStream = jsonObject.optBoolean("stream", false);
|
||||
String model = jsonObject.optString("model", "gpt-3.5-turbo");
|
||||
boolean isStream = (conversation.getStream() != null) ? conversation.getStream() : false;
|
||||
String model = (conversation.getModel() != null) ? conversation.getModel() : "gpt-3.5-turbo";
|
||||
int sleep_time = calculateSleepTime(model, isStream);
|
||||
if (isStream) {
|
||||
response.setContentType("text/event-stream; charset=UTF-8");
|
||||
@@ -1158,8 +1176,6 @@ public class chatController {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("IO Exception occurred", e);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException("JSON Exception occurred", e);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package com.gpt4.copilot;
|
||||
|
||||
|
||||
import com.gpt4.copilot.controller.chatController;
|
||||
import com.gpt4.copilot.pojo.systemSetting;
|
||||
import com.gpt4.copilot.controller.ChatController;
|
||||
import com.gpt4.copilot.pojo.SystemSetting;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@@ -36,13 +36,13 @@ public class copilotApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String configFilePath = System.getProperty("user.dir") + File.separator + "config.json";
|
||||
systemSetting config = loadConfig(configFilePath);
|
||||
SystemSetting config = loadConfig(configFilePath);
|
||||
setSystemProperties(config);
|
||||
SpringApplication.run(copilotApplication.class, args);
|
||||
printStartupMessage(config);
|
||||
}
|
||||
|
||||
private static systemSetting loadConfig(String configFilePath) {
|
||||
private static SystemSetting loadConfig(String configFilePath) {
|
||||
File jsonFile = new File(configFilePath);
|
||||
if (!jsonFile.exists()) {
|
||||
createEmptyConfigFile(configFilePath);
|
||||
@@ -71,9 +71,9 @@ public class copilotApplication {
|
||||
}
|
||||
}
|
||||
|
||||
private static systemSetting parseConfig(String configFilePath, JSONObject jsonObject) {
|
||||
private static SystemSetting parseConfig(String configFilePath, JSONObject jsonObject) {
|
||||
try {
|
||||
systemSetting config = new systemSetting();
|
||||
SystemSetting config = new SystemSetting();
|
||||
config.setServerPort(getIntOrDefault(jsonObject, "serverPort", 8080));
|
||||
config.setPrefix(getStringOrDefault(jsonObject, "prefix", "/"));
|
||||
String updatedJson = jsonObject.toString(2);
|
||||
@@ -110,7 +110,7 @@ public class copilotApplication {
|
||||
}
|
||||
}
|
||||
|
||||
private static void setSystemProperties(systemSetting config) {
|
||||
private static void setSystemProperties(SystemSetting config) {
|
||||
System.setProperty("server.port", String.valueOf(config.getServerPort()));
|
||||
System.setProperty("server.servlet.context-path", config.getPrefix());
|
||||
}
|
||||
@@ -177,10 +177,10 @@ public class copilotApplication {
|
||||
String latestVersion = getLatestVSCodeVersion();
|
||||
String latestChatVersion = getLatestExtensionVersion("GitHub", "copilot-chat");
|
||||
if (latestVersion != null && latestChatVersion != null) {
|
||||
chatController.setVscode_version(latestVersion);
|
||||
chatController.setCopilot_chat_version("copilot-chat/" + latestChatVersion);
|
||||
ChatController.setVscode_version(latestVersion);
|
||||
ChatController.setCopilot_chat_version("copilot-chat/" + latestChatVersion);
|
||||
}
|
||||
String parent = chatController.selectFile();
|
||||
String parent = ChatController.selectFile();
|
||||
// 读取 JSON 文件内容
|
||||
String jsonContent = new String(Files.readAllBytes(Paths.get(parent)));
|
||||
JSONObject jsonObject = new JSONObject(jsonContent);
|
||||
@@ -190,31 +190,30 @@ public class copilotApplication {
|
||||
String updatedJson = jsonObject.toString(2);
|
||||
Files.write(Paths.get(parent), updatedJson.getBytes());
|
||||
System.out.println("===================配置更新说明========================");
|
||||
System.out.println("vscode_version:" + chatController.getVscode_version());
|
||||
System.out.println("copilot_chat_version:" + chatController.getCopilot_chat_version());
|
||||
System.out.println("vscode_version:" + ChatController.getVscode_version());
|
||||
System.out.println("copilot_chat_version:" + ChatController.getCopilot_chat_version());
|
||||
System.out.println("======================================================");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void printStartupMessage(systemSetting config) {
|
||||
private static void printStartupMessage(SystemSetting config) {
|
||||
System.out.println("\n=====================配置说明==========================");
|
||||
System.out.println("serverPort:" + config.getServerPort());
|
||||
System.out.println("prefix:" + config.getPrefix());
|
||||
System.out.println("password:" + chatController.getPassword());
|
||||
System.out.println("maxPoolSize:" + chatController.getMaxPoolSize());
|
||||
System.out.println("gpt3_sleepTime:" + chatController.getGpt3_sleepTime());
|
||||
System.out.println("gpt4_sleepTime:" + chatController.getGpt4_sleepTime());
|
||||
System.out.println("vscode_version:" + chatController.getVscode_version());
|
||||
System.out.println("copilot_chat_version:" + chatController.getCopilot_chat_version());
|
||||
System.out.println("get_token_url:" + chatController.getGet_token_url());
|
||||
System.out.println("password:" + ChatController.getPassword());
|
||||
System.out.println("maxPoolSize:" + ChatController.getMaxPoolSize());
|
||||
System.out.println("gpt3_sleepTime:" + ChatController.getGpt3_sleepTime());
|
||||
System.out.println("gpt4_sleepTime:" + ChatController.getGpt4_sleepTime());
|
||||
System.out.println("vscode_version:" + ChatController.getVscode_version());
|
||||
System.out.println("copilot_chat_version:" + ChatController.getCopilot_chat_version());
|
||||
System.out.println("get_token_url:" + ChatController.getGet_token_url());
|
||||
System.out.println("gpt4-copilot-java 初始化接口成功!");
|
||||
System.out.println("======================================================");
|
||||
System.out.println("******原神gpt4-copilot-java-native v0.0.5启动成功******");
|
||||
System.out.println("******原神gpt4-copilot-java-native v0.0.6启动成功******");
|
||||
System.out.println("* 采用graalvm打包,运行内存大幅度减小");
|
||||
System.out.println("* jar包和二进制文件兼容基本所有系统");
|
||||
System.out.println("* 每隔三天自动更新vscode版本和copilot-chat版本");
|
||||
System.out.println("* 适配官方requestBody,减小被查询异常");
|
||||
System.out.println("URL地址:http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + "");
|
||||
System.out.println("======================================================");
|
||||
}
|
||||
|
23
src/main/java/com/gpt4/copilot/pojo/Conversation.java
Normal file
23
src/main/java/com/gpt4/copilot/pojo/Conversation.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.gpt4.copilot.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Conversation {
|
||||
|
||||
private String model;
|
||||
|
||||
private List<Map<String, String>> messages;
|
||||
|
||||
private Boolean stream;
|
||||
|
||||
}
|
@@ -11,7 +11,7 @@ import lombok.NoArgsConstructor;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class systemSetting {
|
||||
public class SystemSetting {
|
||||
/**
|
||||
* server.port
|
||||
*/
|
@@ -1,6 +1,3 @@
|
||||
management.endpoints.web.exposure.include=metrics,health,info,prometheus
|
||||
quarkus.native.additional-build-args=-H:NativeImageOptions=-march=ivybridge
|
||||
|
||||
|
||||
|
||||
|
||||
|
3
target/classes/application.properties
Normal file
3
target/classes/application.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
management.endpoints.web.exposure.include=metrics,health,info,prometheus
|
||||
|
||||
|
BIN
target/classes/com/gpt4/copilot/config/CorsConfig.class
Normal file
BIN
target/classes/com/gpt4/copilot/config/CorsConfig.class
Normal file
Binary file not shown.
BIN
target/classes/com/gpt4/copilot/config/WebConfig.class
Normal file
BIN
target/classes/com/gpt4/copilot/config/WebConfig.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
target/classes/com/gpt4/copilot/controller/ChatController.class
Normal file
BIN
target/classes/com/gpt4/copilot/controller/ChatController.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
target/classes/com/gpt4/copilot/copilotApplication.class
Normal file
BIN
target/classes/com/gpt4/copilot/copilotApplication.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
target/classes/com/gpt4/copilot/pojo/Conversation.class
Normal file
BIN
target/classes/com/gpt4/copilot/pojo/Conversation.class
Normal file
Binary file not shown.
BIN
target/classes/com/gpt4/copilot/pojo/Result.class
Normal file
BIN
target/classes/com/gpt4/copilot/pojo/Result.class
Normal file
Binary file not shown.
BIN
target/classes/com/gpt4/copilot/pojo/SystemSetting.class
Normal file
BIN
target/classes/com/gpt4/copilot/pojo/SystemSetting.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
target/gpt-4-copilot-0.0.6.jar.original
Normal file
BIN
target/gpt-4-copilot-0.0.6.jar.original
Normal file
Binary file not shown.
3
target/maven-archiver/pom.properties
Normal file
3
target/maven-archiver/pom.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
artifactId=gpt-4-copilot
|
||||
groupId=com.gpt4.copilot
|
||||
version=0.0.6
|
@@ -0,0 +1,10 @@
|
||||
com\gpt4\copilot\controller\ChatController$1.class
|
||||
com\gpt4\copilot\pojo\Conversation$ConversationBuilder.class
|
||||
com\gpt4\copilot\config\WebConfig.class
|
||||
com\gpt4\copilot\controller\ChatController.class
|
||||
com\gpt4\copilot\controller\CustomErrorController.class
|
||||
com\gpt4\copilot\pojo\Conversation.class
|
||||
com\gpt4\copilot\pojo\SystemSetting.class
|
||||
com\gpt4\copilot\config\CorsConfig.class
|
||||
com\gpt4\copilot\pojo\Result.class
|
||||
com\gpt4\copilot\copilotApplication.class
|
@@ -0,0 +1,8 @@
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\config\CorsConfig.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\pojo\Result.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\controller\CustomErrorController.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\pojo\Conversation.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\pojo\SystemSetting.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\controller\ChatController.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\config\WebConfig.java
|
||||
F:\vue\fakeApiTool\graalvm-demos\spring-native-image\GPT4-Copilot\src\main\java\com\gpt4\copilot\copilotApplication.java
|
Reference in New Issue
Block a user