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: 优化聊天交互功能,重命名参数以提高可读性,简化节点和边的检索逻辑

yifanfeng9768c849650444
diff --git a/examples/auto_hypergraph_demo.py b/examples/auto_hypergraph_demo.py
index 4fc98b1..6f77ae4 100644
--- a/examples/auto_hypergraph_demo.py
+++ b/examples/auto_hypergraph_demo.py
@@ -54,10 +54,7 @@ class NarrativeUnit(BaseModel):
summary: str = Field(..., description="简短描述这个单元发生了什么,或者是什么关系")
# 这里体现了"多类型":LLM 会判断这具体是什么类型的连接
- edge_type: str = Field(
- ...,
- description="超边类型: Conflict(冲突), Alliance(结盟), Possession(持有), Meeting(会议), State(状态)",
- )
+ edge_type: str = Field(description="超边类型: Conflict(冲突), Alliance(结盟), Possession(持有), Meeting(会议), State(状态)")
# 核心:连接所有相关的实体
participants: List[str] = Field(..., description="所有牵涉其中的实体名称列表")
diff --git a/hyperextract/graphs/base.py b/hyperextract/graphs/base.py
index 8606c08..eaf6822 100644
--- a/hyperextract/graphs/base.py
+++ b/hyperextract/graphs/base.py
@@ -735,40 +735,39 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[Node, Edge]], Generic[Node, Edge]):
def chat(
self,
query: str,
- top_k: int = 3,
- chat_with_nodes: bool = True,
- chat_with_edges: bool = True,
+ top_k_nodes: int = 3,
+ top_k_edges: int = 3,
) -> AIMessage:
"""Performs a chat-like interaction using graph knowledge.
- Retrieves relevant nodes and/or edges independently based on the query and flags,
+ Retrieves relevant nodes and/or edges based on the query and retrieval counts,
formats them into a structured context with clear headers, and generates an answer.
Args:
query: User query string.
- top_k: Number of relevant items to retrieve per type (default: 3).
- chat_with_nodes: Whether to include relevant entities in context (default: True).
- chat_with_edges: Whether to include relevant edges in context (default: True).
+ top_k_nodes: Number of relevant nodes to retrieve (default: 3). Set to 0 to disable node context.
+ top_k_edges: Number of relevant edges to retrieve (default: 3). Set to 0 to disable edge context.
Returns:
An AIMessage object containing the LLM-generated response.
Access the text content via response.content.
Example:
- >>> response = kb.chat("What is X?", top_k=5)
+ >>> # Chat using 5 nodes and 2 edges as context
+ >>> response = kb.chat("What is X?", top_k_nodes=5, top_k_edges=2)
>>> print(response.content) # Print the generated answer
"""
# Step 1: Validation
- if not chat_with_nodes and not chat_with_edges:
+ if top_k_nodes <= 0 and top_k_edges <= 0:
raise ValueError(
- "At least one of chat_with_nodes or chat_with_edges must be True."
+ "At least one of top_k_nodes or top_k_edges must be positive."
)
context_parts = []
# Step 2: Retrieve and format nodes context
- if chat_with_nodes:
- nodes = self.search_nodes(query, top_k)
+ if top_k_nodes > 0:
+ nodes = self.search_nodes(query, top_k=top_k_nodes)
if nodes:
context_parts.append("=== Relevant Nodes ===")
for node in nodes:
@@ -778,8 +777,8 @@ class AutoGraph(BaseAutoType[AutoGraphSchema[Node, Edge]], Generic[Node, Edge]):
context_parts.append(node.model_dump_json(indent=2))
# Step 3: Retrieve and format edges context
- if chat_with_edges:
- edges = self.search_edges(query, top_k)
+ if top_k_edges > 0:
+ edges = self.search_edges(query, top_k=top_k_edges)
if edges:
context_parts.append("=== Relevant Edges ===")
for edge in edges:
diff --git a/hyperextract/hypergraphs/base.py b/hyperextract/hypergraphs/base.py
index 3f713b2..1a90ef5 100644
--- a/hyperextract/hypergraphs/base.py
+++ b/hyperextract/hypergraphs/base.py
@@ -657,40 +657,39 @@ class AutoHypergraph(
def chat(
self,
query: str,
- top_k: int = 3,
- chat_with_nodes: bool = True,
- chat_with_edges: bool = True,
+ top_k_nodes: int = 3,
+ top_k_edges: int = 3,
) -> AIMessage:
"""Performs a chat-like interaction using hypergraph knowledge.
- Retrieves relevant nodes and/or hyperedges independently based on the query and flags,
+ Retrieves relevant nodes and/or hyperedges based on the query and retrieval counts,
formats them into a structured context with clear headers, and generates an answer.
Args:
query: User query string.
- top_k: Number of relevant items to retrieve per type (default: 3).
- chat_with_nodes: Whether to include relevant entities in context (default: True).
- chat_with_edges: Whether to include relevant hyperedges in context (default: True).
+ top_k_nodes: Number of relevant nodes to retrieve (default: 3). Set to 0 to disable node context.
+ top_k_edges: Number of relevant hyperedges to retrieve (default: 3). Set to 0 to disable edge context.
Returns:
An AIMessage object containing the LLM-generated response.
Access the text content via response.content.
Example:
- >>> response = hg.chat("What participates in X?", top_k=5)
+ >>> # Chat using 5 nodes and 2 hyperedges as context
+ >>> response = hg.chat("What participates in X?", top_k_nodes=5, top_k_edges=2)
>>> print(response.content) # Print the generated answer
"""
# Step 1: Validation
- if not chat_with_nodes and not chat_with_edges:
+ if top_k_nodes <= 0 and top_k_edges <= 0:
raise ValueError(
- "At least one of chat_with_nodes or chat_with_edges must be True."
+ "At least one of top_k_nodes or top_k_edges must be positive."
)
context_parts = []
# Step 2: Retrieve and format nodes context
- if chat_with_nodes:
- nodes = self.search_nodes(query, top_k)
+ if top_k_nodes > 0:
+ nodes = self.search_nodes(query, top_k=top_k_nodes)
if nodes:
context_parts.append("=== Relevant Nodes ===")
for node in nodes:
@@ -700,8 +699,8 @@ class AutoHypergraph(
context_parts.append(node.model_dump_json(indent=2))
# Step 3: Retrieve and format edges context
- if chat_with_edges:
- edges = self.search_edges(query, top_k)
+ if top_k_edges > 0:
+ edges = self.search_edges(query, top_k=top_k_edges)
if edges:
context_parts.append("=== Relevant Edges ===")
for edge in edges: