File tree Expand file tree Collapse file tree 2 files changed +18
-4
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +18
-4
lines changed
Original file line number Diff line number Diff line change @@ -174,7 +174,9 @@ def __init__(
174
174
if self .verbose :
175
175
print (llama_cpp .llama_print_system_info ().decode ("utf-8" ), file = sys .stderr )
176
176
177
- def tokenize (self , text : bytes ) -> List [llama_cpp .llama_token ]:
177
+ def tokenize (
178
+ self , text : bytes , add_bos : bool = True
179
+ ) -> List [llama_cpp .llama_token ]:
178
180
"""Tokenize a string.
179
181
180
182
Args:
@@ -194,10 +196,22 @@ def tokenize(self, text: bytes) -> List[llama_cpp.llama_token]:
194
196
text ,
195
197
tokens ,
196
198
n_ctx ,
197
- llama_cpp .c_bool (True ),
199
+ llama_cpp .c_bool (add_bos ),
198
200
)
199
201
if int (n_tokens ) < 0 :
200
- raise RuntimeError (f'Failed to tokenize: text="{ text } " n_tokens={ n_tokens } ' )
202
+ n_tokens = abs (n_tokens )
203
+ tokens = (llama_cpp .llama_token * int (n_tokens ))()
204
+ n_tokens = llama_cpp .llama_tokenize (
205
+ self .ctx ,
206
+ text ,
207
+ tokens ,
208
+ llama_cpp .c_int (n_tokens ),
209
+ llama_cpp .c_bool (add_bos ),
210
+ )
211
+ if n_tokens < 0 :
212
+ raise RuntimeError (
213
+ f'Failed to tokenize: text="{ text } " n_tokens={ n_tokens } '
214
+ )
201
215
return list (tokens [:n_tokens ])
202
216
203
217
def detokenize (self , tokens : List [llama_cpp .llama_token ]) -> bytes :
Original file line number Diff line number Diff line change @@ -350,7 +350,7 @@ def llama_tokenize(
350
350
tokens , # type: Array[llama_token]
351
351
n_max_tokens : c_int ,
352
352
add_bos : c_bool ,
353
- ) -> c_int :
353
+ ) -> int :
354
354
return _lib .llama_tokenize (ctx , text , tokens , n_max_tokens , add_bos )
355
355
356
356
You can’t perform that action at this time.
0 commit comments