|
@@ -0,0 +1,150 @@
|
|
|
+package com.sxtvs.open.api.chat.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.sxtvs.open.api.chat.dto.KimiRequest;
|
|
|
+import com.sxtvs.open.api.chat.dto.KimiResponse;
|
|
|
+import com.sxtvs.open.api.chat.dto.KimiStreamResponse;
|
|
|
+import com.sxtvs.open.api.chat.dto.Message;
|
|
|
+import com.sxtvs.open.api.chat.entity.KimiChat;
|
|
|
+import com.sxtvs.open.api.chat.entity.KimiChatGroup;
|
|
|
+import com.sxtvs.open.api.chat.mapper.KimiChatMapper;
|
|
|
+import com.sxtvs.open.api.chat.service.IKimiChatService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.sxtvs.open.api.review.dto.AuthReq;
|
|
|
+import com.sxtvs.open.api.review.service.impl.SSEService;
|
|
|
+import com.sxtvs.open.core.auth.AESUtil;
|
|
|
+import com.sxtvs.open.core.auth.HttpContextUtil;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.http.HttpClient;
|
|
|
+import java.net.http.HttpRequest;
|
|
|
+import java.net.http.HttpResponse;
|
|
|
+import java.net.http.WebSocket;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.concurrent.CompletionStage;
|
|
|
+import java.util.concurrent.Flow;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author syj
|
|
|
+ * @since 2024-03-25
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class KimiChatServiceImpl extends ServiceImpl<KimiChatMapper, KimiChat> implements IKimiChatService {
|
|
|
+
|
|
|
+ private final RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ private final String token = "sk-eKQV5KAqkQHINWZV9IBbsuefO21A6oTToY9pv9nyDeXPH0zu";
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private KimiChatGroupServiceImpl kimiChatGroupService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SSEService sseService;
|
|
|
+
|
|
|
+ private HttpHeaders getHeader(){
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ //headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ headers.set("Authorization", "Bearer " + token);
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void chatCompletions(KimiRequest kimiRequest, Long dataId) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ HttpClient client = HttpClient.newBuilder().build();
|
|
|
+
|
|
|
+ URI uri = URI.create("https://api.moonshot.cn/v1/chat/completions"); // 替换为你的SSE服务器地址
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
+ .uri(uri)
|
|
|
+ .header("Accept", "text/event-stream")
|
|
|
+// .header("Content-Type", "application/json")
|
|
|
+ .header("Authorization", "Bearer " + token)
|
|
|
+ .POST( HttpRequest.BodyPublishers.ofString(JSON.toJSONString(
|
|
|
+ kimiRequest
|
|
|
+ )))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 发送请求并接收响应
|
|
|
+ client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
|
|
+ .thenApply(HttpResponse::body)
|
|
|
+ .thenAccept(x -> {
|
|
|
+ sseService.sendData( dataId, x );
|
|
|
+ stream2db(x.substring(6), dataId);
|
|
|
+ log.info("流数据 -- ",x);
|
|
|
+ })
|
|
|
+ .join();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stream2db(String resp, Long groupId){
|
|
|
+ KimiStreamResponse kimiResponse = JSON.parseObject(resp, KimiStreamResponse.class);
|
|
|
+ if (kimiResponse == null || kimiResponse.getChoices() == null || kimiResponse.getChoices().isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ KimiChat kimiChat = lambdaQuery().eq(KimiChat::getId, kimiResponse.getId()).one();
|
|
|
+ if (kimiChat == null) {
|
|
|
+ kimiChat = new KimiChat();
|
|
|
+ kimiChat.setId(kimiResponse.getId());
|
|
|
+ kimiChat.setGroupId(groupId);
|
|
|
+ kimiChat.setIndex(kimiResponse.getChoices().get(0).getIndex());
|
|
|
+ kimiChat.setRole("system");
|
|
|
+ kimiChat.setContent(kimiResponse.getChoices().stream().map(x -> x.getDelta().getContent( )).reduce((a, b) -> a + b).get());
|
|
|
+ kimiChat.setCreateTime(LocalDateTime.now());
|
|
|
+ save(kimiChat);
|
|
|
+ }else {
|
|
|
+ kimiChat.setContent(
|
|
|
+ kimiChat.getContent() + kimiResponse.getChoices().stream().map(x -> x.getDelta().getContent( )).reduce((a, b) -> a + b).get()
|
|
|
+ );
|
|
|
+ kimiChat.setUpdateTime(LocalDateTime.now());
|
|
|
+ updateById(kimiChat);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void create(KimiChat kimiChat) {
|
|
|
+
|
|
|
+ kimiChat.setId("user-" + IdUtil.simpleUUID());
|
|
|
+ kimiChat.setRole("user");
|
|
|
+ kimiChat.setCreateTime(LocalDateTime.now());
|
|
|
+ save(kimiChat);
|
|
|
+
|
|
|
+ KimiRequest kimiRequest = new KimiRequest();
|
|
|
+ kimiRequest.setModel("moonshot-v1-8k");
|
|
|
+ kimiRequest.setTemperature(0.3D);
|
|
|
+ kimiRequest.setStream(true);
|
|
|
+ kimiRequest.setMax_tokens(4096);
|
|
|
+
|
|
|
+ List<Message> messageList = new ArrayList<>();
|
|
|
+
|
|
|
+ lambdaQuery().eq( KimiChat::getGroupId, kimiChat.getGroupId() ).list().forEach( x -> {
|
|
|
+ Message message = new Message();
|
|
|
+ message.setRole(x.getRole());
|
|
|
+ message.setContent(x.getContent());
|
|
|
+ messageList.add(message);
|
|
|
+ });
|
|
|
+
|
|
|
+ kimiRequest.setMessages(messageList);
|
|
|
+
|
|
|
+ chatCompletions( kimiRequest, kimiChat.getGroupId());
|
|
|
+
|
|
|
+ }
|
|
|
+}
|