-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaigob.py
1160 lines (972 loc) · 32.7 KB
/
aigob.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import io
import json
import os
from pathlib import Path
import string
import subprocess
import random
import re
import readline
import sys
import time
import requests
def tolog(txt):
with open("aiclient_debug.log","a") as f:
f.write(txt)
def warn(txt):
print("Warning! "+txt, file=sys.stderr)
def error(txt):
print("Error! "+txt, file=sys.stderr)
def safeinput(prompt):
start = time.time()
while True:
message = input(prompt)
if time.time()-start > 0.1:
return message
def random_string(length, charset=None):
if charset == None:
charset = string.ascii_uppercase+string.digits
return "".join(random.choices(charset, k=length))
def eval_template(template, context):
return re.sub(r'\{\{(.*?)\}\}',
lambda m: str( eval(m[1], context) ), template)
def find_diff(old, new):
min_len = min(len(old), len(new))
for i in range(min_len):
if old[i] != new[i]:
return i
if len(old) != len(new):
return min_len
return -1
def count_newlines(text):
i = len(text)
while text[i-1] == "\n":
i -= 1
return len(text)-i
def split_to_paragraphs(text):
"""Generator, split text maintaining its length."""
pos = 0
while True:
end = text.find("\n\n", pos)
if end < 0:
yield text[pos:]
break
cr_pos = end+2
while text[cr_pos:cr_pos+1] == "\n":
cr_pos += 1
extra = cr_pos-end-2
yield text[pos:end]+" "*extra
pos = cr_pos
def reformat_lines(text, width):
"""Reformat text lines to width. Keep text length."""
text = text.replace("\n", " ")
# text = re.sub("\s{2,}", " ", text)
result = []
while True:
if len(text) <= width:
result.append(text)
break
pos = width+1
pos2 = text.rfind(" ", 0, pos)
if pos2 < 0:
pos2 = text.find(" ", pos)
if pos2 < 0:
result.append(text)
break
pos = pos2
result.append(text[:pos])
text = text[pos+1:]
return "\n".join(result)
def reformat(text, width, keep_nl=True):
parts = split_to_paragraphs(text)
newtext = "\n\n".join(reformat_lines(para, width) for para in parts)
# needed for \n before input() and for del_line
# ugly, to be reworked:
if keep_nl and text.endswith("\n") and not newtext.endswith("\n"):
newtext = newtext[:-1] + "\n"
return newtext
def wrap_text(text, width):
parts = text.split(sep="\n")
newtext = "\n".join(reformat_lines(line, width) for line in parts)
return newtext
################
################ Settings
engine_settings = {
# "stop_sequence": ["You:", "\nYou ", "\n\n"],
"use_default_badwordsids": False,
"genkey": "0I3IVC7D",
"max_context_length": 4096,
"max_length": 16,
#"sampler_seed": 69420, #set the seed
# "temperature": 0.7,
"temperature": 0.8,
"mirostat": 2,
"mirostat_tau": 5.0,
"mirostat_eta": 0.1,
"sampler_order": [6, 0, 1, 3, 4, 2, 5],
"rep_pen": 1.1,
"rep_pen_range": 320,
"rep_pen_slope": 0.7,
"tfs": 1,
"top_a": 0,
"top_k": 100,
"top_p": 0.92,
"typical": 1,
"min_p": 0,
"frmttriminc": False,
"frmtrmblln": False,
}
conf_presets = dict(
strict = dict(
engine = {
"temperature": 0.7,
"mirostat": 0,
},
),
creative = dict(
engine = {
"temperature": 0.8,
"mirostat": 2,
"mirostat_tau": 5.0,
"mirostat_eta": 0.1,
},
),
banrepeat = dict(
engine = {
"rep_pen": 1.15,
"rep_pen_range": 2000,
"rep_pen_slope": 0.0,
},
),
canrepeat = dict(
engine = {
"rep_pen": 1.1,
"rep_pen_range": 320,
"rep_pen_slope": 0.7,
},
),
story = dict(
textmode = "story",
gen_until_end = True,
stop_sequence = "",
engine = {
"max_length": 8,
},
),
chat = dict(
textmode = "chat",
gen_until_end = False,
stop_sequence = "\n{{user}}:||\n{{user}}",
engine = {
"max_length": 32,
},
),
)
def deep_update(storage, data):
for name,value in data.items():
if name not in storage:
raise KeyError(f"Wrong key {name} = {value} in options.")
old = storage[name]
if hasattr(old, "items"):
deep_update(old, value)
else:
storage[name] = value
def deep_diff(storage, data, prefix="", changed=None):
if changed is None:
changed = []
for name,value in data.items():
loop_prefix = prefix+"."+name if prefix else name
if name not in storage:
changed.append(loop_prefix)
else:
old = storage[name]
if hasattr(old, "items"):
deep_diff(old, value, loop_prefix, changed)
elif old != value:
changed.append(loop_prefix)
return changed
class Settings:
override = "aigob.conf"
search_in = ["~/.config/aigob.conf"]
default = dict(
save_on_exit = True,
chardir = "chars",
logdir = "log",
endpoint = "http://127.0.0.1:5001",
username = "You",
textmode = "chat",
format = "wrap",
wrap_at = 72,
editor = "nano",
gen_until_end = True,
lastchar = "",
stop_sequence = "{{user}}:||\n{{user}} ",
engine = engine_settings,
presets = conf_presets,
active_presets = "strict,canrepeat",
)
def __init__(self):
self.data = dict(self.default)
self.generate_key()
def updated(self):
pass
def set(self, var, value):
self.data[var] = value
self.updated()
def __getattr__(self, var):
return self.data[var]
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
self.updated()
def update(self, new):
deep_update(self.data, new)
def generate_key(self):
self.data["engine"]["genkey"] = random_string(8)
def dump(self, path=""):
branch = self.getpath(path)
lines = []
if hasattr(branch, "__getitem__"):
for k,v in self.getpath(path).items():
lines.append(f"{k}={v}")
else:
lines.append(f"{path}={branch}")
return "\n".join(lines)
def getpath(self, name):
"""For name="name1.name2..." get data[name1][name2]..."""
store = self.data
try:
if name:
for part in name.split("."):
store = store[part]
return store
except (KeyError,TypeError):
print(f"Variable {name} not exists.")
def setpath(self, name, value, convert=True):
"""For name="name1.name2..." set data[name1][name2]... to value"""
store = self.data
try:
*path,last = name.split(".")
if name:
for var in path:
store = store[var]
if convert:
store[last] = type(store[last])(value)
else:
store[last] = value
self.updated()
except (KeyError,TypeError):
print(f"Variable {name} not exists.")
except ValueError:
print(f"Wrong value for variable {name}.")
def use_presets(self, names):
presets = self.data["presets"]
used = []
for name in names.split(","):
if name in presets:
self.update(presets[name])
used.append(name)
else:
print(f"Preset [$name] not found.")
self.data["active_presets"] = ",".join(used)
def presets_status(self):
ans = []
presets = self.data["presets"]
for name in self.data["active_presets"].split(","):
if name in presets:
diff = deep_diff(self.data, presets[name])
if diff:
ans.append("\n *".join((name, *diff)))
else:
ans.append(name)
else:
ans.append(name+" - missing preset")
return "\n".join(ans)
def save(self):
with open(Path(self.conffile).expanduser(), "w") as f:
json.dump(self.data, f, indent=4)
def find(self):
if Path(self.override).is_file():
return self.override
for name in self.search_in:
if Path(name).expanduser().is_file():
return name
return self.search_in[0]
def load(self, path=None):
if path is None:
path = self.find()
self.conffile = path
parsed_path = Path(path).expanduser()
if parsed_path.is_file():
with open(parsed_path, "r") as f:
loaded = json.load(f)
self.update(loaded)
else:
warn(f"Configuration file not exist: {path}")
################
################ Characters
class Character:
dupkeys = (
("name", "char_name"),
("description", "char_persona"),
("scenario", "world_scenario"),
("example_dialogue", "mes_example"),
("char_greeting", "first_mes"),
)
def __init__(self, data):
self.data = data
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
def memory(self):
parts = []
for variable,template in (
('system_prompt', "{}"),
('description', "Persona:\n{}"),
('scenario', "[Scenario: {}]"),
('post_history_instructions', "{}"),
('example_dialogue', "{}"),
('patches', "{}"),
):
text = self.data.get(variable, "")
if text:
parts.append(template.format(text))
parts.append("***\n")
return "\n".join(parts)
def strip(self):
for key1,key2 in self.dupkeys:
if self.data.get(key1, None) is None:
self[key1] = self.data.get(key2, "")
self.data.pop(key2, None)
self[key1] = self[key1].strip()
@classmethod
def load(cls, name, dir=""):
if name in ("", "assistant"):
return assistant
names = (name, name+".pch", name+".json")
for testname in names:
path = Path(dir, testname)
if path.is_file():
with path.open() as f:
if testname.endswith(".pch"):
try:
data = eval(f.read(), {"__builtins__": {"dict": dict}})
except Exception as e:
raise ValueError(f'Wrong format in file "{path}"') from e
else:
data = json.load(f)
char = cls(data)
char.strip()
return char
raise FileNotFoundError(f'Unable to load char "{name}"')
def to_json(self, file, dir=""):
save = list(self.data)
for key1,key2 in self.dupkeys:
save[key2] = save[key1]
file = Path(dir, file)
if file.suffix.lower() != ".json":
file.with_suffix(file.suffix + ".json")
with open(file, "w") as f:
json.dump(save, f, indent=3)
def to_pch(self, file, dir=""):
longkeys = (
"description",
"scenario",
"example_dialogue",
"char_greeting",
)
parts = ["dict(\n"]
for k,v in self.data.items():
if isinstance(v, str):
if v == "" or k not in longkeys:
parts.append(f'{k} = """{v}""",\n')
else:
parts.append(f'{k} = """\n\n{v}\n\n""",\n')
else:
parts.append(f'{k} = {v},\n')
parts.append(")\n")
text = "\n".join(parts)
file = Path(dir, file)
if file.suffix.lower() != ".pch":
file.with_suffix(file.suffix + ".pch")
with open(file, "w") as f:
f.write(text)
assistant=Character(dict(
name="Assistant",
description="",
example_dialogue="",
scenario="",
char_greeting="How can I help?",
))
################
################ chat commands
chat_commands = dict()
def chat_cmd(f):
chat_commands[f.__name__[4:]] = f
def chat_cmd_alias(name):
last = list( chat_commands.keys() )[-1]
chat_commands[name] = chat_commands[last]
def chat_cmd_get(name):
return chat_commands.get(name, None)
def chat_cmd_help():
parts = []
for name,f in chat_commands.items():
parts.append(f"/{name}{f.__doc__[3:]}\n")
return "".join(parts)
################
################ history
cutoff_digits = 8
def get_cutoff(text):
digits = text[:cutoff_digits]
if len(text) < cutoff_digits or not digits.isdecimal():
return text, None
cutoff = int(digits)
text = text[cutoff_digits:]
return text, cutoff
def store_cutoff(file, cutoff):
file.seek(0)
file.write(f"{cutoff:0{cutoff_digits}}")
def load_history(file_name):
path = Path(file_name).expanduser()
if not path.is_file():
path.parent.mkdir(parents=True, exist_ok=True)
open(path, "w").close()
with open(path, "r+", errors="replace") as file:
text, cutoff = get_cutoff(file.read())
if cutoff is None:
cutoff = 0
# add cutoff to history file
store_cutoff(file, cutoff)
file.write(text)
return text, cutoff
def update_history(file_name, text, cutoff):
path = Path(file_name).expanduser()
if not path.is_file():
path.parent.mkdir(parents=True, exist_ok=True)
open(path, "w").close()
with open(path, "r+", errors="replace") as f:
history, old_cutoff = get_cutoff( f.read() )
if old_cutoff is None:
store_cutoff(f, 0)
f.write(text)
f.truncate()
return
if cutoff != old_cutoff:
store_cutoff(f, cutoff)
pos = find_diff(history, text)
if pos >= 0:
filepos = cutoff_digits + len( text[:pos].encode('utf-8') )
# todo: not working for os.linesep > 1
f.seek(filepos)
f.write(text[pos:])
f.truncate()
################
################ llm api
class Engine:
def __init__(self, conf):
self.conf = conf
def stop(self):
requests.post(f"{self.conf.endpoint}/api/extra/abort")
def status(self):
""" Get status of KoboldCpp
Result: last_process, last_eval, last_token_count, total_gens, queue, idle,
stop_reason (INVALID=-1, OUT_OF_TOKENS=0, EOS_TOKEN=1, CUSTOM_STOPPER=2)
"""
response = requests.get(f"{self.conf.endpoint}/api/extra/perf")
if response.status_code != 200:
raise IOError("Can not get status from engine")
return response.json()
def idle(self):
return self.status()['idle']
def stop_reason(self):
return self.status()['stop_reason']
def get_max_context(self):
response = requests.get(f"{self.conf.endpoint}/api/extra/true_max_context_length")
if response.status_code != 200:
return None
return response.json()['value']
def count_tokens(self, text):
response = requests.post(f"{self.conf.endpoint}/api/extra/tokencount",
json={"prompt": text})
if response.status_code != 200:
raise IOError("Can not get get token count from engine")
return response.json()['value']
def next_token(self, text, pos):
if pos == 0:
n = 0
else:
n = engine.count_tokens(text[:pos])
while n == engine.count_tokens(text[:pos+1]):
pos += 1
return pos
def get_stream(self, request, session=None):
if session is None:
session = requests
response = session.post(f"{self.conf.endpoint}/api/extra/generate/stream",
json=request, stream=True)
if response.status_code != 200:
raise IOError("Can not get response stream from engine")
if response.encoding is None:
response.encoding = 'utf-8'
return response.iter_lines(chunk_size=20, decode_unicode=True)
def set_memory(self, memory):
if getattr(self, "last_memory", None) != memory:
self.last_mempory = memory
self.memory_tokens = self.count_tokens(memory)
def safe_cut(self, pos):
for end in ('\n\n', '.\n', '"\n', '\n', ' '):
pos2 = self.prompt.find(end, pos, pos+200)
if pos2 >= 0:
# keep single space-like token, it prevents
# triggering context-shifting bug in koboldcpp
pos = pos2+len(end)-1
break
else:
pos = pos + self.engine.next_token(self.prompt[pos:], 0)
return pos
# for reference, up to 300 tokens shifts were observed in koboldcpp
def shift_context(self):
max_ctx = (self.max_context
- self.conf.engine["max_length"] - self.memory_tokens - 10)
pos = self.cutoff
while True:
text = self.prompt[pos:]
tokens = self.count_tokens(text)
extra_tokens = tokens-max_ctx
if extra_tokens <= 0:
break
pos += extra_tokens*len(text)//tokens
if pos <= self.cutoff:
return
self.cutoff = self.safe_cut(pos)
def prepare(self, data):
self.max_context = min(
self.conf.engine["max_context_length"],
self.get_max_context())
self.prompt = data.prompt
self.set_memory(getattr(data, "memory", ""))
self.cutoff = getattr(data, "cutoff", 0)
self.shift_context()
data.cutoff = self.cutoff
request = dict(self.conf.engine)
request.update(
stop_sequence=data.stop_sequence,
memory=data.memory,
prompt=data.prompt[self.cutoff:],
)
return request
def run(self, data):
request = self.prepare(data)
is_message = False
with requests.Session() as session:
for line in self.get_stream(request, session):
if line: # filter out keep-alive new lines
if is_message:
if line.startswith("data:"):
jresponse = json.loads(line.removeprefix("data: "))
yield jresponse['token']
is_message = False
else:
if line == "event: message":
is_message = True
################
################ ChatView
class RawChatView:
def __init__(self, conf, chat):
self.conf = conf
self.chat = chat
def refresh_screen(self, end="", chars=2000):
text = self.chat.prompt[-chars:]
print("\n"*3, text, end, sep="", end="")
def input_prompt(self):
if self.conf.textmode == "chat":
name = f"{self.conf.username} "
else:
name = ""
if self.chat.message_finished():
append = ""
else:
append = "+"
return f"{name}{append}> "
def input(self):
print()
while True:
try:
message = safeinput(self.input_prompt())
break
except KeyboardInterrupt:
input("\nEnter to continue, Ctrl+C second time to exit.")
return message
def info(self, message):
print(f"\n{'-'*32}\n{message}")
def error(self, message):
print(f"\nError: {message}")
def ai_message_part(self, token):
print(token, end="", flush=True)
def ai_message(self, message):
self.refresh_screen(end="")
def user_message(self, message, prefix=""):
pass
def update_message(self):
pass
class RefreshChatView(RawChatView):
def refresh_screen(self, end="", chars=2000):
text = self.chat.prompt[-chars:]
text = self.chat.reformat(text)
print("\n"*3, text, end, sep="", end="")
def input(self):
if self.chat.message_finished():
print("\r", " "*20, "\r", sep="", end="")
else:
print()
# try:
message = safeinput(self.input_prompt())
return message
def user_message(self, message, prefix=""):
self.refresh_screen(end="")
print(prefix, message, sep="", end="", flush=True)
def update_message(self):
self.refresh_screen()
class FormatChatView(RawChatView):
pass
################
class Conversation:
def __init__(self, char, conf, engine=None, view=None):
self.conf = conf
if engine is None:
engine = Engine(conf)
self.engine = engine
if view is None:
# view = RawChatView(conf, self)
view = RefreshChatView(conf, self)
self.view = view
self.stop_reason = 0
self.set_char(char)
def parse_vars(self, text):
context = dict(user=self.conf.username, char=self.char['name'])
return eval_template(text, context)
def reformat(self, text):
if self.conf.format == "reformat":
return reformat(text, self.conf.wrap_at)
if self.conf.format == "wrap":
return wrap_text(text, self.conf.wrap_at)
return text
def init_dialogue(self):
self.prompt, self.cutoff = load_history(self.log)
if self.prompt == "":
self.view.info("History is empty, starting new conversation.")
# first "\n" to avoid failing first context shift:
first = "\n"+self.parse_vars( self.char['char_greeting'] )+"\n\n"
self.to_prompt(first)
else:
self.view.info(f"History loaded: {self.log}")
self.view.refresh_screen(chars=8000)
def set_char(self, char):
self.char = char
self.memory = self.char.memory()
self.memory = self.parse_vars(self.memory)
self.log = f"{self.conf.logdir}/{self.char['name']}.log"
self.view.info(f"\n{'#'*32}\nStarted character: {self.char['name']}")
self.init_dialogue()
def clear_char(self):
self.prompt = ""
self.cutoff = 0
update_history(self.log, self.prompt, self.cutoff)
self.set_char(self.char)
def get_stop_sequence(self):
if self.conf.stop_sequence:
stop_parsed = self.parse_vars(self.conf.stop_sequence)
return stop_parsed.split("||")
else:
return []
def del_prompt_lines(self, count=1):
text = self.prompt[self.cutoff:]
text = self.reformat(text)
pos = len(text)
while count > 0:
count -= 1
pos = text.rfind("\n", 0, pos)
if pos < 0:
pos = 0
self.prompt = self.prompt[:self.cutoff+pos]
update_history(self.log, self.prompt, self.cutoff)
def to_prompt(self, message):
if not message:
return
self.prompt += message
update_history(self.log, self.prompt, self.cutoff)
def read_stream(self):
response = ""
self.stop_sequence = self.get_stop_sequence()
try:
for token in self.engine.run(self):
response += token
self.view.ai_message_part(token)
except KeyboardInterrupt:
# print()
#todo: maybe an option for this? May interrupt multi-user engine.
if not self.engine.idle():
self.engine.stop()
self.stop_reason = 100
else:
self.stop_reason = self.engine.stop_reason()
self.cutoff = self.engine.cutoff
return response
def to_readline(self, response):
if self.prompt and self.prompt[-1] != "\n":
pos = self.prompt.rfind("\n") # -1 is ok
text = self.prompt[pos+1:] + response
else:
text = response
for line in text.splitlines():
if line != "":
readline.add_history(line)
readline.add_history(text.replace("\n", " "))
def stream_response(self):
parts = []
while True:
response = self.read_stream()
if self.stop_reason == 2: # custom stopper == stop word
for suffix in self.stop_sequence:
if response.endswith(suffix):
response = response.removesuffix(suffix)
if suffix.startswith("\n") or suffix.endswith("\n"):
response += "\n"
break
#else: # 0=out of tokens, 1=eos token, -1=invalid, 100=KeyboardInterrupt marker
self.to_readline(response)
self.to_prompt(response)
parts.append(response)
if not( self.conf.gen_until_end and self.stop_reason == 0 ):
break
return "".join(parts)
def ai_message(self):
try:
#todo: maybe an option for this? May interrupt multi-user engine.
if not self.engine.idle():
self.engine.stop()
message = self.stream_response()
except IOError:
self.view.error("Can not receive ai response.")
else:
self.view.ai_message(message)
def user_message(self, message):
newlines = count_newlines(self.prompt)
prefix = ""
if newlines > 2:
# unsaved prompt, update_history() needed later
self.prompt = self.prompt[:2-newlines]
elif newlines < 2:
prefix = "\n"*(2-newlines)
if self.conf.textmode == "chat":
message = f"{self.conf.username}: {message}"
message = self.reformat(message)
if message.endswith("+"):
message = message[:-1]
else:
message = f"{message}\n\n"
self.view.user_message(message, prefix=prefix)
self.to_prompt(message)
def append_message(self, message):
text = self.reformat(self.prompt[self.cutoff:].rstrip())
pos = text.rfind("\n")
message = self.reformat(text[pos+1:] + message)
# unsaved prompt, update_history() needed later
self.prompt = self.prompt[:pos+1]
self.to_prompt(message)
self.view.update_message()
def help(self):
head = """
Help:
Ctrl+c -while receiving llm answer: cancel
Ctrl-z -exit
"=" -add new line
"+text" -append text to last line
"text+" -let llm to continue text
"@" -edit in external editor
/set textmode chat/story - mode of conversation
/set format none/wrap/reformat - text reformatting
/load -load Assistant character
"""
self.view.info(head + chat_cmd_help())
@chat_cmd
def cmd_help(self, params):
"""cmd -display help page."""
self.help()
chat_cmd_alias("h")
@chat_cmd
def cmd_stop(self, params):
"""cmd -command llm to stop generation."""
self.engine.stop()
@chat_cmd
def cmd_test(self, params):
"""cmd -debug."""
print(f"test")
@chat_cmd
def cmd_saveconf(self, params):
"""cmd -save configuration to file."""
self.conf.save()
@chat_cmd
def cmd_ls(self, params):
"""cmd -list available characters."""
names = []
for f in Path(self.conf.chardir).iterdir():
if f.suffix in (".json", ".pch"):
names.append(f.name)
self.view.info("\n".join(names))
@chat_cmd
def cmd_load(self, params):
"""cmd charname -load character."""
name = params.strip()
try:
char = Character.load(name, self.conf.chardir)
except (ValueError, FileNotFoundError) as e:
self.view.error(str(e))
else:
self.set_char(char)
self.conf.set("lastchar", name)