feat(ssw-dos): 阶段5 - 业务包迁移 (3个agent类)
## 包结构 (CLAUDE.md §3.6 BS-14)
- boot/ssw-dos/{pom.xml, ssw-dos-entity/, ssw-dos-biz/}
- 零 MapperConfig, 依赖 ssw-common-biz 共享 SswMapperConfig
- pom 父模块: ssw-boot/modules 加 ssw-dos
## 迁移 3 个业务类 (5.2 → 6.2)
- OllamaService (LLM 调 weaviate 5.1.3, 独立编译)
- DialogService (对话, 依赖 Ollama+Weaviate)
- WeaviateService (Vector DB, 独立编译)
## 跳过 (深依赖 5.2 内部类)
- BlockChainService (AutoML 节点, 依赖 5.2 ChainService/BlockService)
- SandboxService (依赖 5.2 ssw.common.sandbox.SandBoxUtil)
- IntegrateService (依赖 5.2 jnpf.integrate.*)
- 13 Service 接口/9 Mapper/10 Controller/10 Entity — dacli form 替代
## 编译验证
- mvn compile -pl ssw-dos/ssw-dos-biz -am
- BUILD SUCCESS (4.954s)
- BS-14 规范通过 (零 MapperConfig)
This commit is contained in:
1 parent
dc3539c85d
commit
2a2591bf45
8 files changed
+292
No files matched your search
@@ -19,6 +19,7 @@
|
||||
<module>jnpf-admin</module>
|
||||
<module>ssw-common</module>
|
||||
<module>ssw-jyzs</module>
|
||||
<module>ssw-dos</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<artifactId>ssw-boot</artifactId>
|
||||
<version>6.2.0-RELEASE</version>
|
||||
</parent>
|
||||
<packaging>pom</packaging>
|
||||
<artifactId>ssw-dos</artifactId>
|
||||
|
||||
<modules>
|
||||
<module>ssw-dos-entity</module>
|
||||
<module>ssw-dos-biz</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>ssw-dos</artifactId>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<version>6.2.0-RELEASE</version>
|
||||
</parent>
|
||||
<artifactId>ssw-dos-biz</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<artifactId>ssw-dos-entity</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<!-- BS-14: ssw-common-biz 提供共享 SswMapperConfig (@MapperScan ssw.**.mapper) -->
|
||||
<dependency>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<artifactId>ssw-common-biz</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<artifactId>jnpf-common-all</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<!-- 5.2 ssw-dos 用到的关键依赖 -->
|
||||
<dependency>
|
||||
<groupId>io.weaviate</groupId>
|
||||
<artifactId>client</artifactId>
|
||||
<version>5.1.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package ssw.agent.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/dos/dialog/run")
|
||||
public class DialogController {
|
||||
|
||||
private final DialogService dialogService;
|
||||
|
||||
@Autowired
|
||||
public DialogController(DialogService dialogService) {
|
||||
this.dialogService = dialogService;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String getDialogResponse(@RequestBody String request) {
|
||||
return dialogService.generateDialogResponse(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package ssw.agent.service;
|
||||
|
||||
import io.weaviate.client.base.Result;
|
||||
import io.weaviate.client.v1.graphql.model.GraphQLResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@Service
|
||||
public class DialogService {
|
||||
|
||||
private final WeaviateService weaviateService;
|
||||
private final OllamaService ollamaService;
|
||||
|
||||
@Autowired
|
||||
public DialogService(WeaviateService weaviateService, OllamaService ollamaService) {
|
||||
this.weaviateService = weaviateService;
|
||||
this.ollamaService = ollamaService;
|
||||
}
|
||||
|
||||
public String generateDialogResponse(String request) {
|
||||
Float[] vetor = ollamaService.getTextEmbeddingFromOllama(request);
|
||||
// 从Weaviate检索相关文档
|
||||
Result<GraphQLResponse> context = weaviateService.searchSimilarDocuments(vetor, 3);
|
||||
|
||||
// 调用ollama生成回答
|
||||
String response = ollamaService.generateResponse(request, context);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package ssw.agent.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.internal.LinkedTreeMap;
|
||||
import io.weaviate.client.base.Result;
|
||||
import io.weaviate.client.v1.graphql.model.GraphQLResponse;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class OllamaService {
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private static final Gson gson = new Gson();
|
||||
private static final String OLLAMA_API_URL = "http://192.168.1.9:11434/v1/embeddings";
|
||||
|
||||
public String generateResponse(String prompt, Result<GraphQLResponse> context) {
|
||||
// 构建上下文
|
||||
StringBuilder contextBuilder = new StringBuilder();
|
||||
LinkedTreeMap data = (LinkedTreeMap)context.getResult().getData();
|
||||
LinkedTreeMap Get = (LinkedTreeMap) data.get("Get");
|
||||
List c = (List) Get.get("Vector_index_b646fcf2_902d_4070_88cb_03e2873f578a_Node");
|
||||
for (int i = 0; i < c.size(); i++) {
|
||||
String s = ((LinkedTreeMap)c.get(i)).get("text").toString();
|
||||
contextBuilder.append(s).append("\n");
|
||||
}
|
||||
|
||||
// 构建完整提示词
|
||||
String fullPrompt = "以下是相关知识库内容,回答用户问题时请参考这些内容:\n" +
|
||||
contextBuilder.toString() +
|
||||
"\n用户问题:\n" +
|
||||
prompt +
|
||||
"\n回答:";
|
||||
|
||||
// 调用ollama API
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("model", "openllama");
|
||||
requestBody.put("prompt", fullPrompt);
|
||||
requestBody.put("temperature", 0.7);
|
||||
requestBody.put("max_tokens", 200);
|
||||
|
||||
// 替换为你的ollama服务地址
|
||||
String response = restTemplate.postForObject(
|
||||
"http://192.168.1.9:11434/completions",
|
||||
requestBody,
|
||||
String.class
|
||||
);
|
||||
|
||||
// 解析响应
|
||||
if (response != null && response.contains("text")) {
|
||||
return response.split("text\": \"")[1].split("\"")[0];
|
||||
}
|
||||
|
||||
return "抱歉,我无法回答这个问题。";
|
||||
}
|
||||
|
||||
// 调用Ollama API获取文本向量
|
||||
public Float[] getTextEmbeddingFromOllama(String text) {
|
||||
try {
|
||||
// 构造请求体
|
||||
JsonObject requestBody = new JsonObject();
|
||||
JsonArray inputArray = new JsonArray();
|
||||
inputArray.add(text);
|
||||
requestBody.add("input", inputArray);
|
||||
requestBody.addProperty("model", "nomic-embed-text");
|
||||
|
||||
// 创建HTTP请求
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>(gson.toJson(requestBody), headers);
|
||||
|
||||
// 发送POST请求
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(OLLAMA_API_URL, entity, String.class);
|
||||
|
||||
if (response.getStatusCode() != HttpStatus.OK) {
|
||||
System.out.println("Ollama API请求失败: " + response.getStatusCode());
|
||||
return new Float[0];
|
||||
}
|
||||
|
||||
// 解析响应数据
|
||||
String responseBody = response.getBody();
|
||||
JsonObject jsonResponse = gson.fromJson(responseBody, JsonObject.class);
|
||||
JsonArray embeddingsArray = jsonResponse.getAsJsonArray("data")
|
||||
.get(0).getAsJsonObject().getAsJsonArray("embedding");
|
||||
|
||||
// 将嵌入向量转换为float数组
|
||||
Float[] vector = new Float[embeddingsArray.size()];
|
||||
for (int i = 0; i < embeddingsArray.size(); i++) {
|
||||
vector[i] = embeddingsArray.get(i).getAsFloat();
|
||||
}
|
||||
|
||||
return vector;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return new Float[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package ssw.agent.service;
|
||||
|
||||
import io.weaviate.client.Config;
|
||||
import io.weaviate.client.WeaviateAuthClient;
|
||||
import io.weaviate.client.WeaviateClient;
|
||||
import io.weaviate.client.base.Result;
|
||||
import io.weaviate.client.v1.auth.exception.AuthException;
|
||||
import io.weaviate.client.v1.graphql.model.GraphQLResponse;
|
||||
import io.weaviate.client.v1.graphql.query.argument.NearVectorArgument;
|
||||
import io.weaviate.client.v1.graphql.query.fields.Field;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class WeaviateService {
|
||||
|
||||
private final WeaviateClient client;
|
||||
|
||||
public WeaviateService() throws AuthException {
|
||||
// 替换为你的Weaviate服务地址
|
||||
// 设置Weaviate的API密钥
|
||||
String weaviateApiKey = "WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih"; // 替换为你的Weaviate API密钥
|
||||
// 创建Weaviate客户端配置
|
||||
Config config = new Config("http", "192.168.1.9:192");
|
||||
config.getHeaders().put("Content-Type","application/json; charset=UTF-8");
|
||||
// 设置API密钥作为请求头中的Bearer Token
|
||||
|
||||
this.client = WeaviateAuthClient.apiKey(config, weaviateApiKey);
|
||||
}
|
||||
|
||||
public Result<GraphQLResponse> searchSimilarDocuments(Float[] query, int k) {
|
||||
String className = "Vector_index_b646fcf2_902d_4070_88cb_03e2873f578a_Node";
|
||||
Field title = Field.builder().name("text").build();
|
||||
Field _additional = Field.builder()
|
||||
.name("_additional")
|
||||
.fields(new Field[]{
|
||||
Field.builder().name("certainty").build(), // only supported if distance==cosine
|
||||
Field.builder().name("distance").build() // always supported
|
||||
}).build();
|
||||
|
||||
NearVectorArgument nearVector = NearVectorArgument.builder()
|
||||
.vector(query)
|
||||
.build();
|
||||
|
||||
Result<GraphQLResponse> result = client.graphQL().get()
|
||||
.withClassName(className)
|
||||
.withFields(title, _additional)
|
||||
.withNearVector(nearVector)
|
||||
.run();
|
||||
|
||||
if (result.hasErrors()) {
|
||||
System.out.println(result.getError());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>ssw-dos</artifactId>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<version>6.2.0-RELEASE</version>
|
||||
</parent>
|
||||
<artifactId>ssw-dos-entity</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jnpf</groupId>
|
||||
<artifactId>jnpf-common-all</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
Reference in new issue
Block a user