Skip to content

Commit 379c7a8

Browse files
committed
initial commit for handling extra template kwargs
1 parent e2e1ddb commit 379c7a8

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

common/chat.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct templates_params {
2424
std::string grammar;
2525
bool add_generation_prompt = true;
2626
bool extract_reasoning = true;
27+
json extra_context;
2728
};
2829

2930
common_chat_tool_choice common_chat_tool_choice_parse_oaicompat(const std::string & tool_choice) {
@@ -1561,7 +1562,7 @@ static common_chat_msg common_chat_parse_hermes_2_pro(const std::string& input,
15611562

15621563
static common_chat_params common_chat_params_init_without_tools(const common_chat_template & tmpl, const struct templates_params & inputs) {
15631564
common_chat_params data;
1564-
data.prompt = apply(tmpl, inputs.messages, inputs.tools.empty() ? json() : inputs.tools, inputs.add_generation_prompt);
1565+
data.prompt = apply(tmpl, inputs.messages, inputs.tools.empty() ? json() : inputs.tools, inputs.add_generation_prompt,inputs.extra_context);
15651566
data.format = COMMON_CHAT_FORMAT_CONTENT_ONLY;
15661567
data.grammar_lazy = false;
15671568
if (!inputs.json_schema.is_null()) {
@@ -1591,6 +1592,13 @@ static common_chat_params common_chat_templates_apply_jinja(
15911592
params.extract_reasoning = inputs.extract_reasoning;
15921593
params.tool_choice = inputs.tool_choice;
15931594
params.grammar = inputs.grammar;
1595+
1596+
for(auto el: inputs.chat_template_kwargs)
1597+
{
1598+
params.extra_context[el.first] = json::parse(el.second);
1599+
}
1600+
1601+
15941602
if (!inputs.json_schema.empty()) {
15951603
params.json_schema = json::parse(inputs.json_schema);
15961604
}

common/chat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "common.h"
66
#include <string>
77
#include <vector>
8+
#include <map>
89

910
struct common_chat_templates;
1011

@@ -71,6 +72,7 @@ struct common_chat_templates_inputs {
7172
common_chat_tool_choice tool_choice = COMMON_CHAT_TOOL_CHOICE_AUTO;
7273
bool parallel_tool_calls = false;
7374
bool extract_reasoning = true;
75+
std::map<std::string,std::string> chat_template_kwargs;
7476
};
7577

7678
struct common_chat_params {

examples/server/utils.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,12 @@ static json oaicompat_completion_params_parse(
638638
inputs.parallel_tool_calls = json_value(body, "parallel_tool_calls", false);
639639
inputs.extract_reasoning = reasoning_format != COMMON_REASONING_FORMAT_NONE;
640640
inputs.add_generation_prompt = json_value(body, "add_generation_prompt", true);
641+
642+
auto chat_template_kwargs_object = json_value(body, "chat_template_kwargs", json::object());
643+
for (const auto & item : chat_template_kwargs_object.items()) {
644+
inputs.chat_template_kwargs[item.key()] = item.value().dump();
645+
}
646+
641647
if (!inputs.tools.empty() && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE && body.contains("grammar")) {
642648
throw std::runtime_error("Cannot use custom grammar constraints with tools.");
643649
}

0 commit comments

Comments
 (0)