Skip to content

Use chat_template from gguf metadata #1096

Description

@vriesdemichael

Is your feature request related to a problem? Please describe.
When using a model which uses a non-standard chat template it is hard to implement chat functionality using llama-cpp-python. There are quite a few chat templates predefined in llama_chat_format.py, but every time you want to add a new one it requires a new chat formatting function decorated by @register_chat_format.

When you do not have a matching chat format the output of the chat completions will be sub optimal.

Describe the solution you'd like
Huggingface has started pushing the standard to store the chat template in the tokenizer config.
The convert script in llama.cpp has started storing this chat_template too:
gguf_write_call
function to add vocab
Implementation in base model

This means that for any huggingface model with the chat_template in the tokenizer config that gets converted by llama.cpp's convert script it will have the chat_template available in the gguf metadata.

The chat template is a jinja template, the huggingface code has an example on how to apply it: https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_utils_base.py#L1678
(this function is also used in hf_autotokenizer_to_chat_formatter, but the way it is implemented it requires access to the hf model/config files)

When this standard is picked up it will enable users to directly use chat completions for models with custom templates

Describe alternatives you've considered

  • Disable the chat endpoint when there is no known chat_format -> no complaining about bad results
  • Load the tokenizer config outside as a separate file to load the required chat_template
  • supply the chat template as an argument (which is how text-generation-webui does it

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions