Skip to content

Commit 1faf84e

Browse files
committed
(improvement)(common|headless|chat|auth) 鉴权优化与召回优化
1 修复生成的用户token 一生成就失效的问题 2 如果用户设置的token ,需校验是否数据库存在,因为用户可设置一年的token 有泄露风险 3 结果解析优化, 去除不可以解析的情况,解析问题需要改写后的问, 4 召回样例,用相似度,保住至少有一个样例是高相似度的 5 数据集召回,填加完全匹配格式筛选逻辑
1 parent 7e6639d commit 1faf84e

File tree

10 files changed

+36
-23
lines changed

10 files changed

+36
-23
lines changed

auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/adaptor/DefaultUserAdaptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.beans.BeanUtils;
2020

2121
import java.sql.Timestamp;
22+
import java.util.Date;
2223
import java.util.List;
2324
import java.util.Optional;
2425
import java.util.Set;
@@ -223,8 +224,8 @@ public UserToken generateToken(String name, String userName, long expireTime) {
223224
userDO.getEmail(), userDO.getPassword(), userDO.getIsAdmin());
224225

225226
// 使用令牌名称作为生成key ,这样可以区分正常请求和api 请求,api 的令牌失效时间很长,需考虑令牌泄露的情况
226-
String token =
227-
tokenService.generateToken(UserWithPassword.convert(userWithPassword),"SysDbToken:"+name, (new Date().getTime() + expireTime));
227+
String token = tokenService.generateToken(UserWithPassword.convert(userWithPassword),
228+
"SysDbToken:" + name, (new Date().getTime() + expireTime));
228229
UserTokenDO userTokenDO = saveUserToken(name, userName, token, expireTime);
229230
return convertUserToken(userTokenDO);
230231
}

auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/persistence/repository/UserRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface UserRepository {
2121

2222
UserTokenDO getUserToken(Long tokenId);
2323

24+
UserTokenDO getUserTokenByName(String tokenName);
25+
2426
void deleteUserTokenByName(String userName);
2527

2628
void deleteUserToken(Long tokenId);

auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/persistence/repository/impl/UserRepositoryImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public UserTokenDO getUserToken(Long tokenId) {
6565
return userTokenDOMapper.selectById(tokenId);
6666
}
6767

68+
@Override
69+
public UserTokenDO getUserTokenByName(String tokenName) {
70+
QueryWrapper<UserTokenDO> queryWrapper = new QueryWrapper<>();
71+
queryWrapper.lambda().eq(UserTokenDO::getName, tokenName);
72+
return userTokenDOMapper.selectOne(queryWrapper);
73+
}
74+
6875
@Override
6976
public void deleteUserTokenByName(String userName) {
7077
QueryWrapper<UserTokenDO> queryWrapper = new QueryWrapper<>();

auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/utils/TokenService.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ private Optional<Claims> getClaims(String token, HttpServletRequest request) {
9494

9595
public Optional<Claims> getClaims(String token, String appKey) {
9696
try {
97-
if(StringUtils.isNotBlank(appKey)&&appKey.startsWith("SysDbToken:")) {// 如果是配置的长期令牌,需校验数据库是否存在该配置
97+
if (StringUtils.isNotBlank(appKey) && appKey.startsWith("SysDbToken:")) {// 如果是配置的长期令牌,需校验数据库是否存在该配置
9898
UserRepository userRepository = ContextUtils.getBean(UserRepository.class);
99-
UserTokenDO dbToken= userRepository.getUserTokenByName(appKey.substring("SysDbToken:".length()));
100-
if(dbToken==null||!dbToken.getToken().equals(token.replace("Bearer ",""))) {
99+
UserTokenDO dbToken =
100+
userRepository.getUserTokenByName(appKey.substring("SysDbToken:".length()));
101+
if (dbToken == null || !dbToken.getToken().equals(token.replace("Bearer ", ""))) {
101102
throw new AccessException("Token does not exist :" + appKey);
102103
}
103104
}
@@ -133,14 +134,14 @@ private String getTokenSecret(String appKey) {
133134
Map<String, String> appKeyToSecretMap = authenticationConfig.getAppKeyToSecretMap();
134135
String secret = appKeyToSecretMap.get(appKey);
135136
if (StringUtils.isBlank(secret)) {
136-
if(StringUtils.isNotBlank(appKey)&&appKey.startsWith("SysDbToken:")) { // 是配置的长期令牌
137-
String realAppKey=appKey.substring("SysDbToken:".length());
138-
String tmp = "WIaO9YRRVt+7QtpPvyWsARFngnEcbaKBk783uGFwMrbJBaochsqCH62L4Kijcb0sZCYoSsiKGV/zPml5MnZ3uQ==";
139-
if(tmp.length()<=realAppKey.length()) {
137+
if (StringUtils.isNotBlank(appKey) && appKey.startsWith("SysDbToken:")) { // 是配置的长期令牌
138+
String realAppKey = appKey.substring("SysDbToken:".length());
139+
String tmp =
140+
"WIaO9YRRVt+7QtpPvyWsARFngnEcbaKBk783uGFwMrbJBaochsqCH62L4Kijcb0sZCYoSsiKGV/zPml5MnZ3uQ==";
141+
if (tmp.length() <= realAppKey.length()) {
140142
return realAppKey;
141-
}
142-
else{
143-
return realAppKey+tmp.substring(realAppKey.length());
143+
} else {
144+
return realAppKey + tmp.substring(realAppKey.length());
144145
}
145146
}
146147
throw new AccessException("get secret from appKey failed :" + appKey);

chat/server/src/main/java/com/tencent/supersonic/chat/server/processor/execute/DataInterpretProcessor.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public boolean accept(ExecuteContext executeContext) {
4747
Agent agent = executeContext.getAgent();
4848
ChatApp chatApp = agent.getChatAppConfig().get(APP_KEY);
4949
return Objects.nonNull(chatApp) && chatApp.isEnable()
50-
&& StringUtils.isNotBlank(executeContext.getResponse().getTextResult()) // 如果都没结果,则无法处理
51-
&& StringUtils.isBlank(executeContext.getResponse().getTextSummary()); // 如果已经有汇总的结果了,无法再次处理
50+
&& StringUtils.isNotBlank(executeContext.getResponse().getTextResult()) // 如果都没结果,则无法处理
51+
&& StringUtils.isBlank(executeContext.getResponse().getTextSummary()); // 如果已经有汇总的结果了,无法再次处理
5252
}
5353

5454
@Override
@@ -59,10 +59,11 @@ public void process(ExecuteContext executeContext) {
5959

6060
Map<String, Object> variable = new HashMap<>();
6161
String question = executeContext.getResponse().getTextResult();// 结果解析应该用改写的问题,因为改写的内容信息量更大
62-
if(executeContext.getParseInfo().getProperties()!=null&&
63-
executeContext.getParseInfo().getProperties().containsKey("CONTEXT")){
64-
Map<String,Object> context = (Map<String, Object>) executeContext.getParseInfo().getProperties().get("CONTEXT");
65-
if(context.get("queryText")!=null&&"".equals(context.get("queryText"))){
62+
if (executeContext.getParseInfo().getProperties() != null
63+
&& executeContext.getParseInfo().getProperties().containsKey("CONTEXT")) {
64+
Map<String, Object> context = (Map<String, Object>) executeContext.getParseInfo()
65+
.getProperties().get("CONTEXT");
66+
if (context.get("queryText") != null && "".equals(context.get("queryText"))) {
6667
question = context.get("queryText").toString();
6768
}
6869
}

common/src/main/java/com/hankcs/hanlp/LoadRemoveService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public List removeNatures(List value, Set<Long> modelIdOrDataSetIds) {
2121
List<String> resultList = new ArrayList<>(value);
2222
if (!CollectionUtils.isEmpty(modelIdOrDataSetIds)) {
2323
resultList.removeIf(nature -> {
24-
if (Objects.isNull(nature)||!nature.startsWith("_")) { // 系统的字典是以 _ 开头的, 过滤因引用外部字典导致的异常
24+
if (Objects.isNull(nature) || !nature.startsWith("_")) { // 系统的字典是以 _ 开头的,
25+
// 过滤因引用外部字典导致的异常
2526
return false;
2627
}
2728
Long id = getId(nature);

common/src/main/java/com/tencent/supersonic/common/pojo/Text2SQLExemplar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public class Text2SQLExemplar implements Serializable {
2323

2424
private String sql;
2525

26-
protected double similarity; // 传递相似度,可以作为样本筛选的依据
26+
protected double similarity; // 传递相似度,可以作为样本筛选的依据
2727
}

common/src/main/java/com/tencent/supersonic/common/service/impl/ExemplarServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public List<Text2SQLExemplar> recallExemplars(String collection, String query, i
7272
embeddingService.retrieveQuery(collection, retrieveQuery, num);
7373
results.forEach(ret -> {
7474
ret.getRetrieval().forEach(r -> {
75-
Text2SQLExemplar tmp = //传递相似度,可以作为样本筛选的依据
75+
Text2SQLExemplar tmp = // 传递相似度,可以作为样本筛选的依据
7676
JsonUtil.mapToObject(r.getMetadata(), Text2SQLExemplar.class);
7777
tmp.setSimilarity(r.getSimilarity());
7878
exemplars.add(tmp);

headless/api/src/main/java/com/tencent/supersonic/headless/api/pojo/SemanticParseInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public int compare(SemanticParseInfo o1, SemanticParseInfo o2) {
6666
DataSetMatchResult mr2 = getDataSetMatchResult(o2.getElementMatches());
6767

6868
double difference = mr1.getMaxDatesetSimilarity() - mr2.getMaxDatesetSimilarity();
69-
if (Math.abs(difference) < 0.0005) { // 看完全匹配的个数,实践证明,可以用户输入规范后,该逻辑具有优势
69+
if (Math.abs(difference) < 0.0005) { // 看完全匹配的个数,实践证明,可以用户输入规范后,该逻辑具有优势
7070
if (!o1.getDataSetId().equals(o2.getDataSetId())) {
7171
List<SchemaElementMatch> elementMatches1 = o1.getElementMatches().stream()
7272
.filter(e -> e.getSimilarity() == 1).collect(Collectors.toList());

headless/chat/src/main/java/com/tencent/supersonic/headless/chat/parser/llm/PromptHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public List<List<Text2SQLExemplar>> getFewShotExemplars(LLMReq llmReq) {
4949
// use random collection of exemplars for each self-consistency inference
5050
for (int i = 0; i < selfConsistencyNumber; i++) {
5151
List<Text2SQLExemplar> shuffledList = new ArrayList<>(exemplars);
52-
List<Text2SQLExemplar> same = shuffledList.stream() // 相似度极高的话,先找出来
52+
List<Text2SQLExemplar> same = shuffledList.stream() // 相似度极高的话,先找出来
5353
.filter(e -> e.getSimilarity() > 0.989).collect(Collectors.toList());
5454
List<Text2SQLExemplar> noSame = shuffledList.stream()
5555
.filter(e -> e.getSimilarity() <= 0.989).collect(Collectors.toList());

0 commit comments

Comments
 (0)