-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriting_aid_example.py
More file actions
171 lines (141 loc) · 5.75 KB
/
writing_aid_example.py
File metadata and controls
171 lines (141 loc) · 5.75 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import cmd
from contextlib import contextmanager
from copy import copy
from types import SimpleNamespace
from llama import Model, Context, Mirostatv2Sampler
from bindings import load_libllama, lib
from llama.context import Sequence
load_libllama("../llama.cpp")
class Story:
def __init__(self):
self.model = Model("models/nous-hermes-llama2-13b.Q5_K_M.gguf", n_gpu_layers=1000)
self.context = Context(self.model, context_size=10000, rope_scaling_type=lib.LLAMA_ROPE_SCALING_YARN, rope_freq_scale=1.)
self.sampler = Mirostatv2Sampler(self.context, target_entropy=2.)
self.sequence = Sequence(self.context)
self.last_logits = self.sequence.insert(self.model.token_bos)
self.clear()
def insert_text(self, text: str):
self.last_logits = self.sequence.insert(text)
self.context.process()
def remove_last_paragraph(self):
toks = self.sequence.tokens
if toks[-1] == self.model.token_newline:
toks.pop()
if self.model.token_newline in toks:
amount_to_truncate = 1 + list(reversed(toks)).index(self.model.token_newline)
self.sequence.truncate_end(amount_to_truncate)
self.insert_text("\n")
else:
self.clear()
def generate_paragraphs(self, amount: int, max_len=512) -> list[str]:
"""
Generates a paragraph (excluding the newline).
:param amount: The amount of paragraphs to generate
:param max_len: The maximum length of the paragraph in tokens.
"""
batch_n = self.context.max_batch_size
if amount > batch_n:
return self.generate_paragraphs(batch_n) + self.generate_paragraphs(amount - batch_n)
states = [SimpleNamespace(seq=copy(self.sequence),
logits=copy(self.last_logits),
tokens=[],
sampler=copy(self.sampler))
for _ in range(amount)]
for _ in range(max_len):
for state in states:
if not state.tokens or state.tokens[-1] != "\n":
new_tok = state.sampler.sample(state.logits)
if new_tok in (self.model.token_eos, self.model.token_newline):
state.tokens.append("\n")
else:
state.tokens.append(self.model.detokenize(new_tok))
state.logits = state.seq.insert(new_tok)
self.context.process()
for s in states:
s.seq.clear()
return ["".join(s.tokens).strip() for s in states]
def clear(self):
self.sequence.clear()
# insert beginning-of-sequence token
self.last_logits = self.sequence.insert(self.model.token_bos)
self.context.process()
@contextmanager
def rollback(self):
"""For making hypothetical changes and easily rolling them back."""
initial_length = len(self.sequence)
initial_logits = self.last_logits
try:
yield None
finally:
self.last_logits = initial_logits
self.sequence.truncate_end(len(self.sequence) - initial_length)
story = Story()
def parse_num(arg: str) -> int | None:
try:
return int(arg)
except ValueError:
print(f"Expected a number, got {arg}")
class WriterShell(cmd.Cmd):
intro = "Welcome to adventure writer! Type help or ? to list commands.\n"
prompt = "> "
def __init__(self):
super().__init__()
self.current_options = []
def do_clear(self, arg=""):
story.clear()
self.current_options.clear()
def do_write(self, arg):
'write [text]: insert a paragraph of text into the story'
arg = arg.strip()
story.insert_text(arg + "\n")
print(f"{len(story.sequence)}/{story.context.context_size()}")
self.current_options.clear()
def do_save(self, arg):
with open(arg, 'w') as file:
file.write(str(story.sequence))
def do_load(self, arg):
self.do_clear()
try:
with open(arg, 'r') as file:
for line in file.readlines():
print(line)
self.do_write(line.strip())
except FileNotFoundError:
print(f"File not found: {arg}")
def do_par(self, arg, prefix=""):
'par [num]: generate num possible paragraphs'
num = parse_num(arg)
if not num:
return
for option in story.generate_paragraphs(num):
print(f"{len(self.current_options) + 1}. {prefix}{option}\n")
self.current_options.append(option)
def do_extend(self, arg: str):
'extend [num] text: extend given text, generating num possible paragraphs.'
num, text = arg.split(maxsplit=1)
text = text.strip() + " "
with story.rollback():
story.insert_text(text)
self.do_par(num, prefix=text)
def do_pick(self, arg):
'pick [n]: pick the n-th option and write it into the story'
try:
self.do_write(self.current_options[int(arg) - 1])
except IndexError:
print("No such option.")
def do_undo(self, arg):
'removes last paragraph'
self.current_options.clear()
story.remove_last_paragraph()
print("removed last paragraph")
def do_instruct(self, arg: str):
'instruct [n] command: write n paragraphs following the command.'
num, instruction = arg.split(maxsplit=1)
with story.rollback():
story.insert_text(f"### Instruction:\n{instruction.strip()}\n")
self.do_par(num)
def do_print(self, arg):
print(str(story.sequence).replace("\n", "\n\n"))
def do_exit(self, arg):
exit()
WriterShell().cmdloop()