AI Personal Learning
and practical guidance
Beanbag Marscode1

Deconstructing the cue words applied in the process of building a knowledge graph using a big model (example)

Given that many to common sense use of knowledge graph to enhance the recall rate or as a long-term memory storage of friends did not understand the big model era of knowledge graph construction method, here simple popularization of common sense, and the actual construction will have more differences.

In fact, the knowledge graph is simple to say the least, and the big model is responsible for three main blocks in its process: relationship extraction, constructing queries, and answering questions.


In fact there are many more details, as shown in the following example: (optimizing the intermediate query process)

 

Knowledge graph cue word construction

# 步骤1: 问题分析提示词
QUESTION_ANALYSIS_PROMPT = """
You are an expert query analyzer. Your task is to analyze the given question and extract key search terms.

Input Question: {query}

Follow these steps:
1. Identify main entities (nouns, proper nouns)
2. Extract important attributes (adjectives, descriptors)
3. Identify relationship indicators (verbs, prepositions)
4. Note any temporal or conditional terms

Format your output as:
{
"main_entities": ["entity1", "entity2"...],
"attributes": ["attr1", "attr2"...],
"relationships": ["rel1", "rel2"...],
"conditions": ["cond1", "cond2"...]
}

Ensure each term is:
- Specific and relevant
- In its base/root form
- Without duplicates

Examples:
Q: "Who created the Python programming language?"
{
"main_entities": ["Python", "programming language"],
"attributes": ["created"],
"relationships": ["create", "develop"],
"conditions": []
}
"""

# 步骤2: 同义词扩展提示词
SYNONYM_EXPANSION_PROMPT = """
You are an expert synonym expansion system. Your task is to expand each term with relevant alternatives.

Input Terms: {terms}

For each term, provide:
1. Exact synonyms
2. Related terms
3. Common variations
4. Abbreviations/Acronyms
5. Full forms
6. Common misspellings

Rules:
- Include industry-standard terminology
- Consider different naming conventions
- Include both formal and informal terms
- Maintain semantic equivalence

Format your output as:
{
"term": {
"synonyms": ["syn1", "syn2"...],
"variations": ["var1", "var2"...],
"abbreviations": ["abbr1", "abbr2"...],
"related_terms": ["rel1", "rel2"...]
}
}

Example:
Input: "Python"
{
"Python": {
"synonyms": ["Python programming language", "Python lang"],
"variations": ["python", "Python3", "Python2"],
"abbreviations": ["py", ".py"],
"related_terms": ["CPython", "Jython", "IronPython"]
}
}
"""

# 步骤3: 查询构建提示词
QUERY_CONSTRUCTION_PROMPT = """
You are an expert in constructing graph database queries. Your task is to create an optimized search pattern.

Input:
- Primary Terms: {primary_terms}
- Expanded Terms: {expanded_terms}
- Relationships: {relationships}

Generate a query pattern that:
1. Prioritizes exact matches
2. Includes synonym matches
3. Considers relationship patterns
4. Handles variations in terminology

Rules:
- Start with most specific terms
- Include all relevant relationships
- Consider bidirectional relationships
- Limit path length appropriately

Format your output as:
{
"exact_match_patterns": ["pattern1", "pattern2"...],
"fuzzy_match_patterns": ["pattern1", "pattern2"...],
"relationship_patterns": ["pattern1", "pattern2"...],
"priority_order": ["high", "medium", "low"]
}

Example:
{
"exact_match_patterns": ["MATCH (n:Entity {name: 'Python'})", "MATCH (n:Language {type: 'programming'})"],
"fuzzy_match_patterns": ["MATCH (n) WHERE n.name =~ '(?i).*python.*'"],
"relationship_patterns": ["MATCH (creator)-[:CREATED]->(lang)", "MATCH (lang)-[:TYPE_OF]->(prog_lang)"],
"priority_order": ["exact_name_match", "fuzzy_name_match", "relationship_match"]
}
"""

# 步骤4: 结果排序提示词
RESULT_RANKING_PROMPT = """
You are an expert in ranking and ordering search results. Your task is to score and rank the retrieved matches.

Input Results: {query_results}
Original Query: {original_query}

Ranking Criteria:
1. Relevance to original query
2. Match quality (exact vs partial)
3. Relationship distance
4. Information completeness
5. Source reliability

Score each result on:
- Relevance (0-10)
- Confidence (0-10)
- Completeness (0-10)
- Path length penalty (-1 per hop)

Format your output as:
{
"ranked_results": [
{
"result": "result_content",
"relevance_score": score,
"confidence_score": score,
"completeness_score": score,
"final_score": score,
"reasoning": "explanation"
}
],
"summary": {
"total_results": number,
"high_confidence_count": number,
"average_score": number
}
}
"""

