From 8fe36ab3c212a81edf6e4bdae56405ccdc995fb1 Mon Sep 17 00:00:00 2001 From: Angel-HF <49835792+Pelochus@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:29:07 +0200 Subject: [PATCH] Modified example runtime --- rkllm-runtime/example/src/main.cpp | 45 +++++++++++++++++------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/rkllm-runtime/example/src/main.cpp b/rkllm-runtime/example/src/main.cpp index d2a9d37..a52fa8a 100644 --- a/rkllm-runtime/example/src/main.cpp +++ b/rkllm-runtime/example/src/main.cpp @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Modified by Pelochus + #include #include #include @@ -32,7 +34,7 @@ void exit_handler(int signal) if (llmHandle != nullptr) { { - cout << "程序即将退出" << endl; + cout << "Catched exit signal. Exiting..." << endl; LLMHandle _tmp = llmHandle; llmHandle = nullptr; rkllm_destroy(_tmp); @@ -43,16 +45,16 @@ void exit_handler(int signal) void callback(const char *text, void *userdata, LLMCallState state) { - if (state == LLM_RUN_FINISH) { printf("\n"); } else if (state == LLM_RUN_ERROR) { - printf("\\run error\n"); + printf("\\LLM run error\n"); } - else{ + else + { printf("%s", text); } } @@ -61,14 +63,14 @@ int main(int argc, char **argv) { if (argc != 2) { - printf("Usage:%s [rkllm_model_path]\n", argv[0]); + printf("Usage: %s [rkllm_model_path]\n", argv[0]); return -1; } + signal(SIGINT, exit_handler); string rkllm_model(argv[1]); printf("rkllm init start\n"); - //设置参数及初始化 RKLLMParam param = rkllm_createDefaultParam(); param.modelPath = rkllm_model.c_str(); param.target_platform = "rk3588"; @@ -76,35 +78,39 @@ int main(int argc, char **argv) param.top_k = 1; param.max_new_tokens = 256; param.max_context_len = 512; + rkllm_init(&llmHandle, param, callback); - printf("rkllm init success\n"); + printf("RKLLM init success!\n"); vector pre_input; - pre_input.push_back("把下面的现代文翻译成文言文:到了春风和煦,阳光明媚的时候,湖面平静,没有惊涛骇浪,天色湖光相连,一片碧绿,广阔无际;沙洲上的鸥鸟,时而飞翔,时而停歇,美丽的鱼游来游去,岸上与小洲上的花草,青翠欲滴。"); - pre_input.push_back("以咏梅为题目,帮我写一首古诗,要求包含梅花、白雪等元素。"); - pre_input.push_back("上联: 江边惯看千帆过"); - pre_input.push_back("把这句话翻译成中文:Knowledge can be acquired from many sources. These include books, teachers and practical experience, and each has its own advantages. The knowledge we gain from books and formal education enables us to learn about things that we have no opportunity to experience in daily life. We can also develop our analytical skills and learn how to view and interpret the world around us in different ways. Furthermore, we can learn from the past by reading books. In this way, we won't repeat the mistakes of others and can build on their achievements."); - pre_input.push_back("把这句话翻译成英文:RK3588是新一代高端处理器,具有高算力、低功耗、超强多媒体、丰富数据接口等特点"); - cout << "\n**********************可输入以下问题对应序号获取回答/或自定义输入********************\n" - << endl; + pre_input.push_back("Welcome to ezrkllm! This is an adaptation of Rockchip's rknn-llm repo (see github.com/airockchip/rknn-llm) for running LLMs on its SoCs' NPUs."); + pre_input.push_back("You are currently running the runtime for " + param.target_platform); + pre_input.push_back("More information here: https://github.com/Pelochus/ezrknpu"); + pre_input.push_back("Detailed information for devs here: https://github.com/Pelochus/ezrknn-llm"); + + cout << "\n************************ Pelochus' ezrkllm runtime **********************\n" << endl; + for (int i = 0; i < (int)pre_input.size(); i++) { cout << "[" << i << "] " << pre_input[i] << endl; } - cout << "\n*************************************************************************\n" - << endl; + + cout << "\n*************************************************************************\n" << endl; string text; while (true) { std::string input_str; printf("\n"); - printf("user: "); + printf("You: "); std::getline(std::cin, input_str); - if (input_str == "exit") + + if (input_str == "exit" || input_str == "quit") { + cout << "Quitting program..." << endl; break; } + for (int i = 0; i < (int)pre_input.size(); i++) { if (input_str == to_string(i)) @@ -113,8 +119,9 @@ int main(int argc, char **argv) cout << input_str << endl; } } + string text = PROMPT_TEXT_PREFIX + input_str + PROMPT_TEXT_POSTFIX; - printf("robot: "); + printf("LLM Model: "); rkllm_run(llmHandle, text.c_str(), NULL); }