From 79f634a19d1c32a6cfb1befc21551ee684fced6b Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Sat, 1 Jul 2023 18:46:00 +0300 Subject: [PATCH] embd-input : fix returning ptr to temporary --- examples/embd-input/embd-input-lib.cpp | 9 ++++++--- examples/embd-input/embd-input.h | 4 +--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/embd-input/embd-input-lib.cpp b/examples/embd-input/embd-input-lib.cpp index 37de52a..570e273 100644 --- a/examples/embd-input/embd-input-lib.cpp +++ b/examples/embd-input/embd-input-lib.cpp @@ -210,9 +210,12 @@ llama_token sampling_id(struct MyModel* mymodel) { const char * sampling(struct MyModel * mymodel) { llama_context * ctx = mymodel->ctx; int id = sampling_id(mymodel); - std::string ret; - if (id == llama_token_eos()) ret = ""; - else ret = llama_token_to_str(ctx, id); + static std::string ret; + if (id == llama_token_eos()) { + ret = ""; + } else { + ret = llama_token_to_str(ctx, id); + } eval_id(mymodel, id); return ret.c_str(); } diff --git a/examples/embd-input/embd-input.h b/examples/embd-input/embd-input.h index 4fefabd..efb5ba5 100644 --- a/examples/embd-input/embd-input.h +++ b/examples/embd-input/embd-input.h @@ -5,7 +5,6 @@ #include "llama.h" #include "build-info.h" - extern "C" { typedef struct MyModel { @@ -14,14 +13,13 @@ typedef struct MyModel { int n_past = 0; } MyModel; - struct MyModel* create_mymodel(int argc, char ** argv); bool eval_float(void* model, float* input, int N); bool eval_tokens(void* model, std::vector tokens); bool eval_id(struct MyModel* mymodel, int id); bool eval_string(struct MyModel* mymodel, const char* str); -const char* sampling(struct MyModel* mymodel); +const char * sampling(struct MyModel* mymodel); llama_token sampling_id(struct MyModel* mymodel); void free_mymodel(struct MyModel* mymodel);