From 4472b6e899dc6f32d54dbb6f0ef3a2998e416d4e Mon Sep 17 00:00:00 2001 From: zihanjian Date: Wed, 19 Nov 2025 20:35:14 +0800 Subject: [PATCH] refactor(config): consolidate api base urls handling --- .gitignore | 3 ++- app.py | 41 +++++++++++++++++++++++++++++++++++++++ config/api_base_urls.json | 8 -------- docs/beginner-tutorial.md | 4 +--- 4 files changed, 44 insertions(+), 12 deletions(-) delete mode 100644 config/api_base_urls.json diff --git a/.gitignore b/.gitignore index a391afd..5f789ad 100644 --- a/.gitignore +++ b/.gitignore @@ -127,10 +127,11 @@ Thumbs.db # Project specific config/update_info.json config/api_keys.json +config/api_base_urls.json .venv/ venv/ # uv .python-version pyproject.toml -uv.lock \ No newline at end of file +uv.lock diff --git a/app.py b/app.py index 3ed421b..ddd87ab 100644 --- a/app.py +++ b/app.py @@ -38,11 +38,52 @@ os.makedirs(CONFIG_DIR, exist_ok=True) # 密钥和其他配置文件路径 API_KEYS_FILE = os.path.join(CONFIG_DIR, 'api_keys.json') +API_BASE_URLS_FILE = os.path.join(CONFIG_DIR, 'api_base_urls.json') VERSION_FILE = os.path.join(CONFIG_DIR, 'version.json') UPDATE_INFO_FILE = os.path.join(CONFIG_DIR, 'update_info.json') PROMPT_FILE = os.path.join(CONFIG_DIR, 'prompts.json') # 新增提示词配置文件路径 PROXY_API_FILE = os.path.join(CONFIG_DIR, 'proxy_api.json') # 新增中转API配置文件路径 +DEFAULT_API_BASE_URLS = { + "AnthropicApiBaseUrl": "", + "OpenaiApiBaseUrl": "", + "DeepseekApiBaseUrl": "", + "AlibabaApiBaseUrl": "", + "GoogleApiBaseUrl": "", + "DoubaoApiBaseUrl": "" +} + +def ensure_api_base_urls_file(): + """确保 API 基础 URL 配置文件存在并包含所有占位符""" + try: + file_exists = os.path.exists(API_BASE_URLS_FILE) + base_urls = {} + if file_exists: + try: + with open(API_BASE_URLS_FILE, 'r', encoding='utf-8') as f: + loaded = json.load(f) + if isinstance(loaded, dict): + base_urls = loaded + else: + file_exists = False + except json.JSONDecodeError: + file_exists = False + + missing_key_added = False + for key, default_value in DEFAULT_API_BASE_URLS.items(): + if key not in base_urls: + base_urls[key] = default_value + missing_key_added = True + + if not file_exists or missing_key_added or not base_urls: + with open(API_BASE_URLS_FILE, 'w', encoding='utf-8') as f: + json.dump(base_urls or DEFAULT_API_BASE_URLS, f, ensure_ascii=False, indent=2) + except Exception as e: + print(f"初始化API基础URL配置失败: {e}") + +# 确保API基础URL文件已经生成 +ensure_api_base_urls_file() + # 跟踪用户生成任务的字典 generation_tasks = {} diff --git a/config/api_base_urls.json b/config/api_base_urls.json deleted file mode 100644 index 192c0b7..0000000 --- a/config/api_base_urls.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "AnthropicApiBaseUrl": "", - "OpenaiApiBaseUrl": "", - "DeepseekApiBaseUrl": "", - "AlibabaApiBaseUrl": "", - "GoogleApiBaseUrl": "", - "DoubaoApiBaseUrl": "" -} \ No newline at end of file diff --git a/docs/beginner-tutorial.md b/docs/beginner-tutorial.md index 3293358..dc1d819 100644 --- a/docs/beginner-tutorial.md +++ b/docs/beginner-tutorial.md @@ -297,10 +297,8 @@ API Key 相当于你在各大模型平台上的「门票」。不同平台的获 ## 13. 进一步探索 -- `config/models.json`:自定义展示在下拉框的模型列表,包含模型名称、供应商、能力标签等。 +- `config/models.json`:自定义展示在下拉框的模型列表,包含模型名称、供应商、能力标签等,可按需添加。 - `config/prompts.json`:定义默认 prompt,可根据学科优化。 -- `config/api_base_urls.json`:为不同模型指定默认 `base_url`。 -- `static/` 与 `templates/`:自定义网页 UI 样式、增加功能按钮。 - 更新项目:如果是 Git 克隆,执行 `git pull`;压缩包用户可重新下载覆盖。 完成以上步骤后,你已经具备运行和日常使用 Snap-Solver 的全部基础。如果你有新的需求或遇到无法解决的问题,可以先查看 README 或在 Issues 中搜索 / 提问。祝你学习顺利,刷题提效!