-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_artifacts.py
More file actions
64 lines (52 loc) · 1.87 KB
/
Copy pathgraph_artifacts.py
File metadata and controls
64 lines (52 loc) · 1.87 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
from __future__ import annotations
from pathlib import Path
from typing import Iterable
import pandas as pd
DEFAULT_INPUT_DIR = Path("./input/wiki-1k")
ARTIFACT_FILES = {
"documents": "documents.parquet",
"entities": "entities.parquet",
"relationships": "relationships.parquet",
"text_units": "text_units.parquet",
}
REQUIRED_COLUMNS = {
"documents": {"id", "title", "text"},
"entities": {"id", "title", "type", "description", "text_unit_ids"},
"relationships": {
"id",
"source",
"target",
"description",
"weight",
"text_unit_ids",
},
"text_units": {"id", "text", "document_id"},
}
def artifact_path(input_dir: str | Path, artifact: str) -> Path:
"""Return the path to one artifact in a new-format GraphRAG dataset."""
if artifact not in ARTIFACT_FILES:
raise KeyError(f"Unknown GraphRAG artifact: {artifact}")
path = Path(input_dir) / ARTIFACT_FILES[artifact]
if not path.is_file():
raise FileNotFoundError(f"Missing GraphRAG artifact: {path}")
return path
def _require_columns(
dataframe: pd.DataFrame,
required: Iterable[str],
artifact: str,
path: Path,
) -> None:
missing = sorted(set(required).difference(dataframe.columns))
if missing:
raise ValueError(f"{path} ({artifact}) is missing columns: {missing}")
def read_graph_artifacts(
input_dir: str | Path,
artifacts: Iterable[str] = ARTIFACT_FILES,
) -> dict[str, pd.DataFrame]:
"""Load and validate new-format GraphRAG parquet artifacts."""
requested = tuple(dict.fromkeys(artifacts))
paths = {name: artifact_path(input_dir, name) for name in requested}
frames = {name: pd.read_parquet(path) for name, path in paths.items()}
for name, dataframe in frames.items():
_require_columns(dataframe, REQUIRED_COLUMNS[name], name, paths[name])
return frames