Skip to content

Commit cb4d86c

Browse files
authored
server: Retrieve prompt template in /props (#8337)
* server: Retrieve prompt template in /props This PR adds the following: - Expose the model's Jinja2 prompt template from the model in the /props endpoint. - Change log-level from Error to Warning for warning about template mismatch. The front-end stands a better chance of actually executing the Jinja template format correctly. Server is currently just guessing it. Ideally this should have been inside a JSON block that expose the same key/value pairs as listed during startup in "llm_load_print_meta" function. * Make string buffer dynamic * Add doc and better string handling * Using chat_template naming convention * Use intermediate vector for string assignment
1 parent 86e7299 commit cb4d86c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

examples/server/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,16 @@ Notice that each `probs` is an array of length `n_probs`.
366366
"assistant_name": "",
367367
"user_name": "",
368368
"default_generation_settings": { ... },
369-
"total_slots": 1
369+
"total_slots": 1,
370+
"chat_template": ""
370371
}
371372
```
372373

373374
- `assistant_name` - the required assistant name to generate the prompt in case you have specified a system prompt for all slots.
374375
- `user_name` - the required anti-prompt to generate the prompt in case you have specified a system prompt for all slots.
375376
- `default_generation_settings` - the default generation settings for the `/completion` endpoint, which has the same fields as the `generation_settings` response object from the `/completion` endpoint.
376377
- `total_slots` - the total number of slots for process requests (defined by `--parallel` option)
378+
- `chat_template` - the model's original Jinja2 prompt template
377379

378380
- **POST** `/v1/chat/completions`: OpenAI-compatible Chat Completions API. Given a ChatML-formatted json description in `messages`, it returns the predicted completion. Both synchronous and streaming mode are supported, so scripted and interactive applications work fine. While no strong claims of compatibility with OpenAI API spec is being made, in our experience it suffices to support many apps. Only models with a [supported chat template](https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template) can be used optimally with this endpoint. By default, the ChatML template will be used.
379381

examples/server/server.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ int main(int argc, char ** argv) {
26052605
// if a custom chat template is not supplied, we will use the one that comes with the model (if any)
26062606
if (params.chat_template.empty()) {
26072607
if (!ctx_server.validate_model_chat_template()) {
2608-
LOG_ERROR("The chat template that comes with this model is not yet supported, falling back to chatml. This may cause the model to output suboptimal responses", {});
2608+
LOG_WARNING("The chat template that comes with this model is not yet supported, falling back to chatml. This may cause the model to output suboptimal responses", {});
26092609
params.chat_template = "chatml";
26102610
}
26112611
}
@@ -2967,11 +2967,20 @@ int main(int argc, char ** argv) {
29672967
};
29682968

29692969
const auto handle_props = [&ctx_server](const httplib::Request & req, httplib::Response & res) {
2970+
std::string template_key = "tokenizer.chat_template", curr_tmpl;
2971+
int32_t tlen = llama_model_meta_val_str(ctx_server.model, template_key.c_str(), nullptr, 0);
2972+
if (tlen > 0) {
2973+
std::vector<char> curr_tmpl_buf(tlen + 1, 0);
2974+
if (llama_model_meta_val_str(ctx_server.model, template_key.c_str(), curr_tmpl_buf.data(), curr_tmpl_buf.size()) == tlen) {
2975+
curr_tmpl = std::string(curr_tmpl_buf.data(), tlen);
2976+
}
2977+
}
29702978
res.set_header("Access-Control-Allow-Origin", req.get_header_value("Origin"));
29712979
json data = {
29722980
{ "system_prompt", ctx_server.system_prompt.c_str() },
29732981
{ "default_generation_settings", ctx_server.default_generation_settings_for_props },
2974-
{ "total_slots", ctx_server.params.n_parallel }
2982+
{ "total_slots", ctx_server.params.n_parallel },
2983+
{ "chat_template", curr_tmpl.c_str() }
29752984
};
29762985

29772986
res.set_content(data.dump(), "application/json; charset=utf-8");

0 commit comments

Comments
 (0)