MCPcopy Create free account
hub / github.com/THESIS-AGENT/AIRouter / select_the_best_fromAbatch

Method select_the_best_fromAbatch

LoadBalancing.py:527–653  ·  view source on GitHub ↗

从一批模型中选择帕累托最优的模型 Args: model_list (list): 模型名称列表 mode (str): 选择模式,"fast_first"或"cheap_first" input_proportion (int): 输入比例 output_proportion (int): 输出比例 Returns: tuple: (最优模型名, 源名称, 源模型名, API密钥)

(self, model_list, mode="fast_first", input_proportion=60, output_proportion=40)

Source from the content-addressed store, hash-verified

525 return main_source_name, main_source_model_name, main_api_key, backup_source_name, backup_source_model_name, backup_api_key
526
527 def select_the_best_fromAbatch(self, model_list, mode="fast_first", input_proportion=60, output_proportion=40):
528 """从一批模型中选择帕累托最优的模型
529
530 Args:
531 model_list (list): 模型名称列表
532 mode (str): 选择模式,"fast_first""cheap_first"
533 input_proportion (int): 输入比例
534 output_proportion (int): 输出比例
535
536 Returns:
537 tuple: (最优模型名, 源名称, 源模型名, API密钥)
538
539 Raises:
540 ValueError: 如果没有可用的模型
541 """
542 self.logger.info(f"从模型列表中选择最优模型: {model_list}")
543
544 # 刷新健康数据
545 if (self.healthy and "timestamp" in self.healthy and
546 (datetime.now() - datetime.fromisoformat(self.healthy["timestamp"])).total_seconds() > self.healthy.get("check_timer_span", 15)*60):
547 self.logger.info("健康检查数据已过期,正在刷新")
548 self.healthy = Harness_localAPI.check_healthy()
549
550 # 收集每个模型的性能数据
551 model_stats = {}
552
553 for model_name in model_list:
554 # 收集该模型在所有源上的数据
555 for key, value in self.healthy["data"].items():
556 if len(key) >= 2 and key[1] == model_name and value:
557 source_name = key[0]
558
559 # 检查模型在该源上是否有效
560 if not self._check_valid_model(source_name, model_name):
561 continue
562
563 # 计算平均响应时间和成功率
564 valid_times = [t for t in value if t is not None and not np.isnan(t)]
565 if valid_times:
566 avg_time = np.mean(valid_times)
567 success_rate = len(valid_times) / len(value)
568
569 # 获取价格信息
570 source_model_name = self._get_actual_model_name(source_name, model_name)
571 price = 1e8 # 默认高价格
572
573 if source_name in self.source_price and source_model_name in self.source_price[source_name]:
574 price_info = self.source_price[source_name][source_model_name]
575 if price_info is not None:
576 if isinstance(price_info, tuple) and None not in price_info:
577 price = (price_info[0]*input_proportion + price_info[1]*output_proportion)/(input_proportion+output_proportion)
578 elif isinstance(price_info, float):
579 price = price_info
580
581 # 存储统计信息
582 key_str = f"{model_name}|{source_name}"
583 model_stats[key_str] = {
584 'model_name': model_name,

Callers 3

generate_fromTHEbestMethod · 0.95

Calls 5

_check_valid_modelMethod · 0.95
check_healthyMethod · 0.80
get_api_keyMethod · 0.45

Tested by

no test coverage detected