|
| 1 | +from typing import Any, TypeVar |
| 2 | + |
| 3 | +from .config_base import ConfigBase |
| 4 | +from .model_configs import APIProvider, ModelInfo, ModelTaskConfig, OpenAICompatibleAuthType, TaskConfig |
| 5 | + |
| 6 | +T = TypeVar("T", bound=ConfigBase) |
| 7 | + |
| 8 | +DEFAULT_PROVIDER_TEMPLATES: list[dict[str, Any]] = [ |
| 9 | + { |
| 10 | + "name": "BaiLian", |
| 11 | + "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 12 | + "api_key": "your-api-key", |
| 13 | + "auth_type": OpenAICompatibleAuthType.BEARER.value, |
| 14 | + } |
| 15 | +] |
| 16 | + |
| 17 | +DEFAULT_TASK_CONFIG_TEMPLATES: dict[str, dict[str, Any]] = { |
| 18 | + "utils": { |
| 19 | + "model_list": ["qwen3.5-35b-a3b-nonthink"], |
| 20 | + "max_tokens": 4096, |
| 21 | + "temperature": 0.5, |
| 22 | + "slow_threshold": 15.0, |
| 23 | + "selection_strategy": "random", |
| 24 | + }, |
| 25 | + "replyer": { |
| 26 | + "model_list": ["ali-glm-5"], |
| 27 | + "max_tokens": 4096, |
| 28 | + "temperature": 1, |
| 29 | + "slow_threshold": 120.0, |
| 30 | + "selection_strategy": "random", |
| 31 | + }, |
| 32 | + "planner": { |
| 33 | + "model_list": ["qwen3.5-35b-a3b", "qwen3.5-122b-a10b", "qwen3.5-flash"], |
| 34 | + "max_tokens": 8000, |
| 35 | + "temperature": 0.7, |
| 36 | + "slow_threshold": 12.0, |
| 37 | + "selection_strategy": "random", |
| 38 | + }, |
| 39 | + "vlm": { |
| 40 | + "model_list": ["qwen3.5-flash"], |
| 41 | + "max_tokens": 512, |
| 42 | + "temperature": 0.3, |
| 43 | + "slow_threshold": 15.0, |
| 44 | + "selection_strategy": "random", |
| 45 | + }, |
| 46 | + "voice": { |
| 47 | + "model_list": [""], |
| 48 | + "max_tokens": 1024, |
| 49 | + "temperature": 0.3, |
| 50 | + "slow_threshold": 12.0, |
| 51 | + "selection_strategy": "random", |
| 52 | + }, |
| 53 | + "embedding": { |
| 54 | + "model_list": ["qwen3-embedding"], |
| 55 | + "max_tokens": 1024, |
| 56 | + "temperature": 0.3, |
| 57 | + "slow_threshold": 5.0, |
| 58 | + "selection_strategy": "random", |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +DEFAULT_MODEL_TEMPLATES: list[dict[str, Any]] = [ |
| 63 | + { |
| 64 | + "model_identifier": "glm-5", |
| 65 | + "name": "ali-glm-5", |
| 66 | + "api_provider": "BaiLian", |
| 67 | + "price_in": 3.0, |
| 68 | + "price_out": 14.0, |
| 69 | + "temperature": 1.0, |
| 70 | + "visual": False, |
| 71 | + "extra_params": {"enable_thinking": False}, |
| 72 | + }, |
| 73 | + { |
| 74 | + "model_identifier": "qwen3.5-122b-a10b", |
| 75 | + "name": "qwen3.5-122b-a10b", |
| 76 | + "api_provider": "BaiLian", |
| 77 | + "price_in": 0.8, |
| 78 | + "price_out": 6.4, |
| 79 | + "visual": True, |
| 80 | + "extra_params": {"enable_thinking": "false"}, |
| 81 | + }, |
| 82 | + { |
| 83 | + "model_identifier": "qwen3.5-35b-a3b", |
| 84 | + "name": "qwen3.5-35b-a3b", |
| 85 | + "api_provider": "BaiLian", |
| 86 | + "price_in": 0.4, |
| 87 | + "price_out": 3.2, |
| 88 | + "visual": True, |
| 89 | + "extra_params": {}, |
| 90 | + }, |
| 91 | + { |
| 92 | + "model_identifier": "qwen3.5-35b-a3b", |
| 93 | + "name": "qwen3.5-35b-a3b-nonthink", |
| 94 | + "api_provider": "BaiLian", |
| 95 | + "price_in": 0.4, |
| 96 | + "price_out": 3.2, |
| 97 | + "visual": True, |
| 98 | + "extra_params": {"enable_thinking": "false"}, |
| 99 | + }, |
| 100 | + { |
| 101 | + "model_identifier": "qwen3.5-flash", |
| 102 | + "name": "qwen3.5-flash", |
| 103 | + "api_provider": "BaiLian", |
| 104 | + "price_in": 0.2, |
| 105 | + "price_out": 2.0, |
| 106 | + "visual": True, |
| 107 | + "extra_params": {"enable_thinking": "false"}, |
| 108 | + }, |
| 109 | + { |
| 110 | + "model_identifier": "text-embedding-v4", |
| 111 | + "name": "qwen3-embedding", |
| 112 | + "api_provider": "BaiLian", |
| 113 | + "price_in": 0.5, |
| 114 | + "price_out": 0.5, |
| 115 | + "visual": False, |
| 116 | + "extra_params": {}, |
| 117 | + } |
| 118 | +] |
| 119 | + |
| 120 | + |
| 121 | +def build_default_model_templates() -> list[dict[str, Any]]: |
| 122 | + """筛选任务分配中实际用到的模型模板。""" |
| 123 | + |
| 124 | + used_model_names = { |
| 125 | + model_name |
| 126 | + for task_template in DEFAULT_TASK_CONFIG_TEMPLATES.values() |
| 127 | + for model_name in task_template["model_list"] |
| 128 | + } |
| 129 | + return [model_template for model_template in DEFAULT_MODEL_TEMPLATES if model_template["name"] in used_model_names] |
| 130 | + |
| 131 | + |
| 132 | +def create_default_model_config(config_class: type[T]) -> T: |
| 133 | + """根据预置模板创建可通过校验的默认模型配置。""" |
| 134 | + |
| 135 | + task_config_fields = {} |
| 136 | + for field_name, field_info in ModelTaskConfig.model_fields.items(): |
| 137 | + if field_info.annotation is not TaskConfig: |
| 138 | + continue |
| 139 | + |
| 140 | + task_template = DEFAULT_TASK_CONFIG_TEMPLATES.get(field_name, {}) |
| 141 | + task_config_fields[field_name] = TaskConfig(**task_template) |
| 142 | + |
| 143 | + return config_class( |
| 144 | + models=[ModelInfo(**model_template) for model_template in build_default_model_templates()], |
| 145 | + model_task_config=ModelTaskConfig(**task_config_fields), |
| 146 | + api_providers=[APIProvider(**provider_template) for provider_template in DEFAULT_PROVIDER_TEMPLATES], |
| 147 | + ) |
0 commit comments