@@ -1351,13 +1351,11 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
1351
1351
return data;
1352
1352
}
1353
1353
static void common_chat_parse_gpt_oss (common_chat_msg_parser & builder) {
1354
- static const common_regex assistant_regex (" assistant" );
1355
1354
static const common_regex message_regex (" <\\ |message\\ |>" );
1356
- static const common_regex channel_regex (" <\\ |channel\\ |>" );
1357
- static const common_regex start_regex (" <\\ |start\\ |>" );
1355
+ static const common_regex channel_regex (" <\\ |channel\\ |>(final|analysis|commentary) " );
1356
+ static const common_regex start_regex (" <\\ |start\\ |>assistant " );
1358
1357
static const common_regex end_regex (" <\\ |end\\ |>" );
1359
1358
static const common_regex to_regex (" to=" );
1360
- static const common_regex channel_type_regexp (" (final|analysis|commentary)" );
1361
1359
static const common_regex user_tool_call_regex (
1362
1360
" functions\\ .([a-zA-Z_][a-zA-Z0-9_]*)\\ s*(?:(?:<\\ |constrain\\ |>)?([a-zA-Z]+))?\\ s*<\\ |message\\ |>"
1363
1361
);
@@ -1378,7 +1376,24 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1378
1376
return builder.consume_rest ();
1379
1377
};
1380
1378
1379
+ auto try_consume_message = [&]() {
1380
+ if (builder.try_consume_regex (message_regex)) {
1381
+ if (!builder.try_find_regex (end_regex)) {
1382
+ builder.add_content (builder.consume_rest ());
1383
+ }
1384
+ return true ;
1385
+ }
1386
+ return false ;
1387
+ };
1388
+
1381
1389
auto tool_call = [&]() {
1390
+ if (!builder.syntax ().parse_tool_calls ) {
1391
+ // Move back to the start and consume up to the next message
1392
+ builder.move_to (message_start_pos);
1393
+ builder.add_content (consume_until_next (message_start_pos + 1 ));
1394
+ return ;
1395
+ }
1396
+
1382
1397
if (auto res = builder.try_consume_regex (user_tool_call_regex)) {
1383
1398
auto name = builder.str (res->groups [1 ]);
1384
1399
if (auto args = builder.try_consume_json_with_dumped_args ({{}})) {
@@ -1396,30 +1411,12 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1396
1411
1397
1412
auto commentary = [&]() {
1398
1413
if (builder.try_consume_regex (to_regex)) {
1399
- if (builder.syntax ().parse_tool_calls ) {
1400
- tool_call ();
1401
- } else {
1402
- // Move back to the start and consume up to the next message
1403
- builder.move_to (message_start_pos);
1404
- builder.add_content (consume_until_next (message_start_pos + 1 ));
1405
- }
1406
- } else if (builder.try_consume_regex (message_regex)) {
1407
- if (!builder.try_find_regex (end_regex)) {
1408
- builder.add_content (builder.consume_rest ());
1409
- }
1410
- } else {
1414
+ tool_call ();
1415
+ } else if (!try_consume_message ()) {
1411
1416
throw common_chat_msg_parse_exception (" expected: \" to=\" or <|message|>, got: " + consume_until_next ());
1412
1417
}
1413
1418
};
1414
1419
1415
- auto final = [&]() {
1416
- if (builder.try_consume_regex (message_regex)) {
1417
- builder.add_content (builder.consume_rest ());
1418
- } else {
1419
- throw common_chat_msg_parse_exception (" expected: <|message|>, got: " + consume_until_next ());
1420
- }
1421
- };
1422
-
1423
1420
auto analysis = [&]() {
1424
1421
if (builder.try_consume_regex (to_regex)) {
1425
1422
tool_call (); // built-in tools can be called in the analysis channel
@@ -1438,44 +1435,34 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1438
1435
};
1439
1436
1440
1437
auto channel = [&]() {
1441
- if (auto channel = builder.try_consume_regex (channel_regex)) {
1442
- channel_start_pos = channel ->groups [0 ].begin ;
1443
- if ( auto res = builder.try_consume_regex (channel_type_regexp)) {
1444
- auto type = builder. str (res-> groups [ 0 ]);
1445
- if (type == " analysis" ) {
1446
- analysis ();
1447
- } else if (type == " final " ) {
1448
- final ();
1449
- } else if (type == " commentary " ) {
1450
- commentary ( );
1438
+ if (auto res = builder.try_consume_regex (channel_regex)) {
1439
+ channel_start_pos = res ->groups [0 ].begin ;
1440
+ auto type = builder.str (res-> groups [ 1 ]);
1441
+ if ( type == " analysis " ) {
1442
+ analysis ();
1443
+ } else if (type == " commentary " ) {
1444
+ commentary ();
1445
+ } else if (type == " final" ) {
1446
+ if (! try_consume_message () ) {
1447
+ throw common_chat_msg_parse_exception ( " expected: <|message|>, got: " + consume_until_next () );
1451
1448
}
1452
- } else {
1453
- throw common_chat_msg_parse_exception (" expected one of: analysis, final, commentary, got: " + consume_until_next ());
1454
1449
}
1455
1450
} else {
1456
1451
throw common_chat_msg_parse_exception (" expected: <|channel|>, got: " + consume_until_next ());
1457
1452
}
1458
1453
};
1459
1454
1460
- auto start = [&]() {
1461
- if (builder.try_consume_regex (assistant_regex)) {
1462
- channel ();
1463
- } else {
1464
- throw common_chat_msg_parse_exception (" expected: <|assistant|>, got: " + consume_until_next ());
1465
- }
1466
- };
1467
-
1468
1455
try {
1469
1456
channel ();
1470
1457
} catch (const common_chat_msg_parse_exception & e) {
1471
1458
LOG_DBG (" Parse error: %s\n " , e.what ());
1472
1459
}
1473
1460
1474
1461
// Read in complete messages until done or partial exception raised
1475
- while (auto res = builder.try_find_literal ( " <|start|> " )) {
1462
+ while (auto res = builder.try_consume_regex (start_regex )) {
1476
1463
message_start_pos = res->groups [0 ].begin ;
1477
1464
try {
1478
- start ();
1465
+ channel ();
1479
1466
} catch (const common_chat_msg_parse_exception & e) {
1480
1467
LOG_DBG (" Parse error: %s\n " , e.what ());
1481
1468
}
0 commit comments