如何使用AutoSchemaKG构建高质量知识图谱?从文本到图谱的完整指南
如何使用AutoSchemaKG构建高质量知识图谱?从文本到图谱的完整指南
【免费下载链接】AutoSchemaKGThis repository contains the implementation of AutoSchemaKG, a novel framework for automatic knowledge graph construction that combines schema generation via conceptualization.项目地址: https://gitcode.com/gh_mirrors/au/AutoSchemaKG
AutoSchemaKG是一个创新的自动知识图谱构建框架,它通过概念化实现模式生成,能够从非结构化文本中构建高质量知识图谱。本文将为你提供从文本到图谱的完整指南,帮助你快速掌握AutoSchemaKG的使用方法。
1. AutoSchemaKG简介
AutoSchemaKG是一个强大的知识图谱构建工具,它能够自动从文本中提取知识并构建结构化的知识图谱。该框架结合了模式生成和概念化技术,无需预定义模式即可实现全自动的知识图谱构建。
AutoSchemaKG框架图标:展示了知识图谱与AI概念的结合
2. 环境准备
在开始使用AutoSchemaKG之前,需要确保你的环境中安装了必要的依赖包,并配置了LLM端点和嵌入模型。以下是基本的环境配置步骤:
from atlas_rag.kg_construction.triple_extraction import KnowledgeGraphExtractor from atlas_rag.kg_construction.triple_config import ProcessingConfig from atlas_rag.llm_generator import LLMGenerator from openai import OpenAI # 初始化LLM客户端 client = OpenAI(base_url="http://0.0.0.0:8129/v1", api_key="EMPTY") llm_generator = LLMGenerator(client=client, model_name="Qwen/Qwen2.5-7B-Instruct")3. 知识图谱构建完整流程
AutoSchemaKG提供了从原始文本到知识图谱的完整 pipeline。主要包括以下步骤:
3.1 配置处理参数
首先,需要设置处理配置,指定模型路径、数据目录、输出目录等关键参数:
kg_extraction_config = ProcessingConfig( model_path="Qwen/Qwen2.5-7B-Instruct", data_directory='example/example_data', filename_pattern='Dulce', # 仅处理文件名包含此子串的文件 output_directory='example/generated/test_data', ) kg_extractor = KnowledgeGraphExtractor(model=llm_generator, config=kg_extraction_config)3.2 执行知识图谱提取
配置完成后,执行以下步骤完成知识图谱的构建:
# 1. 提取三元组 kg_extractor.run_extraction() # 2. 转换为CSV格式 kg_extractor.convert_json_to_csv() # 3. 模式归纳 kg_extractor.generate_concept_csv_temp() # 4. 创建概念CSV kg_extractor.create_concept_csv() # 5. 转换为GraphML格式(用于NetworkX) kg_extractor.convert_to_graphml()4. 支持的文件格式
AutoSchemaKG支持多种输入格式用于知识图谱构建和基准测试。对于PDF文件,处理流程如下:
- 将PDF转换为Markdown格式
- 将Markdown转换为JSON格式
- 使用JSON文件进行知识图谱构建
详细的PDF/Markdown转换指南可以参考example/pdf_md_conversion/readme.md。
5. 检索增强生成(RAG)
构建知识图谱后,可以使用它进行检索增强生成。以下是基本步骤:
5.1 设置嵌入模型
from sentence_transformers import SentenceTransformer from atlas_rag.vectorstore.embedding_model import SentenceEmbedding encoder_model_name = "sentence-transformers/all-MiniLM-L6-v2" sentence_model = SentenceTransformer(encoder_model_name, trust_remote_code=True) sentence_encoder = SentenceEmbedding(sentence_model)5.2 创建索引
from atlas_rag.vectorstore import create_embeddings_and_index # 使用知识图谱构建步骤中的输出目录 working_directory = 'example/example_data/test_data' data = create_embeddings_and_index( sentence_encoder=sentence_encoder, model_name=encoder_model_name, working_directory=working_directory, keyword='test_data', include_concept=False, # 如果生成了概念,设为True include_events=False, normalize_embeddings=True )5.3 执行检索
from atlas_rag.retriever import HippoRAG2Retriever # 初始化检索器 hipporag2_retriever = HippoRAG2Retriever( llm_generator=llm_generator, sentence_encoder=sentence_encoder, data=data, ) # 检索上下文 query = "你的问题?" content, sorted_context_ids = hipporag2_retriever.retrieve(query, topN=3) print(f"检索到的内容: {content}")5.4 生成答案
sorted_context = "\n".join(content) response = llm_generator.generate_with_context( query, sorted_context, max_new_tokens=2048, temperature=0.5 ) print(response)6. 示例脚本和工具
AutoSchemaKG提供了多个示例脚本,帮助你快速上手:
- atlas_full_pipeline.ipynb:从原始文本到知识图谱构建和RAG的完整端到端 pipeline
- example_scripts/parallel_generation/run_full_pipeline.sh:运行完整 pipeline 的主脚本
- example_scripts/benchmark_extraction_example:用于基准测试的提取 pipeline 示例
7. 总结
AutoSchemaKG是一个功能强大的知识图谱构建框架,它能够自动从非结构化文本中提取知识并构建高质量的知识图谱。通过本文介绍的步骤,你可以快速掌握AutoSchemaKG的使用方法,从文本数据中构建知识图谱并进行检索增强生成。
无论是学术研究还是工业应用,AutoSchemaKG都能为你提供高效、准确的知识图谱构建解决方案。开始使用AutoSchemaKG,释放你的数据价值吧!
【免费下载链接】AutoSchemaKGThis repository contains the implementation of AutoSchemaKG, a novel framework for automatic knowledge graph construction that combines schema generation via conceptualization.项目地址: https://gitcode.com/gh_mirrors/au/AutoSchemaKG
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
