Skip to main content

知识图谱构建管线:文本分块、LLM抽取、实体解析、Neo4j写入

Project description

knowledge-graph-kit

知识图谱构建管线:从教材文本中抽取实体和关系,写入 Neo4j 图数据库。

安装

pip install knowledge-graph-kit

环境变量

变量 说明 默认值
OPENAI_API_KEY OpenAI API 密钥
OPENAI_BASE_URL OpenAI API 地址(可换兼容 API)
LLM_MODEL_NAME 模型名称 gpt-4o-mini
NEO4J_URI Neo4j 连接地址 bolt://localhost:7687
NEO4J_USERNAME Neo4j 用户名 neo4j
NEO4J_PASSWORD Neo4j 密码 12345678

在项目根目录创建 .env 文件即可自动加载,或直接在 shell 中设置。

CLI 命令

安装后提供 4 个命令行工具:

1. 文本分块

kg-chunker <txt_path>

按章节标题将教材文本拆分为语义块。

2. 实体关系抽取

kg-extractor <txt_path>

基于本体 schema 引导 LLM 抽取实体和关系,输出 extraction_result.json

可通过 KG_SCHEMA_PATH 环境变量指定自定义 ontology 文件。

3. 实体解析去重

kg-resolver <input.json>

精确/模糊去重实体,清理属性,更新关系引用,输出 extraction_result_clean.json

4. 写入 Neo4j

kg-neo4j-writer [input.json]

将清洗后的结果写入 Neo4j 图数据库。默认尝试读取 extraction_result_clean.jsonextraction_result.json

程序化使用

from knowledge_graph_kit import chunk_file, Neo4jWriter

# 文本分块
chunks = chunk_file("教材.txt")

# 配置 OpenAI
from knowledge_graph_kit.extractor import configure
configure(api_key="sk-xxx", model="gpt-4o")

# 写入 Neo4j
writer = Neo4jWriter(
    uri="bolt://localhost:7687",
    user="neo4j",
    password="your-password",
)
writer.write_entities(entities)
writer.write_relations(relations)
writer.close()

管线流程

txt 文件 → 分块(chunker) → LLM抽取(extractor) → 实体解析(resolver) → Neo4j写入(writer)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knowledge_graph_kit-0.1.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

knowledge_graph_kit-0.1.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file knowledge_graph_kit-0.1.0.tar.gz.

File metadata

  • Download URL: knowledge_graph_kit-0.1.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for knowledge_graph_kit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e626512f73309e73487f243ea54d808947d0fbfa37fba811c6f75bd6020c13ab
MD5 80356d3804819259165784b6734306aa
BLAKE2b-256 712e2f016c72a79bed830442e233417d1e5e6ee95babb02b98386228e44cc73c

See more details on using hashes here.

File details

Details for the file knowledge_graph_kit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: knowledge_graph_kit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for knowledge_graph_kit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9692db7a35362dcc8a5890bb95d7b56d7289f5eab16651f139a530f7f845e59a
MD5 393b7ace46abb70855e71ccbb2a71db5
BLAKE2b-256 6d58fded7e86a96f242bb35229baee23876b8450ece880472bed342c0c9d2b12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page