Administrator
2025-09-02 f8f1d6a91b32077c69dd97334559fe3165ff79b6
src/main/java/cc/mrbird/febs/ai/strategy/Impl/HsLlmStrategyServiceImpl.java
@@ -1,29 +1,24 @@
package cc.mrbird.febs.ai.strategy.Impl;
import cc.mrbird.febs.ai.entity.AiTalkItem;
import cc.mrbird.febs.ai.res.ai.Report;
import cc.mrbird.febs.ai.res.memberTalk.ApiMemberTalkStreamVo;
import cc.mrbird.febs.ai.strategy.LlmStrategyService;
import cc.mrbird.febs.ai.strategy.enumerates.LlmStrategyContextEnum;
import cc.mrbird.febs.ai.strategy.param.LlmStrategyDto;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.exception.FebsException;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.volcengine.ark.runtime.model.completion.chat.*;
import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -33,7 +28,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 +49,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 +98,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();
@@ -115,7 +117,7 @@
    }
    @Override
    public Flux<FebsResponse> llmInvokeStreaming(List<LlmStrategyDto> dto) {
    public Flux<FebsResponse> llmInvokeStreamingWithThink(List<LlmStrategyDto> dto) {
        if (CollUtil.isEmpty(dto)){
            throw new FebsException("火山大模型初始化异常");
        }
@@ -126,10 +128,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))
@@ -144,21 +146,56 @@
                    }
                    ChatMessage message = choice.getMessage();
                    ApiMemberTalkStreamVo apiMemberTalkStreamVo = new ApiMemberTalkStreamVo();
                    // 处理 reasoning content
                    String reasoningContent = message.getReasoningContent();
                    if (StrUtil.isNotEmpty(reasoningContent)) {
                        apiMemberTalkStreamVo.setReasoningContent(reasoningContent);
                    HashMap<String, String> stringStringHashMap = new HashMap<>();
                    if (ObjectUtil.isNotEmpty(message.getReasoningContent())) {
                        stringStringHashMap.put(LlmStrategyContextEnum.THINK.name(),message.getReasoningContent().toString());
                    }
                    if (ObjectUtil.isNotEmpty(message.getContent())) {
                        stringStringHashMap.put(LlmStrategyContextEnum.CONTENT.name(),message.getContent().toString());
                    }
                    return new FebsResponse().success().data(stringStringHashMap);
                })
                .onErrorResume(throwable -> {
                    throw new FebsException(StrUtil.format("火山大模型流式调用AI服务失:{}",throwable));
                });
    }
    @Override
    public Flux<FebsResponse> llmInvokeStreamingNoThink(List<LlmStrategyDto> dto) {
        if (CollUtil.isEmpty(dto)){
            throw new FebsException("火山大模型初始化异常");
        }
        List<ChatMessage> messages = getMessages(dto);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(LinkId)
                .messages(messages)
                .stream(true)
                .temperature(temperature) // 降低温度参数,提高确定性,可能提升速度
                .topP(topP)        // 调整topP参数
                .maxTokens(maxTokens)  // 减少最大token数
                .frequencyPenalty(frequencyPenalty)
                .build();
        return Flux.from(service.streamChatCompletion(chatCompletionRequest))
                .map(response -> {
                    if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
                        return new FebsResponse().success().data("未响应,请重试");
                    }
                    // 安全处理 content
                    String content = "";
                    if (message.getContent() != null) {
                        content = message.getContent().toString();
                    ChatCompletionChoice choice = response.getChoices().get(0);
                    if (choice == null || choice.getMessage() == null) {
                        return new FebsResponse().success().data("END");
                    }
                    apiMemberTalkStreamVo.setContent(content);
                    return new FebsResponse().success().data(apiMemberTalkStreamVo);
                    ChatMessage message = choice.getMessage();
                    HashMap<String, String> stringStringHashMap = new HashMap<>();
                    if (ObjectUtil.isNotEmpty(message.getContent())) {
                        stringStringHashMap.put(LlmStrategyContextEnum.CONTENT.name(),message.getContent().toString());
                    }
                    return new FebsResponse().success().data(stringStringHashMap);
                })
                .onErrorResume(throwable -> {
                    throw new FebsException(StrUtil.format("火山大模型流式调用AI服务失:{}",throwable));