Based on the above cue words it can be seen that the intermediate process of building a knowledge graph can have a lot of details to deal with. The above example senses are not obvious and the following output example is given as a reference.

 

Knowledge graph content input and output

Step 1: Problem Analysis

Input.

{
    "query": "谷歌开发了哪些深度学习框架?"
}

Output.

{
    "main_entities": ["谷歌", "深度学习框架"],
    "attributes": ["开发"],
    "relationships": ["开发", "创建", "维护"],
    "conditions": []
}

Step 2: Expansion of Synonyms

Input.

{
    "terms": ["谷歌", "深度学习框架"]
}

Output.

{
    "谷歌": {
        "synonyms": ["Google LLC", "Google Inc.", "Google Corporation"],
        "variations": ["google", "GOOGLE"],
        "abbreviations": ["GOOG"],
        "related_terms": ["Alphabet Inc.", "Google AI", "Google Research"]
    },
    "深度学习框架": {
        "synonyms": ["深度学习库", "神经网络框架"],
        "variations": ["DL framework", "深度学习平台"],
        "abbreviations": ["DL framework", "DNN framework"],
        "related_terms": ["机器学习框架", "AI框架", "神经网络库"]
    }
}

Step 3: Query Construction

Input.

{
    "primary_terms": ["谷歌", "深度学习框架"],
    "expanded_terms": [
        "谷歌", "Google LLC", "Google Inc.", "Google AI",
        "深度学习框架", "深度学习库", "神经网络框架"
    ],
    "relationships": ["开发", "创建", "维护"]
}

Output.

{
    "exact_match_patterns": [
        "MATCH (org:Organization {name: 'Google'})",
        "MATCH (framework:Framework {type: 'deep_learning'})"
    ],
    "fuzzy_match_patterns": [
        "MATCH (org) WHERE org.name =~ '(?i).*google.*'",
        "MATCH (framework) WHERE framework.type =~ '(?i).*(deep learning|neural network).*'"
    ],
    "relationship_patterns": [
        "MATCH (org)-[:DEVELOPED]->(framework)",
        "MATCH (org)-[:CREATED]->(framework)",
        "MATCH (org)-[:MAINTAINS]->(framework)",
        "MATCH (framework)-[:DEVELOPED_BY]->(org)"
    ],
    "priority_order": [
        "exact_organization_match",
        "exact_framework_match",
        "relationship_match",
        "fuzzy_framework_match"
    ]
}

Step 4: Sort Results

Input.

{
    "query_results": [
        {
            "org": "谷歌",
            "framework": "TensorFlow",
            "relationship": "开发",
            "year": "2015"
        },
        {
            "org": "谷歌",
            "framework": "JAX",
            "relationship": "开发",
            "year": "2018"
        }
    ],
    "original_query": "谷歌开发了哪些深度学习框架?"
}

Output.

{
    "ranked_results": [
        {
            "result": "谷歌开发了TensorFlow,这是一款流行的深度学习框架",
            "relevance_score": 10.0,
            "confidence_score": 9.5,
            "completeness_score": 9.0,
            "final_score": 9.5,
            "reasoning": "直接答案,信心高 - TensorFlow是谷歌的主要深度学习框架"
        },
        {
            "result": "谷歌开发了JAX,一个深度学习框架",
            "relevance_score": 9.0,
            "confidence_score": 8.5,
            "completeness_score": 8.0,
            "final_score": 8.5,
            "reasoning": "相关但知名度低于TensorFlow"
        }
    ],
    "summary": {
        "total_results": 2,
        "high_confidence_count": 2,
        "average_score": 9.0
    }
}

Final Combined Query (Cypher).

// 1. 首先匹配组织和框架的关系
MATCH (org:Organization)-[r:DEVELOPED|CREATED|MAINTAINS]->(framework:Framework)
WHERE org.name =~ '(?i).*google.*'
  AND framework.type =~ '(?i).*(deep learning|neural network).*'

// 2. 保存中间结果
WITH org, framework, r

// 3. 寻找最短路径
MATCH p = shortestPath((org)-[*1..2]-(framework))

// 4. 返回路径并按流行度排序
RETURN p
ORDER BY framework.popularity DESC
LIMIT 10

Final Answer:Google has developed several deep learning frameworks," + "The most famous are TensorFlow (2015) and JAX (2018)." +"TensorFlow is their main framework and the most widely used.

