{"repo": "charrrrls/ai-scripts", "files": {"ai_config.py": {"d63c57663165b3af540fa7b5a2bc556644b6eaad": {"t": 1785674168.6186538, "n": 9385, "h": "4045b48e490442f7", "c": "#!/usr/bin/env python3\n\"\"\"\nAI配置管理模块 - by 阮阮\n统一管理所有AI相关配置,支持多环境配置\n\"\"\"\n\nimport os\nimport json\nfrom typing import Dict, Any, Optional\nfrom enum import Enum\n\n\nclass Environment(Enum):\n \"\"\"环境类型枚举\"\"\"\n DEVELOPMENT = \"development\"\n PRODUCTION = \"production\"\n TESTING = \"testing\"\n\n\nclass AIProvider(Enum):\n \"\"\"AI服务提供商枚举\"\"\"\n SILICONFLOW = \"siliconflow\"\n OPENAI = \"openai\"\n ANTHROPIC = \"anthropic\"\n QWEN = \"qwen\"\n DASHSCOPE = \"dashscope\"\n\n\nclass AIConfig:\n \"\"\"AI配置管理类\"\"\"\n \n def __init__(self, env: Environment = Environment.PRODUCTION):\n self.env = env\n self._config = self._load_config()\n \n def _load_config(self) -> Dict[str, Any]:\n \"\"\"加载配置\"\"\"\n # 基础配置\n base_config = {\n \"provider\": AIProvider.SILICONFLOW.value,\n \"api_key\": \"sk-luicbwpmghinywzxkumosqyvurpohzgycdmumowkhlwleuwp\",\n \"api_url\": \"https://api.siliconflow.cn/v1/chat/completions\",\n \"model_name\": \"THUDM/GLM-4-32B-0414\",\n \"timeout\": 60,\n \"max_retries\": 3,\n \"retry_delay\": 2, # 秒\n \"max_tokens\": 4096,\n \"temperature\": 0.7,\n \"stream\": False, # 默认批量模式\n\n # 流式模式配置\n \"enable_streaming\": True, # 是否支持流式模式\n \"stream_chunk_size\": 1, # 流式输出块大小\n \"stream_delay\": 0.005, # 流式输出延迟(秒)\n \"show_thinking\": True, # 是否显示思考状态\n\n # 场景特定配置\n \"scenarios\": {\n \"chat\": {\n \"stream\": True, # 对话使用流式模式\n \"temperature\": 0.8, # 对话更有创造性\n \"show_thinking\": True\n },\n \"commit\": {\n \"stream\": False, # commit信息使用批量模式\n \"temperature\": 0.3, # commit信息更稳定\n \"max_tokens\": 100,\n \"show_thinking\": False\n },\n \"blog\": {\n \"stream\": False, # 博客生成使用批量模式\n \"temperature\": 0.7,\n \"max_tokens\": 3000,\n \"show_thinking\": False\n },\n \"qwen\": {\n \"stream\": True, # 千问支持流式\n \"temperature\": 0.7,\n \"max_tokens\": 4096,\n \"show_thinking\": False\n },\n \"vector_query\": {\n \"stream\": False, # 向量查询使用批量模式\n \"temperature\": 0.5, # 查询更精确\n \"max_tokens\": 2000,\n \"show_thinking\": False\n },\n \"vision\": {\n \"stream\": True, # 视觉分析使用流式模式\n \"temperature\": 0.7, # 视觉描述需要一定创造性\n \"max_tokens\": 2000,\n \"model_name\": \"Qwen/Qwen2.5-VL-72B-Instruct\", # 视觉模型\n \"show_thinking\": False\n }\n },\n \n # 千问配置\n \"qwen_config\": {\n \"api_key\": \"\", # 需要配置\n \"api_url\": \"https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation\",\n \"model_name\": \"qwen-turbo\",\n \"workspace_id\": \"\" # 可选\n },\n \n # 向量库配置\n \"vector_config\": {\n \"enabled\": True,\n \"db_path\": \"./vector_db\",\n \"embedding_model\": \"text-embedding-v1\",\n \"chunk_size\": 1000,\n \"chunk_overlap\": 100,\n \"similarity_threshold\": 0.7\n }\n }\n \n # 环境特定配置\n env_configs = {\n Environment.DEVELOPMENT: {\n \"timeout\": 30,\n \"max_retries\": 2,\n \"temperature\": 0.8,\n \"debug\": True\n },\n Environment.PRODUCTION: {\n \"timeout\": 60,\n \"max_retries\": 3,\n \"temperature\": 0.7,\n \"debug\": False\n },\n Environment.TESTING: {\n \"timeout\": 15,\n \"max_retries\": 1,\n \"temperature\": 0.5,\n \"debug\": True,\n \"model_name\": \"THUDM/GLM-4-9B-Chat\" # 测试用较小模型\n }\n }\n \n # 合并配置\n config = base_config.copy()\n if self.env in env_configs:\n config.update(env_configs[self.env])\n \n # 从环境变量覆盖配置\n self._load_from_env(config)\n \n # 从配置文件覆盖配置\n self._load_from_file(config)\n \n return config\n \n def _load_from_env(self, config: Dict[str, Any]) -> None:\n \"\"\"从环境变量加载配置\"\"\"\n env_mappings = {\n \"AI_API_KEY\": \"api_key\",\n \"AI_API_URL\": \"api_url\", \n \"AI_MODEL_NAME\": \"model_name\",\n \"AI_TIMEOUT\": \"timeout\",\n \"AI_MAX_RETRIES\": \"max_retries\",\n \"AI_TEMPERATURE\": \"temperature\"\n }\n \n for env_var, config_key in env_mappings.items():\n value = os.getenv(env_var)\n if value:\n # 类型转换\n if config_key in [\"timeout\", \"max_retries\"]:\n config[config_key] = int(value)\n elif config_key in [\"temperature\"]:\n config[config_key] = float(value)\n else:\n config[config_key] = value\n \n def _load_from_file(self, config: Dict[str, Any]) -> None:\n \"\"\"从配置文件加载配置\"\"\"\n config_file = f\"ai_config_{self.env.value}.json\"\n if os.path.exists(config_file):\n try:\n with open(config_file, 'r', encoding='utf-8') as f:\n file_config = json.load(f)\n config.update(file_config)\n except Exception:\n pass # 忽略配置文件错误\n \n @property\n def api_key(self) -> str:\n \"\"\"API密钥\"\"\"\n return self._config[\"api_key\"]\n \n @property\n def api_url(self) -> str:\n \"\"\"API URL\"\"\"\n return self._config[\"api_url\"]\n \n @property\n def model_name(self) -> str:\n \"\"\"模型名称\"\"\"\n return self._config[\"model_name\"]\n \n @property\n def timeout(self) -> int:\n \"\"\"超时时间(秒)\"\"\"\n return self._config[\"timeout\"]\n \n @property\n def max_retries(self) -> int:\n \"\"\"最大重试次数\"\"\"\n return self._config[\"max_retries\"]\n \n @property\n def retry_delay(self) -> int:\n \"\"\"重试延迟(秒)\"\"\"\n return self._config[\"retry_delay\"]\n \n @property\n def max_tokens(self) -> int:\n \"\"\"最大token数\"\"\"\n return self._config[\"max_tokens\"]\n \n @property\n def temperature(self) -> float:\n \"\"\"温度参数\"\"\"\n return self._config[\"temperature\"]\n \n @property\n def stream(self) -> bool:\n \"\"\"是否流式输出\"\"\"\n return self._config[\"stream\"]\n \n @property\n def debug(self) -> bool:\n \"\"\"是否调试模式\"\"\"\n return self._config.get(\"debug\", False)\n \n @property\n def provider(self) -> str:\n \"\"\"服务提供商\"\"\"\n return self._config[\"provider\"]\n \n def get(self, key: str, default: Any = None) -> Any:\n \"\"\"获取配置项\"\"\"\n return self._config.get(key, default)\n \n def update(self, **kwargs) -> None:\n \"\"\"更新配置\"\"\"\n self._config.update(kwargs)\n \n def to_dict(self) -> Dict[str, Any]:\n \"\"\"转换为字典\"\"\"\n return self._config.copy()\n\n def get_scenario_config(self, scenario: str) -> Dict[str, Any]:\n \"\"\"获取特定场景的配置\"\"\"\n scenarios = self._config.get(\"scenarios\", {})\n scenario_config = scenarios.get(scenario, {})\n\n # 合并基础配置和场景配置\n merged_config = self._config.copy()\n merged_config.update(scenario_config)\n\n return merged_config\n\n def is_streaming_enabled(self) -> bool:\n \"\"\"检查是否启用流式模式\"\"\"\n return self._config.get(\"enable_streaming\", False)\n\n def should_show_thinking(self, scenario: str = None) -> bool:\n \"\"\"检查是否应该显示思考状态\"\"\"\n if scenario:\n scenario_config = self.get_scenario_config(scenario)\n return scenario_config.get(\"show_thinking\", False)\n return self._config.get(\"show_thinking\", False)\n\n @property\n def stream_delay(self) -> float:\n \"\"\"流式输出延迟\"\"\"\n return self._config.get(\"stream_delay\", 0.02)\n\n @property\n def stream_chunk_size(self) -> int:\n \"\"\"流式输出块大小\"\"\"\n return self._config.get(\"stream_chunk_size\", 1)\n\n\n# 全局配置实例\n_global_config: Optional[AIConfig] = None\n\n\ndef get_config(env: Environment = Environment.PRODUCTION) -> AIConfig:\n \"\"\"获取全局配置实例\"\"\"\n global _global_config\n if _global_config is None or _global_config.env != env:\n _global_config = AIConfig(env)\n return _global_config\n\n\ndef set_environment(env: Environment) -> None:\n \"\"\"设置环境\"\"\"\n global _global_config\n _global_config = AIConfig(env)\n\n\n# 便捷函数\ndef get_api_key() -> str:\n \"\"\"获取API密钥\"\"\"\n return get_config().api_key\n\n\ndef get_model_name() -> str:\n \"\"\"获取模型名称\"\"\"\n return get_config().model_name\n\n\ndef get_api_url() -> str:\n \"\"\"获取API URL\"\"\"\n return get_config().api_url\n\n\nif __name__ == \"__main__\":\n # 测试配置\n config = get_config()\n print(\"AI配置信息:\")\n print(f\"环境: {config.env.value}\")\n print(f\"提供商: {config.provider}\")\n print(f\"模型: {config.model_name}\")\n print(f\"API URL: {config.api_url}\")\n print(f\"超时: {config.timeout}秒\")\n print(f\"重试: {config.max_retries}次\")\n print(f\"调试: {config.debug}\")\n"}}}}