维护两套策略, 一套是以便宜为导向, 一套是以时间最少为导向的. Args: model_name (str): 模型名称 mode (str): 模式,可以是"cheap_first"或"fast_first" input_proportion (int): 输入比例 output_proportion (int): 输出比例 Returns: tuple: 包含主源和备用源的配置信
(self, model_name, mode, input_proportion: int, output_proportion: int)
| 320 | return self.source_mapping[source_name][base_model_name] |
| 321 | |
| 322 | def get_config(self, model_name, mode, input_proportion: int, output_proportion: int): |
| 323 | """维护两套策略, 一套是以便宜为导向, |
| 324 | 一套是以时间最少为导向的. |
| 325 | |
| 326 | Args: |
| 327 | model_name (str): 模型名称 |
| 328 | mode (str): 模式,可以是"cheap_first"或"fast_first" |
| 329 | input_proportion (int): 输入比例 |
| 330 | output_proportion (int): 输出比例 |
| 331 | |
| 332 | Returns: |
| 333 | tuple: 包含主源和备用源的配置信息 |
| 334 | |
| 335 | Raises: |
| 336 | ValueError: 如果找不到可用的源或配置 |
| 337 | Exception: 其他错误 |
| 338 | """ |
| 339 | self.logger.info(f"获取模型 {model_name} 的配置,模式: {mode}, 输入比例: {input_proportion}, 输出比例: {output_proportion}") |
| 340 | |
| 341 | # 无论以什么为导向, 最大概率能跑通是最重要的. |
| 342 | # 因为跑不通大概不是发不过去, 而是response收不回来. |
| 343 | # pre-filling也是照常收费的, 与其二遍返工, 还不如一次做好. |
| 344 | # 因此无论如何, 都先用成功率作为筛选. |
| 345 | # |
| 346 | # 如果当前实例初始化的时候距离当前尝试获取配置的时间已过去超过15min了. |
| 347 | # 那么就自动刷新当前健康检查状态. |
| 348 | |
| 349 | # ================ 状态维护与入参校验 ================ |
| 350 | |
| 351 | # 修复bug: 使用total_seconds()而不是seconds |
| 352 | # 同时检查timestamp字段是否存在 |
| 353 | if (self.healthy and "timestamp" in self.healthy and |
| 354 | (datetime.now() - datetime.fromisoformat(self.healthy["timestamp"])).total_seconds() > self.healthy.get("check_timer_span", 15)*60): |
| 355 | self.logger.info("健康检查数据已过期,正在刷新") |
| 356 | self.healthy = Harness_localAPI.check_healthy() |
| 357 | |
| 358 | # 如果健康数据为空,直接使用预设排名 |
| 359 | if self.is_health_data_empty(model_name): |
| 360 | self.logger.info(f"健康数据为空,使用预设排名选择模型 {model_name} 的源") |
| 361 | return self.get_sources_from_ranking(model_name) |
| 362 | |
| 363 | # 验证模式参数 |
| 364 | if mode not in ["cheap_first", "fast_first"]: |
| 365 | self.logger.warning(f"未知的模式 '{mode}',使用默认模式 'cheap_first'") |
| 366 | mode = "cheap_first" |
| 367 | |
| 368 | # 验证比例参数 - 修复:只需要比例关系正确即可 |
| 369 | if input_proportion == 0 and output_proportion == 0: |
| 370 | self.logger.warning(f"输入输出比例都为0,使用默认值 50, 50") |
| 371 | input_proportion = 50 |
| 372 | output_proportion = 50 |
| 373 | |
| 374 | # 处理多模态的情况, 多模态完全默认为等同于常规模型. |
| 375 | # 只是常规模型的另外一种传参模式. |
| 376 | real_model_name = model_name |
| 377 | # 不再转换为base_model_name,直接使用完整的model_name |
| 378 | |
| 379 | span_mean_list = [] |