Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyStrategyFunction的apply方法增加request参数 #265

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.unfbx.chatgpt.function;

import cn.hutool.core.util.RandomUtil;
import okhttp3.Request;

import java.util.List;

Expand All @@ -13,7 +14,7 @@
public class KeyRandomStrategy implements KeyStrategyFunction<List<String>, String> {

@Override
public String apply(List<String> apiKeys) {
public String apply(List<String> apiKeys, Request request) {
return RandomUtil.randomEle(apiKeys);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.unfbx.chatgpt.function;

import okhttp3.Request;

import java.util.function.Function;

/**
Expand All @@ -19,6 +21,6 @@ public interface KeyStrategyFunction<T, R> {
* @param t the function argument
* @return the function result
*/
R apply(T t);
R apply(T t, Request request);

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DefaultOpenAiAuthInterceptor(Map warringConfig) {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
return chain.proceed(auth(super.getKey(), original));
return chain.proceed(auth(super.getKey(original), original));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public DynamicKeyOpenAiAuthInterceptor(Map warringConfig) {

@Override
public Response intercept(Chain chain) throws IOException {
String key = getKey();
Request original = chain.request();
String key = getKey(original);
Request request = this.auth(key, original);
Response response = chain.proceed(request);
if (!response.isSuccessful() && response.body() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public abstract class OpenAiAuthInterceptor implements Interceptor {
*
* @return key
*/
public final String getKey() {
public final String getKey(Request request) {
if (CollectionUtil.isEmpty(apiKey)) {
this.noHaveActiveKeyWarring();
throw new BaseException(CommonError.NO_ACTIVE_API_KEYS);
}
return keyStrategy.apply(apiKey);
return keyStrategy.apply(apiKey, request);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/unfbx/chatgpt/FirstKeyStrategy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.unfbx.chatgpt;

import com.unfbx.chatgpt.function.KeyStrategyFunction;
import okhttp3.Request;

import java.util.List;

Expand All @@ -19,7 +20,7 @@ public class FirstKeyStrategy implements KeyStrategyFunction<List<String>, Strin
* @return
*/
@Override
public String apply(List<String> keys) {
public String apply(List<String> keys, Request request) {
return keys.get(0);
}
}