|
|
@ -13,9 +13,11 @@ import com.supervision.pdfqaserver.service.DomainMetadataService;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.ai.chat.messages.Message;
|
|
|
|
import org.springframework.ai.chat.messages.Message;
|
|
|
|
|
|
|
|
import org.springframework.ai.chat.model.ChatResponse;
|
|
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
|
|
import org.springframework.ai.chat.prompt.Prompt;
|
|
|
|
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
|
|
|
|
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
|
|
|
|
import org.springframework.ai.ollama.OllamaChatModel;
|
|
|
|
import org.springframework.ai.ollama.OllamaChatModel;
|
|
|
|
|
|
|
|
import org.springframework.ai.ollama.api.OllamaOptions;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
|
|
|
|
|
|
@ -69,8 +71,10 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
SystemPromptTemplate textToCypherTemplate = new SystemPromptTemplate(PromptCache.promptMap.get(TEXT_TO_CYPHER));
|
|
|
|
SystemPromptTemplate textToCypherTemplate = new SystemPromptTemplate(PromptCache.promptMap.get(TEXT_TO_CYPHER));
|
|
|
|
Message textToCypherMessage = textToCypherTemplate.createMessage(Map.of(PROMPT_PARAM_SOURCE_TYPE_LIST, sourceTypeListEn, PROMPT_PARAM_RELATION_TYPE_LIST, relationListEn, PROMPT_PARAM_TARGET_TYPE_LIST, targetTypeListEn, PROMPT_PARAM_QUERY, userQuery));
|
|
|
|
Message textToCypherMessage = textToCypherTemplate.createMessage(Map.of(PROMPT_PARAM_SOURCE_TYPE_LIST, sourceTypeListEn, PROMPT_PARAM_RELATION_TYPE_LIST, relationListEn, PROMPT_PARAM_TARGET_TYPE_LIST, targetTypeListEn, PROMPT_PARAM_QUERY, userQuery));
|
|
|
|
log.info("生成CYPHER语句的消息:{}", textToCypherMessage);
|
|
|
|
log.info("生成CYPHER语句的消息:{}", textToCypherMessage);
|
|
|
|
String cypherJsonStr = ollamaChatModel.call(textToCypherMessage.getText());
|
|
|
|
String cypherJsonStr = ollamaChatModel.call(new Prompt(textToCypherMessage, OllamaOptions.builder().temperature(3.0).build())).getResult().getOutput().getText();
|
|
|
|
log.info(cypherJsonStr);
|
|
|
|
log.info(cypherJsonStr);
|
|
|
|
|
|
|
|
log.info(cypherJsonStr.replaceAll("(?is)<think\\b[^>]*>(.*?)</think>", "").trim());
|
|
|
|
|
|
|
|
cypherJsonStr = cypherJsonStr.replaceAll("(?is)<think\\b[^>]*>(.*?)</think>", "").trim();
|
|
|
|
List<String> cypherQueries;
|
|
|
|
List<String> cypherQueries;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject jsonObj = JSONUtil.parseObj(cypherJsonStr);
|
|
|
|
JSONObject jsonObj = JSONUtil.parseObj(cypherJsonStr);
|
|
|
@ -78,7 +82,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
.toList(String.class);
|
|
|
|
.toList(String.class);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("解析CYPHER JSON字符串失败: {}", e.getMessage());
|
|
|
|
log.error("解析CYPHER JSON字符串失败: {}", e.getMessage());
|
|
|
|
return Flux.just("查无结果");
|
|
|
|
return Flux.just("查无结果").concatWith(Flux.just("[END]"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("转换后的Cypher语句:{}", cypherQueries.toString());
|
|
|
|
log.info("转换后的Cypher语句:{}", cypherQueries.toString());
|
|
|
|
|
|
|
|
|
|
|
@ -90,13 +94,14 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (relationObjects.isEmpty()) {
|
|
|
|
if (relationObjects.isEmpty()) {
|
|
|
|
return Flux.just("查无结果");
|
|
|
|
return Flux.just("查无结果").concatWith(Flux.just("[END]"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("三元组数据: {}", relationObjects);
|
|
|
|
log.info("三元组数据: {}", relationObjects);
|
|
|
|
|
|
|
|
|
|
|
|
//生成回答
|
|
|
|
//生成回答
|
|
|
|
SystemPromptTemplate generateAnswerTemplate = new SystemPromptTemplate(PromptCache.promptMap.get(GENERATE_ANSWER));
|
|
|
|
SystemPromptTemplate generateAnswerTemplate = new SystemPromptTemplate(PromptCache.promptMap.get(GENERATE_ANSWER));
|
|
|
|
Message generateAnswerMessage = generateAnswerTemplate.createMessage(Map.of(PROMPT_PARAM_EXAMPLE_TEXT, relationObjects, PROMPT_PARAM_QUERY, userQuery));
|
|
|
|
Message generateAnswerMessage = generateAnswerTemplate.createMessage(Map.of(PROMPT_PARAM_EXAMPLE_TEXT, relationObjects, PROMPT_PARAM_QUERY, userQuery));
|
|
|
|
return ollamaChatModel.stream(new Prompt(generateAnswerMessage)).map(response -> response.getResult().getOutput().getText());
|
|
|
|
log.info("生成回答的提示词:{}", generateAnswerMessage);
|
|
|
|
|
|
|
|
return ollamaChatModel.stream(new Prompt(generateAnswerMessage)).map(response -> response.getResult().getOutput().getText()).concatWith(Flux.just("[END]"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|