//package com.backendsys.modules.sdk.tencentcloud.huanyuan.service.impl;
//
//import cn.hutool.core.util.ArrayUtil;
//import cn.hutool.core.util.ObjectUtil;
//import com.backendsys.modules.ai.chat.entity.Chat;
//import com.backendsys.modules.ai.chat.entity.ChatCompletionParam;
//import com.backendsys.modules.ai.chat.entity.ChatResult;
//import com.backendsys.modules.ai.chat.entity.ChatSseMessage;
//import com.backendsys.modules.common.config.redis.utils.RedisUtil;
//import com.backendsys.modules.sdk.bocha.entity.BochaParam;
//import com.backendsys.modules.sdk.bocha.service.BochaService;
//import com.backendsys.modules.sdk.tencentcloud.huanyuan.service.HunYuanClient;
//import com.backendsys.modules.sse.entity.SseResponse;
//import com.backendsys.modules.sse.entity.SseResponseEnum;
//import com.backendsys.modules.sse.utils.SseUtil;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.JsonMappingException;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.tencentcloudapi.common.Credential;
//import com.tencentcloudapi.common.SSEResponseModel;
//import com.tencentcloudapi.common.exception.TencentCloudSDKException;
//import com.tencentcloudapi.common.profile.ClientProfile;
//import com.tencentcloudapi.hunyuan.v20230901.HunyuanClient;
//import com.tencentcloudapi.hunyuan.v20230901.models.ChatStdRequest;
//import com.tencentcloudapi.hunyuan.v20230901.models.ChatStdResponse;
//import com.tencentcloudapi.hunyuan.v20230901.models.Message;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
//@Service
//public class HunYuanClientImpl implements HunYuanClient {
//
//    @Value("${tencent.hunyuan.secret-id}")
//    private String SECRET_ID;
//    @Value("${tencent.hunyuan.secret-key}")
//    private String SECRET_KEY;
//    @Value("${tencent.hunyuan.region}")
//    private String REGION;
//
//    @Value("${spring.application.name}")
//    private String APPLICATION_NAME;
//
//    @Autowired
//    private SseUtil sseUtil;
//    @Autowired
//    private RedisUtil redisUtil;
//    @Autowired
//    private BochaService bochaService;
//
//    private Message setMessage(String role, String content) {
//        Message msg = new Message();
//        msg.setRole(role);
//        msg.setContent(content);
//        return msg;
//    }
//
//    /**
//     * [HunYuan] 发起对话
//     * https://cloud.tencent.com/document/product/1729/101836
//     */
//    @Override
//    public ChatResult chatCompletion(ChatCompletionParam chatCompletionParam) {
//
//        // 参数化
//        Long user_id = chatCompletionParam.getUser_id();
//        String prompt = chatCompletionParam.getPrompt();
//        String history_code = chatCompletionParam.getHistory_code();
//        List<Chat> chatList = chatCompletionParam.getChatList();
//        Boolean internet = chatCompletionParam.getInternet();
//
//        // 定义作用于全局的变量
//        Long replyDuration = 0L;
//        String requestOfRedisKey = APPLICATION_NAME + "-chat-history-" + history_code;
//        StringBuilder allReplyContent = new StringBuilder();
//
//        ChatResult chatResult = new ChatResult();
//        try {
//
//            // System.out.println("向模型: " + model + " 提问: " + prompt);
//            System.out.println("向混元模型 提问: " + prompt);
//
//            // 记录请求开始时间
//            long allStartTime = System.currentTimeMillis();
//
//            // 加入上下文历史对话
//            System.out.println("---- 历史对话 (history_code): " + history_code + " ----");
//
//            List<Message> messages = new ArrayList<>();
//            if (chatList != null && !chatList.isEmpty()) {
//                chatList.stream().forEach(chat -> {
//                    // 混元没有 THINK
//                    messages.add(setMessage(chat.getRole(), chat.getContent()));
//                });
//                // 反转列表
//                Collections.reverse(messages);
//            }
//
//            // -- [博查] Web Search API ----------------------------------------------
//            if (internet) {
//
//                // 远程查询、统计接口时间、设置返回参数
//                long internetStartTime = System.currentTimeMillis();
//                JsonNode searchResult = bochaService.WebSearch(new BochaParam(prompt));
//                String context = bochaService.WebSearchToString(searchResult);
//                long internetEndTime = System.currentTimeMillis();
//                chatResult.setInternet_duration(internetStartTime - internetEndTime);
//                chatResult.setInternet_content(context);
//
//                // 将搜索结果作为上下文添加到消息中
//                messages.add(setMessage("system", context));
//                messages.add(setMessage("user", "在回答时引用以上全部数据进行分析")); // 的 "name"、"summary"
//                messages.add(setMessage("assistant", "好的"));
//
//                // [SSE] 发送消息
//                ChatSseMessage chatSearchSseMessage = new ChatSseMessage("SEARCH", context, null, history_code);
//                sseUtil.send(user_id, new SseResponse(SseResponseEnum.OLLAMA, chatSearchSseMessage).toJsonStr());
//            }
//            // -----------------------------------------------------------------------
//
//            // 新的对话内容
//            messages.add(setMessage("user", prompt));
//
//            // 输出全部对话内容
//            messages.stream().forEach(msg -> {
//                System.out.println("[" + msg.getRole() + "]: " + msg.getContent());
//            });
//            System.out.println("---------------------------------------------------------------------");
//
//
//            // [混元大模型]
//            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
//            ClientProfile clientProfile = new ClientProfile();
//            HunyuanClient client = new HunyuanClient(cred, REGION, clientProfile);
//
//            ChatStdRequest req = new ChatStdRequest();
//            req.setMessages(ArrayUtil.toArray(messages, Message.class));
//
//            ChatStdResponse resp = client.ChatStd(req);     // 发送对话 (标准版)
////            ChatProResponse resp = client.ChatPro(req);     // 发送对话 (专业版)
//
//
//            for (SSEResponseModel.SSE e : resp) {
//
//
//
////                 System.out.println(e.Data);
//                /**
//                 *
//                 * .Data:
//                 *  {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"当然"}}],"Created":1709618061,"Id":"e73c0a71-5c98-4893-ba90-ad5056d5871a","Usage":{"PromptTokens":7,"CompletionTokens":1,"TotalTokens":8}}
//                 * e.Data:
//                 *  {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"可以"}}],"Created":1709618061,"Id":"e73c0a71-5c98-4893-ba90-ad5056d5871a","Usage":{"PromptTokens":7,"CompletionTokens":2,"TotalTokens":9}}
//                 * e.Data:
//                 *  {"Note":"以上内容为AI生成，不代表开发者立场，请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"，"}}],"Created":1709618061,"Id":"e73c0a71-5c98-4893-ba90-ad5056d5871a","Usage":{"PromptTokens":7,"CompletionTokens":3,"TotalTokens":10}}
//                 * e.Data:
//                 */
//
//                ObjectMapper objectMapper = new ObjectMapper();
//                JsonNode node = objectMapper.readTree(e.Data.toString());
//                JsonNode delta = node.path("Choices").path(0).path("Delta");
//                String content = delta.path("Content").asText("");
//
//                // [SSE] 发送消息
//                ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", content, null, history_code);
//                sseUtil.send(user_id, new SseResponse(SseResponseEnum.HUNYUAN, chatSseMessage).toJsonStr());
//
//                // 收集回答内容
//                allReplyContent.append(content);
//
//                // 判断是否中止
//                if (ObjectUtil.isEmpty(redisUtil.getCacheObject(requestOfRedisKey))) {
//                    System.out.println("中止!");
//                    allReplyContent.append(" (请求中止)");
////                    request.abort();
//                    // 流程结束后，删除锁
//                    redisUtil.delete(requestOfRedisKey);
//                    break;
//                }
//
//            }
//
//            System.out.println("-------------------- 结束流式回答. --------------------");
//            replyDuration = System.currentTimeMillis() - allStartTime;
//
//            System.out.println("全部回答: " + allReplyContent);
//            System.out.println("总输出耗时: " + replyDuration + " 毫秒");
//
//            chatResult.setContent(allReplyContent.toString());
//            chatResult.setContent_duration(replyDuration);
//            return chatResult;
//
//        } catch (TencentCloudSDKException e) {
//            System.out.println("TencentCloudSDKException: " + e.getMessage());
//            // [SSE] 发送消息
//            ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", e.getMessage(), replyDuration, history_code);
//            sseUtil.send(user_id, new SseResponse(SseResponseEnum.HUNYUAN, chatSseMessage).toJsonStr());
//
//            chatResult.setContent(e.getMessage());
//            return chatResult;
//        } catch (JsonMappingException e) {
//            System.out.println("JsonMappingException: " + e.getMessage());
//            // [SSE] 发送消息
//            ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", e.getMessage(), replyDuration, history_code);
//            sseUtil.send(user_id, new SseResponse(SseResponseEnum.HUNYUAN, chatSseMessage).toJsonStr());
//
//            chatResult.setContent(e.getMessage());
//            return chatResult;
//        } catch (JsonProcessingException e) {
//            System.out.println("JsonProcessingException: " + e.getMessage());
//            // [SSE] 发送消息
//            ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", e.getMessage(), replyDuration, history_code);
//            sseUtil.send(user_id, new SseResponse(SseResponseEnum.HUNYUAN, chatSseMessage).toJsonStr());
//
//            chatResult.setContent(e.getMessage());
//            return chatResult;
//        } finally {
//            // [SSE] 发送消息
//            ChatSseMessage chatSseMessage = new ChatSseMessage("REPLY", "[DONE][REPLY]", replyDuration, history_code);
//            sseUtil.send(user_id, new SseResponse(SseResponseEnum.HUNYUAN, chatSseMessage).toJsonStr());
//
//            redisUtil.delete(requestOfRedisKey);
//        }
//
//    }
//}
