@@ -44,8 +44,8 @@ class llm_task {
4444 std::string response_format_;
4545 std::vector<std::string> inputs_;
4646 std::vector<unsigned short > prompt_data_;
47- std::vector<unsigned char > image_data_ ;
48- std::vector<unsigned short > img_embed ;
47+ std::vector<std::vector< unsigned char >> image_datas_ ;
48+ std::vector<std::vector< unsigned short >> img_embeds ;
4949 std::string prompt_;
5050 task_callback_t out_callback_;
5151 bool enoutput_;
@@ -125,6 +125,8 @@ class llm_task {
125125 CONFIG_AUTO_SET (file_body[" mode_param" ], b_use_mmap_load_embed);
126126 CONFIG_AUTO_SET (file_body[" mode_param" ], b_dynamic_load_axmodel_layer);
127127 CONFIG_AUTO_SET (file_body[" mode_param" ], max_token_len);
128+ CONFIG_AUTO_SET (file_body[" mode_param" ], temperature);
129+ CONFIG_AUTO_SET (file_body[" mode_param" ], top_p);
128130
129131 if (mode_config_.filename_tokenizer_model .find (" http:" ) != std::string::npos) {
130132 std::string tokenizer_file;
@@ -171,7 +173,11 @@ class llm_task {
171173 }
172174 };
173175 lLaMa_ = std::make_unique<LLM>();
174- if (!lLaMa_->Init (mode_config_)) return -2 ;
176+ if (!lLaMa_->Init (mode_config_)) {
177+ lLaMa_->Deinit ();
178+ lLaMa_.reset ();
179+ return -2 ;
180+ }
175181
176182 } catch (...) {
177183 SLOGE (" config false" );
@@ -209,18 +215,25 @@ class llm_task {
209215 void inference (const std::string &msg)
210216 {
211217 try {
212- if (image_data_ .empty ()) {
218+ if (image_datas_ .empty ()) {
213219 lLaMa_->Encode (prompt_data_, prompt_complete (msg));
214220 std::string out = lLaMa_->Run (prompt_data_);
215221 if (out_callback_) out_callback_ (out, true );
216222 } else {
217- cv::Mat src = cv::imdecode (image_data_, cv::IMREAD_COLOR);
218- if (src.empty ()) return ;
219- image_data_.clear ();
220- lLaMa_->Encode (src, img_embed);
221- lLaMa_->Encode (img_embed, prompt_data_, prompt_complete (msg));
222- std::string out = lLaMa_->Run (prompt_data_);
223- if (out_callback_) out_callback_ (out, true );
223+ img_embeds.clear ();
224+ for (auto &img_data : image_datas_) {
225+ cv::Mat src = cv::imdecode (img_data, cv::IMREAD_COLOR);
226+ if (src.empty ()) continue ;
227+ std::vector<unsigned short > embed;
228+ lLaMa_->Encode (src, embed);
229+ img_embeds.push_back (embed);
230+ }
231+ image_datas_.clear ();
232+ if (!img_embeds.empty ()) {
233+ lLaMa_->Encode (img_embeds, prompt_data_, prompt_complete (msg));
234+ std::string out = lLaMa_->Run (prompt_data_);
235+ if (out_callback_) out_callback_ (out, true );
236+ }
224237 }
225238 } catch (...) {
226239 SLOGW (" lLaMa_->Run have error!" );
@@ -293,6 +306,33 @@ class llm_llm : public StackFlow {
293306 }
294307 }
295308
309+ void task_pause (const std::weak_ptr<llm_task> llm_task_obj_weak,
310+ const std::weak_ptr<llm_channel_obj> llm_channel_weak)
311+ {
312+ auto llm_task_obj = llm_task_obj_weak.lock ();
313+ auto llm_channel = llm_channel_weak.lock ();
314+ if (!(llm_task_obj && llm_channel)) {
315+ return ;
316+ }
317+ llm_task_obj->lLaMa_ ->Stop ();
318+ }
319+
320+ void pause (const std::string &work_id, const std::string &object, const std::string &data) override
321+ {
322+ SLOGI (" llm_asr::work:%s" , data.c_str ());
323+
324+ nlohmann::json error_body;
325+ int work_id_num = sample_get_work_id_num (work_id);
326+ if (llm_task_.find (work_id_num) == llm_task_.end ()) {
327+ error_body[" code" ] = -6 ;
328+ error_body[" message" ] = " Unit Does Not Exist" ;
329+ send (" None" , " None" , error_body, work_id);
330+ return ;
331+ }
332+ task_pause (llm_task_[work_id_num], get_channel (work_id_num));
333+ send (" None" , " None" , LLM_NO_ERROR, work_id);
334+ }
335+
296336 void task_user_data (const std::weak_ptr<llm_task> llm_task_obj_weak,
297337 const std::weak_ptr<llm_channel_obj> llm_channel_weak, const std::string &object,
298338 const std::string &data)
@@ -336,7 +376,7 @@ class llm_llm : public StackFlow {
336376 next_data = &tmp_msg2;
337377 }
338378 if (object.find (" jpeg" ) != std::string::npos) {
339- llm_task_obj->image_data_ . assign (next_data->begin (), next_data->end ());
379+ llm_task_obj->image_datas_ . emplace_back (next_data->begin (), next_data->end ());
340380 return ;
341381 }
342382 llm_task_obj->inference ((*next_data));
0 commit comments