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.0版本
Browse files Browse the repository at this point in the history
PandoraNext-tokensTool v 0.6.0版本
1.新增修改pool_token功能
2.新增自定义批量刷新,删除Token。
3.新增查看token里的share_token用量
4.新增查看tokensTool转发接口的每日模型用量。
5.新增自定义prefix,可以自行添加。
6.新增检查到token过期之后,自动停止分享。
7.修改之前的历史遗留bug,美化界面。
  • Loading branch information
Yanyutin753 committed Jan 5, 2024
1 parent f5eff71 commit 411f949
Show file tree
Hide file tree
Showing 85 changed files with 323 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

import com.tokensTool.pandoraNext.interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${server.servlet.context-path}")
private String profix;

@Autowired
private LoginCheckInterceptor loginCheckInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/api/**");
registry.addInterceptor(loginCheckInterceptor).addPathPatterns(profix+"/api/**");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ public Result deleteToken(@RequestBody token token) {
}
}

/**
* @return 通过token用户名,删除(tokens.json)文件里的值
* @boby token
*/
@Log
@PutMapping("deleteTokenList")
public Result deleteTokenList(@RequestBody List<token> tokens) {
try {
Integer count = 0;
for (token token : tokens) {
String res = apiService.deleteToken(token);
if (res.contains("成功")) {
count++;
}
}
return Result.success("删除成功:" + count + ",失败:" + (tokens.size() - count));
} catch (Exception e) {
e.printStackTrace();
return Result.error("删除失败");
}
}

/**
* @return 通过访问restart,重启PandoraNext服务
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @author Yangyang
* @create 2023-11-11 18:19
Expand All @@ -32,6 +34,7 @@ public class autoTokenController {
* @return "更新成功" or "更新失败"
* @throws Exception
*/
@Log
@Scheduled(cron = "0 0 3 * * ?")
public void toUpdateToken() {
try {
Expand All @@ -52,6 +55,7 @@ public void toUpdateToken() {
*
* @return
*/
@Log
@GetMapping("updateAllToken")
public Result toUpdateAllToken() {
try {
Expand Down Expand Up @@ -91,8 +95,7 @@ public Result toUpdateToken(@RequestBody token token) {
}

/**
* 自动更新指定用户名的session
*
* 自动更新指定用户名的session或refresh
* @return "更新成功" or "刷新Token失败,请尝重新刷新!”
* @throws Exception
*/
Expand All @@ -109,4 +112,32 @@ public Result toUpdateSessionToken(@RequestBody token token) {
}
return Result.error("刷新失败,请尝重新刷新或检查proxy的url填写是否正确!");
}

/**
* 自动更新指定用户名的session或refresh组
* @return "更新成功" or "刷新Token失败,请尝重新刷新!”
* @throws Exception
*/
@Log
@PostMapping(value = "/updateSessionTokenList", consumes = "application/json")
public Result toUpdateSessionTokenList(@RequestBody List<token> tokens) {
try {
int count = 0;
for(token token : tokens) {
if(token.isSetPoolToken()){
token resToken = apiService.updateSession(token);
if (resToken != null) {
count++;
}
}
else{
count++;
}
}
return Result.success("刷新成功:" + count + ",失败:" + (tokens.size() - count));
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("刷新失败,请尝重新刷新或检查proxy的url填写是否正确!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tokensTool.pandoraNext.chat.Conversation;
import com.tokensTool.pandoraNext.pojo.Result;
import com.tokensTool.pandoraNext.pojo.modelsUsage;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang.StringUtils;
Expand All @@ -12,6 +14,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -24,9 +27,7 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -38,6 +39,12 @@
@RestController()
public class chatController {

private static HashMap<String, Integer> modelsUsage;

static {
modelsUsage = new HashMap<>();
log.info("初始化ipList成功!");
}
/**
* 缓存cocopilotToken
*/
Expand All @@ -47,11 +54,18 @@ public class chatController {
* 缓存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-0613\",\"object\": \"model\",\"created\": 1686588896,\"owned_by\": \"openai\"},\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" +
Expand Down Expand Up @@ -80,8 +94,7 @@ public class chatController {
" {\"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" +
" {\"id\": \"gpt-4-0314\", \"object\": \"model\", \"created\": 1687882410, \"owned_by\": \"openai\"} ], \"object\": \"list\" }";
" {\"id\": \"davinci\", \"object\": \"model\", \"created\": 1649359874, \"owned_by\": \"openai\"},\n";
private static final String machineId;

static {
Expand Down Expand Up @@ -175,6 +188,7 @@ public Object coPilotConversation(HttpServletResponse response, HttpServletReque
}
// 流式和非流式输出
outPutChat(response, resp);
addModel(conversation);
}
return null;
} catch (IOException e) {
Expand Down Expand Up @@ -245,13 +259,33 @@ public Object coCoPilotConversation(HttpServletResponse response, HttpServletReq
}
// 流式和非流式输出
outPutChat(response, resp);
addModel(conversation);
}
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void addModel(Conversation conversation){
String model = conversation.getModel();
if(modelsUsage.containsKey(model)){
modelsUsage.put(model, modelsUsage.get(model) + 1);
}
else {
modelsUsage.put(model,1);
}
}
@GetMapping(value = "api/modelsUsage")
private Result getModelUsage(){
try {
List<modelsUsage> res = new ArrayList();
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@RestController()
@RequestMapping("/api")
public class loginColltroller {


private static final String LOOPBACK_ADDRESS = "127.0.0.1";
private static final String IPV6_ADDRESS = "0:0:0:0:0:0:0:1";
private static final Integer MAX_REQUESTNUMBER = 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public Result addPoolToken(@RequestBody poolToken poolToken) {
return Result.error("添加失败!");
}

@PostMapping("requirePoolToken")
public Result requirePoolToken(@RequestBody poolToken poolToken) {
try {
String res = poolService.toRequirePoolToken(poolToken);
if (res.contains("成功")) {
return Result.success(res);
} else {
return Result.error(res);
}
} catch (Exception e) {
e.printStackTrace();
}
return Result.error("添加失败!");
}

@PostMapping("deletePoolToken")
public Result deletePoolToken(@RequestBody poolToken poolToken) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.tokensTool.pandoraNext.service.impl.poolServiceImpl;
import com.tokensTool.pandoraNext.service.impl.systemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -22,6 +23,9 @@
@RestController
public class tokenController {

@Value("${server.servlet.context-path}")
private String profix;

@Autowired
private systemServiceImpl systemService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
@Slf4j
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {

@Autowired
private systemServiceImpl systemService;


@Override //目标资源方法运行前运行, 返回true: 放行, 放回false, 不放行
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
systemSetting systemSetting = systemService.selectSetting();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tokensTool.pandoraNext.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author Yangyang
* @create 2024-01-04 19:44
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class modelsUsage {
private String Model;
private Integer usage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class poolToken {
private String oneApi_pandoraUrl;

/**
* 接入oneApi的模型
* 接入oneApi的组别
*/
private String groupChecked;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ public String product_requireToken(token tem) {
String previousToken = nodeToModifyInNew.has("token") ? nodeToModifyInNew.get("token").asText() : null;
// 初始修改相应的值
require_beginToken(tem, nodeToModifyInNew);

if (!previousToken.equals(tem.getToken())
&& tem.isSetPoolToken()) {
// 将修改后的 newObjectNode 写回文件
Expand Down Expand Up @@ -908,6 +907,7 @@ public token autoUpdateSimpleToken(token token) {
}
} else {
token.setCheckSession(false);
token.setShared(false);
String res = product_requireToken(token);
if (res.contains("成功")) {
log.info("已为您禁用该session_token!");
Expand Down Expand Up @@ -1006,12 +1006,12 @@ public String deletePoolToken(String pool_token) {
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
String responseContent = response.body().string();
JSONObject jsonResponse = new JSONObject(responseContent);
if (!response.isSuccessful() && !jsonResponse.has("detail")) {
log.info("Request failed: " + response.body().string().trim());
return null;
}
String responseContent = response.body().string();
JSONObject jsonResponse = new JSONObject(responseContent);
String resPoolToken = jsonResponse.getString("detail");
if (response.code() == 200 && resPoolToken.length() > 0) {
log.info("注销pool_token成功!");
Expand Down
Loading

0 comments on commit 411f949

Please sign in to comment.