Skip to content

Commit e829173

Browse files
author
matteo
committed
Prefilling assistant message in openai compatible API
1 parent b6ce743 commit e829173

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

examples/server/utils.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,26 @@ static json oaicompat_completion_params_parse(
642642
throw std::runtime_error("Cannot use custom grammar constraints with tools.");
643643
}
644644

645+
/* Prefill assistant message support */
646+
bool prefill_assistant_message = inputs.messages.size() > 0 && inputs.messages[inputs.messages.size()-1].role == "assistant";
647+
common_chat_msg last_message;
648+
if (prefill_assistant_message)
649+
{
650+
last_message = inputs.messages.back();
651+
inputs.messages.pop_back();
652+
inputs.extract_reasoning = false;
653+
inputs.add_generation_prompt = true;
654+
}
655+
645656
// Apply chat template to the list of messages
646657
auto chat_params = common_chat_templates_apply(tmpls, inputs);
647658

659+
/* Append assistant prefilled message */
660+
if (prefill_assistant_message)
661+
{
662+
chat_params.prompt += last_message.content;
663+
}
664+
648665
llama_params["chat_format"] = static_cast<int>(chat_params.format);
649666
llama_params["prompt"] = chat_params.prompt;
650667
if (!chat_params.grammar.empty()) {

0 commit comments

Comments
 (0)