Skip to content

Commit 6826bf6

Browse files
committed
add json fallback to tgtalker
1 parent c6640e4 commit 6826bf6

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

examples/llm/TGTalker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"""
1818

1919
import argparse
20-
import json
2120
import logging
2221

2322
import numpy as np
@@ -31,6 +30,7 @@
3130
make_system_prompt,
3231
make_user_prompt,
3332
predict_link,
33+
extract_destination_node,
3434
)
3535
from tqdm import tqdm
3636
from transformers import AutoModelForCausalLM, AutoTokenizer
@@ -207,7 +207,7 @@ def main() -> None:
207207

208208
try:
209209
output = model(prompt, schema)
210-
pred_dst = int(json.loads(output)['destination_node'])
210+
pred_dst = extract_destination_node(output)
211211
# Candidates: true destination followed by TGB negatives.
212212
query_dst = torch.cat(
213213
[batch.edge_dst[i].unsqueeze(0), batch.neg_batch_list[i]]

examples/llm/multihop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212

1313
import argparse
14-
import json
1514
import logging
1615

1716
import numpy as np
@@ -24,6 +23,7 @@
2423
make_multihop_user_prompt,
2524
make_system_prompt,
2625
predict_link,
26+
extract_destination_node,
2727
)
2828
from tqdm import tqdm
2929
from transformers import AutoModelForCausalLM, AutoTokenizer
@@ -148,7 +148,7 @@ def main() -> None:
148148

149149
try:
150150
output = model(prompt, schema)
151-
pred_dst = int(json.loads(output)['destination_node'])
151+
pred_dst = extract_destination_node(output)
152152
query_dst = torch.cat(
153153
[batch.edge_dst[i].unsqueeze(0), batch.neg_batch_list[i]]
154154
)

examples/llm/tgtalker_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from __future__ import annotations
1111

1212
import collections
13+
import json
14+
import re
1315
from typing import Iterable, List, Sequence, Tuple
1416

1517
import numpy as np
@@ -222,6 +224,38 @@ def predict_link(query_dst: torch.Tensor, llm_dst: int) -> torch.Tensor:
222224
return (query_dst == llm_dst).float()
223225

224226

227+
def extract_destination_node(output: object) -> int:
228+
"""Extract ``destination_node`` from model output.
229+
230+
Tries default strict JSON parse, with a minimal
231+
fallback for slightly malformed text that still contains the key/value.
232+
"""
233+
if output is None:
234+
raise ValueError('Model output is None')
235+
236+
text = output if isinstance(output, str) else str(output)
237+
text = text.strip()
238+
if not text:
239+
raise ValueError('Model output is empty')
240+
241+
try:
242+
return int(json.loads(text)['destination_node'])
243+
except Exception:
244+
pass
245+
246+
if isinstance(output, dict) and 'destination_node' in output:
247+
return int(output['destination_node'])
248+
if hasattr(output, 'destination_node'):
249+
return int(getattr(output, 'destination_node'))
250+
251+
# Fallback for partially malformed JSON/text containing the key-value pair.
252+
m = re.search(r'"destination_node"\s*:\s*(-?\d+)', text)
253+
if m:
254+
return int(m.group(1))
255+
256+
raise ValueError(f'Could not extract destination_node from output: {text[:160]}')
257+
258+
225259
class BackgroundBuffer:
226260
"""Sliding window of the most recent global ``(src, dst, ts)`` edges.
227261

0 commit comments

Comments
 (0)