Administrator
2025-09-01 b1529402b688ca59f06564175af463dcc0216335
refactor(ai): 重构 AI模型调用逻辑

- 将 API 密钥和模型名称等配置项提取为静态常量
- 优化代码结构,提高可读性和可维护性
- 在 HsLlmStrategyServiceImpl 中添加温度、topP 等参数配置
2 files modified
36 ■■■■■ changed files
src/main/java/cc/mrbird/febs/ai/strategy/Impl/AliLlmStrategyServiceImpl.java 7 ●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/ai/strategy/Impl/HsLlmStrategyServiceImpl.java 29 ●●●●● patch | view | raw | blame | history
src/main/java/cc/mrbird/febs/ai/strategy/Impl/AliLlmStrategyServiceImpl.java
@@ -26,13 +26,16 @@
    private GenerationParam generationParam;
    private static final String apiKey = "sk-babdcf8799144134915cee2683794b2f";
    private static final String model = "qwen-plus";
    @PostConstruct
    public void init() {
        this.generationParam = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey("sk-babdcf8799144134915cee2683794b2f")
                .apiKey(apiKey)
                // 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .model(model)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
    }
src/main/java/cc/mrbird/febs/ai/strategy/Impl/HsLlmStrategyServiceImpl.java
@@ -33,7 +33,14 @@
    private ArkService service;
    private static final String ak = "AKLTZTQxZjMyZTUxMWJmNDEyNDkzNWExOGQ3ODllNzhhNmQ";
    private static final String sk = "TmpFeE1qZ3haREExTW1JeE5HRTBZVGc1WlRRNVlqWXpORGd5TWpsak5HWQ==";
    private static final String baseUrl = "https://ark.cn-beijing.volces.com/api/v3";
    private static final String LinkId = "ep-20250805124033-lhxbf";
    private static final Double temperature = 0.7;
    private static final Double topP = 0.9;
    private static final Integer maxTokens = 2048;
    private static final Double frequencyPenalty = 0.0;
    @PostConstruct
    public void init() {
@@ -47,9 +54,9 @@
        this.service = ArkService.builder()
                .dispatcher(dispatcher)
                .connectionPool(connectionPool)
                .baseUrl("https://ark.cn-beijing.volces.com/api/v3")
                .ak("AKLTZTQxZjMyZTUxMWJmNDEyNDkzNWExOGQ3ODllNzhhNmQ")
                .sk("TmpFeE1qZ3haREExTW1JeE5HRTBZVGc1WlRRNVlqWXpORGd5TWpsak5HWQ==")
                .baseUrl(baseUrl)
                .ak(ak)
                .sk(sk)
                .build();
    }
@@ -96,10 +103,10 @@
                    .model(LinkId)
                    .messages(messages)
                    .stream(false)
                    .temperature(0.7) // 降低温度参数,提高确定性,可能提升速度
                    .topP(0.9)        // 调整topP参数
                    .maxTokens(2048)  // 减少最大token数
                    .frequencyPenalty(0.0)
                    .temperature(temperature) // 降低温度参数,提高确定性,可能提升速度
                    .topP(topP)        // 调整topP参数
                    .maxTokens(maxTokens)  // 减少最大token数
                    .frequencyPenalty(frequencyPenalty)
                    .build();
            List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices();
@@ -126,10 +133,10 @@
                .messages(messages)
                .stream(true)
                .thinking(new ChatCompletionRequest.ChatCompletionRequestThinking("enabled"))
                .temperature(0.7)
                .topP(0.9)
                .maxTokens(2048)
                .frequencyPenalty(0.0)
                .temperature(temperature) // 降低温度参数,提高确定性,可能提升速度
                .topP(topP)        // 调整topP参数
                .maxTokens(maxTokens)  // 减少最大token数
                .frequencyPenalty(frequencyPenalty)
                .build();
        return Flux.from(service.streamChatCompletion(chatCompletionRequest))