对chat接口的模型进行重定向,减少潜在的风险

This commit is contained in:
Yanyutin753
2024-04-13 01:18:12 +08:00
parent 66fe458e9f
commit abaf8437e1
7 changed files with 50 additions and 24 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
e -i 66fe458e9ff3b7072252b455b08ac5c3076591bc^

View File

@@ -506,7 +506,7 @@ public class ChatController {
Map<String, String> headersMap = new HashMap<>(); Map<String, String> headersMap = new HashMap<>();
//添加头部 //添加头部
addHeader(headersMap, chat_token, apiKey); 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); Request streamRequest = getPrompt(conversation, model, headersMap);
try (Response resp = client.newCall(streamRequest).execute()) { try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) { if (!resp.isSuccessful()) {
@@ -535,6 +535,20 @@ public class ChatController {
return getObjectResponseEntity(response, future); 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 * 请求体不是json 会报Request body is missing or not in JSON format
* Authorization token缺失 会报Authorization header is missing * Authorization token缺失 会报Authorization header is missing
@@ -587,7 +601,7 @@ public class ChatController {
Map<String, String> headersMap = new HashMap<>(); Map<String, String> headersMap = new HashMap<>();
//添加头部 //添加头部
addHeader(headersMap, chat_token, apiKey); 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); Request streamRequest = getPrompt(conversation, model, headersMap);
try (Response resp = client.newCall(streamRequest).execute()) { try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) { if (!resp.isSuccessful()) {
@@ -645,7 +659,7 @@ public class ChatController {
* @param conversation * @param conversation
* @throws IOException * @throws IOException
*/ */
private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Conversation conversation) throws IllegalArgumentException { private String[] extractApiKeyAndRequestUrl(String authorizationHeader, Object conversation) throws IllegalArgumentException {
if (conversation == null) { if (conversation == null) {
throw new IllegalArgumentException("Request body is missing or not in JSON format"); throw new IllegalArgumentException("Request body is missing or not in JSON format");
} }
@@ -712,7 +726,7 @@ public class ChatController {
Map<String, String> headersMap = new HashMap<>(); Map<String, String> headersMap = new HashMap<>();
//添加头部 //添加头部
addHeader(headersMap, chat_token, apiKey); 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); Request streamRequest = getPrompt(conversation, model, headersMap);
try (Response resp = client.newCall(streamRequest).execute()) { try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) { if (!resp.isSuccessful()) {
@@ -792,6 +806,22 @@ public class ChatController {
} }
} }
private Request getEmdPrompt(Object conversation,
Map<String, String> 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 请求 * ghu/gho 请求
* 请求体不是json 会报Request body is missing or not in JSON format * 请求体不是json 会报Request body is missing or not in JSON format
@@ -808,7 +838,7 @@ public class ChatController {
@PostMapping(value = "/v1/embeddings") @PostMapping(value = "/v1/embeddings")
public ResponseEntity<Object> coPilotEmbeddings(HttpServletResponse response, public ResponseEntity<Object> coPilotEmbeddings(HttpServletResponse response,
HttpServletRequest request, HttpServletRequest request,
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) { @org.springframework.web.bind.annotation.RequestBody Object conversation) {
String header = request.getHeader("Authorization"); String header = request.getHeader("Authorization");
String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null; String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null;
// 异步处理 // 异步处理
@@ -844,11 +874,7 @@ public class ChatController {
Map<String, String> headersMap = new HashMap<>(); Map<String, String> headersMap = new HashMap<>();
//添加头部 //添加头部
addHeader(headersMap, chat_token, apiKey); addHeader(headersMap, chat_token, apiKey);
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); Request streamRequest = getEmdPrompt(conversation, headersMap);
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();
try (Response resp = client.newCall(streamRequest).execute()) { try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) { if (!resp.isSuccessful()) {
if (resp.code() == 429) { if (resp.code() == 429) {
@@ -908,7 +934,7 @@ public class ChatController {
@PostMapping(value = "/cocopilot/v1/embeddings") @PostMapping(value = "/cocopilot/v1/embeddings")
public ResponseEntity<Object> coCoPilotEmbeddings(HttpServletResponse response, public ResponseEntity<Object> coCoPilotEmbeddings(HttpServletResponse response,
HttpServletRequest request, HttpServletRequest request,
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) { @org.springframework.web.bind.annotation.RequestBody Object conversation) {
String header = request.getHeader("Authorization"); String header = request.getHeader("Authorization");
String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null; String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null;
// 异步处理 // 异步处理
@@ -944,11 +970,7 @@ public class ChatController {
Map<String, String> headersMap = new HashMap<>(); Map<String, String> headersMap = new HashMap<>();
//添加头部 //添加头部
addHeader(headersMap, chat_token, apiKey); addHeader(headersMap, chat_token, apiKey);
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); Request streamRequest = getEmdPrompt(conversation, headersMap);
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();
try (Response resp = client.newCall(streamRequest).execute()) { try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) { if (!resp.isSuccessful()) {
if (resp.code() == 429) { if (resp.code() == 429) {
@@ -993,7 +1015,7 @@ public class ChatController {
@PostMapping(value = "/self/v1/embeddings") @PostMapping(value = "/self/v1/embeddings")
public ResponseEntity<Object> selfEmbeddings(HttpServletResponse response, public ResponseEntity<Object> selfEmbeddings(HttpServletResponse response,
HttpServletRequest request, HttpServletRequest request,
@org.springframework.web.bind.annotation.RequestBody Conversation conversation) { @org.springframework.web.bind.annotation.RequestBody Object conversation) {
String header = request.getHeader("Authorization"); String header = request.getHeader("Authorization");
String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null; String authorizationHeader = (header != null && !header.trim().isEmpty()) ? header.trim() : null;
// 异步处理 // 异步处理
@@ -1022,11 +1044,7 @@ public class ChatController {
Map<String, String> headersMap = new HashMap<>(); Map<String, String> headersMap = new HashMap<>();
//添加头部 //添加头部
addHeader(headersMap, chat_token, apiKey); addHeader(headersMap, chat_token, apiKey);
String json = com.alibaba.fastjson2.JSON.toJSONString(conversation); Request streamRequest = getEmdPrompt(conversation, headersMap);
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();
try (Response resp = client.newCall(streamRequest).execute()) { try (Response resp = client.newCall(streamRequest).execute()) {
if (!resp.isSuccessful()) { if (!resp.isSuccessful()) {
if (resp.code() == 429) { if (resp.code() == 429) {
@@ -1054,7 +1072,7 @@ public class ChatController {
} }
public Object againEmbeddings(HttpServletResponse response, public Object againEmbeddings(HttpServletResponse response,
@org.springframework.web.bind.annotation.RequestBody Conversation conversation, @org.springframework.web.bind.annotation.RequestBody Object conversation,
String token, String token,
String apiKey) { String apiKey) {
try { try {
@@ -1283,6 +1301,11 @@ public class ChatController {
*/ */
private String saveMadchineId(String apiKey) { private String saveMadchineId(String apiKey) {
try { try {
if(machineIdList.containsKey(apiKey)){
String machineId = machineIdList.get(apiKey);
log.info("机械码读取成功!对应的机械码为:" + machineId);
return machineId;
}
String machineId = generateMachineId(); String machineId = generateMachineId();
log.info("机械码生成成功!对应的机械码为:" + machineId); log.info("机械码生成成功!对应的机械码为:" + machineId);
machineIdList.put(apiKey, machineId); machineIdList.put(apiKey, machineId);

View File

@@ -51,7 +51,7 @@ public class copilotApplication {
if (!jsonFile.exists() || jsonFile.length() == 0){ if (!jsonFile.exists() || jsonFile.length() == 0){
try { try {
if (!jsonFile.exists()) { if (!jsonFile.exists()) {
// 创建文件machineIdList.json // 创建文件config.json
Files.createFile(jsonFilePath); Files.createFile(jsonFilePath);
} }
// 往 config.json 文件中添加一个空数组,防止重启报错 // 往 config.json 文件中添加一个空数组,防止重启报错
@@ -298,6 +298,7 @@ public class copilotApplication {
System.out.println("gpt4-copilot-java 初始化接口成功!"); System.out.println("gpt4-copilot-java 初始化接口成功!");
System.out.println("======================================================"); System.out.println("======================================================");
System.out.println("******原神gpt4-copilot-java-native v0.1.3启动成功******"); System.out.println("******原神gpt4-copilot-java-native v0.1.3启动成功******");
System.out.println("* 对chat接口的模型进行重定向减少潜在的风险");
System.out.println("* 使用ConcurrentHashMap粗略的对于每个密钥按每分钟进行限速"); System.out.println("* 使用ConcurrentHashMap粗略的对于每个密钥按每分钟进行限速");
System.out.println("* 新增环境变量用于对gpt-4*等模型进行系统prompt提示"); System.out.println("* 新增环境变量用于对gpt-4*等模型进行系统prompt提示");
System.out.println("* 新增url|apikey形式传入/self/*接口,用于自定义地址和密钥"); System.out.println("* 新增url|apikey形式传入/self/*接口,用于自定义地址和密钥");

Binary file not shown.