优化text-to-cypher提示词

v_0.0.2
xueqingkun 2 weeks ago
parent 89140d638f
commit fd4641cc48

@ -793,38 +793,38 @@ public class PromptCache {
private static final String TEXT_TO_CYPHER_2_PROMPT = """ private static final String TEXT_TO_CYPHER_2_PROMPT = """
CypherCypher`neo4j_schema` CypherCypher`neo4j_schema`
{question} {question}
JSON neo4j_schemaJSON
{schema} {schema}
1. 1.
- 线 - 线
- -
- -
- -
- -
2. 2.
- -
- -
'\\I could not generate a Cypher script; the required information is not part of the Neo4j schema.\\\\n\\n' '\\I could not generate a Cypher script; the required information is not part of the Neo4j schema.\\\\n\\n'
3. MATCH 3. MATCH
- 使 - 使,****
- ****`-[r:REL_TYPE]->`
- 使 - 使
- WHERE - WHERE
4. RETURN 4. RETURN
- 使`RETURN nodes, relationships``RETURN path` - ********
- RETURN
- `RETURN path`
5. Cypher 5. Cypher
- Cypher - Cypher```cypher ```
- 使OPTIONAL MATCH - 使OPTIONAL MATCH
- RETURN使/no_think - ****MATCH ****
- cyphercypher/no_think
"""; """;
} }

@ -56,6 +56,7 @@ public class TripleToCypherExecutorImpl implements TripleToCypherExecutor {
log.info("没有找到匹配的意图query: {}", query); log.info("没有找到匹配的意图query: {}", query);
return null; return null;
} }
log.info("找到匹配的意图query: {}, relations: {}", query, relations.stream().map(Intention::getDigest).collect(Collectors.joining(",")));
List<DomainMetadataDTO> domainMetadataDTOS = domainMetadataService.listByIntentionIds(relations.stream().map(Intention::getId).toList()); List<DomainMetadataDTO> domainMetadataDTOS = domainMetadataService.listByIntentionIds(relations.stream().map(Intention::getId).toList());
CypherSchemaDTO schemaDTO = this.queryRelationSchema(domainMetadataDTOS); CypherSchemaDTO schemaDTO = this.queryRelationSchema(domainMetadataDTOS);
if (CollUtil.isEmpty(schemaDTO.getRelations()) && CollUtil.isEmpty(schemaDTO.getNodes())) { if (CollUtil.isEmpty(schemaDTO.getRelations()) && CollUtil.isEmpty(schemaDTO.getNodes())) {
@ -64,6 +65,7 @@ public class TripleToCypherExecutorImpl implements TripleToCypherExecutor {
} }
String prompt = promptMap.get(TEXT_TO_CYPHER_2); String prompt = promptMap.get(TEXT_TO_CYPHER_2);
String format = StrUtil.format(prompt, Map.of("question", query, "schema", schemaDTO.format())); String format = StrUtil.format(prompt, Map.of("question", query, "schema", schemaDTO.format()));
log.info("生成Cypher脚本query: {}, format: {}", query, format);
String call = aiCallService.call(format); String call = aiCallService.call(format);
if (StrUtil.contains(call,"I could not generate a Cypher script; the required information is not part of the Neo4j schema.")){ if (StrUtil.contains(call,"I could not generate a Cypher script; the required information is not part of the Neo4j schema.")){
log.info("大模型没能生成cypherquery: {}", query); log.info("大模型没能生成cypherquery: {}", query);
@ -209,19 +211,7 @@ public class TripleToCypherExecutorImpl implements TripleToCypherExecutor {
if (null == rel){ if (null == rel){
continue; continue;
} }
List<RelationExtractionDTO> relSourceType = cypherSchemaDTO.getRelations(sourceType); merged.add(rel);
List<RelationExtractionDTO> relTargetType = cypherSchemaDTO.getRelations(targetType);
relSourceType.add(rel);
relSourceType.addAll(relTargetType);
for (RelationExtractionDTO relationExtractionDTO : relSourceType) {
boolean none = merged.stream().noneMatch(i -> StrUtil.equals(i.getRelation(), relationExtractionDTO.getRelation()) &&
StrUtil.equals(i.getSourceType(), relationExtractionDTO.getSourceType()) &&
StrUtil.equals(i.getTargetType(), relationExtractionDTO.getTargetType()));
if (none){
merged.add(relationExtractionDTO);
}
}
} }
List<EntityExtractionDTO> entityExtractionDTOS = new ArrayList<>(); List<EntityExtractionDTO> entityExtractionDTOS = new ArrayList<>();
for (RelationExtractionDTO relationExtractionDTO : merged) { for (RelationExtractionDTO relationExtractionDTO : merged) {
@ -248,7 +238,7 @@ public class TripleToCypherExecutorImpl implements TripleToCypherExecutor {
String prompt = promptMap.get(CLASSIFY_QUERY_INTENT); String prompt = promptMap.get(CLASSIFY_QUERY_INTENT);
List<Intention> result = new ArrayList<>(); List<Intention> result = new ArrayList<>();
log.info("开始分类意图query: {}, intentions size: {}", query, intentions.size()); log.info("开始分类意图query: {}, intentions size: {}", query, intentions.size());
List<List<Intention>> intentionSplit = CollUtil.split(intentions, 200); List<List<Intention>> intentionSplit = CollUtil.split(intentions, 300);
for (List<Intention> intentionList : intentionSplit) { for (List<Intention> intentionList : intentionSplit) {
log.info("分类意图query: {}, intentions size: {}", query, intentionList.size()); log.info("分类意图query: {}, intentions size: {}", query, intentionList.size());
List<String> intents = intentionList.stream().map(Intention::getDigest).toList(); List<String> intents = intentionList.stream().map(Intention::getDigest).toList();
@ -256,7 +246,7 @@ public class TripleToCypherExecutorImpl implements TripleToCypherExecutor {
"intents", JSONUtil.toJsonStr(intents)); "intents", JSONUtil.toJsonStr(intents));
String format = StrUtil.format(prompt, params); String format = StrUtil.format(prompt, params);
String call = aiCallService.call(format); String call = aiCallService.call(format);
log.info("classifyIntents:分类意图结果query: {}, call: {}", query, call);
if (StrUtil.isEmpty(call)) { if (StrUtil.isEmpty(call)) {
return new ArrayList<>(); return new ArrayList<>();
} }

Loading…
Cancel
Save