@@ -1353,54 +1353,61 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
1353
1353
return data;
1354
1354
}
1355
1355
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\\ |>" );
1359
1360
static const common_regex end_regex (" <\\ |end\\ |>" );
1360
1361
static const common_regex to_regex (" to=" );
1361
1362
static const common_regex channel_type_regexp (" (final|analysis|commentary)" );
1362
1363
static const common_regex tool_call_regex (
1363
1364
" functions\\ .([a-zA-Z_][a-zA-Z0-9_]*)\\ s?(?:<\\ |constrain\\ |>([a-zA-Z]+))?<\\ |message\\ |>"
1364
1365
);
1365
1366
1366
- auto user_function = [&]() {
1367
+ auto user_function_call = [&]() {
1367
1368
if (auto res = builder.try_consume_regex (tool_call_regex)) {
1368
1369
auto name = builder.str (res->groups [1 ]);
1369
1370
auto args = builder.consume_rest ();
1370
1371
builder.add_tool_call (name, " " , args);
1372
+ } else {
1373
+ throw common_chat_msg_parse_exception (" expected user function call" );
1371
1374
}
1372
1375
};
1373
1376
1374
1377
auto commentary = [&]() {
1375
1378
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)) {
1380
1381
if (!builder.try_find_regex (end_regex)) {
1381
1382
builder.add_content (builder.consume_rest ());
1382
1383
}
1384
+ } else {
1385
+ throw common_chat_msg_parse_exception (" expected: \" to=\" or <|message|>" );
1383
1386
}
1384
1387
};
1385
1388
1386
1389
auto final = [&]() {
1387
- if (builder.try_consume_literal ( " <|message|> " )) {
1390
+ if (builder.try_consume_regex (message_regex )) {
1388
1391
builder.add_content (builder.consume_rest ());
1392
+ } else {
1393
+ throw common_chat_msg_parse_exception (" expected: <|message|>" );
1389
1394
}
1390
1395
};
1391
1396
1392
1397
auto analysis = [&]() {
1393
- if (builder.try_consume_literal ( " <|message|> " )) {
1398
+ if (builder.try_consume_regex (message_regex )) {
1394
1399
if (auto res = builder.try_find_regex (end_regex, std::string::npos, false )) {
1395
1400
builder.add_reasoning_content (res->prelude );
1396
1401
} else {
1397
1402
builder.add_reasoning_content (builder.consume_rest ());
1398
1403
}
1404
+ } else {
1405
+ throw common_chat_msg_parse_exception (" expected: <|message|>" );
1399
1406
}
1400
1407
};
1401
1408
1402
1409
auto channel = [&]() {
1403
- if (builder.try_consume_literal ( " <|channel|> " )) {
1410
+ if (builder.try_consume_regex (channel_regex )) {
1404
1411
if (auto res = builder.try_consume_regex (channel_type_regexp)) {
1405
1412
auto type = builder.str (res->groups [0 ]);
1406
1413
if (type == " analysis" ) {
@@ -1411,12 +1418,18 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1411
1418
commentary ();
1412
1419
}
1413
1420
}
1421
+ } else {
1422
+ throw common_chat_msg_parse_exception (" expected: <|channel|>" );
1414
1423
}
1415
1424
};
1416
1425
1417
1426
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
+ }
1420
1433
}
1421
1434
};
1422
1435
@@ -1427,14 +1440,10 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1427
1440
}
1428
1441
1429
1442
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 ();
1435
1444
}
1436
1445
1437
- builder.add_content (builder. consume_rest () );
1446
+ builder.consume_rest ();
1438
1447
}
1439
1448
1440
1449
static common_chat_params common_chat_params_init_firefunction_v2 (const common_chat_template & tmpl, const struct templates_params & inputs) {
0 commit comments