提示词调整

master
daixiaoyi 1 month ago
parent 9a45ed30cc
commit 8431ab563e

@ -224,7 +224,9 @@ public class PromptCache {
"\\{
"cypherQueries": [
"MATCH (c:`公司`)-[r:`收购`]->(t:`公司`) RETURN c, r, t",
"MATCH (c:`上市公司`)-[r:`收购`]->(t:`公司`) RETURN c, r, t",
"MATCH (c:`公司`)-[r:`收购`]->(t:`上市公司`) RETURN c, r, t",
"MATCH (c:`电力公司`)-[r:`收购`]->(t:`国有企业`) RETURN c, r, t",
.....
]
\\}"
@ -245,15 +247,15 @@ public class PromptCache {
private static final String GENERATE_ANSWER_PROMPT = """
{query}
{example_text}
{query}
1. 使
2.
3.
"您好!当前系统功能聚焦于审计报告相关内容分析,您的问题暂不在支持范围内。如需查询财务数据、票据详情或其他审计相关信息,请提供具体问题,我们将全力协助。"
1. 使
2.
"抱歉目前知识库没有这个问题的答案."
3.
/no_think
""";

@ -13,9 +13,11 @@ import com.supervision.pdfqaserver.service.DomainMetadataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.SystemPromptTemplate;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
@ -69,8 +71,10 @@ public class ChatServiceImpl implements ChatService {
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));
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.replaceAll("(?is)<think\\b[^>]*>(.*?)</think>", "").trim());
cypherJsonStr = cypherJsonStr.replaceAll("(?is)<think\\b[^>]*>(.*?)</think>", "").trim();
List<String> cypherQueries;
try {
JSONObject jsonObj = JSONUtil.parseObj(cypherJsonStr);
@ -78,7 +82,7 @@ public class ChatServiceImpl implements ChatService {
.toList(String.class);
} catch (Exception e) {
log.error("解析CYPHER JSON字符串失败: {}", e.getMessage());
return Flux.just("查无结果");
return Flux.just("查无结果").concatWith(Flux.just("[END]"));
}
log.info("转换后的Cypher语句{}", cypherQueries.toString());
@ -90,13 +94,14 @@ public class ChatServiceImpl implements ChatService {
}
}
if (relationObjects.isEmpty()) {
return Flux.just("查无结果");
return Flux.just("查无结果").concatWith(Flux.just("[END]"));
}
log.info("三元组数据: {}", relationObjects);
//生成回答
SystemPromptTemplate generateAnswerTemplate = new SystemPromptTemplate(PromptCache.promptMap.get(GENERATE_ANSWER));
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]"));
}
}

Loading…
Cancel
Save