从PyPI安装
1 安装GraphGen
pip install graphg==0.1.0.post202509302 准备配置文件
参考https://github.com/open-sciencelab/GraphGen/tree/main/graphgen/configs,下面以aggregated_config.yaml为例。
# 读取配置
read:
input_file: resources/input_examples/jsonl_demo.jsonl # 输入文件路径,支持 json、jsonl、txt,参考 resources/input_examples 中的示例
# 文本切片配置
split:
chunk_size: 1024 # 每块文本的最大 token 数
chunk_overlap: 100 # 相邻文本块之间的重叠 token 数
# 网络搜索配置
search:
enabled: false # 是否启用联网搜索
search_types: ["google"] # 搜索引擎类型,可选:google、bing、uniprot、wikipedia
# 测验 & 判断:让 LLM 自检是否真正掌握知识点
quiz_and_judge:
enabled: true # 是否开启「出题-判断」流程
quiz_samples: 2 # 一条关系对应出多少题
re_judge: false # 是否对已有测验结果重新判断
# 子图划分配置
partition:
method: ece # 分区方法,目前仅内置 ece(基于理解损失的分区算法),以下参数保持默认即可
method_params:
bidirectional: true # 是否双向遍历图谱
edge_sampling: max_loss # 边采样策略:random(随机)| max_loss(损失最大)| min_loss(损失最小)
expand_method: max_width # 子图扩张方式:max_width(广度优先)| max_depth(深度优先)| max_tokens(按 token 上限)
isolated_node_strategy: ignore # 孤立节点处理:ignore(忽略)| add(强制加入)
max_depth: 5 # 最大遍历深度(深度优先时生效)
max_extra_edges: 20 # 每方向最多扩展的边数(广度优先时生效)
max_tokens: 256 # 子图文本总 token 上限(max_tokens 模式时生效)
loss_strategy: only_edge # 损失计算维度:only_edge(仅边)| both(节点+边)
# 问答对生成配置
generate:
mode: aggregated # 生成模式:atomic(单跳)| aggregated(聚合)| multi_hop(多跳)| cot(思维链)
data_format: ChatML # 输出格式:Alpaca | ShareGPT | ChatML3 运行
2.1 在CLI中运行
运行命令
运行结果如下:

2.2 使用GraphGen构建生成程序
下面是使用 GraphGen 生成数据的示例程序:
生成结果如下:

4 生成数据示例
更多示例可见:https://github.com/open-sciencelab/GraphGen/tree/main/resources/output_examples
最后更新于