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: 在响应元数据中注入检索到的节点和边

yifanfeng9789ae4dbf3b6f
diff --git a/examples/hypergraph_rag_demo.py b/examples/hypergraph_rag_demo.py
index 2076488..952cb8e 100644
--- a/examples/hypergraph_rag_demo.py
+++ b/examples/hypergraph_rag_demo.py
@@ -1,9 +1,8 @@
"""
-HyperGraph_RAG Demo: Edge-First Extraction Strategy
+HyperGraph-RAG Demo: Extracting Hyperedges from Text
-This demo demonstrates the HyperGraph_RAG System:
-1. **Edge-First Strategy**: Extracts Themes/Events (Hyperedges) first, then contextually extracts participating entities.
-2. **Narrative Understanding**: Focuses on identifying high-level themes before drilling down to actors.
+This script demonstrates the usage of the HyperGraph_RAG system, which is a
+system designed to extract hyperedges from text.
"""
import os
@@ -19,7 +18,7 @@ from hyperextract.hypergraphs import HyperGraph_RAG
load_dotenv()
# ============================================================================
-# Sample Story for Extraction (Shared with AutoHypergraph Demo)
+# Sample Story for Extraction
# ============================================================================
STORY = """
diff --git a/hyperextract/core/base.py b/hyperextract/core/base.py
index 5fda6ab..6ba9ec8 100644
--- a/hyperextract/core/base.py
+++ b/hyperextract/core/base.py
@@ -411,7 +411,12 @@ class BaseAutoType(ABC, Generic[T]):
response = qa_chain.invoke({"context": context, "question": query})
- # Step 4: Return the AIMessage response
+ # Step 4: Inject retrieved items into response metadata
+ if not response.additional_kwargs:
+ response.additional_kwargs = {}
+ response.additional_kwargs["retrieved_items"] = search_results
+
+ # Step 5: Return the AIMessage response
return response
# ==================== Serialization: Orchestrator ====================
diff --git a/hyperextract/graphs/base.py b/hyperextract/graphs/base.py
index 61ae6e4..393addf 100644
--- a/hyperextract/graphs/base.py
+++ b/hyperextract/graphs/base.py
@@ -761,6 +761,7 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
context_parts = []
# Step 2: Retrieve and format nodes context
+ nodes = []
if top_k_nodes > 0:
nodes = self.search_nodes(query, top_k=top_k_nodes)
if nodes:
@@ -772,6 +773,7 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
context_parts.append(node.model_dump_json(indent=2))
# Step 3: Retrieve and format edges context
+ edges = []
if top_k_edges > 0:
edges = self.search_edges(query, top_k=top_k_edges)
if edges:
@@ -797,7 +799,15 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[NodeSchema, EdgeSchema]], Generic[N
)
qa_chain = qa_prompt | self.llm_client
- return qa_chain.invoke({"context": context, "question": query})
+ response = qa_chain.invoke({"context": context, "question": query})
+
+ # Step 6: Inject retrieved nodes and edges into response metadata
+ if not response.additional_kwargs:
+ response.additional_kwargs = {}
+ response.additional_kwargs["retrieved_nodes"] = nodes
+ response.additional_kwargs["retrieved_edges"] = edges
+
+ return response
# ==================== Serialization ====================
diff --git a/hyperextract/hypergraphs/base.py b/hyperextract/hypergraphs/base.py
index 5e4261d..db223b9 100644
--- a/hyperextract/hypergraphs/base.py
+++ b/hyperextract/hypergraphs/base.py
@@ -719,6 +719,7 @@ class AutoHypergraph(
context_parts = []
# Step 2: Retrieve and format nodes context
+ nodes = []
if top_k_nodes > 0:
nodes = self.search_nodes(query, top_k=top_k_nodes)
if nodes:
@@ -730,6 +731,7 @@ class AutoHypergraph(
context_parts.append(node.model_dump_json(indent=2))
# Step 3: Retrieve and format edges context
+ edges = []
if top_k_edges > 0:
edges = self.search_edges(query, top_k=top_k_edges)
if edges:
@@ -755,7 +757,15 @@ class AutoHypergraph(
)
qa_chain = qa_prompt | self.llm_client
- return qa_chain.invoke({"context": context, "question": query})
+ response = qa_chain.invoke({"context": context, "question": query})
+
+ # Step 6: Inject retrieved nodes and edges into response metadata
+ if not response.additional_kwargs:
+ response.additional_kwargs = {}
+ response.additional_kwargs["retrieved_nodes"] = nodes
+ response.additional_kwargs["retrieved_edges"] = edges
+
+ return response
# ==================== Serialization ====================