Skip to content

Commit c0c55e3

Browse files
crat0zaldehir
authored andcommitted
common : fix reasoning before forced tool call via tool_choice = required (ggml-org#16264)
* common : fix reasoning before forced tool call via tool_choice = required * common : improve reasoning and commentary handling when tool_choice is required (cherry picked from commit c746984) --------- Co-authored-by: Alde Rojas <[email protected]>
1 parent a737c42 commit c0c55e3

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

common/chat.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,17 +1616,36 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
16161616
);
16171617
});
16181618

1619-
auto recipient_in_role = builder.add_rule("recipient_in_role",
1620-
"\"<|start|>assistant\"? \" to=functions.\" ( " +
1621-
string_join(tool_rules_recipient_in_role, " | ") + " )"
1622-
);
1623-
16241619
auto recipient_in_channel = builder.add_rule("recipient_in_channel",
16251620
channel + " \" to=functions.\" ( " +
16261621
string_join(tool_rules_recipient_in_channel, " | ") + " )"
16271622
);
16281623

1629-
builder.add_rule("root", recipient_in_role + " | " + recipient_in_channel);
1624+
if (data.grammar_lazy) {
1625+
auto recipient_in_role = builder.add_rule("recipient_in_role",
1626+
"\"<|start|>assistant\"? \" to=functions.\" ( " +
1627+
string_join(tool_rules_recipient_in_role, " | ") + " )"
1628+
);
1629+
1630+
builder.add_rule("root", recipient_in_role + " | " + recipient_in_channel);
1631+
} else {
1632+
auto not_end = builder.add_rule("not-end",
1633+
"[^<] | \"<\" [^|] | \"<|\" [^e] | \"<|e\" [^n] | \"<|en\" [^d] | \"<|end\" [^|] | \"<|end|\" [^>]");
1634+
auto analysis = builder.add_rule("analysis",
1635+
"\"<|channel|>analysis<|message|>\" ( " + not_end + " )* \"<|end|>\"");
1636+
auto commentary = builder.add_rule("commentary",
1637+
"\"<|channel|>commentary<|message|>\" ( " + not_end + " )* \"<|end|>\"");
1638+
1639+
auto recipient_in_role = builder.add_rule("recipient_in_role",
1640+
"\" to=functions.\" ( " + string_join(tool_rules_recipient_in_role, " | ") + " )"
1641+
);
1642+
1643+
builder.add_rule("root",
1644+
"( " + analysis + " \"<|start|>assistant\" )? " +
1645+
"( " + commentary + " \"<|start|>assistant\" )? " +
1646+
"( " + recipient_in_role + " | " + recipient_in_channel + " )"
1647+
);
1648+
}
16301649

16311650
// Trigger on tool calls that appear in the commentary channel
16321651
data.grammar_triggers.push_back({

0 commit comments

Comments
 (0)