The above implementation process is also not complete, it is recommended to disassemble a complete project for analysis, Llamaindex is a good choice. Below is a collection of some examples of cue words for you to get a feel for it.

 

Examples of Knowledge Graph Cue Words

Entity Relationship Extraction

系统:你是一名提取知识三元组的专家。 
你的任务是从给定文本中识别实体及其关系。

对于每段文本,生成格式如下的知识三元组: 
(subject, relation, object)

规则: 
1. 主体(subject)和客体(object)必须是具体的实体 
2. 关系(relation)应为清晰简洁的谓词 
3. 每个三元组应表示单一的、基于事实的陈述 
4. 避免泛化或模糊的关系 
5. 保持实体命名的一致性

输入文本:{text_chunk}

从上述文本中提取知识三元组。将每个三元组格式化为: 
(entity1) -> [relationship] -> (entity2)

Note: To aggregate entity relationships based on their industry, extracting entity relationship design is complex, so only the basic template is provided here.

 

Problem Analysis Cue Words

你是一位专业的查询分析专家。你的任务是分析给定的问题,并提取关键组件以用于知识图谱搜索。

输入问题: {query}

请仔细按照以下步骤操作:

1. 提取关键实体:
- 识别所有主要实体(名词、专有名词、技术术语)
- 列出提到的任何特定属性
- 记录任何数值或日期

2. 确定关系:
- 找出表示实体间关系的动词
- 确定显示连接的介词
- 注意任何隐含的关系

3. 检测查询类型:
- 判断是否属于以下类型:
* 事实查询
* 关系查询
* 比较查询
* 属性查询
* 时间线查询

4. 提取约束条件:
- 时间约束
- 地点约束
- 条件约束
- 数量约束

请按以下格式输出你的分析结果:
{
"entities": ["entity1", "entity2", ...],
"attributes": ["attribute1", "attribute2", ...],
"relationships": ["relationship1", "relationship2", ...],
"query_type": "type_of_query",
"constraints": {
"time": [],
"location": [],
"condition": [],
"quantity": []
}
}

记住:
- 精确识别实体
- 包括所有可能的术语变体
- 保持技术准确性
- 保留领域特定术语

 

你是一位专业的查询分析专家。你的任务是分析给定的问题并提取关键搜索词。

输入问题: {query}

按照以下步骤操作:
1. 确定主要实体(名词、专有名词)
2. 提取重要属性(形容词、描述词)
3. 确定关系指示词(动词、介词)
4. 注意任何时间或条件相关的术语

将输出格式化为:
{
"main_entities": ["entity1", "entity2"...],
"attributes": ["attr1", "attr2"...],
"relationships": ["rel1", "rel2"...],
"conditions": ["cond1", "cond2"...]
}

确保每个术语:
- 具体且相关
- 为基础/词根形式
- 无重复

示例:
Q: "谁创建了 Python 编程语言?"
{
"main_entities": ["Python", "编程语言"],
"attributes": ["创建"],
"relationships": ["create", "develop"],
"conditions": []
}

 

分析以下问题并提取关键术语以用于搜索: 
问题:{query}

提取: 
1. 主要实体 
2. 重要属性 
3. 关系指示词

将你的回答格式化为关键术语列表。

 

synonym expansion cue word

你是一位专业的同义词扩展系统。你的任务是为提供的术语生成全面的同义词列表,同时保持技术准确性和领域上下文的完整性。

输入术语:
{terms}

请为每个术语提供以下类别的扩展:

1. 精确同义词:
- 直接等价的术语
- 含义完全相同的变体

2. 相关术语:
- 更广泛的术语
- 更具体的术语
- 相关的概念

3. 缩写及替代形式:
- 常见缩写
- 完整形式
- 替代拼写
- 常见拼写错误

4. 领域特定变体:
- 技术术语
- 行业特定术语
- 在不同上下文中的常见用法

5. 复合术语:
- 相关复合词
- 短语变体
- 常见组合

规则:
1. 保持语义等价性
2. 保持技术准确性
3. 考虑领域上下文
4. 包含常见变体
5. 添加相关技术术语

按照以下格式组织你的回复:
{
"term": {
"exact_synonyms": [],
"related_terms": [],
"abbreviations": [],
"domain_terms": [],
"compound_terms": []
}
}

示例:
对于术语 "machine learning":
{
"machine learning": {
"exact_synonyms": ["ML", "machine learning"],
"related_terms": ["人工智能", "深度学习"],
"abbreviations": ["ML", "M.L."],
"domain_terms": ["统计学习", "计算学习"],
"compound_terms": ["机器学习算法", "ML模型"]
}
}

 

