/
DynamicKeyOpenAiAuthInterceptor.java
108 lines (98 loc) · 3.68 KB
/
DynamicKeyOpenAiAuthInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.unfbx.chatgpt.interceptor;
import cn.hutool.json.JSONUtil;
import com.unfbx.chatgpt.entity.common.OpenAiResponse;
import com.unfbx.chatgpt.exception.BaseException;
import com.unfbx.chatgpt.exception.CommonError;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 描述:动态处理key的鉴权拦截器
*
* @author https:www.unfbx.com
* @since 2023-04-25
*/
@Getter
@Slf4j
public class DynamicKeyOpenAiAuthInterceptor extends OpenAiAuthInterceptor {
/**
* 账号被封了
*/
private static final String ACCOUNT_DEACTIVATED = "account_deactivated";
/**
* key不正确
*/
private static final String INVALID_API_KEY = "invalid_api_key";
/**
* 请求头处理
*
*/
public DynamicKeyOpenAiAuthInterceptor() {
this.setWarringConfig(null);
}
/**
* 构造方法
*
* @param warringConfig 所有的key都失效后的告警参数配置
*/
public DynamicKeyOpenAiAuthInterceptor(Map warringConfig) {
this.setWarringConfig(warringConfig);
}
@Override
public Response intercept(Chain chain) throws IOException {
String key = getKey();
Request original = chain.request();
Request request = this.auth(key, original);
Response response = chain.proceed(request);
if (!response.isSuccessful() && response.body() != null) {
String errorMsg = response.body().string();
if (response.code() == CommonError.OPENAI_AUTHENTICATION_ERROR.code()
|| response.code() == CommonError.OPENAI_LIMIT_ERROR.code()
|| response.code() == CommonError.OPENAI_SERVER_ERROR.code()) {
OpenAiResponse openAiResponse = JSONUtil.toBean(errorMsg, OpenAiResponse.class);
String errorCode = openAiResponse.getError().getCode();
log.error("--------> 请求openai异常,错误code:{}", errorCode);
log.error("--------> 请求异常:{}", errorMsg);
//账号被封或者key不正确就移除掉
if (ACCOUNT_DEACTIVATED.equals(errorCode) || INVALID_API_KEY.equals(errorCode)) {
super.setApiKey(this.onErrorDealApiKeys(key));
}
throw new BaseException(openAiResponse.getError().getMessage());
}
//非官方定义的错误code
log.error("--------> 请求异常:{}", errorMsg);
OpenAiResponse openAiResponse = JSONUtil.toBean(errorMsg, OpenAiResponse.class);
if (Objects.nonNull(openAiResponse.getError())) {
log.error(openAiResponse.getError().getMessage());
throw new BaseException(openAiResponse.getError().getMessage());
}
throw new BaseException(CommonError.RETRY_ERROR);
}
return response;
}
@Override
protected List<String> onErrorDealApiKeys(String errorKey) {
List<String> apiKey = super.getApiKey().stream().filter(e -> !errorKey.equals(e)).collect(Collectors.toList());
log.error("--------> 当前ApiKey:[{}] 失效了,移除!", errorKey);
return apiKey;
}
/**
* 所有的key都失效后,自定义预警配置
* 不配置直接return
*/
@Override
protected void noHaveActiveKeyWarring() {
log.error("--------> [告警] 没有可用的key!!!");
return;
}
@Override
public Request auth(String key, Request original) {
return super.auth(key, original);
}
}