|
@@ -0,0 +1,104 @@
|
|
|
+package com.backendsys.modules.ai.deepSeek.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.backendsys.exception.CustException;
|
|
|
+import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
|
+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.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class OllamaUtil {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SseUtil sseUtil;
|
|
|
+
|
|
|
+ @Value("${spring.application.name}")
|
|
|
+ private String APPLICATION_NAME;
|
|
|
+
|
|
|
+ @Value("${ai.config.deepseek.domain}")
|
|
|
+ private String DOMAIN;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 流式对话
|
|
|
+ */
|
|
|
+ public void chatDeepSeek(String model, String question, Long userId) {
|
|
|
+
|
|
|
+ System.out.println("提问: " + question);
|
|
|
+
|
|
|
+ // 创建一个 CompletableFuture 来执行异步任务
|
|
|
+ CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
|
|
+
|
|
|
+ String emitterKey = APPLICATION_NAME + "-userid-" + Convert.toStr(userId);
|
|
|
+
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
+
|
|
|
+ HttpPost request = new HttpPost(DOMAIN + "/api/generate");
|
|
|
+ Map<String, Object> requestMap = new HashMap<>();
|
|
|
+ requestMap.put("model", model);
|
|
|
+ requestMap.put("prompt", question);
|
|
|
+ requestMap.put("stream", true);
|
|
|
+
|
|
|
+ String requestBody = objectMapper.writeValueAsString(requestMap);
|
|
|
+ request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ try (CloseableHttpResponse response = client.execute(request);
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)))
|
|
|
+ {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ // 每行数据可以是一个JSON对象,根据实际情况处理
|
|
|
+ JSONObject resJson = JSONObject.parseObject(line);
|
|
|
+ String responseContent = resJson.getString("response");
|
|
|
+
|
|
|
+ System.out.println("content: " + responseContent);
|
|
|
+
|
|
|
+ String dataStr = (new SseResponse(SseResponseEnum.DEEPSEEK, responseContent)).toJsonStr();
|
|
|
+ sseUtil.send(emitterKey, dataStr);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ String dataStr = (new SseResponse(SseResponseEnum.DEEPSEEK, "end")).toJsonStr();
|
|
|
+ sseUtil.send(emitterKey, dataStr);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("处理 Deepseek 请求时发生错误: " + e.getMessage());
|
|
|
+ String dataStr = (new SseResponse(SseResponseEnum.DEEPSEEK, e.getMessage())).toJsonStr();
|
|
|
+ sseUtil.send(emitterKey, dataStr);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("处理 Deepseek 请求时发生错误: " + e.getMessage());
|
|
|
+ String dataStr = (new SseResponse(SseResponseEnum.DEEPSEEK, e.getMessage())).toJsonStr();
|
|
|
+ sseUtil.send(emitterKey, dataStr);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+}
|