你是一位专业的同义词扩展系统。你的任务是为每个术语扩展相关的替代词。

输入术语: {terms}

对于每个术语,提供:
1. 完全同义词
2. 相关术语
3. 常见变体
4. 缩写/首字母缩写
5. 完整形式
6. 常见拼写错误

规则:
- 包含行业标准术语
- 考虑不同命名约定
- 包括正式和非正式术语
- 保持语义等价

将输出格式化为:
{
"term": {
"synonyms": ["syn1", "syn2"...],
"variations": ["var1", "var2"...],
"abbreviations": ["abbr1", "abbr2"...],
"related_terms": ["rel1", "rel2"...]
}
}

示例:
输入: "Python"
{
"Python": {
"synonyms": ["Python 编程语言", "Python lang"],
"variations": ["python", "Python3", "Python2"],
"abbreviations": ["py", ".py"],
"related_terms": ["CPython", "Jython", "IronPython"]
}
}

 

你是一个专业的同义词扩展系统。为列表中的每个单词查找同义词或常用于引用同一词语的相关词:

以下是一些示例:
- Palantir 的同义词可能是 Palantir technologies 或 Palantir technologies inc.
- Austin 的同义词可能是 Austin texas
- Taylor swift 的同义词可能是 Taylor
- Winter park 的同义词可能是 Winter park resort

格式: {format_instructions}

文本: {keywords}

 

 对于每个关键术语,提供常见的替代表达方式:
术语:{key_terms}

包括:
- 常见缩写
- 全称
- 类似概念
- 相关术语

 

Query Building Cue Words

你是一位知识图谱查询构造专家。你的任务是使用分析后的术语及其扩展,创建一个结构化的查询模式。

输入:
- 原始查询: {original_query}
- 分析组件: {analyzed_components}
- 扩展术语: {expanded_terms}

操作步骤:

1. 构建主要搜索模式:
考虑以下方面:
- 实体模式
- 关系模式
- 属性约束
- 路径模式

2. 定义搜索优先级:
将搜索元素分类为:
- 必须匹配的术语
- 应该匹配的术语
- 最好匹配的术语

3. 指定关系深度:
确定:
- 直接关系 (1-hop)
- 间接关系 (2-hop)
- 复杂路径 (多跳)

4. 设置约束条件:
包括:
- 时间过滤条件
- 类型约束
- 属性条件
- 值范围

输出格式:
{
"search_patterns": {
"primary_entities": [],
"secondary_entities": [],
"relationships": [],
"attributes": []
},
"priorities": {
"must_match": [],
"should_match": [],
"nice_to_match": []
},
"depth_config": {
"direct_relations": [],
"indirect_relations": [],
"complex_paths": []
},
"constraints": {
"time_filters": [],
"type_constraints": [],
"property_conditions": [],
"value_ranges": []
}
}

示例:
对于“Who contributed to TensorFlow in 2020?”(2020 年谁对 TensorFlow 做出了贡献?):
{
"search_patterns": {
"primary_entities": ["TensorFlow", "contributor"],
"secondary_entities": ["commit", "pull request"],
"relationships": ["contributed_to", "authored"],
"attributes": ["date", "contribution_type"]
},
"priorities": {
"must_match": ["TensorFlow", "2020"],
"should_match": ["contributor", "contribution"],
"nice_to_match": ["commit_message", "pull_request_title"]
}
...
}

 

你是一位构建图数据库查询的专家。你的任务是创建一个优化的搜索模式。

输入:
- 主要术语: {primary_terms}
- 扩展术语: {expanded_terms}
- 关系: {relationships}

生成一个查询模式,该模式需:
1. 优先匹配精确结果
2. 包括同义词匹配
3. 考虑关系模式
4. 处理术语变体

规则:
- 从最具体的术语开始
- 包括所有相关关系
- 考虑双向关系
- 适当限制路径长度

将输出格式化为:
{
"exact_match_patterns": ["pattern1", "pattern2"...],
"fuzzy_match_patterns": ["pattern1", "pattern2"...],
"relationship_patterns": ["pattern1", "pattern2"...],
"priority_order": ["high", "medium", "low"]
}

示例:
{
"exact_match_patterns": ["MATCH (n:Entity {name: 'Python'})", "MATCH (n:Language {type: 'programming'})"],
"fuzzy_match_patterns": ["MATCH (n) WHERE n.name =~ '(?i).*python.*'"],
"relationship_patterns": ["MATCH (creator)-[:CREATED]->(lang)", "MATCH (lang)-[:TYPE_OF]->(prog_lang)"],
"priority_order": ["exact_name_match", "fuzzy_name_match", "relationship_match"]
}

 

