17
17
18
18
import llama_cpp
19
19
from common import GptParams , gpt_params_parse , gpt_random_prompt
20
-
21
- ANSI_COLOR_RESET = "\x1b [0m"
22
- ANSI_COLOR_YELLOW = "\x1b [33m"
23
- ANSI_BOLD = "\x1b [1m"
24
- ANSI_COLOR_GREEN = "\x1b [32m"
25
-
26
- CONSOLE_COLOR_DEFAULT = ANSI_COLOR_RESET
27
- CONSOLE_COLOR_PROMPT = ANSI_COLOR_YELLOW
28
- CONSOLE_COLOR_USER_INPUT = ANSI_BOLD + ANSI_COLOR_GREEN
29
-
30
- # Iterative search
31
- # Actively searches and prevents a pattern from being returned
32
- class IterSearch :
33
- def __init__ (self , pattern ):
34
- self .pattern = list (pattern )
35
- self .buffer = []
36
-
37
- def __call__ (self , char ):
38
- self .buffer += [char ]
39
-
40
- if (self .pattern [:len (self .buffer )] == self .buffer ):
41
- if (len (self .buffer ) >= len (self .pattern )):
42
- self .buffer .clear ()
43
- return []
44
-
45
- _tmp = self .buffer [:]
46
- self .buffer .clear ()
47
- return _tmp
20
+ import util
48
21
49
22
# A LLaMA interactive session
50
23
class LLaMAInteract :
@@ -82,6 +55,7 @@ def __init__(self, params: GptParams) -> None:
82
55
self .first_antiprompt = []
83
56
self .remaining_tokens = self .params .n_predict
84
57
self .output_echo = self .params .input_echo
58
+ self .multibyte_fix = []
85
59
86
60
# model load
87
61
self .lparams = llama_cpp .llama_context_default_params ()
@@ -188,7 +162,7 @@ def __init__(self, params: GptParams) -> None:
188
162
self .params .interactive_start = True
189
163
_ptn = self ._tokenize (self .params .instruct_inp_prefix .strip (), False )
190
164
self .first_antiprompt .append (_ptn )
191
- self .antiecho = IterSearch (_ptn )
165
+ self .antiecho = util . IterSearch (_ptn )
192
166
193
167
# enable interactive mode if reverse prompt or interactive start is specified
194
168
if (len (self .params .antiprompt ) != 0 or self .params .interactive_start ):
@@ -256,14 +230,14 @@ def __init__(self, params: GptParams) -> None:
256
230
- If you want to submit another line, end your input in '\\ '.
257
231
258
232
""" , file = sys .stderr )
259
- self .set_color (CONSOLE_COLOR_PROMPT )
233
+ self .set_color (util . CONSOLE_COLOR_PROMPT )
260
234
261
235
self .need_to_save_session = len (self .params .path_session ) > 0 and n_matching_session_tokens < (len (self .embd_inp ) * 3 / 4 )
262
236
263
237
264
238
# tokenize a prompt
265
239
def _tokenize (self , prompt , bos = True ):
266
- _arr = (llama_cpp .llama_token * (len (prompt ) + 1 ))()
240
+ _arr = (llama_cpp .llama_token * (( len (prompt ) + 1 ) * 4 ))()
267
241
_n = llama_cpp .llama_tokenize (self .ctx , prompt .encode ("utf8" , errors = "ignore" ), _arr , len (_arr ), bos )
268
242
return _arr [:_n ]
269
243
@@ -295,7 +269,6 @@ def generate(self):
295
269
self .params .path_session = ""
296
270
297
271
# try to reuse a matching prefix from the loaded session instead of re-eval (via n_past)
298
- # REVIEW
299
272
if self .n_session_consumed < len (self .session_tokens ):
300
273
for i in range (len (self .embd )):
301
274
if self .embd [i ] != self .session_tokens [self .n_session_consumed ]:
@@ -445,7 +418,7 @@ def generate(self):
445
418
446
419
# reset color to default if we there is no pending user input
447
420
if (self .params .input_echo and len (self .embd_inp ) == self .input_consumed ):
448
- self .set_color (CONSOLE_COLOR_DEFAULT )
421
+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
449
422
450
423
if (self .params .interactive and len (self .embd_inp ) <= self .input_consumed ):
451
424
# if antiprompt is present, stop
@@ -486,12 +459,12 @@ def __exit__(self, type, value, tb):
486
459
487
460
def exit (self ):
488
461
llama_cpp .llama_free (self .ctx )
489
- self .set_color (CONSOLE_COLOR_DEFAULT )
462
+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
490
463
491
464
# return past text
492
465
def past (self ):
493
466
for id in self .last_n_tokens [- self .n_past :]:
494
- yield llama_cpp .llama_token_to_str (self .ctx , id ).decode ("utf-8 " , errors = "ignore" )
467
+ yield llama_cpp .llama_token_to_str (self .ctx , id ).decode ("utf8 " , errors = "ignore" )
495
468
496
469
# write input
497
470
def input (self , prompt : str ):
@@ -505,7 +478,29 @@ def input(self, prompt: str):
505
478
def output (self ):
506
479
self .remaining_tokens = self .params .n_predict
507
480
for id in self .generate ():
508
- yield llama_cpp .llama_token_to_str (self .ctx , id ).decode ("utf-8" )
481
+ cur_char = llama_cpp .llama_token_to_str (self .ctx , id )
482
+
483
+ # Add remainder of missing bytes
484
+ if None in self .multibyte_fix :
485
+ self .multibyte_fix [self .multibyte_fix .index (None )] = cur_char
486
+
487
+ # Return completed utf char
488
+ if len (self .multibyte_fix ) > 0 and not None in self .multibyte_fix :
489
+ yield (b"" .join (self .multibyte_fix )).decode ("utf8" )
490
+ self .multibyte_fix = []
491
+ continue
492
+
493
+ # Contains multi-byte UTF8
494
+ for num , pattern in [(2 , 192 ), (3 , 224 ), (4 , 240 )]:
495
+ # Bitwise AND check
496
+ if pattern & int .from_bytes (cur_char ) == pattern :
497
+ self .multibyte_fix = [cur_char ] + ([None ] * (num - 1 ))
498
+
499
+ # Stop incomplete bytes from passing
500
+ if len (self .multibyte_fix ) > 0 :
501
+ continue
502
+
503
+ yield cur_char .decode ("utf8" )
509
504
510
505
# read user input
511
506
def read_input (self ):
@@ -521,21 +516,21 @@ def interact(self):
521
516
self .params .input_echo = False
522
517
523
518
while self .params .interactive :
524
- self .set_color (CONSOLE_COLOR_USER_INPUT )
519
+ self .set_color (util . CONSOLE_COLOR_USER_INPUT )
525
520
if (self .params .instruct ):
526
521
print ('\n > ' , end = "" )
527
522
self .input (self .read_input ())
528
523
else :
529
524
print (self .params .input_prefix , end = "" )
530
525
self .input (f"{ self .params .input_prefix } { self .read_input ()} { self .params .input_suffix } " )
531
526
print (self .params .input_suffix ,end = "" )
532
- self .set_color (CONSOLE_COLOR_DEFAULT )
527
+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
533
528
534
529
try :
535
530
for i in self .output ():
536
531
print (i ,end = "" ,flush = True )
537
532
except KeyboardInterrupt :
538
- self .set_color (CONSOLE_COLOR_DEFAULT )
533
+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
539
534
if not self .params .instruct :
540
535
print (self .params .fix_prefix ,end = "" )
541
536
self .input (self .params .fix_prefix )
0 commit comments