diff --git a/pom.xml b/pom.xml index e968131..2cee079 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,7 @@ jnpf-admin ssw-common ssw-jyzs + ssw-dos diff --git a/ssw-dos/pom.xml b/ssw-dos/pom.xml new file mode 100644 index 0000000..e8b2b8c --- /dev/null +++ b/ssw-dos/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.jnpf + ssw-boot + 6.2.0-RELEASE + + pom + ssw-dos + + + ssw-dos-entity + ssw-dos-biz + + diff --git a/ssw-dos/ssw-dos-biz/pom.xml b/ssw-dos/ssw-dos-biz/pom.xml new file mode 100644 index 0000000..0e31472 --- /dev/null +++ b/ssw-dos/ssw-dos-biz/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + ssw-dos + com.jnpf + 6.2.0-RELEASE + + ssw-dos-biz + + + + com.jnpf + ssw-dos-entity + ${project.version} + + + + com.jnpf + ssw-common-biz + ${project.version} + + + com.jnpf + jnpf-common-all + ${project.version} + + + + io.weaviate + client + 5.1.3 + + + diff --git a/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/DialogController.java b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/DialogController.java new file mode 100644 index 0000000..6c0b733 --- /dev/null +++ b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/DialogController.java @@ -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); + } +} diff --git a/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/DialogService.java b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/DialogService.java new file mode 100644 index 0000000..bf41bbb --- /dev/null +++ b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/DialogService.java @@ -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 context = weaviateService.searchSimilarDocuments(vetor, 3); + + // 调用ollama生成回答 + String response = ollamaService.generateResponse(request, context); + + return response; + } +} diff --git a/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/OllamaService.java b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/OllamaService.java new file mode 100644 index 0000000..1866b3f --- /dev/null +++ b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/OllamaService.java @@ -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 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 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 entity = new HttpEntity<>(gson.toJson(requestBody), headers); + + // 发送POST请求 + ResponseEntity 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]; + } + } +} diff --git a/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/WeaviateService.java b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/WeaviateService.java new file mode 100644 index 0000000..2357f48 --- /dev/null +++ b/ssw-dos/ssw-dos-biz/src/main/java/ssw/agent/service/WeaviateService.java @@ -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 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 result = client.graphQL().get() + .withClassName(className) + .withFields(title, _additional) + .withNearVector(nearVector) + .run(); + + if (result.hasErrors()) { + System.out.println(result.getError()); + } + + return result; + } +} diff --git a/ssw-dos/ssw-dos-entity/pom.xml b/ssw-dos/ssw-dos-entity/pom.xml new file mode 100644 index 0000000..0eab16d --- /dev/null +++ b/ssw-dos/ssw-dos-entity/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + ssw-dos + com.jnpf + 6.2.0-RELEASE + + ssw-dos-entity + + + + com.jnpf + jnpf-common-all + ${project.version} + + +