Skip to content

Commit fec1878

Browse files
committed
add parse error checking
1 parent 6f5e3d1 commit fec1878

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

common/chat.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,54 +1353,61 @@ 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();
1358-
1356+
static const common_regex assistant_regex("assistant");
1357+
static const common_regex message_regex("<\\|message\\|>");
1358+
static const common_regex channel_regex("<\\|channel\\|>");
1359+
static const common_regex start_regex("<\\|start\\|>");
13591360
static const common_regex end_regex("<\\|end\\|>");
13601361
static const common_regex to_regex(" to=");
13611362
static const common_regex channel_type_regexp("(final|analysis|commentary)");
13621363
static const common_regex tool_call_regex(
13631364
"functions\\.([a-zA-Z_][a-zA-Z0-9_]*)\\s?(?:<\\|constrain\\|>([a-zA-Z]+))?<\\|message\\|>"
13641365
);
13651366

1366-
auto user_function = [&]() {
1367+
auto user_function_call = [&]() {
13671368
if (auto res = builder.try_consume_regex(tool_call_regex)) {
13681369
auto name = builder.str(res->groups[1]);
13691370
auto args = builder.consume_rest();
13701371
builder.add_tool_call(name, "", args);
1372+
} else {
1373+
throw common_chat_msg_parse_exception("expected user function call");
13711374
}
13721375
};
13731376

13741377
auto commentary = [&]() {
13751378
if (builder.try_consume_regex(to_regex)) {
1376-
user_function();
1377-
}
1378-
1379-
if (builder.try_consume_literal("<|message|>")) {
1379+
user_function_call();
1380+
} else if (builder.try_consume_regex(message_regex)) {
13801381
if (!builder.try_find_regex(end_regex)) {
13811382
builder.add_content(builder.consume_rest());
13821383
}
1384+
} else {
1385+
throw common_chat_msg_parse_exception("expected: \" to=\" or <|message|>");
13831386
}
13841387
};
13851388

13861389
auto final = [&]() {
1387-
if (builder.try_consume_literal("<|message|>")) {
1390+
if (builder.try_consume_regex(message_regex)) {
13881391
builder.add_content(builder.consume_rest());
1392+
} else {
1393+
throw common_chat_msg_parse_exception("expected: <|message|>");
13891394
}
13901395
};
13911396

13921397
auto analysis = [&]() {
1393-
if (builder.try_consume_literal("<|message|>")) {
1398+
if (builder.try_consume_regex(message_regex)) {
13941399
if (auto res = builder.try_find_regex(end_regex, std::string::npos, false)) {
13951400
builder.add_reasoning_content(res->prelude);
13961401
} else {
13971402
builder.add_reasoning_content(builder.consume_rest());
13981403
}
1404+
} else {
1405+
throw common_chat_msg_parse_exception("expected: <|message|>");
13991406
}
14001407
};
14011408

14021409
auto channel = [&]() {
1403-
if (builder.try_consume_literal("<|channel|>")) {
1410+
if (builder.try_consume_regex(channel_regex)) {
14041411
if (auto res = builder.try_consume_regex(channel_type_regexp)) {
14051412
auto type = builder.str(res->groups[0]);
14061413
if (type == "analysis") {
@@ -1411,12 +1418,18 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14111418
commentary();
14121419
}
14131420
}
1421+
} else {
1422+
throw common_chat_msg_parse_exception("expected: <|channel|>");
14141423
}
14151424
};
14161425

14171426
auto start = [&]() {
1418-
if (builder.try_consume_literal("assistant")) {
1419-
channel();
1427+
if (builder.try_consume_regex(assistant_regex)) {
1428+
try {
1429+
channel();
1430+
} catch (const common_chat_msg_parse_exception & e) {
1431+
LOG_ERR("Parse error: %s, skipping to next valid start", e.what());
1432+
}
14201433
}
14211434
};
14221435

@@ -1427,14 +1440,10 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14271440
}
14281441

14291442
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-
}
1443+
start();
14351444
}
14361445

1437-
builder.add_content(builder.consume_rest());
1446+
builder.consume_rest();
14381447
}
14391448

14401449
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)