GraphRAG实现原理-09:调用LLM提取实体关系图介绍了如何利用extract_graph借助LLM的力量将所有的文档切片(TextUnit)转换成实体关系图,这张图具体体现在生成entities和relations表中。但是这张图目前仅仅描述了两个实体之间是否有关系,缺少必要的指标。在图论中,度数(Degree)的是一个非常重要的指标,它一个顶点所关联的边的数量。对于我们构建的实体关系图,如果某个实体度数越大,意味着它越接近于图的中心为止。实体的度数利用finalize_graph这个Workflow来计算并添加,这个Wokflow还会额外计算并添加关系组合度数。
1. 我们现在在哪里?
这是standard管道系列的第五篇文章,finalize_graph也是我们介绍的五个Workflow。经过前面的四道工具,我们已经拥有了documents和text_units着两个表,前者存储完整的文档,后者存储分割文档生成的切片,两者之间通过text_unit_ids和document_id保持双向引用。extract_graph这个Workflow进一步将提取出来的实体关系图保存在entities和relations表中。
load_input_documents
└── create_base_text_units
└── create_final_documents
└── extract_graph
└── **finalize_graph**
└── extract_covariates
└── create_communities
└── create_final_text_units
└──create_community_reports
└── generate_text_embeddings
通过GraphRAG实现原理-09:调用LLM提取实体关系图提供的实例演示我们指导目前的entities和relations表具有如下的结果:
entities表:
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 title 34 non-null str
1 type 34 non-null str
2 text_unit_ids 34 non-null object
3 frequency 34 non-null int64
4 description 34 non-null str
relations表:
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 source 33 non-null str
1 target 33 non-null str
2 text_unit_ids 33 non-null object
3 weight 33 non-null float64
4 description 33 non-null str
finalize_graph这个Workflow旨在为提取的实体关系图生成如下两个指标:
- 实体度数(Degree):指一个实体节点在图中直接相连的关系(边)数量。一个实体的度数越高,说明它与越多其他实体存在关系,在图中处于越中心的连接位置;
- 关系组合度数(Combined Degree):这是GraphRAG对关系边引入的一个派生指标,计算方式为该关系源实体的度数加上目标实体的度数。它用于衡量一条关系所连接的两个端点各自的活跃程度之和,帮助识别那些连接着高连接性实体的重要关系。
2. Workflow的实现
finalize_graph这个Workflow的WorkflowFunction对应如下这个run_workflow函数(所在模块目录为graphrag/workflows/finalize_graph)。它会利用PipelineRunContext上下文提供的TableProvider开启entities和relationships表,并将它们作为参数调用finalize_graph函数完成上述两个指标的计算和写入。如果GraphRagConfig提供的配置开启了记录基于GraphML的快照,还会进一步读取relationships表并将其转换成DataFrame,并将后者作为参数调用snapshot_graphml函数将最终构建的图以GraphML的形式保存下来。当我们需要可视化展现图的时候,存下来的这个快照会很有用。
async def run_workflow(
config: GraphRagConfig,
context: PipelineRunContext,
) -> WorkflowFunctionOutput:
async with (
context.output_table_provider.open(
"entities",
transformer=transform_entity_row,
) as entities_table,
context.output_table_provider.open(
"relationships",
transformer=transform_relationship_row,
) as relationships_table,
):
result = await finalize_graph(
entities_table,
relationships_table,
)
if config.snapshots.graphml:
rels = await context.output_table_provider.read_dataframe("relationships")
await snapshot_graphml(
rels,
name="graph",
storage=context.output_storage,
)
return WorkflowFunctionOutput(result=result)
finalize_graph函数的处理逻辑很简单:就是读取每一条关系记录,然后调用_build_degree_map计算每个实体的度数(不考虑方向),然后基于这个度数分别调用finalize_entities将实体实体度数写入entities表,在调用finalize_relationships函数计算每个关系的关系组合度数(两个实体度数之和)并写入relationships表。最终返回前5条entities和relationships记录。
async def finalize_graph(
entities_table: Table,
relationships_table: Table,
) -> dict[str, list[dict[str, Any]]]:
degree_map = await _build_degree_map(relationships_table)
entity_samples = await finalize_entities(entities_table, degree_map)
relationship_samples = await finalize_relationships(relationships_table, degree_map)
return {
"entities": entity_samples,
"relationships": relationship_samples,
}
async def _build_degree_map(
relationships_table: Table,
) -> dict[str, int]:
seen: set[tuple[str, str]] = set()
degree: Counter[str] = Counter()
async for row in relationships_table:
lo, hi = sorted((row["source"], row["target"]))
if (lo, hi) not in seen:
seen.add((lo, hi))
degree[lo] += 1
degree[hi] += 1
return dict(degree)
async def finalize_entities(
entities_table: Table,
degree_map: dict[str, int],
) -> list[dict[str, Any]]:
sample_rows: list[dict[str, Any]] = []
seen_titles: set[str] = set()
human_readable_id = 0
async for row in entities_table:
title = row.get("title")
if not title or title in seen_titles:
continue
seen_titles.add(title)
row["degree"] = degree_map.get(title, 0)
row["human_readable_id"] = human_readable_id
row["id"] = str(uuid4())
human_readable_id += 1
out = {col: row.get(col) for col in ENTITIES_FINAL_COLUMNS}
await entities_table.write(out)
if len(sample_rows) < 5:
sample_rows.append(out)
return sample_rows
async def finalize_relationships(
relationships_table: Table,
degree_map: dict[str, int],
) -> list[dict[str, Any]]:
sample_rows: list[dict[str, Any]] = []
seen: set[tuple[str, str]] = set()
human_readable_id = 0
async for row in relationships_table:
key = (row.get("source", ""), row.get("target", ""))
if key in seen:
continue
seen.add(key)
row["combined_degree"] = degree_map.get(key[0], 0) + degree_map.get(key[1], 0)
row["human_readable_id"] = human_readable_id
row["id"] = str(uuid4())
human_readable_id += 1
final = {col: row.get(col) for col in RELATIONSHIPS_FINAL_COLUMNS}
await relationships_table.write(final)
if len(sample_rows) < 5:
sample_rows.append(final)
return sample_rows
3. 查看最终的实体关系图
#[GraphRAG实现原理-09]调用LLM提取实体关系图()提供了演示程序完成了初始实体关系图的构建,现在我们在此基础上应用finalize_graph这个Workflow完成针对实体度数和关系组合度数的计算和写入。我们通过如下的程序执行一个由finalize_graph这个单一Workflow构建的管道。在完成管道执行后,我们将./output/entities.parquet和./output/relationships.parquet加载到一个DataFrame中,并将两个表的结构和数据输出来。从输出可以看出:实体度数通过degree字段附加到每个实体记录,关系组合度数通过combined_degree字段附加到每一条关系记录上。
import asyncio
import graphrag.index.workflows.finalize_graph as finalize_graph
from graphrag.index.typing.pipeline import Pipeline
from graphrag.config.load_config import load_config
from graphrag.index.run.run_pipeline import run_pipeline
from graphrag.callbacks.noop_workflow_callbacks import NoopWorkflowCallbacks
import pandas as pd
async def main():
pipeline = Pipeline(workflows=[
("finalize_graph", finalize_graph.run_workflow)
])
async for _ in run_pipeline(
pipeline = pipeline,
config = load_config(root_dir="./"),
callbacks= NoopWorkflowCallbacks(),
):
pass
print(f"----------entities table schema----------")
df = pd.read_parquet('./output/entities.parquet')
print(df.info())
print(f"----------entities----------")
for _, row in df.iterrows():
for col in df.columns:
print(f"{col}: {row[col]}, ")
print()
print(f"----------relationships table schema----------")
df = pd.read_parquet('./output/relationships.parquet')
print(df.info())
print(f"----------relationships----------")
for _, row in df.iterrows():
for col in df.columns:
print(f"{col}: {row[col]}, ")
print()
asyncio.run(main())
entities表的结构:
<class 'pandas.DataFrame'>
RangeIndex: 34 entries, 0 to 33
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 id 34 non-null str
1 human_readable_id 34 non-null int64
2 title 34 non-null str
3 type 34 non-null str
4 description 34 non-null str
5 text_unit_ids 34 non-null object
6 frequency 34 non-null int64
7 degree 34 non-null int64
dtypes: int64(3), object(1), str(4)
memory usage: 7.7+ KB
None
entities表的数据:
id: 9c867d16-5825-44a6-bbb1-c6d61d888a48,
human_readable_id: 0,
title: 李明,
type: PERSON,
description: 李明是一位在杭州经营咖啡店的个体经营者。他在2024年3月决定开设一家咖啡店,并在考察多个选址后,最终选择了一个写字楼商圈的商业区域,签下了为期两年的租约。开业后,李明将门店的核心产品定位为拿铁和美式咖啡,同时推出了季节限定的桂花拿铁,以丰富产品线并提升门店吸引力。
在门店运营方面,李明在开业初期通过小红书和抖音进行引流,借助社交平台扩大曝光和吸引顾客。他还建立了会员体系,以增强顾客复购和长期黏性,并与周边商家开展合作互推,借助社区联动拓展客源。为保障供应链稳定,李明后来又增加了一家供应商作为备选,以降低单一供应风险,提升门店运营的稳定性。,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'
'a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 4,
degree: 17,
id: 3a823ac8-7f73-4bf2-9ff3-642a0fe1212e,
human_readable_id: 1,
title: 杭州,
type: GEO,
description: Hangzhou is the city where 李明 planned to open a coffee shop,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
frequency: 1,
degree: 1,
id: 01a9d251-99e8-41fc-9750-7572448e35c1,
human_readable_id: 2,
title: 学校附近,
type: GEO,
description: The area near a school, evaluated by 李明 as a potential coffee shop location with high foot traffic but high rent,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
frequency: 1,
degree: 1,
id: 53634c26-e8c8-48f4-9998-0b9fd7488ba1,
human_readable_id: 3,
title: 写字楼商圈,
type: GEO,
description: An office-district commercial area evaluated by 李明 as a potential coffee shop location with stable weekday traffic and quiet weekends; it was ultimately chosen for the coffee shop,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
frequency: 1,
degree: 2,
id: 51225d37-3298-4650-944a-bae033f38527,
human_readable_id: 4,
title: 老城区街角,
type: GEO,
description: A street corner in the old城区 evaluated by 李明 as a potential coffee shop location with low rent but fewer young customers,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
frequency: 1,
degree: 1,
id: 9c277686-5f60-4ee0-b55b-8fae694b5c82,
human_readable_id: 5,
title: 咖啡店,
type: EVENT,
description: The planned opening of a coffee shop by 李明 in Hangzhou in March 2024,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
frequency: 1,
degree: 2,
id: 4d104a70-3605-4c87-81ef-e1300f3412e6,
human_readable_id: 6,
title: 王芳,
type: PERSON,
description: 王芳 is the store manager who suggested adding member discounts to improve repurchase rate,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 2,
id: 958ed09e-c043-475a-a72d-f9bac6e7c4f5,
human_readable_id: 7,
title: 拿铁,
type: EVENT,
description: Latte is one of the core products chosen by 李明, priced at 28 yuan and described as suitable for大众口味,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 2,
id: 4377333d-b67a-4765-aaab-29df431a9e9a,
human_readable_id: 8,
title: 美式,
type: EVENT,
description: Americano is one of the core products chosen by 李明, priced at 22 yuan and aimed at coffee lovers,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 2,
id: 28492532-d148-49de-b33b-e6e52b39831c,
human_readable_id: 9,
title: 桂花拿铁,
type: EVENT,
description: Osmanthus latte is a seasonal limited product launched by 李明 in May 2024, with first-month sales accounting for 30% of the total,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 3,
id: f4b15245-448c-4b7f-87ef-080a988ac0c3,
human_readable_id: 10,
title: 会员折扣,
type: EVENT,
description: A proposed member discount promotion suggested by 王芳 to improve repurchase rate,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 2,
id: d9c520c1-7f41-4ccc-a527-96505d81cc0d,
human_readable_id: 11,
title: 复购率,
type: EVENT,
description: The repurchase rate metric that 王芳 wanted to improve through member discounts,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 1,
id: 5810ddc3-916b-4c3c-b9f9-c286ef68ebca,
human_readable_id: 12,
title: 季节限定款,
type: EVENT,
description: A seasonal limited product launch category associated with 桂花拿铁 in 2024年5月,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 1,
id: 17d517b8-1208-4f17-8837-517aa2328df7,
human_readable_id: 13,
title: 首月销量,
type: EVENT,
description: The first-month sales performance metric for 桂花拿铁, which accounted for 30% of total sales,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 2,
id: 0b896a65-d80b-4abc-ac5c-9c1f58e4a6cc,
human_readable_id: 14,
title: 总额,
type: EVENT,
description: The total sales amount used as the basis for calculating 桂花拿铁's first-month share,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 1,
id: 26b2de16-aca9-4dd9-bc1e-1a7e37be3f8b,
human_readable_id: 15,
title: 大众口味,
type: EVENT,
description: A broad consumer taste segment that 拿铁 is intended to suit,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 1,
id: f951e814-7a29-4461-bef0-4dc2bdb07211,
human_readable_id: 16,
title: 咖啡爱好者,
type: EVENT,
description: A target customer group that 美式 is aimed at,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
frequency: 1,
degree: 1,
id: bb1ec95e-8fa8-4828-b79c-ff063e7e1bbb,
human_readable_id: 17,
title: 小红书,
type: ORGANIZATION,
description: 小红书是李明用于开业初期引流的社交平台之一,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 1,
degree: 1,
id: 98412328-9620-4467-8b4a-334e8edfe300,
human_readable_id: 18,
title: 抖音,
type: ORGANIZATION,
description: 抖音是李明用于开业初期引流的社交平台之一,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 2,
degree: 1,
id: 25744a41-96ae-49aa-a1fe-26192cc27dc4,
human_readable_id: 19,
title: 周边书店,
type: ORGANIZATION,
description: 周边书店是与李明开展合作互推的本地商家之一,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 2,
degree: 1,
id: 76353321-1578-4788-98ce-de7b7c141b89,
human_readable_id: 20,
title: 健身房,
type: ORGANIZATION,
description: 健身房是与李明开展合作互推的本地商家之一,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 2,
degree: 1,
id: 16bfc6a9-195a-4b15-97b2-b112fc72d1a5,
human_readable_id: 21,
title: 会员体系,
type: EVENT,
description: 会员体系是李明于2024年6月建立的营销和客户留存活动,通过充值赠送优惠来提升会员数量,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 2,
degree: 2,
id: 09994328-862a-48dc-b852-4e72ac39603e,
human_readable_id: 22,
title: 开业初期,
type: EVENT,
description: 开业初期是李明进行引流并在首周新增粉丝五千人的时间阶段,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 2,
degree: 1,
id: 1b3f34e9-366d-40f8-b608-a1e25cf129e0,
human_readable_id: 23,
title: 2024年6月,
type: EVENT,
description: 2024年6月是李明建立会员体系的时间节点,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
frequency: 2,
degree: 1,
id: c84d0f9c-5ac7-4e90-b7a1-a6fc947f2f75,
human_readable_id: 24,
title: 张伟,
type: PERSON,
description: 张伟 is a supplier in Pu'er, Yunnan, who supplies coffee beans once a month at a unit price of 120 yuan per kilogram),
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 2,
id: 6fa972a6-e769-4c63-8cd7-0ea32ba41001,
human_readable_id: 25,
title: 云南普洱,
type: GEO,
description: Yunnan Pu'er is the geographic location of the coffee-bean supplier Zhang Wei),
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 1,
id: f446b208-63cc-4a1f-b4da-9a6e75bf37fd,
human_readable_id: 26,
title: 本地乳企,
type: ORGANIZATION,
description: A local dairy enterprise that delivers milk to the store every day),
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 2,
id: 9c47f7bd-2313-430f-a848-540699a7e06a,
human_readable_id: 27,
title: 第二家供应商,
type: ORGANIZATION,
description: An additional backup supplier added by Li Ming to reduce supply disruption risk),
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 1,
id: fdaf7e37-c7bd-4fef-a61f-ac827ca1178b,
human_readable_id: 28,
title: 2024年7月,
type: EVENT,
description: A time period in July 2024 when rainy-season transportation delays caused the store to run out of stock for two days and lose about 4,000 yuan),
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 2,
id: 0b9fc0a4-29b1-4940-9e3b-d88ef0963c33,
human_readable_id: 29,
title: 牛奶,
type: ORGANIZATION,
description: Milk is a supplied product delivered daily by a local dairy enterprise,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 1,
id: 03add403-9061-4bb1-8e6a-0b1a04b1233d,
human_readable_id: 30,
title: 门店,
type: ORGANIZATION,
description: The store experienced a two-day stockout and financial loss due to rainy-season transportation delays,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 2,
id: ada56761-6a06-42c4-9275-137d01dafa33,
human_readable_id: 31,
title: 雨季运输延误,
type: EVENT,
description: Rainy-season transportation delays in July 2024 caused the store to run out of stock for two days and lose about 4,000 yuan,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 2,
id: a4c8eba2-8b33-4c1e-bb59-5ded8c2a76bc,
human_readable_id: 32,
title: 断货两天,
type: EVENT,
description: A two-day stockout event at the store caused by transportation delays during the rainy season,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 2,
id: 26e053a6-5d80-489d-bc0b-0357cf2ad3f1,
human_readable_id: 33,
title: 损失约四千元,
type: EVENT,
description: A financial-loss event in which the store lost about 4,000 yuan due to the stockout),
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
frequency: 1,
degree: 1,
relationships表的结构:
<class 'pandas.DataFrame'>
RangeIndex: 33 entries, 0 to 32
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 id 33 non-null str
1 human_readable_id 33 non-null int64
2 source 33 non-null str
3 target 33 non-null str
4 description 33 non-null str
5 weight 33 non-null float64
6 combined_degree 33 non-null int64
7 text_unit_ids 33 non-null object
dtypes: float64(1), int64(2), object(1), str(4)
memory usage: 6.1+ KB
None
relationships表记录:
id: 735bc825-d971-47cf-90a4-f0fa0c3ef813,
human_readable_id: 0,
source: 李明,
target: 杭州,
description: 李明 decided to open a coffee shop in Hangzhou,
weight: 9.0,
combined_degree: 18,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
id: 1c63f47b-b87b-41a8-b466-329409c61681,
human_readable_id: 1,
source: 李明,
target: 咖啡店,
description: 李明 planned and initiated the opening of the coffee shop,
weight: 10.0,
combined_degree: 19,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
id: a040ce72-ed4c-48d0-a107-a0b20bc03bbe,
human_readable_id: 2,
source: 李明,
target: 学校附近,
description: 李明 evaluated the area near a school as one possible location for the coffee shop,
weight: 8.0,
combined_degree: 18,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
id: a5d33262-6b05-4c5d-84a2-68d8e2a8d998,
human_readable_id: 3,
source: 李明,
target: 写字楼商圈,
description: 李明 evaluated the office-district commercial area and ultimately selected it for the coffee shop,
weight: 10.0,
combined_degree: 19,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
id: 5d116773-3dd5-48d5-bb0a-33b75e5999f4,
human_readable_id: 4,
source: 李明,
target: 老城区街角,
description: 李明 evaluated the old-town street corner as one possible location for the coffee shop,
weight: 8.0,
combined_degree: 18,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
id: 0320c25e-6265-48ad-80cb-f789d930c9e6,
human_readable_id: 5,
source: 咖啡店,
target: 写字楼商圈,
description: The coffee shop was ultimately planned to be located in the office-district commercial area,
weight: 9.0,
combined_degree: 4,
text_unit_ids: ['301f5f4fbb4ea9f27ad8305acf10c48413f6e090bb00cdc9db5222a0a87373aaf005d1aeb3b918324e1849e02937990c8262b268b1fadafab4d960f34cd2a707'],
id: 0ed642d8-fb07-4d1d-9db8-243aa9c387ee,
human_readable_id: 6,
source: 李明,
target: 拿铁,
description: 李明 designated 拿铁 as one of the core products,
weight: 9.0,
combined_degree: 19,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: e1515b8e-f9bd-4f8a-87b9-ec8cb7175e34,
human_readable_id: 7,
source: 李明,
target: 美式,
description: 李明 designated 美式 as one of the core products,
weight: 9.0,
combined_degree: 19,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: 40ca6bc6-6c05-4d6c-9640-5839d02ef30a,
human_readable_id: 8,
source: 李明,
target: 桂花拿铁,
description: 李明于2024年5月推出了季节限定的桂花拿铁,并在同一时间发布了桂花拿铁这一产品。该产品由李明主导上线,属于2024年5月推出的限定饮品,体现了其在该时期对季节性新品的开发与推广。,
weight: 19.0,
combined_degree: 20,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'
'a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: baff7cc1-b90a-4f84-9d73-827220fdf74c,
human_readable_id: 9,
source: 王芳,
target: 李明,
description: 王芳 is the store manager giving a suggestion in the same business context as 李明's product strategy,
weight: 1.0,
combined_degree: 19,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: 906e6321-cc8c-49ba-8d21-da897af7c358,
human_readable_id: 10,
source: 拿铁,
target: 大众口味,
description: 拿铁 is described as suitable for 大众口味,
weight: 8.0,
combined_degree: 3,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: 2328ef49-dc98-43b3-9e71-2626a472feee,
human_readable_id: 11,
source: 美式,
target: 咖啡爱好者,
description: 美式 is described as aimed at 咖啡爱好者,
weight: 8.0,
combined_degree: 3,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: 94758fba-7bb9-4e86-baf2-6f53122cc1b4,
human_readable_id: 12,
source: 桂花拿铁,
target: 季节限定款,
description: 桂花拿铁 is a seasonal limited product,
weight: 9.0,
combined_degree: 4,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: f1b50cf1-e121-436e-a911-2598e8aaf01e,
human_readable_id: 13,
source: 桂花拿铁,
target: 首月销量,
description: 桂花拿铁 had first-month sales performance described by 首月销量,
weight: 7.0,
combined_degree: 5,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: f3fedcca-40ec-4ddf-a710-d9591065fd2e,
human_readable_id: 14,
source: 首月销量,
target: 总额,
description: 首月销量 is measured as a share of 总额,
weight: 7.0,
combined_degree: 3,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: bf1f251b-4ece-4872-abd2-5a1fcc79789d,
human_readable_id: 15,
source: 王芳,
target: 会员折扣,
description: 王芳 suggested adding 会员折扣,
weight: 10.0,
combined_degree: 4,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: 50faa652-88e2-45fa-8b8e-6205baf8ba6b,
human_readable_id: 16,
source: 会员折扣,
target: 复购率,
description: 会员折扣 was proposed to improve 复购率,
weight: 1.0,
combined_degree: 3,
text_unit_ids: ['a63ee3ab5c7fb0edf8a8a5beeafc567d2e1e82e6b4d7d3db04b8f07a27c89954e7e18e8625471d029037d34a21d7148d822ca536dc39d4035ffab3421751acd4'],
id: 947a9e5e-0df8-488d-b82c-de1e6a9900ee,
human_readable_id: 17,
source: 李明,
target: 小红书,
description: 李明通过小红书进行引流获客,
weight: 18.0,
combined_degree: 18,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: a04a2cbd-a12c-4333-905d-7e6bafbdf9f1,
human_readable_id: 18,
source: 李明,
target: 抖音,
description: 李明通过抖音进行引流获客,
weight: 18.0,
combined_degree: 18,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: 94894497-be39-4a72-8877-25f43e75056e,
human_readable_id: 19,
source: 李明,
target: 周边书店,
description: 李明与周边书店合作互推以降低获客成本,
weight: 16.0,
combined_degree: 18,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: 9c814eb9-e70a-4a1e-8b9e-b7ff646429ee,
human_readable_id: 20,
source: 李明,
target: 健身房,
description: 李明与健身房合作互推以降低获客成本,
weight: 16.0,
combined_degree: 18,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: fb05614d-4b48-4a1a-a403-df46f0aa645f,
human_readable_id: 21,
source: 李明,
target: 会员体系,
description: 李明在2024年6月建立了会员体系,
weight: 18.0,
combined_degree: 19,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: 2cb41edf-b40e-4828-85c5-b7f665e85fbf,
human_readable_id: 22,
source: 会员体系,
target: 2024年6月,
description: 会员体系是在2024年6月建立的,
weight: 20.0,
combined_degree: 3,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: 2a12d950-302a-4b9e-bf67-60276158eb58,
human_readable_id: 23,
source: 开业初期,
target: 李明,
description: 李明在开业初期通过社交平台引流并快速增加粉丝,
weight: 8.0,
combined_degree: 18,
text_unit_ids: ['d1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'
'd1da6221f377cc7af3f4622e1716480446bc837cf55742e9f5010ad4af342490837cf8d4fed3a1980fded46a75c6431b389a0097859c5884392d691ddb362fc2'],
id: 38737c40-bcf2-44fb-b2d5-627bf38cf731,
human_readable_id: 24,
source: 张伟,
target: 云南普洱,
description: Zhang Wei is described as a supplier from Yunnan Pu'er,
weight: 8.0,
combined_degree: 3,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: b935f8a7-bdcf-41a0-bff9-f6b6fce933d8,
human_readable_id: 25,
source: 李明,
target: 第二家供应商,
description: Li Ming added the second supplier as a backup,
weight: 9.0,
combined_degree: 18,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: 9a3324bb-bd27-4fc8-a1d8-4f23ad0a090a,
human_readable_id: 26,
source: 本地乳企,
target: 2024年7月,
description: The local dairy enterprise is part of the store's supply situation affected by the July 2024 disruption context,
weight: 3.0,
combined_degree: 4,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: 4cd68752-5b32-419e-b39f-8eeef5c25f3c,
human_readable_id: 27,
source: 张伟,
target: 2024年7月,
description: The coffee-bean supply from Zhang Wei was affected during the July 2024 rainy-season delay period,
weight: 1.0,
combined_degree: 4,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: 6a8f36e3-20bd-460b-8818-19886205285b,
human_readable_id: 28,
source: 本地乳企,
target: 牛奶,
description: The local dairy enterprise delivers milk every day,
weight: 10.0,
combined_degree: 3,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: a42c8b99-790b-4984-b246-8aca78e79926,
human_readable_id: 29,
source: 门店,
target: 雨季运输延误,
description: The store was affected by rainy-season transportation delays,
weight: 9.0,
combined_degree: 4,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: 48985ff4-52a6-4c6a-9566-451b77c31dc5,
human_readable_id: 30,
source: 雨季运输延误,
target: 断货两天,
description: The transportation delay caused the two-day stockout,
weight: 10.0,
combined_degree: 4,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: 3ab20504-5025-4455-9190-353e94c893e8,
human_readable_id: 31,
source: 断货两天,
target: 损失约四千元,
description: The two-day stockout resulted in a loss of about 4,000 yuan,
weight: 9.0,
combined_degree: 3,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
id: 3dd5c112-2553-4a6c-a646-214229a8e730,
human_readable_id: 32,
source: 李明,
target: 门店,
description: Li Ming took action to improve the store's supply resilience by adding a backup supplier,
weight: 1.0,
combined_degree: 19,
text_unit_ids: ['f2d54bee6965bb80537dbc03402d24b17195cd53b9bdb3f33f98581738eab7044b8e7f9c7a55ff91e485a2850f2526ad1fb907d9078880335bf0a5eec477457c'],
4. 以可视化形成查看实体关系图
我们在上面提到过,如果在执行finalize_graph这个Workflow时按照如下的方式将RagConfig的snapshots.graphml字段设置为True,它会将当前这个图以GraphML文件的形式存下来。
import asyncio
import graphrag.index.workflows.finalize_graph as finalize_graph
from graphrag.index.typing.pipeline import Pipeline
from graphrag.config.load_config import load_config
from graphrag.index.run.run_pipeline import run_pipeline
from graphrag.callbacks.noop_workflow_callbacks import NoopWorkflowCallbacks
import pandas as pd
async def main():
pipeline = Pipeline(workflows=[
("finalize_graph", finalize_graph.run_workflow)
])
config = load_config(root_dir="./")
config.snapshots.graphml = True
async for _ in run_pipeline(
pipeline = pipeline,
config=config,
callbacks= NoopWorkflowCallbacks(),
):
pass
asyncio.run(main())
上面这个程序执行后,生成的GraphML文件会保存在./output/graph.graphml。我们可以使用一些工具打开这个文件并查看实体之间的关系,比如使用桌面应用Gephi。
本文介绍GraphRAG中对初始实体关系图的增强方法,重点实现两个图论指标:实体度数(反映节点中心性)和关系组合度数(源+目标实体度数之和)。通过Workflow调用LLM提取结果后,在数据库表中注入这两个指标,并支持GraphML格式导出以供Gephi等工具可视化。该步骤提升了关系图的分析深度与可解释性,是构建高质量知识图谱的关键环节。

868

被折叠的 条评论
为什么被折叠?



