@@ -324,6 +324,18 @@ void server_models::unload_lru() {
324324 }
325325}
326326
327+ static void add_or_replace_arg (std::vector<std::string> & args, const std::string & key, const std::string & value) {
328+ for (size_t i = 0 ; i < args.size (); i++) {
329+ if (args[i] == key && i + 1 < args.size ()) {
330+ args[i + 1 ] = value;
331+ return ;
332+ }
333+ }
334+ // not found, append
335+ args.push_back (key);
336+ args.push_back (value);
337+ }
338+
327339void server_models::load (const std::string & name, bool auto_load) {
328340 if (!has_model (name)) {
329341 throw std::runtime_error (" model name=" + name + " is not found" );
@@ -356,33 +368,23 @@ void server_models::load(const std::string & name, bool auto_load) {
356368
357369 std::vector<std::string> child_args;
358370 if (auto_load && !meta.args .empty ()) {
359- child_args = meta.args ; // reuse previous args
360- // update port arg
361- for (size_t i = 0 ; i < child_args.size (); i++) {
362- if (child_args[i] == " --port" && i + 1 < child_args.size ()) {
363- child_args[i + 1 ] = std::to_string (inst.meta .port );
364- break ;
365- }
366- }
371+ child_args = meta.args ; // copy previous args
367372 } else {
368373 child_args = base_args; // copy
369374 if (inst.meta .in_cache ) {
370- child_args.push_back (" -hf" );
371- child_args.push_back (inst.meta .name );
375+ add_or_replace_arg (child_args, " -hf" , inst.meta .name );
372376 } else {
373- child_args.push_back (" -m" );
374- child_args.push_back (inst.meta .path );
377+ add_or_replace_arg (child_args, " -m" , inst.meta .path );
375378 if (!inst.meta .path_mmproj .empty ()) {
376- child_args.push_back (" --mmproj" );
377- child_args.push_back (inst.meta .path_mmproj );
379+ add_or_replace_arg (child_args, " --mmproj" , inst.meta .path_mmproj );
378380 }
379381 }
380- child_args.push_back (" --alias" );
381- child_args.push_back (inst.meta .name );
382- child_args.push_back (" --port" );
383- child_args.push_back (std::to_string (inst.meta .port ));
384382 }
385383
384+ // set model args
385+ add_or_replace_arg (child_args, " --port" , std::to_string (inst.meta .port ));
386+ add_or_replace_arg (child_args, " --alias" , inst.meta .name );
387+
386388 std::vector<std::string> child_env = base_env; // copy
387389 child_env.push_back (" LLAMA_SERVER_ROUTER_PORT=" + std::to_string (base_params.port ));
388390
0 commit comments