使用llama-cpp-python调用llama_tokenize()时缺失参数报错求助
llama-cpp-python中llama_tokenize参数错误的解决方法
问题背景
使用Python 3.11 + 最新版llama-cpp-python + gguf模型开发聊天机器人时,触发参数错误。
报错信息
Traceback (most recent call last): File "d:\AI Custom\AI Arush\server.py", line 223, in <module> init() File "d:\AI Custom\AI Arush\server.py", line 57, in init m_eval(model, m_tokenize(model, PROMPT_INIT, True), False, "Starting up...") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "d:\AI Custom\AI Arush\server.py", line 182, in m_tokenize n_tokens = llama_cpp.llama_tokenize( ^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: llama_tokenize() missing 2 required positional arguments: 'add_bos' and 'special'
现有tokenize代码
def m_tokenize(model: llama_cpp.Llama, text: bytes, add_bos=False, special=False): assert model.ctx is not None n_ctx = llama_cpp.llama_n_ctx(model.ctx) tokens = (llama_cpp.llama_token * int(n_ctx))() n_tokens = llama_cpp.llama_tokenize( model.ctx, text, tokens, n_ctx, llama_cpp.c_bool(add_bos), ) if int(n_tokens) < 0: raise RuntimeError(f'Failed to tokenize: text="{text}" n_tokens={n_tokens}') return list(tokens[:n_tokens])
解决方法
最新版llama-cpp-python对llama_tokenize的参数要求做了更新,必须传入add_bos和special两个布尔参数。原代码仅传递了add_bos,需补充special参数的传递。
修改后的m_tokenize函数代码:
def m_tokenize(model: llama_cpp.Llama, text: bytes, add_bos=False, special=False): assert model.ctx is not None n_ctx = llama_cpp.llama_n_ctx(model.ctx) tokens = (llama_cpp.llama_token * int(n_ctx))() n_tokens = llama_cpp.llama_tokenize( model.ctx, text, tokens, n_ctx, llama_cpp.c_bool(add_bos), llama_cpp.c_bool(special) # 补充special参数 ) if int(n_tokens) < 0: raise RuntimeError(f'Failed to tokenize: text="{text}" n_tokens={n_tokens}') return list(tokens[:n_tokens])
内容的提问来源于stack exchange,提问作者Aryan Rathore
相关产品推荐
相关产品推荐

