Skip to content

Commit 6f5e3d1

Browse files
committed
Move everything into the parse function
1 parent 83c46ba commit 6f5e3d1

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

common/chat-parser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class common_chat_msg_partial_exception : public std::runtime_error {
1515
common_chat_msg_partial_exception(const std::string & message) : std::runtime_error(message) {}
1616
};
1717

18+
class common_chat_msg_parse_exception : public std::runtime_error {
19+
public:
20+
common_chat_msg_parse_exception(const std::string & message) : std::runtime_error(message) {}
21+
};
22+
1823
class common_chat_msg_parser {
1924
std::string input_;
2025
bool is_partial_;

common/chat.cpp

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,8 +1353,88 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
13531353
return data;
13541354
}
13551355
static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1356-
harmony_msg_parser parser(builder);
1357-
parser.parse();
1356+
//harmony_msg_parser parser(builder);
1357+
//parser.parse();
1358+
1359+
static const common_regex end_regex("<\\|end\\|>");
1360+
static const common_regex to_regex(" to=");
1361+
static const common_regex channel_type_regexp("(final|analysis|commentary)");
1362+
static const common_regex tool_call_regex(
1363+
"functions\\.([a-zA-Z_][a-zA-Z0-9_]*)\\s?(?:<\\|constrain\\|>([a-zA-Z]+))?<\\|message\\|>"
1364+
);
1365+
1366+
auto user_function = [&]() {
1367+
if (auto res = builder.try_consume_regex(tool_call_regex)) {
1368+
auto name = builder.str(res->groups[1]);
1369+
auto args = builder.consume_rest();
1370+
builder.add_tool_call(name, "", args);
1371+
}
1372+
};
1373+
1374+
auto commentary = [&]() {
1375+
if (builder.try_consume_regex(to_regex)) {
1376+
user_function();
1377+
}
1378+
1379+
if (builder.try_consume_literal("<|message|>")) {
1380+
if (!builder.try_find_regex(end_regex)) {
1381+
builder.add_content(builder.consume_rest());
1382+
}
1383+
}
1384+
};
1385+
1386+
auto final = [&]() {
1387+
if (builder.try_consume_literal("<|message|>")) {
1388+
builder.add_content(builder.consume_rest());
1389+
}
1390+
};
1391+
1392+
auto analysis = [&]() {
1393+
if (builder.try_consume_literal("<|message|>")) {
1394+
if (auto res = builder.try_find_regex(end_regex, std::string::npos, false)) {
1395+
builder.add_reasoning_content(res->prelude);
1396+
} else {
1397+
builder.add_reasoning_content(builder.consume_rest());
1398+
}
1399+
}
1400+
};
1401+
1402+
auto channel = [&]() {
1403+
if (builder.try_consume_literal("<|channel|>")) {
1404+
if (auto res = builder.try_consume_regex(channel_type_regexp)) {
1405+
auto type = builder.str(res->groups[0]);
1406+
if (type == "analysis") {
1407+
analysis();
1408+
} else if (type == "final") {
1409+
final();
1410+
} else if (type == "commentary") {
1411+
commentary();
1412+
}
1413+
}
1414+
}
1415+
};
1416+
1417+
auto start = [&]() {
1418+
if (builder.try_consume_literal("assistant")) {
1419+
channel();
1420+
}
1421+
};
1422+
1423+
try {
1424+
channel();
1425+
} catch (const common_chat_msg_parse_exception & e) {
1426+
LOG_ERR("Parse error: %s", e.what());
1427+
}
1428+
1429+
while (builder.try_find_literal("<|start|>")) {
1430+
try {
1431+
start();
1432+
} catch(const common_chat_msg_parse_exception & e) {
1433+
LOG_ERR("Parse error: %s, skipping to next valid token", e.what());
1434+
}
1435+
}
1436+
1437+
builder.add_content(builder.consume_rest());
13581438
}
13591439

13601440
static common_chat_params common_chat_params_init_firefunction_v2(const common_chat_template & tmpl, const struct templates_params & inputs) {

0 commit comments

Comments
 (0)