Skip to content

Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.

feat: 更新文档图片和示例代码路径

yifanfeng97ba59372c457a
diff --git a/README.md b/README.md
index 7a13e03..7b1499f 100644
--- a/README.md
+++ b/README.md
@@ -16,12 +16,12 @@
Hyper-Extract is an intelligent, LLM-powered knowledge extraction and evolution framework. It radically simplifies transforming highly unstructured texts into persistent, predictable, and strongly-typed knowledge summaries. It effortlessly extracts information into a wide spectrum of formats—ranging from simple **Collections** (Lists/Sets) and **Pydantic Models**, to complex **Knowledge Graphs**, **Hypergraphs**, and even **Spatio-Temporal Graphs**.
-![Hero & Workflow](docs/assets/hero-v2.jpg)
+![Hero & Workflow](docs/assets/hero.jpg)
## ✨ Core Features
- 🔷 **8 Auto-Types:** From basic `AutoModel`/`AutoList` to advanced `AutoGraph`, `AutoHypergraph`, and `AutoSpatioTemporalGraph`.
-- 🧠 **10+ Extraction Engines:** Out-of-the-box support for cutting-edge retrieval paradigms like `graph_rag`, `light_rag`, and `hyper_rag`.
+- 🧠 **10+ Extraction Engines:** Out-of-the-box support for cutting-edge retrieval paradigms like `GraphRAG`, `LightRAG`, `Hyper-RAG`, and `KG-Gen`.
- 📝 **Declarative YAML Templates:** Zero-code extraction definition. Includes 80+ presets across 6 domains.
- 🔄 **Incremental Evolution:** Feed new documents on the fly to continuously map out and expand the extracted knowledge.
@@ -123,13 +123,19 @@ result = ka.parse(annual_report_text)
Our framework embraces complexity without making you write boilerplate code.
-![Knowledge Structures Matrix](docs/assets/8-types-v2.png)
+![Knowledge Structures Matrix](docs/assets/autotypes.png)
+
+### Example: AutoGraph Visualization
+
+Here is the knowledge graph visualization after `AutoGraph` extraction:
+
+![AutoGraph Visualization](docs/assets/en_show.png)
## 🛠️ Architecture Overview
The system is built on a robust triad: **Auto-Types** (Multi-typed structures), **Methods** (The Execution strategy), and **Templates** (Declarative schema).
-![Architecture](docs/assets/architecture-v2.png)
+![Architecture](docs/assets/arch.png)
* **Design Guide**: [Template Design Guide](./hyperextract/templates/DESIGN_GUIDE.md)
* **Preset Templates**: [presets directory](./hyperextract/templates/presets/)
diff --git a/README_ZH.md b/README_ZH.md
index d7b4301..4cd30cb 100644
--- a/README_ZH.md
+++ b/README_ZH.md
@@ -15,12 +15,12 @@
## 🚀 什么是 Hyper-Extract
Hyper-Extract 是一个智能的、由大语言模型(LLM)驱动的知识提取与演进框架。它极大地简化了将杂乱不堪的文本转化为持久化、强类型的知识摘要的过程。无论从基础的**集合(Collection/List)**和**结构化模型(Model)**,还是到高阶复杂的**知识图谱(Knowledge Graph)**、**超图(Hypergraph)**,甚至是**时空图谱(Spatio-Temporal Graph)**,它都能轻松拿捏。
-![Hero & Workflow](docs/assets/hero-v2.jpg)
+![Hero & Workflow](docs/assets/hero.jpg)
## ✨ 核心亮点
- 🔷 **8大自动数据结构(Auto-Types):** 从基础的 `AutoModel`/`AutoList` 到高阶的 `AutoGraph`, `AutoHypergraph`, 以及 `AutoSpatioTemporalGraph`(时空图)。
-- 🧠 **10+ 前沿提取引擎:** 开箱即用整合了业界顶尖的检索范式,例如 `graph_rag`, `light_rag`, 和 `hyper_rag`。
+- 🧠 **10+ 前沿提取引擎:** 开箱即用整合了业界顶尖的检索范式,例如 `GraphRAG`、`LightRAG`、`Hyper-RAG` 和 `KG-Gen`。
- 📝 **声明式 YAML 模板:** 零代码定义提取策略。内置覆盖 6 大领域的 80+ 预设模板。
- 🔄 **知识增量演进:** 支持动态喂入新文档(Feed),让提取的知识图谱自动补全和扩展。
@@ -118,19 +118,25 @@ result = ka.parse(annual_report_text)
> 🔗 完整示例代码,请参阅 [examples/zh](./examples/zh/)
-## 🧩 深入探究:8 种核心抽象类型
+## 🧩 8 种核心知识结构
拒绝样板代码,纯干货聚焦数据本身。
-![Knowledge Structures Matrix](docs/assets/8-types-v2.png)
+![Knowledge Structures Matrix](docs/assets/autotypes.png)
-## 🛠️ 系统架构揭秘
+### 示例:AutoGraph 知识图谱可视化
+
+以下是 `AutoGraph` 类型提取后的知识图谱可视化效果:
+
+![AutoGraph 可视化](docs/assets/zh_show.png)
+
+## 🛠️ 系统架构
系统底座基于坚实的铁三角架构:**Auto-Types** (提取结构)、**Methods** (执行策略)、以及 **Templates** (声明式配置)。
-![Architecture](docs/assets/architecture-v2.png)
+![Architecture](docs/assets/arch.png)
-* **设计指南**: [模板设计指南](./hyperextract/templates/DESIGN_GUIDE.md)
+* **设计指南**: [模板设计指南](./hyperextract/templates/DESIGN_GUIDE_ZH.md)
* **内置模板**: [预设模板目录](./hyperextract/templates/presets/)
## 📈 与其他流行库的对比
diff --git a/docs/assets/8-types.jpg b/docs/assets/8-types.jpg
deleted file mode 100644
index eaac3b2..0000000
Binary files a/docs/assets/8-types.jpg and /dev/null differ
diff --git a/docs/assets/arch.png b/docs/assets/arch.png
new file mode 100644
index 0000000..754e866
Binary files /dev/null and b/docs/assets/arch.png differ
diff --git a/docs/assets/architecture-v2.png b/docs/assets/architecture-v2.png
deleted file mode 100644
index 944c628..0000000
Binary files a/docs/assets/architecture-v2.png and /dev/null differ
diff --git a/docs/assets/architecture.jpg b/docs/assets/architecture.jpg
deleted file mode 100644
index 48e273f..0000000
Binary files a/docs/assets/architecture.jpg and /dev/null differ
diff --git a/docs/assets/8-types-v2.png b/docs/assets/autotypes.png
similarity index 100%
rename from docs/assets/8-types-v2.png
rename to docs/assets/autotypes.png
diff --git a/docs/assets/cli.png b/docs/assets/cli.png
index 339b971..1b8979f 100644
Binary files a/docs/assets/cli.png and b/docs/assets/cli.png differ
diff --git a/docs/assets/en_show.png b/docs/assets/en_show.png
new file mode 100644
index 0000000..485b3eb
Binary files /dev/null and b/docs/assets/en_show.png differ
diff --git a/docs/assets/hero-v2.jpg b/docs/assets/hero-v2.jpg
deleted file mode 100644
index fbb9246..0000000
Binary files a/docs/assets/hero-v2.jpg and /dev/null differ
diff --git a/docs/assets/hero.jpg b/docs/assets/hero.jpg
index ec1c423..80d1f1e 100644
Binary files a/docs/assets/hero.jpg and b/docs/assets/hero.jpg differ
diff --git a/docs/assets/zh_show.png b/docs/assets/zh_show.png
new file mode 100644
index 0000000..0f97d33
Binary files /dev/null and b/docs/assets/zh_show.png differ
diff --git a/docs/en/concepts/templates.md b/docs/en/concepts/templates.md
index a62c8d3..db56100 100644
--- a/docs/en/concepts/templates.md
+++ b/docs/en/concepts/templates.md
@@ -4,7 +4,7 @@ Templates define WHAT to extract using declarative YAML. They provide a powerful
## Type Selection
-![Knowledge Structures Matrix](../assets/8-types-v2.png)
+![Knowledge Structures Matrix](../assets/autotypes.png)
Before designing a template, determine which type fits your needs:
diff --git a/docs/en/getting-started/quickstart.md b/docs/en/getting-started/quickstart.md
index e34b783..5c4b608 100644
--- a/docs/en/getting-started/quickstart.md
+++ b/docs/en/getting-started/quickstart.md
@@ -99,6 +99,10 @@ The extracted knowledge will be saved in your output directory. You can use the
he search output/ "What financial events were mentioned?"
```
+**AutoGraph Visualization Example:**
+
+![AutoGraph Visualization](../assets/en_show.png)
+
## Next Steps
- Learn about the [8 Auto-Types](../concepts/auto-types.md)
diff --git a/docs/en/index.md b/docs/en/index.md
index 07bf7ab..74eb87a 100644
--- a/docs/en/index.md
+++ b/docs/en/index.md
@@ -4,7 +4,7 @@
**Transform documents into knowledge abstracts — with just one command.**
-![Hero & Workflow](assets/hero-v2.jpg)
+![Hero & Workflow](assets/hero.jpg)
[![Python Version](https://img.shields.io/badge/python-3.11%2B-blue)](https://python.org)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
@@ -17,7 +17,7 @@ Hyper-Extract is an intelligent, LLM-powered knowledge extraction and evolution
## Core Features
- 🔷 **8 Auto-Types:** From basic `AutoModel`/`AutoList` to advanced `AutoGraph`, `AutoHypergraph`, and `AutoSpatioTemporalGraph`.
-- 🧠 **10+ Extraction Engines:** Out-of-the-box support for cutting-edge retrieval paradigms like `graph_rag`, `light_rag`, and `hyper_rag`.
+- 🧠 **10+ Extraction Engines:** Out-of-the-box support for cutting-edge retrieval paradigms like `GraphRAG`, `LightRAG`, `Hyper-RAG`, and `KG-Gen`.
- 📝 **Declarative YAML Templates:** Zero-code extraction definition. Includes 80+ presets across 6 domains.
- 🔄 **Incremental Evolution:** Feed new documents on the fly to continuously map out and expand the extracted knowledge.
@@ -70,9 +70,15 @@ result = ka.parse(annual_report_text)
| AutoSpatialGraph | Spatial knowledge graph |
| AutoSpatioTemporalGraph | Spatio-temporal knowledge graph |
+### Example: AutoGraph Visualization
+
+After extraction with `AutoGraph`, you can visualize your knowledge graph:
+
+![AutoGraph Visualization](assets/en_show.png)
+
## Architecture
-![Architecture](assets/architecture-v2.png)
+![Architecture](assets/arch.png)
The system is built on a robust triad: **Auto-Types** (Multi-typed structures), **Methods** (The Execution strategy), and **Templates** (Declarative schema).
diff --git a/docs/zh/concepts/templates.md b/docs/zh/concepts/templates.md
index 62102e3..5eeed5e 100644
--- a/docs/zh/concepts/templates.md
+++ b/docs/zh/concepts/templates.md
@@ -4,7 +4,7 @@
## 类型选择
-![Knowledge Structures Matrix](../assets/8-types-v2.png)
+![Knowledge Structures Matrix](../assets/autotypes.png)
在设计模板之前,先确定哪种类型适合您的需求:
diff --git a/docs/zh/getting-started/quickstart.md b/docs/zh/getting-started/quickstart.md
index 3e0f16f..338dfa7 100644
--- a/docs/zh/getting-started/quickstart.md
+++ b/docs/zh/getting-started/quickstart.md
@@ -99,6 +99,10 @@ print(result.relations)
he search output/ "有哪些财务事件?"
```
+**AutoGraph 可视化示例:**
+
+![AutoGraph 可视化](../assets/zh_show.png)
+
## 下一步
- 了解 [8 种自动类型](../concepts/auto-types.md)
diff --git a/docs/zh/index.md b/docs/zh/index.md
index d2aa6b8..5cdaf52 100644
--- a/docs/zh/index.md
+++ b/docs/zh/index.md
@@ -4,7 +4,7 @@
**一行命令,将文档转化为结构化知识摘要。**
-![Hero & Workflow](assets/hero-v2.jpg)
+![Hero & Workflow](assets/hero.jpg)
[![Python版本](https://img.shields.io/badge/python-3.11%2B-blue)](https://python.org)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
@@ -17,7 +17,7 @@ Hyper-Extract 是一个智能的、基于 LLM 的知识提取与演化框架。
## 核心特性
- 🔷 **8 种自动类型:** 从基础的 `AutoModel`/`AutoList` 到高级的 `AutoGraph`、`AutoHypergraph` 和 `AutoSpatioTemporalGraph`。
-- 🧠 **10+ 提取引擎:** 开箱即用的前沿检索范式支持,如 `graph_rag`、`light_rag` 和 `hyper_rag`。
+- 🧠 **10+ 提取引擎:** 开箱即用的前沿检索范式支持,如 `GraphRAG`、`LightRAG`、`Hyper-RAG` 和 `KG-Gen`。
- 📝 **声明式 YAML 模板:** 零代码提取定义。包含 6 个领域 80+ 预设模板。
- 🔄 **增量演化:** 动态输入新文档,持续扩展和更新提取的知识。
@@ -70,9 +70,15 @@ result = ka.parse(annual_report_text)
| AutoSpatialGraph | 空间知识图谱 |
| AutoSpatioTemporalGraph | 时空知识图谱 |
+### 示例:AutoGraph 可视化
+
+使用 `AutoGraph` 提取后,您可以可视化知识图谱:
+
+![AutoGraph 可视化](assets/zh_show.png)
+
## 架构
-![Architecture](assets/architecture-v2.png)
+![Architecture](assets/arch.png)
系统构建在稳固的三元组上:**自动类型**(多类型结构)、**方法**(执行策略)和**模板**(声明式模式)。
diff --git a/examples/en/autotypes/graph_demo.py b/examples/en/autotypes/graph_demo.py
index b10052b..0ecdb58 100644
--- a/examples/en/autotypes/graph_demo.py
+++ b/examples/en/autotypes/graph_demo.py
@@ -16,21 +16,20 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoGraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
class Entity(BaseModel):
"""Entity in the knowledge graph"""
name: str = Field(description="Entity name")
- type: str = Field(
- description="Entity type: person/location/invention", default="person"
- )
+ type: str = Field(description="Entity type: person/location/invention/etc")
+ description: str = Field(description="Entity description")
class Relation(BaseModel):
@@ -38,7 +37,8 @@ class Relation(BaseModel):
source: str = Field(description="Source entity")
target: str = Field(description="Target entity")
- type: str = Field(description="Relation type: employer/partner/rival/invented")
+ type: str = Field(description="Relation type: employer/partner/rival/invented/etc")
+ description: str = Field(description="Relation description")
if __name__ == "__main__":
@@ -82,5 +82,7 @@ if __name__ == "__main__":
except Exception as e:
print(f"Error: {e}")
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: f"{x.type}")
-
\ No newline at end of file
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.type}",
+ )
diff --git a/examples/en/autotypes/hypergraph_demo.py b/examples/en/autotypes/hypergraph_demo.py
index e6657b9..967ead1 100644
--- a/examples/en/autotypes/hypergraph_demo.py
+++ b/examples/en/autotypes/hypergraph_demo.py
@@ -16,25 +16,27 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoHypergraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
class Entity(BaseModel):
"""Entity node"""
name: str = Field(description="Entity name")
type: str = Field(description="Type: person/location/invention", default="person")
+ description: str = Field(description="Entity description")
-class HyperEdge(BaseModel):
- """Hyper-edge (multi-entity relationship)"""
+class Relation(BaseModel):
+ """Hyperedge (multi-entity relationship)"""
description: str = Field(description="Relationship description")
members: list[str] = Field(description="Entities involved")
type: str = Field(description="Relationship type")
+ description: str = Field(description="Relationship description")
if __name__ == "__main__":
@@ -51,9 +53,9 @@ if __name__ == "__main__":
print("=" * 60)
print("Extracting multi-entity relationships...")
- graph = AutoHypergraph[Entity, HyperEdge](
+ graph = AutoHypergraph[Entity, Relation](
node_schema=Entity,
- edge_schema=HyperEdge,
+ edge_schema=Relation,
node_key_extractor=lambda x: x.name,
edge_key_extractor=lambda x: f"{x.type}_{'_'.join(sorted(x.members))}",
nodes_in_edge_extractor=lambda x: tuple(x.members),
diff --git a/examples/en/autotypes/list_demo.py b/examples/en/autotypes/list_demo.py
index 34ff7e3..1f5a1d0 100644
--- a/examples/en/autotypes/list_demo.py
+++ b/examples/en/autotypes/list_demo.py
@@ -16,12 +16,12 @@ from pydantic import BaseModel, Field
from hyperextract import AutoList
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
class TimelineEvent(BaseModel):
diff --git a/examples/en/autotypes/model_demo.py b/examples/en/autotypes/model_demo.py
index c7876ae..ef2a1d3 100644
--- a/examples/en/autotypes/model_demo.py
+++ b/examples/en/autotypes/model_demo.py
@@ -17,12 +17,12 @@ from pydantic import BaseModel, Field
from hyperextract import AutoModel
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
class BiographySummary(BaseModel):
diff --git a/examples/en/autotypes/set_demo.py b/examples/en/autotypes/set_demo.py
index 65329b8..3345b78 100644
--- a/examples/en/autotypes/set_demo.py
+++ b/examples/en/autotypes/set_demo.py
@@ -16,12 +16,12 @@ from pydantic import BaseModel, Field
from hyperextract import AutoSet
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
class Entity(BaseModel):
diff --git a/examples/en/autotypes/spatial_demo.py b/examples/en/autotypes/spatial_demo.py
deleted file mode 100644
index 3196f19..0000000
--- a/examples/en/autotypes/spatial_demo.py
+++ /dev/null
@@ -1,87 +0,0 @@
-"""
-Spatial Graph Demo - Tesla Biography
-
-Extract spatial relationships from text using AutoSpatialGraph.
-This demo shows how to understand location-based relationships.
-
-Usage:
- python examples/en/autotypes/spatial_demo.py
-"""
-
-from pathlib import Path
-
-from dotenv import load_dotenv
-from langchain_openai import ChatOpenAI, OpenAIEmbeddings
-from pydantic import BaseModel, Field
-
-from hyperextract.types import AutoSpatialGraph
-
-project_root = Path(__file__).resolve().parent.parent.parent
-
-load_dotenv()
-
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
-
-
-class Location(BaseModel):
- """Location entity"""
- name: str = Field(description="Location name")
- category: str = Field(description="Category: university/corporate/historical", default="historical")
- description: str = Field(description="Brief description", default="")
-
-
-class SpatialRelation(BaseModel):
- """Spatial relationship"""
- source: str = Field(description="Source location")
- target: str = Field(description="Target location")
- relation: str = Field(description="Spatial relationship type")
-
-
-if __name__ == "__main__":
- with open(INPUT_FILE, encoding="utf-8") as f:
- text = f.read()
- with open(QUESTION_FILE, encoding="utf-8") as f:
- questions = [line.strip() for line in f if line.strip()]
-
- llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings(model="text-embedding-3-small")
-
- print("\n" + "=" * 60)
- print(" Spatial Graph Demo")
- print("=" * 60)
- print("Extracting spatial relationships...")
-
- graph = AutoSpatialGraph[Location, SpatialRelation](
- node_schema=Location,
- edge_schema=SpatialRelation,
- node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.source}-{x.relation}-{x.target}",
- nodes_in_edge_extractor=lambda x: (x.source, x.target),
- location_in_edge_extractor=lambda x: x.target,
- llm_client=llm,
- embedder=embedder,
- observation_location="United States",
- )
-
- graph.feed_text(text)
-
- print(f"\nExtracted {len(graph.nodes)} locations and {len(graph.edges)} relations")
-
- for node in graph.nodes[:5]:
- print(f" {node.name}")
-
- graph.build_index()
-
- print("-" * 60)
- print("Q&A")
- print("-" * 60)
- for q in questions:
- print(f"\nQ: {q}")
- try:
- result = graph.chat(q)
- print(f"A: {result.content}")
- except Exception as e:
- print(f"Error: {e}")
-
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: x.relation)
diff --git a/examples/en/autotypes/spatial_graph_demo.py b/examples/en/autotypes/spatial_graph_demo.py
new file mode 100644
index 0000000..724fe51
--- /dev/null
+++ b/examples/en/autotypes/spatial_graph_demo.py
@@ -0,0 +1,93 @@
+"""
+Spatial Graph Demo - Tesla Biography
+
+Extract spatial relationships from text using AutoSpatialGraph.
+This demo shows how to understand location-based relationships between entities.
+
+Usage:
+ python examples/en/autotypes/spatial_graph_demo.py
+"""
+
+from pathlib import Path
+from typing import Optional
+
+from dotenv import load_dotenv
+from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+from pydantic import BaseModel, Field
+
+from hyperextract.types import AutoSpatialGraph
+
+project_root = Path(__file__).resolve().parent.parent.parent.parent
+
+load_dotenv()
+
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
+
+
+class Entity(BaseModel):
+ """Entity node"""
+ name: str = Field(description="Entity name")
+ category: str = Field(description="Category, e.g., person, location, invention, etc.")
+ description: str = Field(description="Entity description")
+
+
+class SpatialRelation(BaseModel):
+ """Spatial relation"""
+ source: str = Field(description="Source entity")
+ target: str = Field(description="Target entity")
+ relation_type: str = Field(description="Relation type, e.g., father, brother, invention, etc.")
+ location: Optional[str] = Field(description="Location, e.g., New York", default=None)
+ description: str = Field(description="Relation description")
+
+
+if __name__ == "__main__":
+ with open(INPUT_FILE, encoding="utf-8") as f:
+ text = f.read()
+ with open(QUESTION_FILE, encoding="utf-8") as f:
+ questions = [line.strip() for line in f if line.strip()]
+
+ llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
+
+ print("\n" + "=" * 60)
+ print(" Spatial Graph Demo")
+ print("=" * 60)
+ print("Extracting spatial relationships...")
+
+ graph = AutoSpatialGraph[Entity, SpatialRelation](
+ node_schema=Entity,
+ edge_schema=SpatialRelation,
+ node_key_extractor=lambda x: x.name,
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
+ nodes_in_edge_extractor=lambda x: (x.source, x.target),
+ location_in_edge_extractor=lambda x: x.location or "",
+ llm_client=llm,
+ embedder=embedder,
+ observation_location="United States",
+ )
+
+ graph.feed_text(text)
+
+ print(f"\nExtracted {len(graph.nodes)} entities and {len(graph.edges)} relations")
+
+ for node in graph.nodes[:5]:
+ print(f" {node.name}")
+
+ graph.build_index()
+
+ print("-" * 60)
+ print("Q&A")
+ print("-" * 60)
+ for q in questions:
+ print(f"\nQ: {q}")
+ try:
+ result = graph.chat(q)
+ print(f"A: {result.content}")
+ except Exception as e:
+ print(f"Error: {e}")
+
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.relation_type}@{x.location}" if x.location else x.relation_type,
+ )
diff --git a/examples/en/autotypes/spatio_temporal_demo.py b/examples/en/autotypes/spatio_temporal_graph_demo.py
similarity index 55%
rename from examples/en/autotypes/spatio_temporal_demo.py
rename to examples/en/autotypes/spatio_temporal_graph_demo.py
index 0f8e2ef..8e6a2d1 100644
--- a/examples/en/autotypes/spatio_temporal_demo.py
+++ b/examples/en/autotypes/spatio_temporal_graph_demo.py
@@ -17,27 +17,29 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoSpatioTemporalGraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
class Entity(BaseModel):
"""Entity node"""
name: str = Field(description="Entity name")
- category: str = Field(description="Category", default="person")
+ category: str = Field(description="Category, e.g., person, location, invention, etc.")
+ description: str = Field(description="Entity description")
-class SpatioTemporalEvent(BaseModel):
- """Spatio-temporal event"""
+class SpatioTemporalRelation(BaseModel):
+ """Spatio-temporal relation"""
source: str = Field(description="Source entity")
target: str = Field(description="Target entity")
- action: str = Field(description="Action or relationship")
- time: Optional[str] = Field(description="When", default=None)
- location: Optional[str] = Field(description="Where", default=None)
+ relation_type: str = Field(description="Relation type, e.g., father, brother, invention, etc.")
+ event_date: Optional[str] = Field(description="Event date, e.g., 2023-01-01", default=None)
+ location: Optional[str] = Field(description="Location, e.g., New York", default=None)
+ description: str = Field(description="Relation description")
if __name__ == "__main__":
@@ -52,30 +54,31 @@ if __name__ == "__main__":
print("\n" + "=" * 60)
print(" Spatio-Temporal Graph Demo")
print("=" * 60)
- print("Extracting spatio-temporal events...")
+ print("Extracting spatio-temporal relations...")
- graph = AutoSpatioTemporalGraph[Entity, SpatioTemporalEvent](
+ graph = AutoSpatioTemporalGraph[Entity, SpatioTemporalRelation](
node_schema=Entity,
- edge_schema=SpatioTemporalEvent,
+ edge_schema=SpatioTemporalRelation,
node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.source}-{x.action}-{x.target}",
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
nodes_in_edge_extractor=lambda x: (x.source, x.target),
- time_in_edge_extractor=lambda x: x.time or "",
+ time_in_edge_extractor=lambda x: x.event_date or "",
location_in_edge_extractor=lambda x: x.location or "",
llm_client=llm,
embedder=embedder,
+ observation_time="2024-01-01",
observation_location="United States",
)
graph.feed_text(text)
- print(f"\nExtracted {len(graph.nodes)} entities and {len(graph.edges)} events")
+ print(f"\nExtracted {len(graph.nodes)} entities and {len(graph.edges)} relations")
- sorted_edges = sorted(graph.edges, key=lambda x: x.time or "")
+ sorted_edges = sorted(graph.edges, key=lambda x: x.event_date or "")
for edge in sorted_edges[:5]:
- time_info = f" ({edge.time})" if edge.time else ""
+ time_info = f" ({edge.event_date})" if edge.event_date else ""
loc_info = f" in {edge.location}" if edge.location else ""
- print(f" {edge.source} --[{edge.action}]--> {edge.target}{time_info}{loc_info}")
+ print(f" {edge.source} --[{edge.relation_type}]--> {edge.target}{time_info}{loc_info}")
graph.build_index()
@@ -90,4 +93,7 @@ if __name__ == "__main__":
except Exception as e:
print(f"Error: {e}")
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: f"{x.action}@{x.time}")
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.relation_type}@{x.event_date}" if x.event_date else x.relation_type,
+ )
diff --git a/examples/en/autotypes/temporal_demo.py b/examples/en/autotypes/temporal_demo.py
deleted file mode 100644
index 1a38cd4..0000000
--- a/examples/en/autotypes/temporal_demo.py
+++ /dev/null
@@ -1,86 +0,0 @@
-"""
-Temporal Graph Demo - Tesla Biography
-
-Extract temporal relationships from text using AutoTemporalGraph.
-This demo shows how to understand time-based relationships.
-
-Usage:
- python examples/en/autotypes/temporal_demo.py
-"""
-
-from pathlib import Path
-
-from dotenv import load_dotenv
-from langchain_openai import ChatOpenAI, OpenAIEmbeddings
-from pydantic import BaseModel, Field
-
-from hyperextract.types import AutoTemporalGraph
-
-project_root = Path(__file__).resolve().parent.parent.parent
-
-load_dotenv()
-
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
-
-
-class Entity(BaseModel):
- """Entity node"""
- name: str = Field(description="Entity name")
- type: str = Field(description="Type: person/location/invention", default="person")
-
-
-class TimelineEvent(BaseModel):
- """Timeline event"""
- time: str = Field(description="Time (year or date)")
- event: str = Field(description="Event description")
- entities: list[str] = Field(description="Entities involved")
-
-
-if __name__ == "__main__":
- with open(INPUT_FILE, encoding="utf-8") as f:
- text = f.read()
- with open(QUESTION_FILE, encoding="utf-8") as f:
- questions = [line.strip() for line in f if line.strip()]
-
- llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings(model="text-embedding-3-small")
-
- print("\n" + "=" * 60)
- print(" Temporal Graph Demo")
- print("=" * 60)
- print("Extracting temporal relationships...")
-
- graph = AutoTemporalGraph[Entity, TimelineEvent](
- node_schema=Entity,
- edge_schema=TimelineEvent,
- node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.time}-{x.event}",
- nodes_in_edge_extractor=lambda x: tuple(x.entities) if x.entities else (),
- time_in_edge_extractor=lambda x: x.time,
- llm_client=llm,
- embedder=embedder,
- )
-
- graph.feed_text(text)
-
- print(f"\nExtracted {len(graph.nodes)} entities and {len(graph.edges)} events")
-
- sorted_events = sorted(graph.edges, key=lambda x: x.time)
- for event in sorted_events[:5]:
- print(f" {event.time}: {event.event}")
-
- graph.build_index()
-
- print("-" * 60)
- print("Q&A")
- print("-" * 60)
- for q in questions:
- print(f"\nQ: {q}")
- try:
- result = graph.chat(q)
- print(f"A: {result.content}")
- except Exception as e:
- print(f"Error: {e}")
-
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: f"{x.event}@{x.time}")
diff --git a/examples/en/autotypes/temporal_graph_demo.py b/examples/en/autotypes/temporal_graph_demo.py
new file mode 100644
index 0000000..bcab05c
--- /dev/null
+++ b/examples/en/autotypes/temporal_graph_demo.py
@@ -0,0 +1,95 @@
+"""
+Temporal Graph Demo - Tesla Biography
+
+Extract temporal relationships from text using AutoTemporalGraph.
+This demo shows how to understand time-based relationships between entities.
+
+Usage:
+ python examples/en/autotypes/temporal_demo.py
+"""
+
+from pathlib import Path
+from typing import Optional
+
+from dotenv import load_dotenv
+from langchain_openai import ChatOpenAI, OpenAIEmbeddings
+from pydantic import BaseModel, Field
+
+from hyperextract.types import AutoTemporalGraph
+
+project_root = Path(__file__).resolve().parent.parent.parent.parent
+
+load_dotenv()
+
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
+
+
+class Entity(BaseModel):
+ """Entity node"""
+ name: str = Field(description="Entity name")
+ category: str = Field(description="Category, e.g., person, location, invention, etc.")
+ description: str = Field(description="Entity description")
+
+
+class TemporalRelation(BaseModel):
+ """Temporal relation"""
+ source: str = Field(description="Source entity")
+ target: str = Field(description="Target entity")
+ relation_type: str = Field(description="Relation type, e.g., father, brother, invention, etc.")
+ event_date: Optional[str] = Field(description="Event date, e.g., 2023-01-01", default=None)
+ description: str = Field(description="Relation description")
+
+
+if __name__ == "__main__":
+ with open(INPUT_FILE, encoding="utf-8") as f:
+ text = f.read()
+ with open(QUESTION_FILE, encoding="utf-8") as f:
+ questions = [line.strip() for line in f if line.strip()]
+
+ llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
+
+ print("\n" + "=" * 60)
+ print(" Temporal Graph Demo")
+ print("=" * 60)
+ print("Extracting temporal relationships...")
+
+ graph = AutoTemporalGraph[Entity, TemporalRelation](
+ node_schema=Entity,
+ edge_schema=TemporalRelation,
+ node_key_extractor=lambda x: x.name,
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
+ nodes_in_edge_extractor=lambda x: (x.source, x.target),
+ time_in_edge_extractor=lambda x: x.event_date or "",
+ llm_client=llm,
+ embedder=embedder,
+ observation_time="2024-01-01",
+ )
+
+ graph.feed_text(text)
+
+ print(f"\nExtracted {len(graph.nodes)} entities and {len(graph.edges)} relations")
+
+ sorted_edges = sorted(graph.edges, key=lambda x: x.event_date or "")
+ for edge in sorted_edges[:5]:
+ time_info = f" ({edge.event_date})" if edge.event_date else ""
+ print(f" {edge.source} --[{edge.relation_type}]--> {edge.target}{time_info}")
+
+ graph.build_index()
+
+ print("-" * 60)
+ print("Q&A")
+ print("-" * 60)
+ for q in questions:
+ print(f"\nQ: {q}")
+ try:
+ result = graph.chat(q)
+ print(f"A: {result.content}")
+ except Exception as e:
+ print(f"Error: {e}")
+
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.relation_type}@{x.event_date}" if x.event_date else x.relation_type,
+ )
diff --git a/examples/en/methods/atom_demo.py b/examples/en/methods/atom_demo.py
index aa42dd6..fc4863d 100644
--- a/examples/en/methods/atom_demo.py
+++ b/examples/en/methods/atom_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import Atom
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Atom Demo")
diff --git a/examples/en/methods/cog_rag_demo.py b/examples/en/methods/cog_rag_demo.py
index 67b11f3..13df658 100644
--- a/examples/en/methods/cog_rag_demo.py
+++ b/examples/en/methods/cog_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Cog_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Cog RAG Demo")
diff --git a/examples/en/methods/graph_rag_demo.py b/examples/en/methods/graph_rag_demo.py
index a7ba941..32eca48 100644
--- a/examples/en/methods/graph_rag_demo.py
+++ b/examples/en/methods/graph_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Graph_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Graph RAG Demo")
diff --git a/examples/en/methods/hyper_rag_demo.py b/examples/en/methods/hyper_rag_demo.py
index 52f9f3d..71ffcd9 100644
--- a/examples/en/methods/hyper_rag_demo.py
+++ b/examples/en/methods/hyper_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Hyper_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Hyper RAG Demo")
diff --git a/examples/en/methods/hypergraph_rag_demo.py b/examples/en/methods/hypergraph_rag_demo.py
index a7635fc..9f296f4 100644
--- a/examples/en/methods/hypergraph_rag_demo.py
+++ b/examples/en/methods/hypergraph_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import HyperGraph_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("HyperGraph RAG Demo")
diff --git a/examples/en/methods/itext2kg_demo.py b/examples/en/methods/itext2kg_demo.py
index 528df8e..2c59027 100644
--- a/examples/en/methods/itext2kg_demo.py
+++ b/examples/en/methods/itext2kg_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import iText2KG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("iText2KG Demo")
diff --git a/examples/en/methods/itext2kg_star_demo.py b/examples/en/methods/itext2kg_star_demo.py
index 5894906..cd29382 100644
--- a/examples/en/methods/itext2kg_star_demo.py
+++ b/examples/en/methods/itext2kg_star_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import iText2KG_Star
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("iText2KG Star Demo")
diff --git a/examples/en/methods/kg_gen_demo.py b/examples/en/methods/kg_gen_demo.py
index 8cfd68c..d96353c 100644
--- a/examples/en/methods/kg_gen_demo.py
+++ b/examples/en/methods/kg_gen_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import KG_Gen
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("KG Gen Demo")
diff --git a/examples/en/methods/light_rag_demo.py b/examples/en/methods/light_rag_demo.py
index bc7227e..dacb6d2 100644
--- a/examples/en/methods/light_rag_demo.py
+++ b/examples/en/methods/light_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Light_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "en" / "tesla.md"
-QUESTION_FILE = project_root / "en" / "tesla_question.md"
+INPUT_FILE = project_root / "examples" / "en" / "tesla.md"
+QUESTION_FILE = project_root / "examples" / "en" / "tesla_question.md"
if __name__ == "__main__":
with open(INPUT_FILE) as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Light RAG Demo")
diff --git a/examples/en/templates/finance_template.py b/examples/en/templates/finance_template.py
index c37be13..11be70a 100644
--- a/examples/en/templates/finance_template.py
+++ b/examples/en/templates/finance_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/en/templates/general_template.py b/examples/en/templates/general_template.py
index 2acd851..204d35d 100644
--- a/examples/en/templates/general_template.py
+++ b/examples/en/templates/general_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/en/templates/industry_template.py b/examples/en/templates/industry_template.py
index ea438e6..90e96b3 100644
--- a/examples/en/templates/industry_template.py
+++ b/examples/en/templates/industry_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/en/templates/legal_template.py b/examples/en/templates/legal_template.py
index 4503238..b64f357 100644
--- a/examples/en/templates/legal_template.py
+++ b/examples/en/templates/legal_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/en/templates/medicine_template.py b/examples/en/templates/medicine_template.py
index a675a0d..2a690f5 100644
--- a/examples/en/templates/medicine_template.py
+++ b/examples/en/templates/medicine_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/en/templates/tcm_template.py b/examples/en/templates/tcm_template.py
index efd4609..2035df5 100644
--- a/examples/en/templates/tcm_template.py
+++ b/examples/en/templates/tcm_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/zh/autotypes/graph_demo.py b/examples/zh/autotypes/graph_demo.py
index add3576..2a4341e 100644
--- a/examples/zh/autotypes/graph_demo.py
+++ b/examples/zh/autotypes/graph_demo.py
@@ -15,7 +15,7 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoGraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
@@ -26,14 +26,16 @@ QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
class Entity(BaseModel):
"""知识图谱中的实体"""
name: str = Field(description="实体名称")
- type: str = Field(description="实体类型:人物/地名/作品", default="人物")
+ type: str = Field(description="实体类型,例如:人物/地名/事件/物品/作品等")
+ description: str = Field(description="实体的详细描述")
class Relation(BaseModel):
"""实体之间的关系"""
source: str = Field(description="关系起点实体")
target: str = Field(description="关系终点实体")
- type: str = Field(description="关系类型:父亲/兄弟/师徒/朋友/政敌")
+ type: str = Field(description="关系类型,例如:父亲/兄弟/发明/参与等")
+ description: str = Field(description="关系的详细描述")
if __name__ == "__main__":
diff --git a/examples/zh/autotypes/hypergraph_demo.py b/examples/zh/autotypes/hypergraph_demo.py
index 99ff208..7503935 100644
--- a/examples/zh/autotypes/hypergraph_demo.py
+++ b/examples/zh/autotypes/hypergraph_demo.py
@@ -15,7 +15,7 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoHypergraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
@@ -26,14 +26,15 @@ QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
class Entity(BaseModel):
"""实体节点"""
name: str = Field(description="实体名称")
- type: str = Field(description="类型:人物/地名/作品", default="人物")
+ type: str = Field(description="类型:人物/地名/事件/物品/作品等")
+ description: str = Field(description="实体描述")
-class HyperEdge(BaseModel):
+class Relation(BaseModel):
"""超边(多实体关系)"""
- description: str = Field(description="关系描述")
members: list[str] = Field(description="参与的实体")
- type: str = Field(description="关系类型")
+ type: str = Field(description="关系类型,例如:父亲/兄弟/发明/参与等")
+ description: str = Field(description="关系的详细描述")
if __name__ == "__main__":
@@ -50,9 +51,9 @@ if __name__ == "__main__":
print("=" * 60)
print("提取多实体关系...")
- graph = AutoHypergraph[Entity, HyperEdge](
+ graph = AutoHypergraph[Entity, Relation](
node_schema=Entity,
- edge_schema=HyperEdge,
+ edge_schema=Relation,
node_key_extractor=lambda x: x.name,
edge_key_extractor=lambda x: f"{x.type}_{'_'.join(sorted(x.members))}",
nodes_in_edge_extractor=lambda x: tuple(x.members),
diff --git a/examples/zh/autotypes/list_demo.py b/examples/zh/autotypes/list_demo.py
index 48d9bfd..833d0ab 100644
--- a/examples/zh/autotypes/list_demo.py
+++ b/examples/zh/autotypes/list_demo.py
@@ -15,7 +15,7 @@ from pydantic import BaseModel, Field
from hyperextract import AutoList
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
diff --git a/examples/zh/autotypes/model_demo.py b/examples/zh/autotypes/model_demo.py
index a84db1c..fa55742 100644
--- a/examples/zh/autotypes/model_demo.py
+++ b/examples/zh/autotypes/model_demo.py
@@ -16,7 +16,7 @@ from pydantic import BaseModel, Field
from hyperextract import AutoModel
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
diff --git a/examples/zh/autotypes/set_demo.py b/examples/zh/autotypes/set_demo.py
index 4e463df..a451b6e 100644
--- a/examples/zh/autotypes/set_demo.py
+++ b/examples/zh/autotypes/set_demo.py
@@ -15,7 +15,7 @@ from pydantic import BaseModel, Field
from hyperextract import AutoSet
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
diff --git a/examples/zh/autotypes/spatial_demo.py b/examples/zh/autotypes/spatial_graph_demo.py
similarity index 57%
rename from examples/zh/autotypes/spatial_demo.py
rename to examples/zh/autotypes/spatial_graph_demo.py
index bbd4876..4aba58b 100644
--- a/examples/zh/autotypes/spatial_demo.py
+++ b/examples/zh/autotypes/spatial_graph_demo.py
@@ -2,12 +2,14 @@
空间图示例 - 苏轼传记
使用 AutoSpatialGraph 从文本中提取空间关系。
+展示如何理解实体之间的空间位置关系。
使用方法:
- python examples/zh/autotypes/spatial_demo.py
+ python examples/zh/autotypes/spatial_graph_demo.py
"""
from pathlib import Path
+from typing import Optional
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
@@ -15,7 +17,7 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoSpatialGraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
@@ -23,18 +25,18 @@ INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
-class Location(BaseModel):
- """地点实体"""
- name: str = Field(description="地点名称")
- category: str = Field(description="类别:城市/建筑/地区", default="地区")
- description: str = Field(description="简要描述", default="")
+class Entity(BaseModel):
+ """实体节点"""
+ name: str = Field(description="实体名称")
+ category: str = Field(description="类别,例如:人物/地点/发明等")
class SpatialRelation(BaseModel):
"""空间关系"""
- source: str = Field(description="起点地点")
- target: str = Field(description="终点地点")
- relation: str = Field(description="空间关系类型")
+ source: str = Field(description="源实体")
+ target: str = Field(description="目标实体")
+ relation_type: str = Field(description="关系类型,例如:父亲/兄弟/发明/参与等")
+ location: Optional[str] = Field(description="关系发生地点,例如:北京等", default=None)
if __name__ == "__main__":
@@ -51,13 +53,13 @@ if __name__ == "__main__":
print("=" * 60)
print("提取空间关系...")
- graph = AutoSpatialGraph[Location, SpatialRelation](
- node_schema=Location,
+ graph = AutoSpatialGraph[Entity, SpatialRelation](
+ node_schema=Entity,
edge_schema=SpatialRelation,
node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.source}-{x.relation}-{x.target}",
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
nodes_in_edge_extractor=lambda x: (x.source, x.target),
- location_in_edge_extractor=lambda x: x.target,
+ location_in_edge_extractor=lambda x: x.location or "",
llm_client=llm,
embedder=embedder,
observation_location="北宋疆域",
@@ -65,10 +67,7 @@ if __name__ == "__main__":
graph.feed_text(text)
- print(f"\n提取了 {len(graph.nodes)} 个地点和 {len(graph.edges)} 个关系")
-
- for node in graph.nodes[:5]:
- print(f" {node.name}")
+ print(f"\n提取了 {len(graph.nodes)} 个实体和 {len(graph.edges)} 个关系")
graph.build_index()
@@ -83,4 +82,7 @@ if __name__ == "__main__":
except Exception as e:
print(f"错误: {e}")
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: x.relation)
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.relation_type}@{x.location}" if x.location else x.relation_type,
+ )
diff --git a/examples/zh/autotypes/spatio_temporal_demo.py b/examples/zh/autotypes/spatio_temporal_graph_demo.py
similarity index 55%
rename from examples/zh/autotypes/spatio_temporal_demo.py
rename to examples/zh/autotypes/spatio_temporal_graph_demo.py
index 67f8926..f290d9e 100644
--- a/examples/zh/autotypes/spatio_temporal_demo.py
+++ b/examples/zh/autotypes/spatio_temporal_graph_demo.py
@@ -2,6 +2,7 @@
时空图示例 - 苏轼传记
使用 AutoSpatioTemporalGraph 从文本中提取时空关系。
+展示如何同时理解时间和空间维度的实体关系。
使用方法:
python examples/zh/autotypes/spatio_temporal_demo.py
@@ -16,7 +17,7 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoSpatioTemporalGraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
@@ -27,16 +28,17 @@ QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
class Entity(BaseModel):
"""实体节点"""
name: str = Field(description="实体名称")
- category: str = Field(description="类别", default="人物")
+ category: str = Field(description="类别,例如:人物/地点/发明等")
+ description: str = Field(description="实体描述")
-class SpatioTemporalEvent(BaseModel):
- """时空事件"""
- source: str = Field(description="起点实体")
- target: str = Field(description="终点实体")
- action: str = Field(description="动作或关系")
- time: Optional[str] = Field(description="何时", default=None)
- location: Optional[str] = Field(description="何地", default=None)
+class SpatioTemporalRelation(BaseModel):
+ """时空关系"""
+ source: str = Field(description="源实体")
+ target: str = Field(description="目标实体")
+ relation_type: str = Field(description="关系类型,例如:父亲/兄弟/发明/参与等")
+ event_date: Optional[str] = Field(description="关系发生的时间", default=None)
+ location: Optional[str] = Field(description="关系发生地点,例如:北京等", default=None)
if __name__ == "__main__":
@@ -51,30 +53,31 @@ if __name__ == "__main__":
print("\n" + "=" * 60)
print(" 时空图示例")
print("=" * 60)
- print("提取时空事件...")
+ print("提取时空关系...")
- graph = AutoSpatioTemporalGraph[Entity, SpatioTemporalEvent](
+ graph = AutoSpatioTemporalGraph[Entity, SpatioTemporalRelation](
node_schema=Entity,
- edge_schema=SpatioTemporalEvent,
+ edge_schema=SpatioTemporalRelation,
node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.source}-{x.action}-{x.target}",
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
nodes_in_edge_extractor=lambda x: (x.source, x.target),
- time_in_edge_extractor=lambda x: x.time or "",
+ time_in_edge_extractor=lambda x: x.event_date or "",
location_in_edge_extractor=lambda x: x.location or "",
llm_client=llm,
embedder=embedder,
+ observation_time="2024-01-01",
observation_location="北宋",
)
graph.feed_text(text)
- print(f"\n提取了 {len(graph.nodes)} 个实体和 {len(graph.edges)} 个事件")
+ print(f"\n提取了 {len(graph.nodes)} 个实体和 {len(graph.edges)} 个关系")
- sorted_edges = sorted(graph.edges, key=lambda x: x.time or "")
+ sorted_edges = sorted(graph.edges, key=lambda x: x.event_date or "")
for edge in sorted_edges[:5]:
- time_info = f"({edge.time})" if edge.time else ""
+ time_info = f"({edge.event_date})" if edge.event_date else ""
loc_info = f" 在 {edge.location}" if edge.location else ""
- print(f" {edge.source} --[{edge.action}]--> {edge.target}{time_info}{loc_info}")
+ print(f" {edge.source} --[{edge.relation_type}]--> {edge.target}{time_info}{loc_info}")
graph.build_index()
@@ -89,4 +92,7 @@ if __name__ == "__main__":
except Exception as e:
print(f"错误: {e}")
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: f"{x.action}@{x.time}")
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.relation_type}@{x.event_date}" if x.event_date else x.relation_type,
+ )
diff --git a/examples/zh/autotypes/temporal_demo.py b/examples/zh/autotypes/temporal_graph_demo.py
similarity index 51%
rename from examples/zh/autotypes/temporal_demo.py
rename to examples/zh/autotypes/temporal_graph_demo.py
index a16d6db..3e14f58 100644
--- a/examples/zh/autotypes/temporal_demo.py
+++ b/examples/zh/autotypes/temporal_graph_demo.py
@@ -2,12 +2,14 @@
时序图示例 - 苏轼传记
使用 AutoTemporalGraph 从文本中提取时序关系。
+展示如何理解实体之间的时间顺序关系。
使用方法:
python examples/zh/autotypes/temporal_demo.py
"""
from pathlib import Path
+from typing import Optional
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
@@ -15,7 +17,7 @@ from pydantic import BaseModel, Field
from hyperextract.types import AutoTemporalGraph
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
@@ -26,14 +28,17 @@ QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
class Entity(BaseModel):
"""实体节点"""
name: str = Field(description="实体名称")
- type: str = Field(description="类型:人物/地名/作品", default="人物")
+ category: str = Field(description="类别,例如:人物/地点/发明等")
+ description: str = Field(description="实体描述")
-class TimelineEvent(BaseModel):
- """时间线事件"""
- time: str = Field(description="时间(年份)")
- event: str = Field(description="事件描述")
- entities: list[str] = Field(description="参与实体")
+class TemporalRelation(BaseModel):
+ """时序关系"""
+ source: str = Field(description="源实体")
+ target: str = Field(description="目标实体")
+ relation_type: str = Field(description="关系类型,例如:父亲/兄弟/发明/参与等")
+ event_date: Optional[str] = Field(description="关系发生的时间(年份或日期)", default=None)
+ description: str = Field(description="关系描述")
if __name__ == "__main__":
@@ -50,24 +55,26 @@ if __name__ == "__main__":
print("=" * 60)
print("提取时序关系...")
- graph = AutoTemporalGraph[Entity, TimelineEvent](
+ graph = AutoTemporalGraph[Entity, TemporalRelation](
node_schema=Entity,
- edge_schema=TimelineEvent,
+ edge_schema=TemporalRelation,
node_key_extractor=lambda x: x.name,
- edge_key_extractor=lambda x: f"{x.time}-{x.event}",
- nodes_in_edge_extractor=lambda x: tuple(x.entities) if x.entities else (),
- time_in_edge_extractor=lambda x: x.time,
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
+ nodes_in_edge_extractor=lambda x: (x.source, x.target),
+ time_in_edge_extractor=lambda x: x.event_date or "",
llm_client=llm,
embedder=embedder,
+ observation_time="2024-01-01",
)
graph.feed_text(text)
- print(f"\n提取了 {len(graph.nodes)} 个实体和 {len(graph.edges)} 个事件")
+ print(f"\n提取了 {len(graph.nodes)} 个实体和 {len(graph.edges)} 个关系")
- sorted_events = sorted(graph.edges, key=lambda x: x.time)
- for event in sorted_events[:5]:
- print(f" {event.time}:{event.event}")
+ sorted_edges = sorted(graph.edges, key=lambda x: x.event_date or "")
+ for edge in sorted_edges[:5]:
+ time_info = f"({edge.event_date})" if edge.event_date else ""
+ print(f" {edge.source} --[{edge.relation_type}]--> {edge.target}{time_info}")
graph.build_index()
@@ -82,4 +89,7 @@ if __name__ == "__main__":
except Exception as e:
print(f"错误: {e}")
- graph.show(node_label_extractor=lambda x: x.name, edge_label_extractor=lambda x: f"{x.event}@{x.time}")
+ graph.show(
+ node_label_extractor=lambda x: x.name,
+ edge_label_extractor=lambda x: f"{x.relation_type}@{x.event_date}" if x.event_date else x.relation_type,
+ )
diff --git a/examples/zh/methods/atom_demo.py b/examples/zh/methods/atom_demo.py
index 0654edd..4fe3f6b 100644
--- a/examples/zh/methods/atom_demo.py
+++ b/examples/zh/methods/atom_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import Atom
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Atom 示例")
@@ -37,8 +37,6 @@ if __name__ == "__main__":
atom = Atom(llm_client=llm, embedder=embedder)
atom.feed_text(text)
- print(f"\n✓ 提取了 {len(atom.facts)} 条时序事实\n")
-
print("-" * 60)
print("问答")
print("-" * 60)
diff --git a/examples/zh/methods/cog_rag_demo.py b/examples/zh/methods/cog_rag_demo.py
index c10f95f..49893c4 100644
--- a/examples/zh/methods/cog_rag_demo.py
+++ b/examples/zh/methods/cog_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Cog_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Cog RAG 示例")
diff --git a/examples/zh/methods/graph_rag_demo.py b/examples/zh/methods/graph_rag_demo.py
index 61e279c..a5fb615 100644
--- a/examples/zh/methods/graph_rag_demo.py
+++ b/examples/zh/methods/graph_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Graph_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Graph RAG 示例")
diff --git a/examples/zh/methods/hyper_rag_demo.py b/examples/zh/methods/hyper_rag_demo.py
index ba3a7f7..6e6a646 100644
--- a/examples/zh/methods/hyper_rag_demo.py
+++ b/examples/zh/methods/hyper_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Hyper_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Hyper RAG 示例")
diff --git a/examples/zh/methods/hypergraph_rag_demo.py b/examples/zh/methods/hypergraph_rag_demo.py
index 162c598..00d3ccd 100644
--- a/examples/zh/methods/hypergraph_rag_demo.py
+++ b/examples/zh/methods/hypergraph_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import HyperGraph_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("HyperGraph RAG 示例")
diff --git a/examples/zh/methods/itext2kg_demo.py b/examples/zh/methods/itext2kg_demo.py
index c63bad7..e495e47 100644
--- a/examples/zh/methods/itext2kg_demo.py
+++ b/examples/zh/methods/itext2kg_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import iText2KG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("iText2KG 示例")
diff --git a/examples/zh/methods/itext2kg_star_demo.py b/examples/zh/methods/itext2kg_star_demo.py
index 9cafc4d..45d83fa 100644
--- a/examples/zh/methods/itext2kg_star_demo.py
+++ b/examples/zh/methods/itext2kg_star_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import iText2KG_Star
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("iText2KG Star 示例")
diff --git a/examples/zh/methods/kg_gen_demo.py b/examples/zh/methods/kg_gen_demo.py
index b4fb027..1063929 100644
--- a/examples/zh/methods/kg_gen_demo.py
+++ b/examples/zh/methods/kg_gen_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.typical import KG_Gen
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("KG Gen 示例")
diff --git a/examples/zh/methods/light_rag_demo.py b/examples/zh/methods/light_rag_demo.py
index e77bccb..7213639 100644
--- a/examples/zh/methods/light_rag_demo.py
+++ b/examples/zh/methods/light_rag_demo.py
@@ -14,12 +14,12 @@ from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from hyperextract.methods.rag import Light_RAG
-project_root = Path(__file__).resolve().parent.parent.parent
+project_root = Path(__file__).resolve().parent.parent.parent.parent
load_dotenv()
-INPUT_FILE = project_root / "zh" / "sushi.md"
-QUESTION_FILE = project_root / "zh" / "sushi_question.md"
+INPUT_FILE = project_root / "examples" / "zh" / "sushi.md"
+QUESTION_FILE = project_root / "examples" / "zh" / "sushi_question.md"
if __name__ == "__main__":
with open(INPUT_FILE, encoding="utf-8") as f:
@@ -28,7 +28,7 @@ if __name__ == "__main__":
questions = [line.strip() for line in f if line.strip()]
llm = ChatOpenAI(model="gpt-4o-mini")
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
print("=" * 60)
print("Light RAG 示例")
diff --git a/examples/zh/sushi.md b/examples/zh/sushi.md
index a7c59e8..b4e2869 100644
--- a/examples/zh/sushi.md
+++ b/examples/zh/sushi.md
@@ -1,81 +1,77 @@
# 苏轼传
-苏轼,字子瞻,号东坡居士,是北宋著名的文学家、书画家、政治家。他与父亲苏洵、弟弟苏辙并称"三苏",同列唐宋八大家。
+苏轼,字子瞻,号东坡居士,是北宋时期最杰出的文学家、书画家、政治家。他与父亲苏洵、弟弟苏辙并称"三苏",同列唐宋八大家。
## 早年与家学
-苏轼于1037年出生于眉州眉山。他的父亲苏洵是著名学者,对苏轼和弟弟苏辙的教育极为严格。苏轼自幼聪慧,在父亲的教导下,与弟弟苏辙一起苦读诗文。
+苏轼于1037年出生于眉州眉山(今四川眉山)。他的父亲苏洵是北宋著名文学家,对两个儿子的教育极为严格。据记载,苏轼的母亲程氏曾用"断机教子"的方法激励他学习。一天小苏轼读书心不在焉,程氏便剪断正在织的布,说:"你读书就像织布,半途而废便毫无用处。"苏轼深受触动,从此发奋读书。
-1056年,19岁的苏轼与父亲、弟弟一起进京赶考。当时的主考官是大文豪欧阳修,欧阳修对苏轼的策论赞叹不已,认为他日后必成大器。
+苏轼十岁时读《后汉书·范滂传》,对母亲说:"我长大后若能像范滂一样做个忠臣孝子,您会允许吗?"程氏回答:"你能做范滂,我难道不能做范滂的母亲吗?"可见苏轼从小便立下了报国济民的志向。
-## 仕途与起伏
+## 初入仕途
+
+1056年,19岁的苏轼与父亲、弟弟一起进京赶考。1057年科举考试由欧阳修担任主考官,他看到苏轼的策论文章,大为赞赏。欧阳修以为文章是自己的学生曾巩所作,为避嫌将它列为第二名。放榜后欧阳修才知道作者是苏轼,专门接见了他说:"我应当为你让路,才能使你海阔天空地发展。"他还预言:"三十年后,无人再谈论老夫,将被苏轼的光芒所掩盖。"
+
+苏轼与弟弟苏辙同科中举,后又参加制科考试被任命为大理评事。然而母亲程氏病逝,苏轼回乡守孝三年,期间专心读书写作。
-1057年,苏轼与弟弟苏辙同科中举,被任命为福昌县主簿。但苏轼心中有大志,他参加了制科考试,以优异成绩被任命为大理评事。
+## 仕途与起伏
-1069年,苏轼回到京城开封,适逢王安石推行新法。苏轼与王安石政见不合,多次上书反对新法。他认为新法过于激进,损害了百姓利益。
+守孝期满后,苏轼任凤翔府判官,后回京入直史馆。1067年宋神宗即位,王安石推行新法。苏轼与王安石政见不合,多次上书批评新法,认为新法过于激进,损害了百姓利益。这些批评触怒了变法派。
-1071年,苏轼主动请求外放,被任命为杭州通判。此后,他先后在密州、徐州、湖州等地任职。
+1071年苏轼主动请求外放,任杭州通判。在杭州期间他常常泛舟西湖,写下了著名的《饮湖上初晴后雨》:"欲把西湖比西子,淡妆浓抹总相宜。"后任密州知州,写下《江城子·密州出猎》,其中"老夫聊发少年狂"展现了他豪迈的词风。1077年任徐州知州时,黄河决口,苏轼亲自指挥抗洪抢险,成功保住徐州城。
## 乌台诗案
-1079年,苏轼在湖州任上写了许多讽刺新法的诗作,被人告发。这就是著名的"乌台诗案"。苏轼被押送回京,下狱待审。
+1079年,苏轼调任湖州知州这一年,发生了著名的"乌台诗案"。苏轼一直对新法持有异议,写了许多讽刺新法的诗作。御史台官员李定、舒亶等人搜集他的诗作,断章取义,指控他讪谤朝政。苏轼被押送回京,关进御史台监狱。
-在狱中,苏轼几乎以为自己必死无疑。他的弟弟苏辙上书请求以自己的官职为哥哥赎罪。许多朋友和官员也为他求情。
+在狱中苏轼几乎以为自己必死无疑,写下了"梦绕云山心似鹿,魂飞汤火命如鸡"的诗句。弟弟苏辙请求以自己的官职为哥哥赎罪,好友王诜多方奔走营救。许多正直官员纷纷上书求情,已退休的欧阳修也写信说:"陛下若杀苏轼,是诛天下之才也。"
-最终,苏轼被贬为黄州团练副使,这是一个没有实权的闲职。在黄州期间,苏轼开垦荒地,自号"东坡居士",创作了许多著名诗文,包括《念奴娇·赤壁怀古》和前、后《赤壁赋》。
+最终苏轼免于一死,被贬为黄州团练副使。在押送途中,船行至长江时,苏轼曾想跳水自杀,被士兵及时拉住。他后来才知道,好友佛印禅师早就在江边等候,担心他想不开。
-## 与佛印的友谊
+## 黄州岁月
-在黄州期间,苏轼与高僧佛印禅师结为好友。两人经常在一起参禅论道,留下了许多趣事。
+1080年苏轼抵达黄州,寄住在定慧院,后来在城东荒废的山坡上开垦种地。这片山坡后来被称为"东坡",苏轼也因此自号"东坡居士"。团练副使是闲散官职,没有俸禄,苏轼不得不过着清贫的躬耕生活。
-一次,苏轼写诗说自己"八风吹不动",佛印回复"放屁"二字。苏轼大怒,过江找佛印理论。佛印笑着说:"你不是八风吹不动吗?怎么一屁就打过江来了?"
+黄州时期是苏轼文学创作的黄金时期。他在这里创作了大量千古传诵的名篇。《念奴娇·赤壁怀古》写道:"大江东去,浪淘尽,千古风流人物。"这首词被誉为"千古第一词",气势磅礴,成为豪放词的代表作。《前赤壁赋》描写泛舟夜游的场景,"清风徐来,水波不兴",语言优美,意境深远。《定风波》写道:"莫听穿林打叶声,何妨吟啸且徐行。一蓑烟雨任平生。"展现了他旷达超脱的人生态度。
-苏轼哑然失笑,从此更加敬重佛印。
+在黄州期间,苏轼与高僧佛印禅师结为挚友。最著名的是"八风吹不动"的典故。苏轼写诗说自己"八风吹不动",佛印只批了"放屁"二字。苏轼大怒过江理论,却发现门上贴着:"八风吹不动,一屁打过江。"苏轼恍然大悟,从此更加敬重佛印。
## 杭州岁月
-1089年,苏轼任杭州知州。他关心民生,治理西湖,修建了著名的"苏堤"。这道堤坝至今仍是西湖十景之一。
+1089年苏轼任杭州知州,这是他第二次到杭州。西湖多年淤积,湖面缩小。苏轼提出"取葑泥、筑长堤"的办法,组织民工挖掘淤泥筑起一条长长的堤坝。这条长约2.8公里的堤坝将西湖分为里湖和外湖,有效改善了水质。苏轼又在堤上种植芙蓉、杨柳,后人将这条堤称为"苏堤",苏堤春晓成为"西湖十景"之首。
-在杭州,苏轼还救治了许多贫苦百姓。他自己生活简朴,却经常资助他人。
+苏轼还在杭州创立了"安乐坊",这是中国历史上最早由政府创办的医院之一,专门收治贫苦病人。
## 晚年与贬谪
-1093年,苏轼的结发妻子王弗病逝,他悲痛欲绝。后来,侍妾朝云一直陪伴在他身边。
+1093年苏轼深爱的妻子王弗病逝,年仅二十七岁。苏轼悲痛欲绝,十年后在密州写下了感人至深的《江城子》:"十年生死两茫茫,不思量,自难忘。"这首词被誉为"千古第一悼亡词"。
-1097年,62岁的苏轼被贬至惠州,后又被贬至更远的海南岛儋州。在那个时代,儋州是极偏僻的地方,几乎等同于流放。
+侍妾朝云一直陪伴在苏轼身边,无论顺境逆境始终不离不弃。苏轼被贬黄州时,朝云不顾家人反对毅然追随。朝云还是苏轼在佛学上的知音。
-但苏轼依然乐观豁达。他与当地百姓相处融洽,教书育人,传播文化。
+1094年苏轼被贬惠州,写下"日啖荔枝三百颗,不辞长作岭南人"。1097年62岁的苏轼被一贬再贬,最后被贬至海南岛儋州。在宋代,儋州是极偏僻的地方,几乎等同于流放。但苏轼依然关心百姓,教书育人,传播中原文化。
## 逝世
-1101年,苏轼获准北归,但身体已经衰弱。他病逝于常州,享年65岁。
-
-苏轼一生坎坷,却始终保持旷达的心态。他的诗文、书画流传千古,他与弟弟苏辙的手足之情也被传为佳话。
-
-## 人物关系
+1101年苏轼遇赦北归,这时的他已是65岁的老人。当他到达常州时一病不起,于七月二十八日病逝。
-### 家人
-- 父亲苏洵:著名学者,教导苏轼成才
-- 弟弟苏辙:同为文学家,苏轼最亲密的手足
-- 妻子王弗:结发妻子,早年病逝
-- 侍妾朝云:陪伴苏轼晚年的红颜知己
+苏轼一生坎坷,宦海沉浮,但他始终保持着旷达乐观的心态。他曾说:"问汝平生功业,黄州惠州儋州。"
-### 师友
-- 欧阳修:恩师,苏轼科考的主考官
-- 佛印禅师:高僧好友,常与苏轼论禅
-- 黄庭坚:学生,书法家
+苏轼在中国文学史上的地位无与伦比。在诗歌方面与黄庭坚并称"苏黄";在词方面是豪放派开创者,与辛弃疾并称"苏辛";在散文方面是"唐宋八大家"之一;在书法方面是"宋四家"之首,其《黄州寒食诗帖》被誉为"天下第三行书";在绘画方面是文人画的开创者之一。苏轼发明的"东坡肉"至今仍是名菜。
-### 政敌
-- 王安石:变法领袖,与苏轼政见不合
-- 变法派:多次迫害苏轼
+苏轼与弟弟苏辙的手足之情是中国文学史上最动人的兄弟情谊之一。苏轼去世后,苏辙将他安葬并撰写墓志铭。苏轼以自己的才华和品格,赢得了身后千年的荣耀。
## 重要时间线
-- 1037年:出生于眉州眉山
-- 1056年:随父、弟进京赶考
-- 1057年:科考中举,师从欧阳修
-- 1069年:回京,反对王安石变法
-- 1079年:乌台诗案,被贬黄州
-- 1089年:任杭州知州,修建苏堤
-- 1097年:被贬惠州,后至儋州
-- 1101年:病逝于常州
+- **1037年**:出生于眉州眉山
+- **1057年**:科举中举,受欧阳修赏识
+- **1065年**:母亲病逝,回乡守孝
+- **1071年**:自请外任为杭州通判
+- **1079年**:乌台诗案被捕入狱
+- **1080年**:被贬黄州,躬耕东坡
+- **1082年**:创作《赤壁怀古》《赤壁赋》等名作
+- **1089年**:任杭州知州,修筑苏堤
+- **1094年**:被贬惠州
+- **1097年**:被贬海南儋州
+- **1101年**:病逝于常州
+
+苏轼用自己的一生诠释了"穷则独善其身,达则兼济天下"的人生哲学,他的名字将永远镌刻在中国文化的丰碑上。
diff --git a/examples/zh/templates/finance_template.py b/examples/zh/templates/finance_template.py
index 0c7d888..8c015a1 100644
--- a/examples/zh/templates/finance_template.py
+++ b/examples/zh/templates/finance_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/zh/templates/general_template.py b/examples/zh/templates/general_template.py
index 8148e2d..2584ac6 100644
--- a/examples/zh/templates/general_template.py
+++ b/examples/zh/templates/general_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/zh/templates/industry_template.py b/examples/zh/templates/industry_template.py
index 90f152b..cd14881 100644
--- a/examples/zh/templates/industry_template.py
+++ b/examples/zh/templates/industry_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/zh/templates/legal_template.py b/examples/zh/templates/legal_template.py
index 97984dd..a761208 100644
--- a/examples/zh/templates/legal_template.py
+++ b/examples/zh/templates/legal_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/zh/templates/medicine_template.py b/examples/zh/templates/medicine_template.py
index 5253477..6c4253f 100644
--- a/examples/zh/templates/medicine_template.py
+++ b/examples/zh/templates/medicine_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/examples/zh/templates/tcm_template.py b/examples/zh/templates/tcm_template.py
index b5788f2..0b38135 100644
--- a/examples/zh/templates/tcm_template.py
+++ b/examples/zh/templates/tcm_template.py
@@ -53,7 +53,7 @@ def main():
print("=" * 80)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
- embedder = OpenAIEmbeddings()
+ embedder = OpenAIEmbeddings(model="text-embedding-3-small")
extractor = Template.create(
template_name,
diff --git a/hyperextract/methods/typical/atom.py b/hyperextract/methods/typical/atom.py
index f86e5c7..6763f73 100644
--- a/hyperextract/methods/typical/atom.py
+++ b/hyperextract/methods/typical/atom.py
@@ -203,7 +203,7 @@ Given an input paragraph and an `observation_time`, generate a list of all disti
"""
# Atom FACToid prompt ends, edge prompt begins
-Atom_EDGE_EXTRACTION_PROMPT = f"""
+Atom_EDGE_EXTRACTION_PROMPT = """
Observation Date: {observation_time}
You are a precise knowledge extraction engine designed to distill unstructured text into a structured Knowledge Graph.
@@ -500,21 +500,14 @@ class Atom(AutoGraph[NodeSchema, EdgeSchema]):
fact_chunks.append(formatted_chunk)
# 6. Batch Extract Edges directly from Fact Chunks
- # Format the edge prompt with observation date
- edge_prompt_with_date = self.edge_prompt.format(observation_time=obs_date_str)
- edge_prompt_template = ChatPromptTemplate.from_template(edge_prompt_with_date)
- edge_chain = edge_prompt_template | self.llm_client.with_structured_output(
- self.edge_list_schema
- )
-
- edge_inputs = [{"source_text": chunk} for chunk in fact_chunks]
- chunk_edge_lists = edge_chain.batch(
+ edge_inputs = [{"source_text": chunk, "observation_time": obs_date_str} for chunk in fact_chunks]
+ chunk_edge_lists = self.edge_extractor.batch(
edge_inputs, config={"max_concurrency": self.max_workers}
)
# ==================== Step 3: Post-process & Merge ====================
- # 8. Post-process: Derive Nodes & Set t_obs
+ # 7. Post-process: Derive Nodes & Set t_obs
all_nodes_lists, all_edges_lists = [], []
for edge_list in chunk_edge_lists:
@@ -534,10 +527,10 @@ class Atom(AutoGraph[NodeSchema, EdgeSchema]):
all_nodes_lists.append(derived_nodes)
all_edges_lists.append(processed_edges)
- # 9. Construct Partial Graphs
+ # 8. Construct Partial Graphs
partial_graphs = (all_nodes_lists, all_edges_lists)
- # 10. Global Merge
+ # 9. Global Merge
raw_graph = self.merge_batch_data(partial_graphs)
return self._prune_dangling_edges(raw_graph)