1717
1818import llama_cpp
1919from 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
4821
4922# A LLaMA interactive session
5023class LLaMAInteract :
@@ -82,6 +55,7 @@ def __init__(self, params: GptParams) -> None:
8255 self .first_antiprompt = []
8356 self .remaining_tokens = self .params .n_predict
8457 self .output_echo = self .params .input_echo
58+ self .multibyte_fix = []
8559
8660 # model load
8761 self .lparams = llama_cpp .llama_context_default_params ()
@@ -188,7 +162,7 @@ def __init__(self, params: GptParams) -> None:
188162 self .params .interactive_start = True
189163 _ptn = self ._tokenize (self .params .instruct_inp_prefix .strip (), False )
190164 self .first_antiprompt .append (_ptn )
191- self .antiecho = IterSearch (_ptn )
165+ self .antiecho = util . IterSearch (_ptn )
192166
193167 # enable interactive mode if reverse prompt or interactive start is specified
194168 if (len (self .params .antiprompt ) != 0 or self .params .interactive_start ):
@@ -256,14 +230,14 @@ def __init__(self, params: GptParams) -> None:
256230 - If you want to submit another line, end your input in '\\ '.
257231
258232""" , file = sys .stderr )
259- self .set_color (CONSOLE_COLOR_PROMPT )
233+ self .set_color (util . CONSOLE_COLOR_PROMPT )
260234
261235 self .need_to_save_session = len (self .params .path_session ) > 0 and n_matching_session_tokens < (len (self .embd_inp ) * 3 / 4 )
262236
263237
264238 # tokenize a prompt
265239 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 ))()
267241 _n = llama_cpp .llama_tokenize (self .ctx , prompt .encode ("utf8" , errors = "ignore" ), _arr , len (_arr ), bos )
268242 return _arr [:_n ]
269243
@@ -295,7 +269,6 @@ def generate(self):
295269 self .params .path_session = ""
296270
297271 # try to reuse a matching prefix from the loaded session instead of re-eval (via n_past)
298- # REVIEW
299272 if self .n_session_consumed < len (self .session_tokens ):
300273 for i in range (len (self .embd )):
301274 if self .embd [i ] != self .session_tokens [self .n_session_consumed ]:
@@ -445,7 +418,7 @@ def generate(self):
445418
446419 # reset color to default if we there is no pending user input
447420 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 )
449422
450423 if (self .params .interactive and len (self .embd_inp ) <= self .input_consumed ):
451424 # if antiprompt is present, stop
@@ -486,12 +459,12 @@ def __exit__(self, type, value, tb):
486459
487460 def exit (self ):
488461 llama_cpp .llama_free (self .ctx )
489- self .set_color (CONSOLE_COLOR_DEFAULT )
462+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
490463
491464 # return past text
492465 def past (self ):
493466 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" )
495468
496469 # write input
497470 def input (self , prompt : str ):
@@ -505,7 +478,29 @@ def input(self, prompt: str):
505478 def output (self ):
506479 self .remaining_tokens = self .params .n_predict
507480 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" )
509504
510505 # read user input
511506 def read_input (self ):
@@ -521,21 +516,21 @@ def interact(self):
521516 self .params .input_echo = False
522517
523518 while self .params .interactive :
524- self .set_color (CONSOLE_COLOR_USER_INPUT )
519+ self .set_color (util . CONSOLE_COLOR_USER_INPUT )
525520 if (self .params .instruct ):
526521 print ('\n > ' , end = "" )
527522 self .input (self .read_input ())
528523 else :
529524 print (self .params .input_prefix , end = "" )
530525 self .input (f"{ self .params .input_prefix } { self .read_input ()} { self .params .input_suffix } " )
531526 print (self .params .input_suffix ,end = "" )
532- self .set_color (CONSOLE_COLOR_DEFAULT )
527+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
533528
534529 try :
535530 for i in self .output ():
536531 print (i ,end = "" ,flush = True )
537532 except KeyboardInterrupt :
538- self .set_color (CONSOLE_COLOR_DEFAULT )
533+ self .set_color (util . CONSOLE_COLOR_DEFAULT )
539534 if not self .params .instruct :
540535 print (self .params .fix_prefix ,end = "" )
541536 self .input (self .params .fix_prefix )
0 commit comments