Skip to content

Commit c746984

Browse files
committed
common : improve reasoning and commentary handling when tool_choice is required
1 parent e05fcb1 commit c746984

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

common/chat.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,23 +1615,37 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
16151615
)
16161616
);
16171617
});
1618-
1619-
auto not_end = builder.add_rule("not-end",
1620-
"[^<] | \"<\" [^|] | \"<|\" [^e] | \"<|e\" [^n] | \"<|en\" [^d] | \"<|end\" [^|] | \"<|end|\" [^>]");
1621-
auto analysis = builder.add_rule("analysis",
1622-
"\"<|channel|>analysis<|message|>\" ( " + not_end + " )* \"<|end|>\"");
1623-
1624-
auto recipient_in_role = builder.add_rule("recipient_in_role",
1625-
"(" + analysis + ")?" + "\"<|start|>assistant\"? \" to=functions.\" ( " +
1626-
string_join(tool_rules_recipient_in_role, " | ") + " )"
1627-
);
16281618

16291619
auto recipient_in_channel = builder.add_rule("recipient_in_channel",
16301620
channel + " \" to=functions.\" ( " +
16311621
string_join(tool_rules_recipient_in_channel, " | ") + " )"
16321622
);
16331623

1634-
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+
}
16351649

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

0 commit comments

Comments
 (0)