Skip to content

Commit 3cb203c

Browse files
authored
llama-chat : Do not throw when tool parsing fails (#14012)
Currently when a model generates output which looks like a tool call, but is invalid an exception is thrown and not handled, causing the cli or llama-server to bail. Instead, handle the chat parser exception and simply return the generated text in such cases. Signed-off-by: Piotr Stankiewicz <[email protected]>
1 parent 2e42be4 commit 3cb203c

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

common/chat-parser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ bool common_chat_msg_parser::add_tool_call(const std::string & name, const std::
4949

5050
// LOG_DBG("Tool call arguments:\n\traw: %s\n\tresult: %s\n", arguments.c_str(), tool_call.arguments.c_str());
5151
result_.tool_calls.emplace_back(tool_call);
52+
5253
return true;
5354
}
5455
bool common_chat_msg_parser::add_tool_call(const json & tool_call) {
@@ -378,3 +379,7 @@ std::optional<common_chat_msg_parser::consume_json_result> common_chat_msg_parse
378379
/* .is_partial = */ found_healing_marker,
379380
};
380381
}
382+
383+
void common_chat_msg_parser::clear_tools() {
384+
result_.tool_calls.clear();
385+
}

common/chat-parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,6 @@ class common_chat_msg_parser {
115115
const std::vector<std::vector<std::string>> & args_paths = {},
116116
const std::vector<std::vector<std::string>> & content_paths = {}
117117
);
118+
119+
void clear_tools();
118120
};

common/chat.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,9 @@ common_chat_msg common_chat_parse(const std::string & input, bool is_partial, co
19211921
} catch (const common_chat_msg_partial_exception & ex) {
19221922
LOG_DBG("Partial parse: %s\n", ex.what());
19231923
if (!is_partial) {
1924-
throw std::runtime_error(ex.what());
1924+
builder.clear_tools();
1925+
builder.move_to(0);
1926+
common_chat_parse_content_only(builder);
19251927
}
19261928
}
19271929
auto msg = builder.result();

0 commit comments

Comments
 (0)