Skip to content

Conversation

pnb
Copy link
Contributor

@pnb pnb commented Jan 29, 2025

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).

@pnb pnb requested a review from ngxson as a code owner January 29, 2025 14:33
@github-actions github-actions bot added examples python python script changes server labels Jan 29, 2025
@ngxson ngxson merged commit eb7cf15 into ggml-org:master Jan 29, 2025
47 checks passed
tinglou pushed a commit to tinglou/llama.cpp that referenced this pull request Feb 13, 2025
…ja functionality (ggml-org#11489)

* add /apply-template endpoint to server

* remove unnecessary line

* add /apply-template documentation

* return only "prompt" field in /apply-template

* use suggested idea instead of my overly verbose way
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Feb 26, 2025
…ja functionality (ggml-org#11489)

* add /apply-template endpoint to server

* remove unnecessary line

* add /apply-template documentation

* return only "prompt" field in /apply-template

* use suggested idea instead of my overly verbose way
mglambda pushed a commit to mglambda/llama.cpp that referenced this pull request Mar 8, 2025
…ja functionality (ggml-org#11489)

* add /apply-template endpoint to server

* remove unnecessary line

* add /apply-template documentation

* return only "prompt" field in /apply-template

* use suggested idea instead of my overly verbose way
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples python python script changes server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants