@@ -147,7 +147,22 @@ class Handler {
147147 return true ;
148148 }
149149
150+ bool process (const MMInputData& msg, MMInputItem& input) {
151+ if (!this ->load (msg, input)) {
152+ LOG (ERROR) << " load mm data failed" ;
153+ return false ;
154+ }
155+
156+ if (!this ->decode (input)) {
157+ LOG (ERROR) << " decode mm data failed" ;
158+ return false ;
159+ }
160+
161+ return true ;
162+ }
163+
150164 virtual bool load (const proto::MMInputData& msg, MMInputItem& input) = 0;
165+ virtual bool load (const MMInputData& msg, MMInputItem& input) = 0;
151166 virtual bool decode (MMInputItem& input) = 0;
152167
153168 protected:
@@ -197,6 +212,23 @@ class ImageHandler : public Handler {
197212 }
198213 }
199214
215+ virtual bool load (const MMInputData& msg, MMInputItem& input) {
216+ input.clear ();
217+
218+ const auto & url = msg.image_url ;
219+ if (url.compare (0 , dataurl_prefix_.size (), dataurl_prefix_) ==
220+ 0 ) { // data url
221+
222+ input.type_ = MMType::IMAGE;
223+ return this ->load_from_dataurl (url, input.raw_data_ );
224+ } else if (url.compare (0 , httpurl_prefix_.size (), httpurl_prefix_) ==
225+ 0 ) { // http url
226+
227+ input.type_ = MMType::IMAGE;
228+ return this ->load_from_http (url, input.raw_data_ );
229+ }
230+ }
231+
200232 virtual bool decode (MMInputItem& input) {
201233 OpenCVImageDecoder decoder;
202234 return decoder.decode (input.raw_data_ , input.decode_data_ );
@@ -223,6 +255,18 @@ class MMHandlerSet {
223255 return handler->process (msg, input);
224256 }
225257
258+ bool process (const std::string& type,
259+ const MMInputData& msg,
260+ MMInputItem& input) {
261+ auto itor = handlers_.find (type);
262+ if (itor == handlers_.end ()) {
263+ return false ;
264+ }
265+
266+ auto & handler = itor->second ;
267+ return handler->process (msg, input);
268+ }
269+
226270 private:
227271 std::unordered_map<std::string, std::unique_ptr<Handler>> handlers_;
228272};
@@ -259,6 +303,32 @@ bool MMInputHelper::trans(const MMChatMessageVec& vec,
259303 return true ;
260304}
261305
306+ bool MMInputHelper::trans (const std::vector<MMChatMessage>& raw_input_data,
307+ std::vector<Message>& messages,
308+ MMInputItemVec& inputs) {
309+ messages.clear ();
310+ inputs.clear ();
311+ messages.reserve (raw_input_data.size ());
312+ inputs.reserve (raw_input_data.size ());
313+
314+ for (int idx = 0 ; idx < raw_input_data.size (); ++idx) {
315+ const auto & chat = raw_input_data[idx];
316+ const auto & role = chat.role ;
317+ const auto & content = chat.content ;
318+
319+ Message::MMContentVec mmc;
320+ MMInputItemVec ins;
321+ if (!this ->trans (content, mmc, ins)) {
322+ return false ;
323+ }
324+
325+ messages.emplace_back (role, mmc);
326+ inputs.insert (inputs.end (), ins.begin (), ins.end ());
327+ }
328+
329+ return true ;
330+ }
331+
262332bool MMInputHelper::trans (const MMInputDataVec& vec,
263333 Message::MMContentVec& mmc,
264334 MMInputItemVec& inputs) {
@@ -285,4 +355,30 @@ bool MMInputHelper::trans(const MMInputDataVec& vec,
285355 return true ;
286356}
287357
358+ bool MMInputHelper::trans (const std::vector<MMInputData>& vec,
359+ Message::MMContentVec& mmc,
360+ MMInputItemVec& inputs) {
361+ mmc.clear ();
362+ inputs.clear ();
363+
364+ for (int idx = 0 ; idx < vec.size (); ++idx) {
365+ const auto & item = vec[idx];
366+ const auto & type = item.type ;
367+
368+ if (type == " text" ) {
369+ mmc.emplace_back (type, item.text );
370+ } else {
371+ MMInputItem input;
372+ if (!mm_handlers_->process (type, item, input)) {
373+ return false ;
374+ }
375+
376+ mmc.emplace_back (type);
377+ inputs.emplace_back (input);
378+ }
379+ }
380+
381+ return true ;
382+ }
383+
288384} // namespace xllm
0 commit comments