|
@@ -1,7 +1,21 @@
|
|
|
package org.eco.als.controller;
|
|
|
|
|
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.google.gson.JsonArray;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
|
|
|
+import io.github.pigmesh.ai.deepseek.core.EmbeddingClient;
|
|
|
+import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionModel;
|
|
|
+import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionRequest;
|
|
|
+import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;
|
|
|
+import io.milvus.v2.client.ConnectConfig;
|
|
|
+import io.milvus.v2.client.MilvusClientV2;
|
|
|
+import io.milvus.v2.service.vector.request.InsertReq;
|
|
|
+import io.milvus.v2.service.vector.request.SearchReq;
|
|
|
+import io.milvus.v2.service.vector.request.data.FloatVec;
|
|
|
+import io.milvus.v2.service.vector.response.SearchResp;
|
|
|
import jakarta.annotation.Resource;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.eco.als.domain.bo.AlgorithmBo;
|
|
@@ -11,9 +25,15 @@ import org.eco.als.domain.bo.TaskBo;
|
|
|
import org.eco.als.service.IAlgorithmService;
|
|
|
import org.eco.als.service.IFormulaService;
|
|
|
import org.eco.common.core.core.domain.CommonResult;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import reactor.core.publisher.Flux;
|
|
|
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -153,4 +173,118 @@ public class AlgorithmController {
|
|
|
Object result = formulaService.evaluateExpression(express, variables);
|
|
|
return CommonResult.success(result);
|
|
|
}
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeepSeekClient deepSeekClient;
|
|
|
+
|
|
|
+ @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
|
+ public Flux<ChatCompletionResponse> chat(String prompt) {
|
|
|
+ return deepSeekClient.chatFluxCompletion(prompt);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/chat/advanced", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
|
+ public Flux<ChatCompletionResponse> chatAdvanced(String prompt) {
|
|
|
+ ChatCompletionRequest request = ChatCompletionRequest.builder()
|
|
|
+ // 模型选择,支持 DEEPSEEK_CHAT、DEEPSEEK_REASONER 等
|
|
|
+ .model(ChatCompletionModel.DEEPSEEK_REASONER)
|
|
|
+ // 添加用户消息
|
|
|
+ .addUserMessage(prompt)
|
|
|
+ // 添加助手消息,用于多轮对话
|
|
|
+ .addAssistantMessage("上轮结果")
|
|
|
+ // 添加系统消息,用于设置角色和行为
|
|
|
+ .addSystemMessage("你是一个专业的助手")
|
|
|
+ // 设置最大生成 token 数,默认 2048
|
|
|
+ .maxCompletionTokens(1000)
|
|
|
+ // 设置响应格式,支持 JSON 结构化输出
|
|
|
+// .responseFormat(...) // 可选
|
|
|
+ // function calling
|
|
|
+// .tools(...) // 可选
|
|
|
+ .build();
|
|
|
+
|
|
|
+ return deepSeekClient.chatFluxCompletion(request);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ EmbeddingClient embeddingClient;
|
|
|
+ @GetMapping(value = "/chat/build")
|
|
|
+ public CommonResult<Void> build(){
|
|
|
+ String uri = "https://in03-1ad7b3ffa88b8b6.serverless.ali-cn-hangzhou.cloud.zilliz.com.cn";
|
|
|
+ ConnectConfig connectConfig = ConnectConfig.builder()
|
|
|
+ // 1.2 获取的 Milvus 链接端点
|
|
|
+ .uri(uri)
|
|
|
+ // 1.2 获取的 Milvus 链接信息
|
|
|
+ .token("e6da17d76e60260cd94e8c84c0ee3da0719f5132c0ba5c30f9b19980d56ccb68704493dc7ca996065c49f8f00bd0f4345b47f97c")
|
|
|
+ .build();
|
|
|
+ MilvusClientV2 milvusClientV2 = new MilvusClientV2(connectConfig);
|
|
|
+
|
|
|
+ // 这里以 2025最新的我司保密条例演示,可以换成你自己的
|
|
|
+ String law = FileUtil.readString("C:\\Users\\wanggaokun\\Desktop\\新建 文本文档 (2).txt", Charset.defaultCharset());
|
|
|
+ String[] lawSplits = StrUtil.split(law, 400);
|
|
|
+
|
|
|
+
|
|
|
+ List<JsonObject> data = new ArrayList<>();
|
|
|
+ for (String lawSplit : lawSplits) {
|
|
|
+ List<Float> floatList = embeddingClient.embed(lawSplit);
|
|
|
+
|
|
|
+ JsonObject jsonObject = new JsonObject();
|
|
|
+
|
|
|
+ // 将 List<Float> 转换为 JsonArray
|
|
|
+ JsonArray jsonArray = new JsonArray();
|
|
|
+ for (Float value : floatList) {
|
|
|
+ jsonArray.add(value);
|
|
|
+ }
|
|
|
+ jsonObject.add("vector", jsonArray);
|
|
|
+ jsonObject.addProperty("text", lawSplit);
|
|
|
+
|
|
|
+ data.add(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ InsertReq insertReq = InsertReq.builder()
|
|
|
+ .collectionName("deepseek")
|
|
|
+ .data(data)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ milvusClientV2.insert(insertReq);
|
|
|
+ return CommonResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/chatrag", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
|
+ public Flux<ChatCompletionResponse> chatrag(String prompt) {
|
|
|
+ String uri = "https://in03-1ad7b3ffa88b8b6.serverless.ali-cn-hangzhou.cloud.zilliz.com.cn";
|
|
|
+ ConnectConfig connectConfig = ConnectConfig.builder()
|
|
|
+ // 1.2 获取的 Milvus 链接端点
|
|
|
+ .uri(uri)
|
|
|
+ // 1.2 获取的 Milvus 链接信息
|
|
|
+ .token("e6da17d76e60260cd94e8c84c0ee3da0719f5132c0ba5c30f9b19980d56ccb68704493dc7ca996065c49f8f00bd0f4345b47f97c")
|
|
|
+ .build();
|
|
|
+ MilvusClientV2 milvusClientV2 = new MilvusClientV2(connectConfig);
|
|
|
+
|
|
|
+ List<Float> floatList = embeddingClient.embed(prompt);
|
|
|
+
|
|
|
+ SearchReq searchReq = SearchReq.builder()
|
|
|
+ .collectionName("deepseek")
|
|
|
+ .data(Collections.singletonList(new FloatVec(floatList)))
|
|
|
+ .outputFields(Collections.singletonList("text"))
|
|
|
+ .topK(3)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ SearchResp searchResp = milvusClientV2.search(searchReq);
|
|
|
+
|
|
|
+ List<String> resultList = new ArrayList<>();
|
|
|
+ List<List<SearchResp.SearchResult>> searchResults = searchResp.getSearchResults();
|
|
|
+ for (List<SearchResp.SearchResult> results : searchResults) {
|
|
|
+ System.out.println("TopK results:");
|
|
|
+ for (SearchResp.SearchResult result : results) {
|
|
|
+ resultList.add(result.getEntity().get("text").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ChatCompletionRequest request = ChatCompletionRequest.builder()
|
|
|
+ // 根据渠道模型名称动态修改这个参数
|
|
|
+ .model("deepseek-r1:1.5b")
|
|
|
+ .addUserMessage(String.format("你要根据用户输入的问题:%s \n \n 参考如下内容: %s \n\n 整理处理最终结果", prompt, resultList)).build();
|
|
|
+
|
|
|
+ return deepSeekClient.chatFluxCompletion(request);
|
|
|
+ }
|
|
|
}
|