server : add /apply-template endpoint for additional use cases of Minja functionality #11489
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds an endpoint to examples/server,
/apply-template
, which will apply the model's chat template to the given messages, like for chat completion, but then will simply return the formatted prompt rather than running inference.Sometimes I modify prompts after formatting them for chat interactions, especially for cases where the goal is to insert something into the beginning of the model's response. For example, the classic "Sure!" insertion to reduce refusals, or more often some partially-constructed solutions that I would like the model to finish.
Previously I have implemented chat templates for each model myself, but with the addition of some Jinja support via Minja it is very tempting to let the server do this for users instead, as it is more likely to be correct and offer good coverage of prompt templates.
This PR also adds a CI test, which I did using the Command-R template to provide some variety versus the closely related
test_chat_template
unit test (side note: I did not find__verbose
very useful for prompt formatting use cases because it includes BOS, hence the new endpoint).