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

(improvement)(auth) Optimize the code to support configurable token timeout duration, with a default value set to 2 hours. #1077

Merged
merged 1 commit into from
Jun 1, 2024
Merged
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
Expand Up @@ -33,4 +33,6 @@ public class AuthenticationConfig {
@Value("${authentication.app.signature:signature}")
private String signature;

@Value("${authentication.token.timeout:7200000}")
private Long tokenTimeout;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public class UserConstants {
public static final String TOKEN_CREATE_TIME = "token_create_time";

public static final String TOKEN_PREFIX = "Bearer";

public static final Long TOKEN_TIME_OUT = 25920000000L;

public static final String INTERNAL = "internal";


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_CREATE_TIME;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_IS_ADMIN;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_PREFIX;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_TIME_OUT;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_USER_DISPLAY_NAME;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_USER_EMAIL;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_USER_ID;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_USER_NAME;
import static com.tencent.supersonic.auth.api.authentication.constant.UserConstants.TOKEN_USER_PASSWORD;

import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.auth.api.authentication.pojo.UserWithPassword;
Expand Down Expand Up @@ -48,14 +48,13 @@ public String generateToken(UserWithPassword user) {
}

public String generateAdminToken() {
Map<String, Object> claims = new HashMap<>(5);
claims.put(TOKEN_USER_ID, "1");
claims.put(TOKEN_USER_NAME, "admin");
claims.put(TOKEN_USER_PASSWORD, "admin");
claims.put(TOKEN_USER_DISPLAY_NAME, "admin");
claims.put(TOKEN_CREATE_TIME, System.currentTimeMillis());
claims.put(TOKEN_IS_ADMIN, 1);
return generate(claims);
UserWithPassword admin = new UserWithPassword("admin");
admin.setId(1L);
admin.setName("admin");
admin.setPassword("admin");
admin.setDisplayName("admin");
admin.setIsAdmin(1);
return generateToken(admin);
}

public User getUser(HttpServletRequest request) {
Expand Down Expand Up @@ -107,13 +106,15 @@ private String generate(Map<String, Object> claims) {
}

private String toTokenString(Map<String, Object> claims) {
long expiration = Long.parseLong(claims.get(TOKEN_CREATE_TIME) + "") + TOKEN_TIME_OUT;
Long tokenTimeout = authenticationConfig.getTokenTimeout();
long expiration = Long.parseLong(claims.get(TOKEN_CREATE_TIME) + "") + tokenTimeout;
Date expirationDate = new Date(expiration);

SignatureAlgorithm.valueOf(TOKEN_ALGORITHM);
return Jwts.builder()
.setClaims(claims)
.setSubject(claims.get(TOKEN_USER_NAME).toString())
.setExpiration(new Date(expiration))
.setExpiration(expirationDate)
.signWith(SignatureAlgorithm.valueOf(TOKEN_ALGORITHM),
authenticationConfig.getTokenSecret().getBytes(StandardCharsets.UTF_8))
.compact();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;

public class FullOpenAiChatModel implements ChatLanguageModel, TokenCountEstimator {

private final OpenAiClient client;
private final String modelName;
private final Double temperature;
Expand All @@ -34,45 +35,45 @@ public class FullOpenAiChatModel implements ChatLanguageModel, TokenCountEstimat
private final Tokenizer tokenizer;

public FullOpenAiChatModel(String baseUrl, String apiKey, String modelName, Double temperature,
Double topP, List<String> stop, Integer maxTokens, Double presencePenalty,
Double frequencyPenalty, Duration timeout, Integer maxRetries, Proxy proxy,
Boolean logRequests, Boolean logResponses, Tokenizer tokenizer) {
baseUrl = (String) Utils.getOrDefault(baseUrl, "https://api.openai.com/v1");
Double topP, List<String> stop, Integer maxTokens, Double presencePenalty,
Double frequencyPenalty, Duration timeout, Integer maxRetries, Proxy proxy,
Boolean logRequests, Boolean logResponses, Tokenizer tokenizer) {
baseUrl = Utils.getOrDefault(baseUrl, "https://api.openai.com/v1");
if ("demo".equals(apiKey)) {
baseUrl = "http://langchain4j.dev/demo/openai/v1";
}

timeout = (Duration) Utils.getOrDefault(timeout, Duration.ofSeconds(60L));
timeout = Utils.getOrDefault(timeout, Duration.ofSeconds(60L));
this.client = OpenAiClient.builder().openAiApiKey(apiKey)
.baseUrl(baseUrl).callTimeout(timeout).connectTimeout(timeout)
.readTimeout(timeout).writeTimeout(timeout).proxy(proxy)
.logRequests(logRequests).logResponses(logResponses).build();
this.modelName = (String) Utils.getOrDefault(modelName, "gpt-3.5-turbo");
this.temperature = (Double) Utils.getOrDefault(temperature, 0.7D);
.baseUrl(baseUrl).callTimeout(timeout).connectTimeout(timeout)
.readTimeout(timeout).writeTimeout(timeout).proxy(proxy)
.logRequests(logRequests).logResponses(logResponses).build();
this.modelName = Utils.getOrDefault(modelName, "gpt-3.5-turbo");
this.temperature = Utils.getOrDefault(temperature, 0.7D);
this.topP = topP;
this.stop = stop;
this.maxTokens = maxTokens;
this.presencePenalty = presencePenalty;
this.frequencyPenalty = frequencyPenalty;
this.maxRetries = (Integer) Utils.getOrDefault(maxRetries, 3);
this.tokenizer = (Tokenizer) Utils.getOrDefault(tokenizer, new OpenAiTokenizer(this.modelName));
this.maxRetries = Utils.getOrDefault(maxRetries, 3);
this.tokenizer = Utils.getOrDefault(tokenizer, new OpenAiTokenizer(this.modelName));
}

public Response<AiMessage> generate(List<ChatMessage> messages) {
return this.generate(messages, (List) null, (ToolSpecification) null);
return this.generate(messages, null, null);
}

public Response<AiMessage> generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications) {
return this.generate(messages, toolSpecifications, (ToolSpecification) null);
return this.generate(messages, toolSpecifications, null);
}

public Response<AiMessage> generate(List<ChatMessage> messages, ToolSpecification toolSpecification) {
return this.generate(messages, Collections.singletonList(toolSpecification), toolSpecification);
}

private Response<AiMessage> generate(List<ChatMessage> messages,
List<ToolSpecification> toolSpecifications,
ToolSpecification toolThatMustBeExecuted) {
List<ToolSpecification> toolSpecifications,
ToolSpecification toolThatMustBeExecuted) {
Builder requestBuilder = null;
if (modelName.contains(ChatModel.ZHIPU.toString()) || modelName.contains(ChatModel.ALI.toString())) {
requestBuilder = ChatCompletionRequest.builder()
Expand Down Expand Up @@ -107,15 +108,12 @@ public int estimateTokenCount(List<ChatMessage> messages) {
return this.tokenizer.estimateTokenCountInMessages(messages);
}

public static FullOpenAiChatModel withApiKey(String apiKey) {
return builder().apiKey(apiKey).build();
}

public static FullOpenAiChatModel.FullOpenAiChatModelBuilder builder() {
return new FullOpenAiChatModel.FullOpenAiChatModelBuilder();
}

public static class FullOpenAiChatModelBuilder {

private String baseUrl;
private String apiKey;
private String modelName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package dev.langchain4j.model.openai;

import dev.ai4j.openai4j.chat.ChatCompletionChoice;
import dev.ai4j.openai4j.chat.ChatCompletionResponse;
import dev.ai4j.openai4j.chat.Function;
import dev.ai4j.openai4j.chat.FunctionCall;
import dev.ai4j.openai4j.chat.Message;
import dev.ai4j.openai4j.chat.Parameters;
import dev.ai4j.openai4j.chat.Role;
import dev.ai4j.openai4j.shared.Usage;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.agent.tool.ToolParameters;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.ChatModel;
import dev.langchain4j.model.output.TokenUsage;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class ImproveInternalOpenAiHelper {
static final String OPENAI_URL = "https://api.openai.com/v1";
static final String OPENAI_DEMO_API_KEY = "demo";
static final String OPENAI_DEMO_URL = "http://langchain4j.dev/demo/openai/v1";

public ImproveInternalOpenAiHelper() {
}
Expand Down Expand Up @@ -77,35 +63,4 @@ public static Role roleFrom(ChatMessage message, String modelName) {
}
}

public static List<Function> toFunctions(Collection<ToolSpecification> toolSpecifications) {
return (List) toolSpecifications.stream().map(ImproveInternalOpenAiHelper::toFunction)
.collect(Collectors.toList());
}

private static Function toFunction(ToolSpecification toolSpecification) {
return Function.builder().name(toolSpecification.name())
.description(toolSpecification.description())
.parameters(toOpenAiParameters(toolSpecification.parameters())).build();
}

private static Parameters toOpenAiParameters(ToolParameters toolParameters) {
return toolParameters == null ? Parameters.builder().build() : Parameters.builder()
.properties(toolParameters.properties()).required(toolParameters.required()).build();
}

public static AiMessage aiMessageFrom(ChatCompletionResponse response) {
if (response.content() != null) {
return AiMessage.aiMessage(response.content());
} else {
FunctionCall functionCall = ((ChatCompletionChoice) response.choices().get(0)).message().functionCall();
ToolExecutionRequest toolExecutionRequest = ToolExecutionRequest.builder()
.name(functionCall.name()).arguments(functionCall.arguments()).build();
return AiMessage.aiMessage(toolExecutionRequest);
}
}

public static TokenUsage tokenUsageFrom(Usage openAiUsage) {
return openAiUsage == null ? null : new TokenUsage(openAiUsage.promptTokens(),
openAiUsage.completionTokens(), openAiUsage.totalTokens());
}
}
Loading
Loading