Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
PandoraNext-tokensTool v 0.6.5版本
Browse files Browse the repository at this point in the history
1.优化copilot接口的流式输出
2.修复历史问题bug,优化代码,优化前端
  • Loading branch information
Yanyutin753 committed Jan 11, 2024
1 parent 3ea9e88 commit b5fb7c5
Show file tree
Hide file tree
Showing 44 changed files with 71 additions and 71 deletions.
11 changes: 0 additions & 11 deletions rearServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- json-->
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,15 +22,16 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -53,6 +56,7 @@ 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;
private static final Logger logger = LoggerFactory.getLogger(chatController.class);
private static HashMap<String, Integer> modelsUsage;

static {
Expand All @@ -68,6 +72,9 @@ public class chatController {

@Value("${copilot_interface}")
private boolean copilot_interface;
private ExecutorService executor = new ThreadPoolExecutor(0, 1000,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());

private static String generateMachineId() {
try {
Expand Down Expand Up @@ -167,7 +174,6 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
}
}


/**
* 请求体不是json 会报Request body is missing or not in JSON format
* Authorization token缺失 会报Authorization header is missing
Expand Down Expand Up @@ -303,7 +309,6 @@ private String getCoCoToken(String apiKey) throws IOException {
}
}


@GetMapping("/v1/models")
public JsonNode models() throws JsonProcessingException {
String jsonString = models;
Expand Down Expand Up @@ -333,31 +338,46 @@ private void addHeader(Map<String, String> headersMap, String chat_token) {
headersMap.put("User-Agent", "GitHubCopilotChat/0.11.1");
}


private void outPutChat(HttpServletResponse response, Response resp, Conversation conversation) {
try {
Boolean isStream = conversation.getStream();
if (isStream != null && isStream) {
response.setContentType("text/event-stream; charset=UTF-8");
private void outPutChat(HttpServletResponse response, Response resp, Conversation conversation) throws IOException {
Boolean isStream = conversation.getStream();
int one_messageByte;
int sleep_time;
if (isStream != null && isStream) {
if (!conversation.getModel().contains("gpt-4")) {
one_messageByte = 2048;
sleep_time = 0;
} else {
response.setContentType("application/json; charset=utf-8");
one_messageByte = 128;
sleep_time = 25;
}
// 输出流
ServletOutputStream out = response.getOutputStream();
// 输入流
InputStream in = resp.body().byteStream();
response.setContentType("text/event-stream; charset=UTF-8");
} else {
one_messageByte = 8192;
sleep_time = 0;
response.setContentType("application/json; charset=utf-8");
}
try {
OutputStream out = new BufferedOutputStream(response.getOutputStream());
InputStream in = new BufferedInputStream(resp.body().byteStream());
// 一次拿多少数据 迭代循环
byte[] buffer = new byte[4096];
byte[] buffer = new byte[one_messageByte];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
out.flush();
try {
Thread.sleep(sleep_time);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}

}


public OkHttpClient productClient(Integer timeout) {
try {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class tokensToolApplication {

public static void main(String[] args) {
log.info("--------------------------------------------------------------\n" +
"PandoraNext-tokensTool v 0.6.4版本\n" +
"1.新增share_token可直接转入one-api,并可自定义模型重定向\n" +
"PandoraNext-tokensTool v 0.6.5版本\n" +
"1.优化copilot接口的流式输出\n" +
"2.修复历史问题bug,优化代码,优化前端\n" +
"--------------------------------------------------------------\n");
Instant instant = Instant.now();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5fb7c5

Please sign in to comment.