Skip to content

Commit dabd89d

Browse files
committed
Bugfix: Fix broken: UnicodeDecodeError: 'utf-8' codec can't decode
1 parent e636214 commit dabd89d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

llama_cpp/llama.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,24 @@ def _create_completion(
360360
break
361361
text = all_text[: len(all_text) - longest]
362362
returned_characters += len(text[start:])
363+
_text = ''
364+
try:
365+
_text = text[start:].decode("utf-8")
366+
except UnicodeDecodeError:
367+
for i in range(1,4):
368+
try:
369+
_text = text[start:-i].decode("utf-8")
370+
break
371+
except UnicodeDecodeError:
372+
continue
363373
yield {
364374
"id": completion_id,
365375
"object": "text_completion",
366376
"created": created,
367377
"model": self.model_path,
368378
"choices": [
369379
{
370-
"text": text[start:].decode("utf-8"),
380+
"text": _text,
371381
"index": 0,
372382
"logprobs": None,
373383
"finish_reason": None,

0 commit comments

Comments
 (0)