gpt4-copilot-java-native v0.0.5

This commit is contained in:
Yang Yang
2024-02-25 06:15:39 +00:00
parent 8812c0f89d
commit d791247591
7 changed files with 185 additions and 70 deletions

View File

@@ -8,7 +8,7 @@ LABEL maintainer="Yanyutin753"
USER root
# 复制JAR文件到容器的/app目录下
COPY /target/gpt-4-copilot-0.0.4.jar app.jar
COPY /target/gpt-4-copilot-0.0.5.jar app.jar
# 声明服务运行在8081端口
EXPOSE 8081

View File

@@ -3,7 +3,6 @@
"password": "",
"get_token_url": "",
"prefix": "",
"vscode_version": "vscode\/1.85.2",
"serverPort": 8080,
"maxPoolSize": 300,
"gpt4_sleepTime": 110

View File

@@ -25,7 +25,7 @@ public class CustomErrorController implements ErrorController {
" <title>Document</title>\n" +
"</head>\n" +
"<body>\n" +
" <p>Thanks you use gpt4-copilot-java</p>\n" +
" <p>Thanks you use gpt4-copilot-java-0.0.5</p>\n" +
" <p><a href=\"https://apifox.com/apidoc/shared-4301e565-a8df-48a0-85a5-bda2c4c3965a\">详细使用文档</a></p>\n" +
" <p><a href=\"https://github.com/Yanyutin753/gpt4-copilot-java-sh\">项目地址</a></p>\n" +
"</body>\n" +

View File

@@ -3,6 +3,7 @@ package com.gpt4.copilot.controller;
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.Result;
import com.gpt4.copilot.pojo.systemSetting;
import jakarta.servlet.http.HttpServletRequest;
@@ -59,7 +60,7 @@ public class chatController {
private static final String machineId;
private final static String get_cocopilotToken_url = "https://api.cocopilot.org/copilot_internal/v2/token";
private final static String github_get_token_url = "https://api.github.com/copilot_internal/v2/token";
private final static String github_chat = "https://api.githubcopilot.com/chat/completions";
private final static String github_chat_url = "https://api.githubcopilot.com/chat/completions";
private final static String github_embaddings = "https://api.githubcopilot.com/embeddings";
/**
* gpt4单字符睡眠时间
@@ -81,6 +82,10 @@ public class chatController {
* 自定义vscode_version
*/
private static String vscode_version;
/**
* 自定义copilot_chat_version;
*/
private static String copilot_chat_version;
/**
* 自定义maxPoolSize
*/
@@ -107,10 +112,19 @@ public class chatController {
setPassword(systemSetting.getPassword());
setGet_token_url(systemSetting.getGet_token_url());
setVscode_version(systemSetting.getVscode_version());
setCopilot_chat_version(systemSetting.getCopilot_chat_version());
setMaxPoolSize(systemSetting.getMaxPoolSize());
setExecutor(systemSetting.getMaxPoolSize());
}
public static String getCopilot_chat_version() {
return copilot_chat_version;
}
public static void setCopilot_chat_version(String copilot_chat_version) {
chatController.copilot_chat_version = copilot_chat_version;
}
public static String getVscode_version() {
return vscode_version;
}
@@ -198,10 +212,10 @@ public class chatController {
// 将 JSON 字符串解析为 JSONObject
JSONObject jsonObject = new JSONObject(jsonContent);
try {
jsonObject.getInt("gpt4_sleepTime");
jsonObject.getString("password");
} catch (JSONException e) {
jsonObject.put("gpt4_sleepTime", "100");
log.info("config.json没有新增gpt4_sleepTime参数,现已增加!");
jsonObject.put("password", UUID.randomUUID().toString());
log.info("config.json没有新增password参数,现已增加!");
exist = false;
}
try {
@@ -212,10 +226,39 @@ public class chatController {
exist = false;
}
try {
jsonObject.getString("password");
jsonObject.getInt("gpt4_sleepTime");
} catch (JSONException e) {
jsonObject.put("password", UUID.randomUUID().toString());
log.info("config.json没有新增password参数,现已增加!");
jsonObject.put("gpt4_sleepTime", "100");
log.info("config.json没有新增gpt4_sleepTime参数,现已增加!");
exist = false;
}
try {
jsonObject.getInt("maxPoolSize");
} catch (JSONException e) {
jsonObject.put("maxPoolSize", 300);
log.info("config.json没有新增maxPoolSize参数,现已增加!");
exist = false;
}
try {
jsonObject.getString("vscode_version");
} catch (JSONException e) {
String latestVSCodeVersion = copilotApplication.getLatestVSCodeVersion();
if (latestVSCodeVersion != null) {
jsonObject.put("vscode_version", latestVSCodeVersion);
log.info("config.json没有新增vscode_version参数,现已增加!");
}
exist = false;
}
try {
jsonObject.getString("copilot_chat_version");
} catch (JSONException e) {
String latestChatVersion = copilotApplication.getLatestExtensionVersion("GitHub", "copilot-chat");
if (latestChatVersion != null) {
jsonObject.put("copilot_chat_version", latestChatVersion);
log.info("config.json没有新增copilot_chat_version参数,现已增加!");
}
exist = false;
}
@@ -226,31 +269,15 @@ public class chatController {
log.info("config.json没有新增get_token_url参数,现已增加!");
exist = false;
}
try {
jsonObject.getString("vscode_version");
} catch (JSONException e) {
jsonObject.put("vscode_version", "vscode/1.85.2");
log.info("config.json没有新增vscode_version参数,现已增加!");
exist = false;
}
try {
jsonObject.getInt("maxPoolSize");
} catch (JSONException e) {
jsonObject.put("maxPoolSize", 300);
log.info("config.json没有新增maxPoolSize参数,现已增加!");
exist = false;
}
// 将 JSONObject 转换为 Config 类的实例
systemSetting config = new systemSetting();
config.setGpt4_sleepTime(jsonObject.optInt("gpt4_sleepTime"));
config.setGpt3_sleepTime(jsonObject.optInt("gpt3_sleepTime"));
config.setPassword(jsonObject.optString("password"));
config.setGet_token_url(jsonObject.optString("get_token_url"));
config.setVscode_version(jsonObject.optString("vscode_version"));
config.setMaxPoolSize(jsonObject.optInt("maxPoolSize"));
config.setGpt3_sleepTime(jsonObject.optInt("gpt3_sleepTime"));
config.setGpt4_sleepTime(jsonObject.optInt("gpt4_sleepTime"));
config.setVscode_version(jsonObject.optString("vscode_version"));
config.setCopilot_chat_version(jsonObject.optString("copilot_chat_version"));
config.setGet_token_url(jsonObject.optString("get_token_url"));
if (exist == false) {
// 将修改后的 JSONObject 转换为格式化的 JSON 字符串
@@ -347,7 +374,7 @@ public class chatController {
.addHeader("Host", "api.github.com")
.addHeader("authorization", "token " + apiKey)
.addHeader("Editor-Version", vscode_version)
.addHeader("Editor-Plugin-Version", "copilot-chat/0.11.1")
.addHeader("Editor-Plugin-Version", "copilot-chat/" + copilot_chat_version)
.addHeader("User-Agent", "GitHubCopilotChat/0.11.1")
.addHeader("Accept", "*/*").build();
try (Response res = client.newCall(request_token).execute()) {
@@ -430,7 +457,7 @@ public class chatController {
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder().url(github_chat).post(requestBody);
Request.Builder requestBuilder = new Request.Builder().url(github_chat_url).post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
@@ -517,7 +544,7 @@ public class chatController {
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder().url(github_chat).post(requestBody);
Request.Builder requestBuilder = new Request.Builder().url(github_chat_url).post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
@@ -590,7 +617,7 @@ public class chatController {
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder().url(github_chat).post(requestBody);
Request.Builder requestBuilder = new Request.Builder().url(github_chat_url).post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
@@ -650,7 +677,7 @@ public class chatController {
// 创建一个 RequestBody 对象
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(json, JSON);
Request.Builder requestBuilder = new Request.Builder().url(github_chat).post(requestBody);
Request.Builder requestBuilder = new Request.Builder().url(github_chat_url).post(requestBody);
headersMap.forEach(requestBuilder::addHeader);
Request streamRequest = requestBuilder.build();
try (Response resp = client.newCall(streamRequest).execute()) {
@@ -962,8 +989,8 @@ public class chatController {
.addHeader("Host", "api.github.com")
.addHeader("authorization", "token " + apiKey)
.addHeader("Editor-Version", vscode_version)
.addHeader("Editor-Plugin-Version", "copilot-chat/0.11.1")
.addHeader("User-Agent", "GitHubCopilotChat/0.11.1")
.addHeader("Editor-Plugin-Version", "copilot-chat/" + copilot_chat_version)
.addHeader("User-Agent", "GitHubCopilotChat/" + copilot_chat_version)
.addHeader("Accept", "*/*").build();
try (Response response = client.newCall(request).execute()) {
log.info(response.toString());
@@ -989,8 +1016,8 @@ public class chatController {
Request request = new Request.Builder().url(get_cocopilotToken_url)
.addHeader("authorization", "token " + apiKey)
.addHeader("Editor-Version", vscode_version)
.addHeader("Editor-Plugin-Version", "copilot-chat/0.11.1")
.addHeader("User-Agent", "GitHubCopilotChat/0.11.1")
.addHeader("Editor-Plugin-Version", "copilot-chat/" + copilot_chat_version)
.addHeader("User-Agent", "GitHubCopilotChat/" + copilot_chat_version)
.addHeader("Accept", "*/*").build();
try (Response response = client.newCall(request).execute()) {
log.info(response.toString());
@@ -1016,8 +1043,8 @@ public class chatController {
Request request = new Request.Builder().url(get_token_url)
.addHeader("authorization", "token " + apiKey)
.addHeader("Editor-Version", vscode_version)
.addHeader("Editor-Plugin-Version", "copilot-chat/0.11.1")
.addHeader("User-Agent", "GitHubCopilotChat/0.11.1")
.addHeader("Editor-Plugin-Version", "copilot-chat/" + copilot_chat_version)
.addHeader("User-Agent", "GitHubCopilotChat/" + copilot_chat_version)
.addHeader("Accept", "*/*").build();
try (Response response = client.newCall(request).execute()) {
log.info(response.toString());
@@ -1087,11 +1114,11 @@ public class chatController {
headersMap.put("Vscode-Sessionid", UUID.randomUUID().toString() + System.currentTimeMillis());
headersMap.put("vscode-machineid", machineId);
headersMap.put("Editor-Version", vscode_version);
headersMap.put("Editor-Plugin-Version", "copilot-chat/0.11.1");
headersMap.put("Editor-Plugin-Version", "copilot-chat/" + copilot_chat_version);
headersMap.put("Openai-Organization", "github-copilot");
headersMap.put("Copilot-Integration-Id", "vscode-chat");
headersMap.put("Openai-Intent", "conversation-panel");
headersMap.put("User-Agent", "GitHubCopilotChat/0.11.1");
headersMap.put("User-Agent", "GitHubCopilotChat/" + copilot_chat_version);
}
/**
@@ -1124,7 +1151,7 @@ public class chatController {
Thread.sleep(sleep_time);
}
}
log.info("使用模型:" + model + ",字符间隔时间:" + sleep_time + "ms响应" + resp);
log.info("使用模型:" + model + "vscode_version" + vscode_version + "copilot_chat_version" + copilot_chat_version + "字符间隔时间:" + sleep_time + "ms响应" + resp);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Thread was interrupted", e);
@@ -1173,6 +1200,4 @@ public class chatController {
throw new RuntimeException(e);
}
}
}

View File

@@ -4,14 +4,17 @@ package com.gpt4.copilot;
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;
import org.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
@@ -28,7 +31,10 @@ import java.nio.file.Paths;
@EnableScheduling
@SpringBootApplication
public class copilotApplication {
public static void main(String[] args) {
private static final String VS_CODE_API_URL = "https://api.github.com/repos/microsoft/vscode/releases/latest";
private static final String VS_CODE_CHAT_URL = "https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery";
public static void main(String[] args) throws Exception {
String configFilePath = System.getProperty("user.dir") + File.separator + "config.json";
systemSetting config = loadConfig(configFilePath);
setSystemProperties(config);
@@ -109,28 +115,108 @@ public class copilotApplication {
System.setProperty("server.servlet.context-path", config.getPrefix());
}
private static void printStartupMessage(systemSetting config) {
System.out.println("\n-------------------------------------------------------");
System.out.println("------原神gpt4-copilot-java-native v0.0.4启动成功--------");
System.out.println("* 采用graalvm打包运行内存大幅度减小");
System.out.println("* 增加自定义获取token渠道");
System.out.println("* 增加自定义/self/*接口");
System.out.println("* 增加反代/copilot_internal/v2/token接口");
System.out.println("URL地址http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + "");
System.out.println("-------------------------------------------------------");
System.out.println("---------------------配置说明---------------------------");
System.out.println("serverPort" + config.getServerPort());
System.out.println("prefix" + config.getPrefix());
System.out.println("gpt3_sleepTime" + chatController.getGpt3_sleepTime());
System.out.println("gpt4_sleepTime" + chatController.getGpt4_sleepTime());
System.out.println("get_token_url" + chatController.getGet_token_url());
System.out.println("vscode_version" + chatController.getVscode_version());
System.out.println("maxPoolSize" + chatController.getMaxPoolSize());
System.out.println("password" + chatController.getPassword());
System.out.println("gpt4-copilot-java-native初始化接口成功");
System.out.println("-------------------------------------------------------");
public static String getLatestVSCodeVersion() {
try {
URL url = new URL(VS_CODE_API_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/vnd.github.v3+json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
StringBuilder response = new StringBuilder();
while ((output = br.readLine()) != null) {
response.append(output);
}
conn.disconnect();
JSONObject jsonObject = new JSONObject(response.toString());
return jsonObject.getString("tag_name");
} catch (IOException e) {
throw new RuntimeException(e);
} catch (RuntimeException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public static String getLatestExtensionVersion(String publisher, String name) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(VS_CODE_CHAT_URL).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json;api-version=6.1-preview.1");
conn.setDoOutput(true);
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("filters", new JSONArray().put(new JSONObject().put("criteria", new JSONArray().put(new JSONObject().put("filterType", 7).put("value", publisher + "." + name)))));
jsonRequest.put("flags", 870);
OutputStream os = conn.getOutputStream();
os.write(jsonRequest.toString().getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder response = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
response.append(output);
}
conn.disconnect();
JSONObject jsonResponse = new JSONObject(response.toString());
return jsonResponse.getJSONArray("results").getJSONObject(0).getJSONArray("extensions").getJSONObject(0).getJSONArray("versions").getJSONObject(0).getString("version");
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
@Scheduled(cron = "0 0 3 1/3 * ?")
private static void updateLatestVersion() {
try {
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);
}
String parent = chatController.selectFile();
// 读取 JSON 文件内容
String jsonContent = new String(Files.readAllBytes(Paths.get(parent)));
JSONObject jsonObject = new JSONObject(jsonContent);
jsonObject.put("vscode_version", latestVersion);
jsonObject.put("copilot_chat_version", latestChatVersion);
// 将修改后的 JSONObject 转换为格式化的 JSON 字符串
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("======================================================");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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("gpt4-copilot-java 初始化接口成功!");
System.out.println("======================================================");
System.out.println("******原神gpt4-copilot-java-native v0.0.5启动成功******");
System.out.println("* 采用graalvm打包运行内存大幅度减小");
System.out.println("* jar包和二进制文件兼容基本所有系统");
System.out.println("* 每隔三天自动更新vscode版本和copilot-chat版本");
System.out.println("URL地址http://0.0.0.0:" + config.getServerPort() + config.getPrefix() + "");
System.out.println("======================================================");
}
}

View File

@@ -46,6 +46,11 @@ public class systemSetting {
*/
private String vscode_version;
/**
* copilot_chat_version
*/
private String copilot_chat_version;
/**
* maxPoolSize
*/

Binary file not shown.