Models-Phi-8B/scripts/json_stopping.py

73 lines
1.7 KiB
Python

from __future__ import annotations
import torch
from transformers import StoppingCriteria
def contains_complete_json_object(text: str) -> bool:
started = False
depth = 0
in_string = False
escaped = False
for character in text:
if not started:
if character == "{":
started = True
depth = 1
continue
if in_string:
if escaped:
escaped = False
elif character == "\\":
escaped = True
elif character == '"':
in_string = False
continue
if character == '"':
in_string = True
elif character == "{":
depth += 1
elif character == "}":
depth -= 1
if depth == 0:
return True
return False
class JsonObjectStoppingCriteria(StoppingCriteria):
def __init__(self, tokenizer, prompt_length: int) -> None:
self.tokenizer = tokenizer
self.prompt_length = prompt_length
def __call__(
self,
input_ids: torch.LongTensor,
scores: torch.FloatTensor | None,
**kwargs,
) -> torch.BoolTensor:
completed = []
for row in input_ids:
generated_text = self.tokenizer.decode(
row[self.prompt_length:],
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
completed.append(
contains_complete_json_object(generated_text)
)
return torch.tensor(
completed,
dtype=torch.bool,
device=input_ids.device,
)