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.3版本
Browse files Browse the repository at this point in the history
PandoraNext-tokensTool v 0.6.3版本
1.新增share_token可直接转入one-api
2.新增pool_token列表的批量操作
3.修改copilot查询模型接口的bug
4.修复历史问题bug,优化代码,优化前端
  • Loading branch information
Yanyutin753 committed Jan 8, 2024
1 parent b9355ad commit 60b0435
Show file tree
Hide file tree
Showing 72 changed files with 823 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.tokensTool.pandoraNext.pojo.Result;
import com.tokensTool.pandoraNext.pojo.token;
import com.tokensTool.pandoraNext.service.impl.poolServiceImpl;
import com.tokensTool.pandoraNext.service.impl.shareServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -26,6 +27,9 @@ public class autoTokenController {
@Autowired
private poolServiceImpl poolService;

@Autowired
private shareServiceImpl shareService;

/**
* 自动更新access_Token和share_token
* 更换tokens.json里存储的Tokens
Expand All @@ -35,10 +39,10 @@ public class autoTokenController {
* @throws Exception
*/
@Log
@Scheduled(cron = "0 0/6 * * * ?")
@Scheduled(cron = "0 0 */6 * * ?")
public void toUpdateToken() {
try {
log.info("开始自动更新Token..........................");
log.info("开始自动检查更新refresh_token,session_token,生成和刷新share_token,pool_token..........................");
toUpdateAllToken();
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -51,6 +55,7 @@ public void toUpdateToken() {
* 失效变黄
* 并更新所有access_token和share_token
* 并重新组成pool_token
*
* @return
*/
@Log
Expand All @@ -61,9 +66,10 @@ public Result toUpdateAllToken() {
if (res.contains("生成Token成功")) {
try {
String s = poolService.refreshAllTokens();
return Result.success(res + "\n" + s);
String s1 = shareService.refreshAllToken();
return Result.success(res + s + s1);
} catch (Exception e) {
return Result.success(res + "\n但是自动更新pool_token失败,请手动点击一键全更新pool_token!");
return Result.success(res + "<br>但是自动更新pool_token和oneApi里的share_token失败");
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -94,6 +100,7 @@ public Result toUpdateToken(@RequestBody token token) {

/**
* 自动更新指定用户名的session或refresh
*
* @return "更新成功" or "刷新Token失败,请尝重新刷新!”
* @throws Exception
*/
Expand All @@ -113,6 +120,7 @@ public Result toUpdateSessionToken(@RequestBody token token) {

/**
* 自动更新指定用户名的session或refresh组
*
* @return "更新成功" or "刷新Token失败,请尝重新刷新!”
* @throws Exception
*/
Expand All @@ -121,14 +129,13 @@ public Result toUpdateSessionToken(@RequestBody token token) {
public Result toUpdateSessionTokenList(@RequestBody List<token> tokens) {
try {
int count = 0;
for(token token : tokens) {
if(token.isSetPoolToken()){
for (token token : tokens) {
if (token.isSetPoolToken()) {
token resToken = apiService.updateSession(token);
if (resToken != null) {
count++;
}
}
else{
} else {
count++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,25 @@
@RestController()
public class chatController {

private static HashMap<String, Integer> modelsUsage;

static {
modelsUsage = new HashMap<>();
log.info("初始化ipList成功!");
}
/**
* 缓存cocopilotToken
*/
private static final HashMap<String, String> copilotTokenList;

/**
* 缓存copilotToken
*/
private static final HashMap<String, String> coCopilotTokenList;

@Scheduled(cron = "0 0 0 * * ?")
private void clearModelsUsage() {
HashMap<String, Integer> newModelsUsaget = new HashMap<>();
modelsUsage = newModelsUsaget;
log.info("重置modelsUsage成功!");
}

/**
* 模型
*/
private static final String models = "{ \"data\": [ {\"id\": \"text-search-babbage-doc-001\",\"object\": \"model\",\"created\": 1651172509,\"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"gpt-4\", \"object\": \"model\", \"created\": 1687882411, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"babbage\", \"object\": \"model\", \"created\": 1649358449, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"gpt-3.5-turbo-0613\", \"object\": \"model\", \"created\": 1686587434, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"text-babbage-001\", \"object\": \"model\", \"created\": 1649364043, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"gpt-3.5-turbo\", \"object\": \"model\", \"created\": 1677610602, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"gpt-3.5-turbo-1106\", \"object\": \"model\", \"created\": 1698959748, \"owned_by\": \"system\"},\n" +
" {\"id\": \"curie-instruct-beta\", \"object\": \"model\", \"created\": 1649364042, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"gpt-3.5-turbo-0301\", \"object\": \"model\", \"created\": 1677649963, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"gpt-3.5-turbo-16k-0613\", \"object\": \"model\", \"created\": 1685474247, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"text-embedding-ada-002\", \"object\": \"model\", \"created\": 1671217299, \"owned_by\": \"openai-internal\"},\n" +
" {\"id\": \"davinci-similarity\", \"object\": \"model\", \"created\": 1651172509, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"curie-similarity\", \"object\": \"model\", \"created\": 1651172510, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"babbage-search-document\", \"object\": \"model\", \"created\": 1651172510, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"curie-search-document\", \"object\": \"model\", \"created\": 1651172508, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"babbage-code-search-code\", \"object\": \"model\", \"created\": 1651172509, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"ada-code-search-text\", \"object\": \"model\", \"created\": 1651172510, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"text-search-curie-query-001\", \"object\": \"model\", \"created\": 1651172509, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"text-davinci-002\", \"object\": \"model\", \"created\": 1649880484, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"ada\", \"object\": \"model\", \"created\": 1649357491, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"text-ada-001\", \"object\": \"model\", \"created\": 1649364042, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"ada-similarity\", \"object\": \"model\", \"created\": 1651172507, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"code-search-ada-code-001\", \"object\": \"model\", \"created\": 1651172507, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"text-similarity-ada-001\", \"object\": \"model\", \"created\": 1651172505, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"text-davinci-edit-001\", \"object\": \"model\", \"created\": 1649809179, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"code-davinci-edit-001\", \"object\": \"model\", \"created\": 1649880484, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"text-search-curie-doc-001\", \"object\": \"model\", \"created\": 1651172509, \"owned_by\": \"openai-dev\"},\n" +
" {\"id\": \"text-curie-001\", \"object\": \"model\", \"created\": 1649364043, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"curie\", \"object\": \"model\", \"created\": 1649359874, \"owned_by\": \"openai\"},\n" +
" {\"id\": \"davinci\", \"object\": \"model\", \"created\": 1649359874, \"owned_by\": \"openai\"},\n";
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 HashMap<String, Integer> modelsUsage;

static {
modelsUsage = new HashMap<>();
log.info("初始化ipList成功!");
}

static {
copilotTokenList = new HashMap<>();
Expand Down Expand Up @@ -127,6 +89,13 @@ private static String generateMachineId() {
}
}

@Scheduled(cron = "0 0 0 * * ?")
private void clearModelsUsage() {
HashMap<String, Integer> newModelsUsaget = new HashMap<>();
modelsUsage = newModelsUsaget;
log.info("重置modelsUsage成功!");
}

/**
* 请求体不是json 会报Request body is missing or not in JSON format
* Authorization token缺失 会报Authorization header is missing
Expand All @@ -140,7 +109,7 @@ private static String generateMachineId() {
* @throws IOException
*/
@PostMapping(value = "/v1/chat/completions")
public Object coPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation){
public Object coPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
try {
if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
Expand Down Expand Up @@ -210,7 +179,7 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
* @throws IOException
*/
@PostMapping(value = "/cocopilot/v1/chat/completions")
public Object coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation){
public Object coCoPilotConversation(HttpServletResponse response, HttpServletRequest request, @org.springframework.web.bind.annotation.RequestBody Conversation conversation) {
try {
if (conversation == null) {
return new ResponseEntity<>("Request body is missing or not in JSON format", HttpStatus.BAD_REQUEST);
Expand Down Expand Up @@ -266,25 +235,26 @@ public Object coCoPilotConversation(HttpServletResponse response, HttpServletReq
}
}

private void addModel(Conversation conversation){
private void addModel(Conversation conversation) {
String model = conversation.getModel();
if(modelsUsage.containsKey(model)){
if (modelsUsage.containsKey(model)) {
modelsUsage.put(model, modelsUsage.get(model) + 1);
}
else {
modelsUsage.put(model,1);
} else {
modelsUsage.put(model, 1);
}
}

@GetMapping(value = "api/modelsUsage")
private Result getModelUsage(){
private Result getModelUsage() {
try {
List<modelsUsage> res = new ArrayList();
modelsUsage.forEach((key, value) -> res.add(new modelsUsage(key,value)));
modelsUsage.forEach((key, value) -> res.add(new modelsUsage(key, value)));
return Result.success(res);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private String getCopilotToken(String apiKey) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
Expand Down Expand Up @@ -362,7 +332,7 @@ private void addHeader(Map<String, String> headersMap, String chat_token) {
}


private void outPutChat(HttpServletResponse response, Response resp , Conversation conversation) {
private void outPutChat(HttpServletResponse response, Response resp, Conversation conversation) {
try {
Boolean isStream = conversation.getStream();
if (isStream != null && isStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Result requirePoolToken(@RequestBody poolToken poolToken) {
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("添加失败!");
return Result.error("修改失败!");
}

@PostMapping("deletePoolToken")
Expand All @@ -67,6 +67,23 @@ public Result deletePoolToken(@RequestBody poolToken poolToken) {
return Result.error("删除失败!");
}

@PostMapping("deletePoolTokenList")
public Result deletePoolTokenList(@RequestBody List<poolToken> poolTokens) {
try {
int count = 0;
for (poolToken poolToken : poolTokens) {
String res = poolService.deletePoolToken(poolToken);
if (res.contains("成功")) {
count++;
}
}
return Result.success("删除成功:" + count + ",失败:" + (poolTokens.size() - count));
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("删除失败!");
}

@PostMapping("refreshSimplyPoolToken")
public Result refreshSimplyPoolToken(@RequestBody poolToken poolToken) {
try {
Expand All @@ -82,6 +99,23 @@ public Result refreshSimplyPoolToken(@RequestBody poolToken poolToken) {
return Result.error("刷新失败!");
}

@PostMapping("refreshSimplyPoolTokenList")
public Result refreshSimplyPoolTokenList(@RequestBody List<poolToken> poolTokens) {
try {
int count = 0;
for (poolToken poolToken : poolTokens) {
String res = poolService.refreshSimplyToken(poolToken);
if (res.contains("成功")) {
count++;
}
}
return Result.success("刷新成功:" + count + ",失败:" + (poolTokens.size() - count));
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("刷新失败!");
}

@PostMapping("changePoolToken")
public Result changePoolToken(@RequestBody poolToken poolToken) {
try {
Expand All @@ -97,6 +131,23 @@ public Result changePoolToken(@RequestBody poolToken poolToken) {
return Result.error("更换失败!");
}

@PostMapping("changePoolTokenList")
public Result changePoolTokenList(@RequestBody List<poolToken> poolTokens) {
try {
int count = 0;
for (poolToken poolToken : poolTokens) {
String res = poolService.changePoolToken(poolToken);
if (res.contains("成功")) {
count++;
}
}
return Result.success("更换成功:" + count + ",失败:" + (poolTokens.size() - count));
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("更换失败!");
}

@GetMapping("selectPoolToken")
public Result selectPoolToken(@RequestParam("name") String name) {
try {
Expand Down Expand Up @@ -129,6 +180,23 @@ public Result verifySimplyPoolToken(@RequestBody poolToken poolToken) {
}
}

@PostMapping("verifySimplyPoolTokenList")
public Result verifySimplyPoolTokenList(@RequestBody List<poolToken> poolTokens) {
try {
int count = 0;
for (poolToken poolToken : poolTokens) {
String res = poolService.verifySimplyPoolToken(poolToken);
if (res.contains("正常")) {
count++;
}
}
return Result.success("检验请求成功:" + count + ",失败:" + (poolTokens.size() - count));
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("poolToken失效或请求网址失效,请刷新或检查请求网址!");
}

@GetMapping("verifyAllPoolToken")
public Result verifyAllPoolToken() {
try {
Expand Down
Loading

0 comments on commit 60b0435

Please sign in to comment.