Skip to content

Add Annotation to Stored Triples #10410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: latest-txt2kg
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
581c864
Add support for storing model name alongside triples
nv-rliu Aug 11, 2025
b3353f6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2025
2dfde6c
Add messaging
nv-rliu Aug 11, 2025
df3e8b1
Merge branch 'add-annotation-to-triples' of https://github.com/nv-rli…
nv-rliu Aug 11, 2025
850b41f
update
nv-rliu Aug 11, 2025
b3a93d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2025
a5e3d37
Merge branch 'latest-txt2kg' into add-annotation-to-triples
nv-rliu Aug 11, 2025
baf737e
Fix style
nv-rliu Aug 11, 2025
796fa3f
Merge branch 'add-annotation-to-triples' of https://github.com/nv-rli…
nv-rliu Aug 11, 2025
25a5e1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2025
b64f17e
Add support for storing model and timestamp in filename
nv-rliu Aug 12, 2025
6ca48d3
Merge branch 'add-annotation-to-triples' of https://github.com/nv-rli…
nv-rliu Aug 12, 2025
ad2644f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2025
4270ab8
Revert
nv-rliu Aug 12, 2025
1c3369e
Revert
nv-rliu Aug 12, 2025
a7abdd4
Merge branch 'add-annotation-to-triples' of https://github.com/nv-rli…
nv-rliu Aug 12, 2025
54b6c8c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2025
389835e
Revert README
nv-rliu Aug 12, 2025
3f72d19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 12, 2025
b132bd7
Merge branch 'latest-txt2kg' into add-annotation-to-triples
puririshi98 Aug 12, 2025
b430610
Merge branch 'latest-txt2kg' into add-annotation-to-triples
puririshi98 Aug 12, 2025
434827e
Merge branch 'latest-txt2kg' into add-annotation-to-triples
puririshi98 Aug 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions examples/llm/txt2kg_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,15 @@ def index_kg(args, context_docs):
checkpoint_path = os.path.join(args.dataset, "checkpoint_kg.pt")
if os.path.exists(checkpoint_path):
print("Restoring KG from checkpoint...")
saved_relevant_triples = torch.load(checkpoint_path,
weights_only=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove weights_only=False?

i think you should leave that arg

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought weights_only was only useful if we want to extract model weights and biases, aka, tensors. Otherwise, isn't the argument not really useful in this situation? I can add it back if that's incorrect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK thats the case when you set True, here we want to load items that are not Tensors (W&B), so we need to set it to False, otherwise pytorch might complaint

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay. I thought it was okay to not use it since this Data object is never storing weights and biases. I'll add that back in.

checkpoint_data = torch.load(checkpoint_path)

# check to see if triples generation are using the correct model
if args.NV_NIM_MODEL.split('/')[-1] != checkpoint_data['model']:
raise RuntimeError(
"Error: The stored triples were generated using a different model"
)

saved_relevant_triples = checkpoint_data['triples']
kg_maker.relevant_triples = saved_relevant_triples
kg_maker.doc_id_counter = len(saved_relevant_triples)
initial_tqdm_count = kg_maker.doc_id_counter
Expand All @@ -346,7 +353,10 @@ def index_kg(args, context_docs):
for triple_set in relevant_triples.values()))
triples = list(dict.fromkeys(triples))
raw_triples_path = os.path.join(args.dataset, "raw_triples.pt")
torch.save(triples, raw_triples_path)
torch.save({
"model": kg_maker.get_model_name(),
"triples": triples,
}, raw_triples_path)
if os.path.exists(checkpoint_path):
os.remove(checkpoint_path)
return triples
Expand Down Expand Up @@ -409,12 +419,18 @@ def make_dataset(args):
triples = []
raw_triples_path = os.path.join(args.dataset, "raw_triples.pt")
if os.path.exists(raw_triples_path):
triples = torch.load(raw_triples_path, weights_only=False)
saved_data = torch.load(raw_triples_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz save/load w format {llm-name}{datetime}raw_triples.pt

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed format

if args.NV_NIM_MODEL.split('/')[-1] != saved_data['model']:
raise RuntimeError(
"Error: The stored triples were generated using a different model"
)

print(f" -> Saved triples generated with: {saved_data['model']}")
triples = saved_data['triples']
else:
triples = index_kg(args, context_docs)

print("Number of triples in our GraphDB =", len(triples))

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# creating the embedding model
Expand Down Expand Up @@ -746,7 +762,6 @@ def eval(question: str, pred: str, correct_answer: str):
data_lists = update_data_lists(args, data_lists)
else:
data_lists = make_dataset(args)

batch_size = args.batch_size
eval_batch_size = args.eval_batch_size
train_loader = DataLoader(data_lists["train"], batch_size=batch_size,
Expand Down
11 changes: 10 additions & 1 deletion torch_geometric/nn/nlp/txt2kg.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def save_kg(self, path: str) -> None:
Returns:
None
"""
torch.save(self.relevant_triples, path)
torch.save(
{
"model": self.get_model_name(),
"triples": self.relevant_triples
}, path)

def _chunk_to_triples_str_local(self, txt: str) -> str:
# call LLM on text
Expand Down Expand Up @@ -192,6 +196,11 @@ def add_doc_2_KG(
# Increment the doc_id_counter for the next document
self.doc_id_counter += 1

def get_model_name(self) -> str:
"""Returns the name of model used to generate triples
"""
return self.NIM_MODEL.split('/')[-1] if not self.local_LM else "local"


def _chunk_to_triples_str_cloud(
txt: str, GLOBAL_NIM_KEY='',
Expand Down