ChatGLM3报错:No chat template is defined for this tokenizer

使用官方提供的脚本创建ChatGLM3的DEMO:

bash 复制代码
cd basic_demo
python web_demo_gradio.py

出现效果异常问题:

====conversation====

[{'role': 'user', 'content': '你好'}, {'role': 'assistant', 'content': '你好,有什么我可以帮助你的吗?\n\n<|im_end|>'}, {'role': 'user', 'content': '你好'}]

No chat template is defined for this tokenizer - using a default chat template that implements the ChatML format (without BOS/EOS tokens!). If the default is not appropriate for your model, please set `tokenizer.chat_template` to an appropriate template. See https://huggingface.co/docs/transformers/main/chat_templating for more information.

原因分析:

  • 模型版本与代码不匹配,tokenizer_config.json配置文件中缺少prompt模板
  • 官方代码存在问题,尚不支持本地模型使用apply_chat_template方法

解决方案:修改tokenizer方式,不要使用apply_chat_template方法,单轮对话可以改用build_chat_input方法

python 复制代码
def predict(history, max_length, top_p, temperature):
    stop = StopOnTokens()
    messages = []
    for idx, (user_msg, model_msg) in enumerate(history):
        if idx == len(history) - 1 and not model_msg:
            messages.append({"role": "user", "content": user_msg})
            break
        if user_msg:
            messages.append({"role": "user", "content": user_msg})
        if model_msg:
            messages.append({"role": "assistant", "content": model_msg})

    print("\n\n====conversation====\n", messages)
    model_inputs = tokenizer.apply_chat_template(messages,
                                                 add_generation_prompt=True,
                                                 tokenize=True,
                                                 return_tensors="pt").to(next(model.parameters()).device)
    print('debug: old: model_inputs: {}'.format(model_inputs))
    model_inputs = tokenizer.build_chat_input(messages[-1]['content'], history=None, role="user").input_ids.to(model.device)
    print('debug: new: model_inputs: {}'.format(model_inputs))
    streamer = TextIteratorStreamer(tokenizer, timeout=60, skip_prompt=True, skip_special_tokens=True)
    generate_kwargs = {
        "input_ids": model_inputs,
        "streamer": streamer,
        "max_new_tokens": max_length,
        "do_sample": True,
        "top_p": top_p,
        "temperature": temperature,
        "stopping_criteria": StoppingCriteriaList([stop]),
        "repetition_penalty": 1.2,
    }
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    for new_token in streamer:
        if new_token != '':
            history[-1][1] += new_token
            yield history

tokenizer.chat_template介绍

Next time you use apply_chat_template(), it will use your new template! This attribute will be saved in the tokenizer_config.json file, so you can use push_to_hub() to upload your new template to the Hub and make sure everyone's using the right template for your model!

设置tokenizer.chat_template属性后,下次使用apply_chat_template()时,将使用您的新模板!此属性保存在tokenizer_config.json文件中,因此您可以用push_to_hub()将新模板上传到Hub,确保大家都能使用正确的模板!
If a model does not have a chat template set, but there is a default template for its model class, the ConversationalPipeline class and methods like apply_chat_template will use the class template instead. You can find out what the default template for your tokenizer is by checking the tokenizer.default_chat_template attribute.

如果模型没有设置聊天模板,但有其模型类的默认模板,则ConversationalPipeline类和apply_chat_template等方法将使用类模板代替。你可以通过检查tokenizer.default_chat_template属性来了解你的tokenizer的默认模板是什么。

python 复制代码
def predict(history, max_length, top_p, temperature):
    stop = StopOnTokens()
    messages = []
    for idx, (user_msg, model_msg) in enumerate(history):
        if idx == len(history) - 1 and not model_msg:
            messages.append({"role": "user", "content": user_msg})
            break
        if user_msg:
            messages.append({"role": "user", "content": user_msg})
        if model_msg:
            messages.append({"role": "assistant", "content": model_msg})

    print("\n\n====conversation====\n", messages)
    print('debug: tokenizer.chat_template:\n{}'.format(tokenizer.chat_template))
    print('debug: tokenizer.default_chat_template:\n{}'.format(tokenizer.default_chat_template))
    model_inputs = tokenizer.apply_chat_template(messages,
                                                 add_generation_prompt=True,
                                                 tokenize=True,
                                                 return_tensors="pt").to(next(model.parameters()).device)
    streamer = TextIteratorStreamer(tokenizer, timeout=600, skip_prompt=True, skip_special_tokens=True)
    generate_kwargs = {
        "input_ids": model_inputs,
        "streamer": streamer,
        "max_new_tokens": max_length,
        "do_sample": True,
        "top_p": top_p,
        "temperature": temperature,
        "stopping_criteria": StoppingCriteriaList([stop]),
        "repetition_penalty": 1.2,
    }
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    for new_token in streamer:
        if new_token != '':
            history[-1][1] += new_token
            yield history
相关推荐
大耳朵爱学习3 小时前
掌握Transformer之注意力为什么有效
人工智能·深度学习·自然语言处理·大模型·llm·transformer·大语言模型
洛阳泰山3 小时前
如何使用Chainlit让所有网站快速嵌入一个AI聊天助手Copilot
人工智能·ai·llm·copilot·网站·chainlit·copliot
数据智能老司机18 小时前
从零开始构建大型语言模型——微调用于分类
深度学习·神经网络·llm
大耳朵爱学习20 小时前
大模型预训练的降本增效之路——从信息密度出发
人工智能·深度学习·机器学习·自然语言处理·大模型·llm·大语言模型
数据智能老司机1 天前
从零开始构建大型语言模型——实现注意力机制
深度学习·神经网络·llm
Seal软件1 天前
GPUStack 0.2:开箱即用的分布式推理、CPU推理和调度策略
大模型·llm·aigc·gpu·genai·gpu集群
阿里云大数据AI技术1 天前
对接开源大模型应用开发平台最佳实践
人工智能·阿里云·llm·opensearch
skywalk81631 天前
使用PaddleNLP调用大模型ChatGLM3-6b进行信息抽取
人工智能·llm·chatglm
Hoper.J1 天前
使用 HFD 加快 Hugging Face 模型和数据集的下载
llm·aigc·hugging face·hfd
真-忒修斯之船2 天前
搭配Knowledge Graph的RAG架构
人工智能·大模型·llm·知识图谱·graph·rag·knowledgegraph