diff --git a/llama_cpp/llama.py b/llama_cpp/llama.py index 32d542441..c34c8358a 100644 --- a/llama_cpp/llama.py +++ b/llama_cpp/llama.py @@ -589,6 +589,16 @@ def _create_completion( break text = all_text[: len(all_text) - longest] returned_characters += len(text[start:]) + _text = '' + try: + _text = text[start:].decode("utf-8") + except UnicodeDecodeError: + for i in range(1,4): + try: + _text = text[start:-i].decode("utf-8") + break + except UnicodeDecodeError: + continue yield { "id": completion_id, "object": "text_completion", @@ -596,7 +606,7 @@ def _create_completion( "model": self.model_path, "choices": [ { - "text": text[start:].decode("utf-8", errors="ignore"), + "text": _text, "index": 0, "logprobs": None, "finish_reason": None,