From 88ed688df522c60e67e1b3760854f7308a1745af Mon Sep 17 00:00:00 2001 From: ZHEQIUSHUI Date: Wed, 24 Apr 2024 15:03:45 +0800 Subject: [PATCH] ui good --- qtproj/LLM-Chat/main.cpp | 6 ++++ qtproj/LLM-Chat/mainwindow.cpp | 59 ++++++++++++++++++++++++++++++++-- qtproj/LLM-Chat/mainwindow.h | 9 ++++++ qtproj/LLM-Chat/mainwindow.ui | 45 +++++++++++++++++++++++++- src/runner/LLM.hpp | 5 +++ 5 files changed, 120 insertions(+), 4 deletions(-) diff --git a/qtproj/LLM-Chat/main.cpp b/qtproj/LLM-Chat/main.cpp index c4dd5d6..709d7cd 100644 --- a/qtproj/LLM-Chat/main.cpp +++ b/qtproj/LLM-Chat/main.cpp @@ -14,6 +14,12 @@ int main(int argc, char *argv[]) { LLMAttrType attr; + attr.template_filename_axmodel = "tinyllama-bf16/tinyllama_l%d.axmodel"; + attr.filename_post_axmodel = "tinyllama-bf16/tinyllama_post.axmodel"; + attr.filename_tokenizer_model = "tokenizer.model"; + attr.filename_tokens_embed = "tinyllama-bf16/tinyllama.model.embed_tokens.weight.bfloat16.bin"; + + cmdline::parser cmd; cmd.add("template_filename_axmodel", 0, "axmodel path template", false, attr.template_filename_axmodel); cmd.add("filename_post_axmodel", 0, "post axmodel path", false, attr.filename_post_axmodel); diff --git a/qtproj/LLM-Chat/mainwindow.cpp b/qtproj/LLM-Chat/mainwindow.cpp index 6c50245..569ae24 100644 --- a/qtproj/LLM-Chat/mainwindow.cpp +++ b/qtproj/LLM-Chat/mainwindow.cpp @@ -21,6 +21,29 @@ void thread_llm_runing(LLM *p_llm, QString *p_str, MainWindow *p_win) ALOGI("thread_llm_runing end"); } +std::string prompt_complete(std::string prompt, TokenizerType tokenizer_type) +{ + std::ostringstream oss_prompt; + switch (tokenizer_type) + { + case TKT_LLaMa: + oss_prompt << "<|user|>\n" + << prompt << "<|assistant|>\n"; + break; + case TKT_Qwen: + oss_prompt << "<|im_start|>system\nYou are a helpful assistant.<|im_end|>"; + oss_prompt << "\n<|im_start|>user\n" + << prompt << "<|im_end|>\n<|im_start|>assistant\n"; + break; + case TKT_HTTP: + default: + oss_prompt << prompt; + break; + } + + return oss_prompt.str(); +} + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -28,6 +51,9 @@ MainWindow::MainWindow(QWidget *parent) m_vBoxLayout = new QVBoxLayout(); ui->widget->setLayout(m_vBoxLayout); m_vBoxLayout->addSpacing(1080); + + connect(this,&MainWindow::sig_llm_output,this,&MainWindow::on_llm_output); + connect(this,&MainWindow::sig_enable_controls,this,&MainWindow::on_enable_controls); } MainWindow::~MainWindow() @@ -43,12 +69,25 @@ bool MainWindow::InitLLM(LLMAttrType attr) void MainWindow::append_textedit(const char *p_str) { - m_textedit_output_vec.back()->append(p_str); - printf("%s", p_str); + emit sig_llm_output(QString(p_str)); +} + +void MainWindow::on_llm_output(QString str) +{ + auto src = m_textedit_output_vec.back()->toPlainText();//str); + src.append(str); + m_textedit_output_vec.back()->setText(src); + printf("%s", str.toStdString().c_str()); fflush(stdout); } + void MainWindow::enable_controls(bool b_enable) +{ + emit sig_enable_controls(b_enable); +} + +void MainWindow::on_enable_controls(bool b_enable) { ui->btn_ask->setEnabled(b_enable); ui->txt_msg->setEnabled(b_enable); @@ -81,6 +120,10 @@ QTextEdit *create_textedit(QString rgb_color = "220,127,125") void MainWindow::on_btn_ask_clicked() { + if(ui->txt_msg->text().trimmed().isEmpty()) + { + return; + } auto txtedit_input = create_textedit(); QHBoxLayout *hboxlayout = new QHBoxLayout(); @@ -103,8 +146,12 @@ void MainWindow::on_btn_ask_clicked() txtedit_input->setText(ui->txt_msg->text()); + auto prompt = prompt_complete(ui->txt_msg->text().toStdString(),m_llm.getAttr()->tokenizer_type); + printf("%s\n",prompt.c_str()); + fflush(stdout); + // 开线程run - QString *str_msg = new QString(ui->txt_msg->text()); + QString *str_msg = new QString(prompt.c_str()); std::thread t(thread_llm_runing, &m_llm, str_msg, this); t.detach(); ui->txt_msg->clear(); @@ -128,3 +175,9 @@ void MainWindow::on_txt_msg_returnPressed() { on_btn_ask_clicked(); } + +void MainWindow::on_btn_stop_clicked() +{ + m_llm.Stop(); +} + diff --git a/qtproj/LLM-Chat/mainwindow.h b/qtproj/LLM-Chat/mainwindow.h index ad92464..b9766ac 100644 --- a/qtproj/LLM-Chat/mainwindow.h +++ b/qtproj/LLM-Chat/mainwindow.h @@ -17,6 +17,9 @@ class MainWindow : public QMainWindow { Q_OBJECT +signals: + void sig_llm_output(QString str); + void sig_enable_controls(bool enable); public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); @@ -27,12 +30,18 @@ class MainWindow : public QMainWindow void enable_controls(bool b_enable); +private slots: + void on_btn_stop_clicked(); + private slots: void on_btn_ask_clicked(); void resizeEvent(QResizeEvent *event) override; void on_txt_msg_returnPressed(); + void on_llm_output(QString str); + void on_enable_controls(bool b_enable); + private: Ui::MainWindow *ui; QVBoxLayout *m_vBoxLayout; diff --git a/qtproj/LLM-Chat/mainwindow.ui b/qtproj/LLM-Chat/mainwindow.ui index 209dcf6..dd784f9 100644 --- a/qtproj/LLM-Chat/mainwindow.ui +++ b/qtproj/LLM-Chat/mainwindow.ui @@ -40,7 +40,7 @@ - + @@ -87,6 +87,7 @@ background-color: rgb(99, 122, 125); border: 1px solid #dcdfe6; padding: 10px; border-radius: 20px; + background-color: rgb(87, 227, 137); } QPushButton:hover { @@ -104,6 +105,48 @@ QPushButton:pressed, QPushButton:checked { + + + + + 100 + 60 + + + + + 150 + 16777215 + + + + + DejaVu Sans + 14 + + + + QPushButton { + border: 1px solid #dcdfe6; + padding: 10px; + border-radius: 20px; + background-color: rgb(246, 97, 81); +} + +QPushButton:hover { + background-color: rgb(224, 27, 36); +} + +QPushButton:pressed, QPushButton:checked { + border: 1px solid #3a8ee6; + background-color: rgb(192, 28, 40); +} + + + Stop + + + diff --git a/src/runner/LLM.hpp b/src/runner/LLM.hpp index 282b8e7..f5e6668 100644 --- a/src/runner/LLM.hpp +++ b/src/runner/LLM.hpp @@ -200,6 +200,11 @@ class LLM return true; } + LLMAttrType* getAttr() + { + return &_attr; + } + void Deinit() { for (int i = 0; i < _attr.axmodel_num; i++)