8000 Handle errors returned by llama.cpp · coderonion/llama-cpp-python@b936756 · GitHub
[go: up one dir, main page]

Skip to content

Commit b936756

Browse files
committed
Handle errors returned by llama.cpp
1 parent bcde1f1 commit b936756

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

llama_cpp/llama.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import uuid
23
import time
34
import multiprocessing
@@ -35,6 +36,9 @@ def __init__(
3536

3637
self.tokens = (llama_cpp.llama_token * self.params.n_ctx)()
3738

39+
if not os.path.exists(model_path):
40+
raise ValueError(f"Model path does not exist: {model_path}")
41+
3842
self.ctx = llama_cpp.llama_init_from_file(
3943
self.model_path.encode("utf-8"), self.params
4044
)
@@ -66,6 +70,8 @@ def __call__(
6670
llama_cpp.llama_n_ctx(self.ctx),
6771
True,
6872
)
73+
if prompt_tokens < 0:
74+
raise RuntimeError(f"Failed to tokenize prompt: {prompt_tokens}")
6975

7076
if prompt_tokens + max_tokens > self.params.n_ctx:
7177
raise ValueError(
@@ -115,13 +121,15 @@ def __call__(
115121
finish_reason = "stop"
116122
break
117123

118-
llama_cpp.llama_eval(
124+
rc = llama_cpp.llama_eval(
119125
self.ctx,
120126
(llama_cpp.llama_token * 1)(self.tokens[prompt_tokens + i]),
121127
1,
122128
prompt_tokens + completion_tokens,
123129
self.n_threads,
124130
)
131+
if rc != 0:
132+
raise RuntimeError(f"Failed to evaluate next token: {rc}")
125133

126134
text = text.decode("utf-8")
127135

0 commit comments

Comments
 (0)
0