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 skipUser skipUser = new skipUser();
|
||||||
|
|
||||||
|
private LLmModel llmModel = new LLmModel();
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class UserRegisterProperties {
|
public static class UserRegisterProperties {
|
||||||
|
|
||||||
@@ -73,4 +75,9 @@ public class AppConfig {
|
|||||||
public static class customerMailConfig{
|
public static class customerMailConfig{
|
||||||
private String customerMail = "";
|
private String customerMail = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class LLmModel{
|
||||||
|
private String model = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,12 +24,16 @@ import java.util.Map;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class LLMConfig {
|
public class LLMConfig {
|
||||||
|
|
||||||
|
private final NacosAppConfigCenter.DynamicAppConfig cfgHolder;
|
||||||
|
|
||||||
@Value("${spring.ai.openai.api-key}")
|
@Value("${spring.ai.openai.api-key}")
|
||||||
private String apiKey;
|
private String apiKey;
|
||||||
@Value("${spring.ai.openai.base-url}")
|
@Value("${spring.ai.openai.base-url}")
|
||||||
private String baseUrl;
|
private String baseUrl;
|
||||||
@Value("${spring.ai.openai.chat.options.model}")
|
|
||||||
private String openRouterChatModel;
|
public LLMConfig(NacosAppConfigCenter.DynamicAppConfig cfgHolder) {
|
||||||
|
this.cfgHolder = cfgHolder;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public OpenAiApi openAiApi() {
|
public OpenAiApi openAiApi() {
|
||||||
@@ -44,7 +48,7 @@ public class LLMConfig {
|
|||||||
public ChatClient chatClient(ChatModel geminiModel) {
|
public ChatClient chatClient(ChatModel geminiModel) {
|
||||||
return ChatClient.builder(geminiModel)
|
return ChatClient.builder(geminiModel)
|
||||||
.defaultOptions(OpenAiChatOptions.builder()
|
.defaultOptions(OpenAiChatOptions.builder()
|
||||||
.model(openRouterChatModel)
|
.model(getConfiguredChatModel())
|
||||||
.build())
|
.build())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
@@ -64,4 +68,9 @@ public class LLMConfig {
|
|||||||
RetryUtils.DEFAULT_RETRY_TEMPLATE);
|
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())
|
.user(chatReq.getMessage())
|
||||||
// 配置OpenAI选项
|
// 配置OpenAI选项
|
||||||
.options(OpenAiChatOptions.builder()
|
.options(buildChatOptions(StpUtil.getLoginIdAsString()))
|
||||||
.user(StpUtil.getLoginIdAsString())
|
|
||||||
.build())
|
|
||||||
// 启用流式响应
|
// 启用流式响应
|
||||||
.stream()
|
.stream()
|
||||||
.chatResponse()
|
.chatResponse()
|
||||||
@@ -517,10 +515,19 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
.prompt()
|
.prompt()
|
||||||
.system(systemPrompt)
|
.system(systemPrompt)
|
||||||
.messages(messages)
|
.messages(messages)
|
||||||
|
.options(buildChatOptions(null))
|
||||||
.call()
|
.call()
|
||||||
.content();
|
.content();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OpenAiChatOptions buildChatOptions(String user) {
|
||||||
|
AppConfig appConfig = cfgHolder.getRef().get();
|
||||||
|
return OpenAiChatOptions.builder()
|
||||||
|
.model(appConfig.getLlmModel().getModel())
|
||||||
|
.user(user)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步处理音频:TTS 转换 + 上传 R2
|
* 异步处理音频:TTS 转换 + 上传 R2
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package com.yolo.keyborad;
|
package com.yolo.keyborad;
|
||||||
|
|
||||||
import com.apple.itunes.storekit.signature.JWSSignatureCreator;
|
import com.apple.itunes.storekit.signature.JWSSignatureCreator;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -14,19 +16,43 @@ import java.util.UUID;
|
|||||||
*/
|
*/
|
||||||
public class test {
|
public class test {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
try {
|
// try {
|
||||||
String jwt = AppStoreJWT.generateJWT();
|
// 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"));
|
||||||
|
}
|
||||||
|
|
||||||
URL url = new URL("https://api.storekit.itunes.apple.com/inApps/v1/subscriptions"); // 你需要访问的 Apple API URL
|
public static String resolveLanguage(String acceptLanguage) {
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
if (!StringUtils.hasText(acceptLanguage)) {
|
||||||
connection.setRequestMethod("GET");
|
return defaultLanguage();
|
||||||
connection.setRequestProperty("Authorization", "Bearer " + jwt); // 设置 JWT 为 Bearer Token
|
|
||||||
|
|
||||||
// 处理响应
|
|
||||||
int responseCode = connection.getResponseCode();
|
|
||||||
System.out.println("Response Code: " + responseCode);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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