From acb3a33d780be4883f340662d500fb8fd4aec5b5 Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Thu, 2 Nov 2023 16:29:30 -0700 Subject: [PATCH] fix: handle non utf-8 / utf-16 error --- crates/llama-cpp-bindings/src/engine.cc | 9 +++++++-- crates/llama-cpp-bindings/src/lib.rs | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/llama-cpp-bindings/src/engine.cc b/crates/llama-cpp-bindings/src/engine.cc index a314a6a9c665..5b48090e053f 100644 --- a/crates/llama-cpp-bindings/src/engine.cc +++ b/crates/llama-cpp-bindings/src/engine.cc @@ -246,9 +246,14 @@ class TextInferenceEngineImpl : public TextInferenceEngine { } if (request.multibyte_pending == 0) { - rust::String generated_text = is_eos ? "" : request.generated_text; - result.push_back({request.id, generated_text}); + rust::String generated_text; + try { + generated_text = is_eos ? "" : request.generated_text; + } catch (const std::invalid_argument& e) { + fprintf(stderr, "%s:%d [%s] - ignoring non utf-8/utf-16 output\n", __FILE__, __LINE__, __func__); + } + result.push_back({request.id, generated_text}); request.generated_text.clear(); } } diff --git a/crates/llama-cpp-bindings/src/lib.rs b/crates/llama-cpp-bindings/src/lib.rs index 5bd33d1e2d18..b2b2a32e81ac 100644 --- a/crates/llama-cpp-bindings/src/lib.rs +++ b/crates/llama-cpp-bindings/src/lib.rs @@ -76,7 +76,9 @@ impl AsyncTextInferenceEngine { let result = match engine.as_mut().unwrap().step() { Ok(result) => result, - Err(err) => panic!("Failed to step: {}", err), + Err(err) => { + panic!("Failed to step: {}", err) + } }; for ffi::StepOutput { request_id, text } in result {