Metadata-Version: 2.4
Name: knowledge-graph-kit
Version: 0.1.0
Summary: 知识图谱构建管线：文本分块、LLM抽取、实体解析、Neo4j写入
Author-email: hbue_jerry <lovecpp@foxmail.com>
License: MIT
Project-URL: Homepage, https://github.com/jerryhbue/property-graph
Project-URL: Source, https://github.com/jerryhbue/property-graph
Project-URL: Tracker, https://github.com/jerryhbue/property-graph/issues
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: openai>=1.0.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: neo4j>=5.0.0
Requires-Dist: httpx>=0.28.0
Dynamic: license-file

# knowledge-graph-kit

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

## 安装

```bash
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. 文本分块

```bash
kg-chunker <txt_path>
```

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

### 2. 实体关系抽取

```bash
kg-extractor <txt_path>
```

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

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

### 3. 实体解析去重

```bash
kg-resolver <input.json>
```

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

### 4. 写入 Neo4j

```bash
kg-neo4j-writer [input.json]
```

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

## 程序化使用

```python
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
