Skip to content

Commit 32732f2

Browse files
authored
model : gpt-oss add response_format support (ggml-org#15494)
1 parent 92f7f0a commit 32732f2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

common/chat.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,26 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
13611361
"<|end|>",
13621362
};
13631363

1364+
if (!inputs.json_schema.is_null()) {
1365+
data.grammar_lazy = false;
1366+
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
1367+
auto schema = inputs.json_schema;
1368+
builder.resolve_refs(schema);
1369+
1370+
auto not_end = builder.add_rule("not-end",
1371+
"[^<] | \"<\" [^|] | \"<|\" [^e] | \"<|e\" [^n] | \"<|en\" [^d] | \"<|end\" [^|] | \"<|end|\" [^>]");
1372+
auto analysis = builder.add_rule("analysis",
1373+
"\"<|channel|>analysis<|message|>\" ( " + not_end + " )* \"<|end|>\"");
1374+
auto constraint = builder.add_rule("constraint", "\"<|constrain|>\"? [a-zA-Z0-9_-]+");
1375+
auto final = builder.add_rule("final",
1376+
"\"<|channel|>final\" ( \" \" " + constraint + " )? \"<|message|>\" " +
1377+
builder.add_schema("response", schema)
1378+
);
1379+
1380+
builder.add_rule("root", "( " + analysis + " \"<|start|>assistant\" )? " + final);
1381+
});
1382+
}
1383+
13641384
if (inputs.tools.is_array() && !inputs.tools.empty()) {
13651385
data.grammar_lazy = inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
13661386
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
@@ -2121,7 +2141,7 @@ static common_chat_params common_chat_templates_apply_jinja(
21212141
}
21222142

21232143
// GPT-OSS
2124-
if (src.find("<|channel|>") != std::string::npos && params.json_schema.is_null()) {
2144+
if (src.find("<|channel|>") != std::string::npos) {
21252145
return common_chat_params_init_gpt_oss(tmpl, params);
21262146
}
21272147

0 commit comments

Comments
 (0)