| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Flux<FebsResponse> llmInvokeStreaming(List<LlmStrategyDto> dto) { |
| | | public Flux<FebsResponse> llmInvokeStreamingWithThink(List<LlmStrategyDto> dto) { |
| | | if (CollUtil.isEmpty(dto)){ |
| | | throw new FebsException("火山大模型初始化异常"); |
| | | } |
| | |
| | | } |
| | | |
| | | 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)); |