@@ -449,7 +449,7 @@ std::string common_chat_format_name(common_chat_format format) {
449449 }
450450}
451451
452- static std::optional<json> parse_json (std::string::const_iterator & it, const std::string::const_iterator & end) {
452+ static bool parse_json (std::string::const_iterator & it, const std::string::const_iterator & end, json & out ) {
453453 // // https://json.nlohmann.me/features/parsing/sax_interface/
454454 struct json_error_locator : public nlohmann ::json_sax<json> {
455455 std::size_t position;
@@ -486,11 +486,11 @@ static std::optional<json> parse_json(std::string::const_iterator & it, const st
486486 }
487487 std::string json_sub {it, temptative_end};
488488 try {
489- auto out = json::parse (json_sub);
489+ out = json::parse (json_sub);
490490 it = temptative_end;
491- return out ;
491+ return true ;
492492 } catch (const std::exception &) {
493- return std:: nullopt ;
493+ return false ;
494494 }
495495}
496496
@@ -562,12 +562,13 @@ static common_chat_msg parse_json_tool_calls(
562562 result.content += std::string (it, rit->prefix ().second );
563563 it = rit->suffix ().first ;
564564
565- if (auto arguments = parse_json (it, end)) {
565+ json arguments;
566+ if (parse_json (it, end, arguments)) {
566567 if (!std::regex_search (it, end, match, close_regex)) {
567568 throw std::runtime_error (" Malformed input, missing closing pattern: " + input);
568569 }
569570 it = match.suffix ().first ;
570- result.tool_calls .push_back ({name, arguments-> is_string () ? arguments-> get <std::string>() : arguments-> dump (), /* id= */ " " });
571+ result.tool_calls .push_back ({name, arguments. is_string () ? arguments. get <std::string>() : arguments. dump (), /* id= */ " " });
571572 } else {
572573 if (allow_raw_python && name == " python" ) {
573574 result.tool_calls .push_back ({name, json ({{" code" , std::string (it, end)}}).dump (), /* id= */ " " });
@@ -1472,10 +1473,10 @@ static common_chat_msg common_chat_parse_hermes_2_pro(const std::string& input)
14721473 if (match[3 ].matched ) {
14731474 close_tag = open_tag.empty () ? " " : " </" + open_tag.substr (1 );
14741475 auto json_it = match[3 ].first ;
1475- auto tool_call = parse_json (json_it, end) ;
1476- if (tool_call && tool_call-> contains (" name" ) && tool_call-> contains (" arguments" )) {
1476+ json tool_call;
1477+ if (parse_json (json_it, end, tool_call) && tool_call. contains (" name" ) && tool_call. contains (" arguments" )) {
14771478
1478- msg.tool_calls .emplace_back (process_tool_call (* tool_call));
1479+ msg.tool_calls .emplace_back (process_tool_call (tool_call));
14791480 it = json_it; // Move iterator past parsed JSON
14801481
14811482 // Handle close tags
@@ -1503,10 +1504,11 @@ static common_chat_msg common_chat_parse_hermes_2_pro(const std::string& input)
15031504 close_tag = " </function>" ;
15041505 // Start parsing from after the opening tags
15051506 auto json_it = match[6 ].first ;
1506- if (auto arguments = parse_json (json_it, end)) {
1507+ json arguments;
1508+ if (parse_json (json_it, end, arguments)) {
15071509 msg.tool_calls .emplace_back (process_tool_call ({
15081510 {" name" , function_name},
1509- {" arguments" , * arguments},
1511+ {" arguments" , arguments},
15101512 }));
15111513 it = json_it; // Move iterator past parsed JSON
15121514
0 commit comments