Skip to content

Add function for reversing chat templates #40776

@LakeYin

Description

@LakeYin

Feature request

Currently tokenizer.apply_chat_template can be used to convert a chat in the form list of dicts into a formatted string for text generation. However, there is currently no convenient method for doing the reverse. A new function (hypothetically called tokenizer.parse_chat_template) could handle this.

See also:
https://stackoverflow.com/questions/79248499/how-to-reverse-the-tokenizer-apply-chat-template-method-and-handle-streaming-r
https://stackoverflow.com/questions/79248486/how-to-reverse-the-tokenizer-apply-chat-template

Motivation

Logically if there is a function for converting a chat into a string, there should be a function that does the reverse, which would make things easier when working with AutoModelForCausalLM with chat models. This feature is already implemented in the pipeline API, so it should be straightforward to create a version that's exposed to end users.

Your contribution

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions