From 94377d743c27b10f75d4556e1ed2933b69f6f80f Mon Sep 17 00:00:00 2001 From: ochafik Date: Thu, 26 Sep 2024 03:42:36 +0100 Subject: [PATCH] `server`: catch errors in format_final_response_oaicompat instead of taking server down --- examples/server/server.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index aea498f96..10fec4174 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -2879,8 +2879,12 @@ int main(int argc, char ** argv) { if (!stream) { ctx_server.receive_cmpl_results(task_ids, [&](const std::vector & results) { // multitask is never support in chat completion, there is only one result - json result_oai = format_final_response_oaicompat(data, results[0].data, completion_id, /*.streaming =*/ false, verbose); - res_ok(res, result_oai); + try { + json result_oai = format_final_response_oaicompat(data, results[0].data, completion_id, /*.streaming =*/ false, verbose); + res_ok(res, result_oai); + } catch (const std::runtime_error & e) { + res_error(res, format_error_response(e.what(), ERROR_TYPE_SERVER)); + } }, [&](const json & error_data) { res_error(res, error_data); });