-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.py
62 lines (46 loc) · 1.94 KB
/
example.py
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
import sys
from pathlib import Path
from lark import Lark
from transformers import AutoModelForCausalLM, AutoTokenizer
sys.path.append(str(Path(__file__).resolve().parent.parent))
from parserllm import complete_cf # noqa: E402
model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-3b")
tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-3b")
# model = AutoModelForCausalLM.from_pretrained("distilgpt2", trust_remote_code=True)
# tokenizer = AutoTokenizer.from_pretrained("distilgpt2", trust_remote_code=True)
json_grammar = r"""
?start: value
?value: object
| array
| string
| "true" -> true
| "false" -> false
| "null" -> null
array : "[" [value ("," value)*] "]"
object : "{" [pair ("," pair)*] "}"
pair : string ":" value
string : ESCAPED_STRING
%import common.ESCAPED_STRING
%import common.SIGNED_NUMBER
%import common.WS
%ignore WS
"""
### Create the JSON parser with Lark, using the LALR algorithm
json_parser = Lark(json_grammar, parser='lalr',
# Using the basic lexer isn't required, and isn't usually recommended.
# But, it's good enough for JSON, and it's slightly faster.
lexer='basic',
# Disabling propagate_positions and placeholders slightly improves speed
propagate_positions=False,
maybe_placeholders=False,
regex=True)
prompt = "Write the first three letters of the alphabet in valid JSON format\n"
print(complete_cf(prompt, json_parser, "",
tokenizer,
model,
max_new_tokens=15,
debug=True))
print("regular\n", ' '.join(tokenizer.batch_decode(model.generate(tokenizer.encode(prompt, return_tensors="pt"),
max_new_tokens=30,
pad_token_id=tokenizer.eos_token_id,
))))