使用扩展的术语,创建一个搜索模式: 
术语:{expanded_terms}

生成: 
1. 主要搜索术语 
2. 次要术语 
3. 关系模式

 

鉴于以下问题:
{query}

提取关键概念并构建一个搜索模式,以帮助在知识图谱中找到相关信息。

关键概念:
- 确定主要实体
- 确定感兴趣的关系
- 考虑相似术语/同义词

搜索模式应包括:
1. 要查找的主要实体
2. 相关关系
3. 任何约束或条件

 

Result Sorting Cues

你是一位搜索结果排序和排列的专家。你的任务是对检索到的结果进行评分和排名。

输入结果: {query_results}
原始查询: {original_query}

排序标准:
1. 与原始查询的相关性
2. 匹配质量(精确 vs 部分)
3. 关系距离
4. 信息完整性
5. 来源可靠性

对每个结果评分:
- 相关性(0-10)
- 置信度(0-10)
- 完整性(0-10)
- 路径长度惩罚(每跳 -1)

将输出格式化为:
{
"ranked_results": [
{
"result": "result_content",
"relevance_score": score,
"confidence_score": score,
"completeness_score": score,
"final_score": score,
"reasoning": "explanation"
}
],
"summary": {
"total_results": number,
"high_confidence_count": number,
"average_score": number
}
}

 

Results Processing Answer Question Prompt Words

你是一名查询结果处理器。你的任务是将知识图谱查询结果处理并格式化为连贯的回复。

输入:
- 原始问题: {original_question}
- 查询结果: {query_results}
- 上下文信息: {context}

处理步骤:

1. 分析结果:
评估:
- 结果的完整性
- 结果的相关性
- 结果的质量
- 问题各方面的覆盖情况

2. 综合信息:
结合:
- 直接匹配的结果
- 间接关系
- 支持信息
- 上下文细节

3. 格式化回复:
结构化回复包括:
- 主要发现
- 支持性细节
- 相关上下文
- 置信度水平

4. 识别信息空白:
记录:
- 缺失信息
- 不确定的方面
- 潜在的后续步骤
- 可能的其他解释

输出格式:
{
"answer": {
"main_response": "",
"supporting_facts": [],
"confidence_level": "",
"information_gaps": []
},
"metadata": {
"sources_used": [],
"result_quality": "",
"processing_notes": []
},
"follow_up": {
"suggested_questions": [],
"clarification_needed": [],
"additional_context": []
}
}

指导原则:
- 精确和准确
- 保持技术正确性
- 指出置信度水平
- 记录任何不确定性
- 如有需要,建议后续问题

 

你是一位将图数据库查询结果合成为自然语言答案的专家。

输入:
1. 原始问题: {original_question}
2. 排序结果: {ranked_results}
3. 查询元数据: {query_metadata}

任务:
生成一个全面的答案,该答案需:
1. 直接回答原始问题
2. 融入来自排序结果的高置信度信息
3. 保持事实准确性并正确归属来源

指导原则:
- 从最相关的信息开始
- 当置信度较高时,包含支持性细节
- 承认任何不确定性或信息缺失
- 保持清晰简洁的风格
- 使用正确的技术术语

将你的回答格式化为如下形式:
{
"main_answer": "针对核心问题的主要回答",
"supporting_details": [
"附加相关事实 1",
"附加相关事实 2"
],
"metadata": {
"confidence_score": float,
"source_count": integer,
"information_completeness": float
},
"query_coverage": "对可用信息如何回答原始问题的解释"
}

示例输出:
{
"main_answer": "Google 开发了 TensorFlow 作为其主要的深度学习框架,并于 2015 年发布。",
"supporting_details": [
"Google 于 2018 年开发了另一个深度学习框架 JAX",
"TensorFlow 已成为使用最广泛的深度学习框架之一"
],
"metadata": {
"confidence_score": 9.5,
"source_count": 2,
"information_completeness": 0.95
},
"query_coverage": "查询结果提供了关于 Google 主要深度学习框架发展的全面信息"
}

 

根据检索到的信息: 
{context}

回答原始问题: 
{query}

提供一个清晰且简洁的回答,该回答需要: 
1. 直接回应问题 
2. 仅使用来自检索内容的信息 
3. 指出是否有任何信息缺失或不确定
May not be reproduced without permission:Chief AI Sharing Circle " Deconstructing the cue words applied in the process of building a knowledge graph using a big model (example)
en_USEnglish