Skip to content

Missing add_generation_prompt #2035

Closed

Description

@davebulaval

Model name: Raj-Maharajwala/Open-Insurance-LLM-Llama3-8B

It is a Llama model; it should be fixed easily using the Llama chat template.

RuntimeError: Unsloth: The tokenizer `Raj-Maharajwala/Open-Insurance-LLM-Llama3-8B`
does not have a {% if add_generation_prompt %} for generation purposes.
Please file a bug report immediately - thanks!

Activity

  1. KareemMusleh commented on Mar 15, 2025

    @KareemMusleh
    Contributor

    It seems that there is a problem with the chat template of the model.

    from unsloth.tokenizer_utils import _load_correct_tokenizer
    tokenizer = _load_correct_tokenizer(model_name)
    messages = [
      {"role": "user", "content": "Who are you?"},
    ]
    yes = tokenizer.apply_chat_template(messages, add_generation_prompt =  True, tokenize = False)

    For the open insurance model yes="<|begin_of_text|>System: This is a chat between a user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions based on the context. The assistant should also indicate when the answer cannot be found in the context.\n\nUser: Who are you?\n\nAssistant:", while for the tokenizer of a typical llama model yes='<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\nWho are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n'

  2. davebulaval commented on Mar 16, 2025

    @davebulaval
    Author

    I tried changing the template later on using the Llama template, but it failed during the unslothloading.

  3. KareemMusleh commented on Mar 16, 2025

    @KareemMusleh
    Contributor

    is it throwing the same error?
    I've tested this and it works

    model_name = "Raj-Maharajwala/Open-Insurance-LLM-Llama3-8B"
    from unsloth.tokenizer_utils import _load_correct_tokenizer, fix_chat_template
    tokenizer = _load_correct_tokenizer(model_name)
    llama_chat_template = "{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}{% endif %}"
    tokenizer.chat_template = llama_chat_template
    tokenizer = fix_chat_template(tokenizer)

    both Raj-Maharajwala/Open-Insurance-LLM-Llama3-8B and the model it's based on nvidia/Llama3-ChatQA-1.5-8B seem to have the same chat_template.

  4. KareemMusleh commented on Mar 16, 2025

    @KareemMusleh
    Contributor

    try this:

    new_chat_template = "{{ bos_token }}{%- if messages[0]['role'] == 'system' -%}{% set loop_messages = messages[1:] %}{%- else -%}{% set loop_messages = messages %}{% endif %}System: This is a chat between a user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions based on the context. The assistant should also indicate when the answer cannot be found in the context.\n\n{% for message in loop_messages %}{%- if message['role'] == 'user' -%}User: {{ message['content'].strip() + '\n\n' }}{%- else -%}Assistant: {{ message['content'].strip() + '\n\n' }}{%- endif %}{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}"
  5. davebulaval commented on Mar 16, 2025

    @davebulaval
    Author

    It works for a patch like this, but the problem is I use FastLanguageModel.from_pretrained to init the model and tokenizer, and it fails to load since this call model, tokenizer = dispatch_model.from_pretrained use load_correct_tokenizer and seems to do some verification over the chat template. Thus, I do not have time to change it.

  6. KareemMusleh commented on Mar 17, 2025

    @KareemMusleh
    Contributor

    I don't know what should be the 'right' way to do this, but for now you could try this:

    from inspect import getsource
    from unsloth import load_correct_tokenizer
    
    template = r"{{ bos_token }}{%- if messages[0]['role'] == 'system' -%}{% set loop_messages = messages[1:] %}{%- else -%}{% set loop_messages = messages %}{% endif %}System: This is a chat between a user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions based on the context. The assistant should also indicate when the answer cannot be found in the context.\n\n{% for message in loop_messages %}{%- if message['role'] == 'user' -%}User: {{ message['content'].strip() + '\n\n' }}{%- else -%}Assistant: {{ message['content'].strip() + '\n\n' }}{%- endif %}{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}"
    src = getsource(load_correct_tokenizer)
    i = src.index('old_chat_template')
    new_src = src[:i] + f'tokenizer.chat_template = "{template}"\n    ' + src[i:]
    exec(new_src, globals())
    # Load the model
    exec(src, globals())
  7. davebulaval commented on Mar 17, 2025

    @davebulaval
    Author

    Good idea.

    I've used this instead, which I found to be easier.

    import unsloth
    unsloth.tokenizer_utils.fix_chat_template = lambda x : template
    # load model
    # [...]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions