-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathpost_extraction_canonicalization.py
More file actions
194 lines (160 loc) · 6.48 KB
/
Copy pathpost_extraction_canonicalization.py
File metadata and controls
194 lines (160 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import asyncio
import os
import time
import cognee
import numpy as np
from typing import Dict, Type, List, Optional, Any
import pandas as pd
from pandas import DataFrame
from pydantic import BaseModel
from cognee.infrastructure.databases.graph import get_graph_engine
from cognee.infrastructure.databases.vector import get_vector_engine_async
from cognee.infrastructure.llm.extraction import extract_content_graph
from cognee.modules.chunking.models import DocumentChunk
def _get_closest_match(df: DataFrame, query_vector) -> list[Any] | tuple[Any, float]:
if df is None or df.empty:
return None
# columns are vectors; shape: (dim, n)
M = df.to_numpy(dtype=float) # shape (dim, n_cols)
q = np.asarray(query_vector, dtype=float) # shape (dim,)
q_norm = np.linalg.norm(q)
if q_norm == 0 or M.size == 0:
return None
# cosine similarity for all columns at once
denom = np.linalg.norm(M, axis=0) * q_norm
# avoid divide-by-zero
denom = np.where(denom == 0, np.inf, denom)
sims = (M.T @ q) / denom # shape (n_cols,)
closest_idx = int(np.argmax(sims))
similarity_val = float(sims[closest_idx])
names = df.columns.to_numpy()
return names[closest_idx], similarity_val
async def _get_closest_match_1(
node, vector_engine, df: DataFrame, df_new: DataFrame, similarity_threshold, stats
) -> tuple[Any, Any, float]:
query_vector = await vector_engine.embed_data(node.name)
if node.name not in df_new.columns and node.name not in df.columns:
df_new[node.name] = pd.Series(query_vector[0], dtype=float)
if df is None or df.empty:
return None
# columns are vectors; shape: (dim, n)
M = df.to_numpy(dtype=float) # shape (dim, n_cols)
q = np.asarray(query_vector[0], dtype=float) # shape (dim,)
q_norm = np.linalg.norm(q)
if q_norm == 0 or M.size == 0:
return None
# cosine similarity for all columns at once
denom = np.linalg.norm(M, axis=0) * q_norm
# avoid divide-by-zero
denom = np.where(denom == 0, np.inf, denom)
sims = (M.T @ q) / denom # shape (n_cols,)
closest_idx = int(np.argmax(sims))
similarity_val = float(sims[closest_idx])
names = df.columns.to_numpy()
closest_match_name = names[closest_idx]
print(
f"node={node.name}, closest_match={closest_match_name}, match_similarity={similarity_val}"
)
if similarity_val > similarity_threshold:
node.name = closest_match_name
if isinstance(stats, dict):
stats["reused_entities"] = (stats.get("reused_entities") or 0) + 1
return node.name, names[closest_idx], similarity_val
async def cache_and_replace_nodes(graphs, **kwargs):
df = kwargs.get("df", None)
similarity_threshold = kwargs.get("similarity_threshold", 1.0)
stats = kwargs.get("stats", None)
vector_engine = await get_vector_engine_async()
df_new = pd.DataFrame()
for graph in graphs:
await asyncio.gather(
*[
_get_closest_match_1(node, vector_engine, df, df_new, similarity_threshold, stats)
for node in graph.nodes
]
)
if not df_new.empty:
# Drop only overlapping columns in one shot to avoid in-place mutation
# during iteration and to tolerate any concurrent column changes.
overlap = df_new.columns.intersection(df.columns)
if len(overlap) > 0:
df_new.drop(columns=overlap, inplace=True, errors="ignore")
# avoid fragmentation, improve speed, keep the same df
df[df_new.columns] = df_new
async def _get_entity_names_from_graph() -> set[str]:
graph_engine = await get_graph_engine()
nodes, _ = await graph_engine.get_graph_data()
names = set()
for _node_id, props in nodes:
props = props or {}
if props.get("type") in {"Entity", "GraphEntity"}:
name = props.get("name")
if isinstance(name, str) and name.strip():
names.add(name.strip())
return names
def _report_disambiguation_rate(graph_entity_names, entities_to_disambiguate):
unresolved_entities_count = 0
for name in graph_entity_names:
if name in entities_to_disambiguate:
unresolved_entities_count += 1
print(
f"Disambiguated entities: {(len(entities_to_disambiguate) - unresolved_entities_count) / len(entities_to_disambiguate) * 100}%"
)
async def calculate_chunk_graphs_post_extraction_canonicalization(
data_chunks: List[DocumentChunk],
graph_model: Type[BaseModel],
custom_prompt: Optional[str] = None,
**kwargs,
):
extractor_kwargs = {
key: value
for key, value in kwargs.items()
if key
not in {
"calculate_chunk_graphs",
"cache_entity_embeddings",
"df",
"similarity_threshold",
"stats",
}
}
chunk_graphs = await asyncio.gather(
*[
extract_content_graph(
chunk.text, graph_model, custom_prompt=custom_prompt, **extractor_kwargs
)
for chunk in data_chunks
]
)
return chunk_graphs
async def post_extraction_canonicalization(
parts_dir,
custom_prompt,
disambiguated_entities_names_file: Optional[str] = None,
):
df = pd.DataFrame()
kwargs = {
"calculate_chunk_graphs": calculate_chunk_graphs_post_extraction_canonicalization,
"cache_entity_embeddings": cache_and_replace_nodes,
"df": df,
"similarity_threshold": 0.8,
"stats": {"reused_entities": 0},
}
parent_folder = os.path.dirname(os.path.abspath(__file__))
if disambiguated_entities_names_file is None:
disambiguated_entities_names_file = os.path.join(
parent_folder, "data", "example2", "expected_disambiguation_entities.txt"
)
with open(disambiguated_entities_names_file, "r", encoding="utf-8") as f:
disambiguated_entities_names = f.read().split("\n")
start = time.perf_counter()
for part in sorted(parts_dir.glob("part_*.txt")):
print(part)
text = part.read_text(encoding="utf-8").replace("\n", " ")
await cognee.add(text)
await cognee.cognify(chunk_size=1024, custom_prompt=custom_prompt, **kwargs)
elapsed = time.perf_counter() - start
print(f"Elapsed: {elapsed:.6f} seconds")
graph_entity_names = await _get_entity_names_from_graph()
_report_disambiguation_rate(graph_entity_names, disambiguated_entities_names)
print(f"Reused instances: {kwargs.get('stats').get('reused_entities')}")