Skip to content

Commit 905996d

Browse files
committed
Add in builtin tools
1 parent fec1878 commit 905996d

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

common/chat.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,23 +1360,32 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
13601360
static const common_regex end_regex("<\\|end\\|>");
13611361
static const common_regex to_regex(" to=");
13621362
static const common_regex channel_type_regexp("(final|analysis|commentary)");
1363-
static const common_regex tool_call_regex(
1363+
static const common_regex user_tool_call_regex(
13641364
"functions\\.([a-zA-Z_][a-zA-Z0-9_]*)\\s?(?:<\\|constrain\\|>([a-zA-Z]+))?<\\|message\\|>"
13651365
);
1366+
static const common_regex browser_tool_call_regex("browser\\.(search|open|find)[\\s\\S]*<\\|message\\|>");
1367+
static const common_regex python_tool_call_regex("python\\s?(?:<|constrain|>code)?<\\|message\\|>");
13661368

1367-
auto user_function_call = [&]() {
1368-
if (auto res = builder.try_consume_regex(tool_call_regex)) {
1369+
auto tool_call = [&]() {
1370+
if (auto res = builder.try_consume_regex(user_tool_call_regex)) {
13691371
auto name = builder.str(res->groups[1]);
13701372
auto args = builder.consume_rest();
13711373
builder.add_tool_call(name, "", args);
1374+
} else if (auto res = builder.try_consume_regex(browser_tool_call_regex)) {
1375+
auto code = builder.consume_rest();
1376+
LOG_DBG("builtin tool call to python code: %s", code.c_str());
1377+
} else if (auto res = builder.try_consume_regex(python_tool_call_regex)) {
1378+
auto name = builder.str(res->groups[1]);
1379+
auto args = builder.consume_rest();
1380+
LOG_DBG("builtin tool call to browser.%s %s", name.c_str(), args.c_str());
13721381
} else {
1373-
throw common_chat_msg_parse_exception("expected user function call");
1382+
throw common_chat_msg_parse_exception("expected function call");
13741383
}
13751384
};
13761385

13771386
auto commentary = [&]() {
13781387
if (builder.try_consume_regex(to_regex)) {
1379-
user_function_call();
1388+
tool_call();
13801389
} else if (builder.try_consume_regex(message_regex)) {
13811390
if (!builder.try_find_regex(end_regex)) {
13821391
builder.add_content(builder.consume_rest());
@@ -1395,7 +1404,9 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
13951404
};
13961405

13971406
auto analysis = [&]() {
1398-
if (builder.try_consume_regex(message_regex)) {
1407+
if (builder.try_consume_regex(to_regex)) {
1408+
tool_call(); // built-in tools can be called in the analysis channel
1409+
} else if (builder.try_consume_regex(message_regex)) {
13991410
if (auto res = builder.try_find_regex(end_regex, std::string::npos, false)) {
14001411
builder.add_reasoning_content(res->prelude);
14011412
} else {
@@ -1425,11 +1436,9 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14251436

14261437
auto start = [&]() {
14271438
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-
}
1439+
channel();
1440+
} else {
1441+
throw common_chat_msg_parse_exception("expected: <|assistant|>");
14331442
}
14341443
};
14351444

@@ -1439,8 +1448,13 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14391448
LOG_ERR("Parse error: %s", e.what());
14401449
}
14411450

1451+
// Read in complete messages until done or partial exception raised
14421452
while (builder.try_find_literal("<|start|>")) {
1443-
start();
1453+
try {
1454+
start();
1455+
} catch (const common_chat_msg_parse_exception & e) {
1456+
LOG_ERR("Parse error: %s", e.what());
1457+
}
14441458
}
14451459

14461460
builder.consume_rest();

0 commit comments

Comments
 (0)