test(config): 新增模型配置字段并重构测试语言解析
refactor(service): 抽取动态模型配置与聊天选项构造 feat(config): 支持运行时切换 LLM 模型
This commit is contained in:
@@ -24,6 +24,8 @@ public class AppConfig {
|
||||
|
||||
private skipUser skipUser = new skipUser();
|
||||
|
||||
private LLmModel llmModel = new LLmModel();
|
||||
|
||||
@Data
|
||||
public static class UserRegisterProperties {
|
||||
|
||||
@@ -73,4 +75,9 @@ public class AppConfig {
|
||||
public static class customerMailConfig{
|
||||
private String customerMail = "";
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class LLmModel{
|
||||
private String model = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,12 +24,16 @@ import java.util.Map;
|
||||
@Configuration
|
||||
public class LLMConfig {
|
||||
|
||||
private final NacosAppConfigCenter.DynamicAppConfig cfgHolder;
|
||||
|
||||
@Value("${spring.ai.openai.api-key}")
|
||||
private String apiKey;
|
||||
@Value("${spring.ai.openai.base-url}")
|
||||
private String baseUrl;
|
||||
@Value("${spring.ai.openai.chat.options.model}")
|
||||
private String openRouterChatModel;
|
||||
|
||||
public LLMConfig(NacosAppConfigCenter.DynamicAppConfig cfgHolder) {
|
||||
this.cfgHolder = cfgHolder;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OpenAiApi openAiApi() {
|
||||
@@ -44,7 +48,7 @@ public class LLMConfig {
|
||||
public ChatClient chatClient(ChatModel geminiModel) {
|
||||
return ChatClient.builder(geminiModel)
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
.model(openRouterChatModel)
|
||||
.model(getConfiguredChatModel())
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
@@ -64,4 +68,9 @@ public class LLMConfig {
|
||||
RetryUtils.DEFAULT_RETRY_TEMPLATE);
|
||||
}
|
||||
|
||||
private String getConfiguredChatModel() {
|
||||
AppConfig appConfig = cfgHolder.getRef().get();
|
||||
return appConfig.getLlmModel().getModel();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -213,9 +213,7 @@ public class ChatServiceImpl implements ChatService {
|
||||
// 设置用户消息
|
||||
.user(chatReq.getMessage())
|
||||
// 配置OpenAI选项
|
||||
.options(OpenAiChatOptions.builder()
|
||||
.user(StpUtil.getLoginIdAsString())
|
||||
.build())
|
||||
.options(buildChatOptions(StpUtil.getLoginIdAsString()))
|
||||
// 启用流式响应
|
||||
.stream()
|
||||
.chatResponse()
|
||||
@@ -517,10 +515,19 @@ public class ChatServiceImpl implements ChatService {
|
||||
.prompt()
|
||||
.system(systemPrompt)
|
||||
.messages(messages)
|
||||
.options(buildChatOptions(null))
|
||||
.call()
|
||||
.content();
|
||||
}
|
||||
|
||||
private OpenAiChatOptions buildChatOptions(String user) {
|
||||
AppConfig appConfig = cfgHolder.getRef().get();
|
||||
return OpenAiChatOptions.builder()
|
||||
.model(appConfig.getLlmModel().getModel())
|
||||
.user(user)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步处理音频:TTS 转换 + 上传 R2
|
||||
*/
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.yolo.keyborad;
|
||||
|
||||
import com.apple.itunes.storekit.signature.JWSSignatureCreator;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
/*
|
||||
@@ -14,19 +16,43 @@ import java.util.UUID;
|
||||
*/
|
||||
public class test {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
String jwt = AppStoreJWT.generateJWT();
|
||||
|
||||
URL url = new URL("https://api.storekit.itunes.apple.com/inApps/v1/subscriptions"); // 你需要访问的 Apple API URL
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setRequestProperty("Authorization", "Bearer " + jwt); // 设置 JWT 为 Bearer Token
|
||||
|
||||
// 处理响应
|
||||
int responseCode = connection.getResponseCode();
|
||||
System.out.println("Response Code: " + responseCode);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// try {
|
||||
// String jwt = AppStoreJWT.generateJWT();
|
||||
//
|
||||
// URL url = new URL("https://api.storekit.itunes.apple.com/inApps/v1/subscriptions"); // 你需要访问的 Apple API URL
|
||||
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
// connection.setRequestMethod("GET");
|
||||
// connection.setRequestProperty("Authorization", "Bearer " + jwt); // 设置 JWT 为 Bearer Token
|
||||
//
|
||||
// // 处理响应
|
||||
// int responseCode = connection.getResponseCode();
|
||||
// System.out.println("Response Code: " + responseCode);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
System.out.println(resolveLanguage("zh-Hant-Bopomofo"));
|
||||
}
|
||||
|
||||
public static String resolveLanguage(String acceptLanguage) {
|
||||
if (!StringUtils.hasText(acceptLanguage)) {
|
||||
return defaultLanguage();
|
||||
}
|
||||
|
||||
String preferredLanguage = acceptLanguage.split(",")[0].split(";")[0].trim();
|
||||
if (!StringUtils.hasText(preferredLanguage)) {
|
||||
return defaultLanguage();
|
||||
}
|
||||
|
||||
String normalizedTag = preferredLanguage.replace('_', '-');
|
||||
Locale locale = Locale.forLanguageTag(normalizedTag);
|
||||
String resolvedTag = locale.toLanguageTag();
|
||||
if (!StringUtils.hasText(resolvedTag) || "und".equalsIgnoreCase(resolvedTag)) {
|
||||
return normalizedTag;
|
||||
}
|
||||
return resolvedTag;
|
||||
}
|
||||
|
||||
private static String defaultLanguage() {
|
||||
return Locale.getDefault().getLanguage().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user