refactor(config): consolidate api base urls handling

This commit is contained in:
zihanjian
2025-11-19 20:35:14 +08:00
parent 48247dae95
commit 4472b6e899
4 changed files with 44 additions and 12 deletions

3
.gitignore vendored
View File

@@ -127,10 +127,11 @@ Thumbs.db
# Project specific # Project specific
config/update_info.json config/update_info.json
config/api_keys.json config/api_keys.json
config/api_base_urls.json
.venv/ .venv/
venv/ venv/
# uv # uv
.python-version .python-version
pyproject.toml pyproject.toml
uv.lock uv.lock

41
app.py
View File

@@ -38,11 +38,52 @@ os.makedirs(CONFIG_DIR, exist_ok=True)
# 密钥和其他配置文件路径 # 密钥和其他配置文件路径
API_KEYS_FILE = os.path.join(CONFIG_DIR, 'api_keys.json') 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') VERSION_FILE = os.path.join(CONFIG_DIR, 'version.json')
UPDATE_INFO_FILE = os.path.join(CONFIG_DIR, 'update_info.json') UPDATE_INFO_FILE = os.path.join(CONFIG_DIR, 'update_info.json')
PROMPT_FILE = os.path.join(CONFIG_DIR, 'prompts.json') # 新增提示词配置文件路径 PROMPT_FILE = os.path.join(CONFIG_DIR, 'prompts.json') # 新增提示词配置文件路径
PROXY_API_FILE = os.path.join(CONFIG_DIR, 'proxy_api.json') # 新增中转API配置文件路径 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 = {} generation_tasks = {}

View File

@@ -1,8 +0,0 @@
{
"AnthropicApiBaseUrl": "",
"OpenaiApiBaseUrl": "",
"DeepseekApiBaseUrl": "",
"AlibabaApiBaseUrl": "",
"GoogleApiBaseUrl": "",
"DoubaoApiBaseUrl": ""
}

View File

@@ -297,10 +297,8 @@ API Key 相当于你在各大模型平台上的「门票」。不同平台的获
## 13. 进一步探索 ## 13. 进一步探索
- `config/models.json`:自定义展示在下拉框的模型列表,包含模型名称、供应商、能力标签等。 - `config/models.json`:自定义展示在下拉框的模型列表,包含模型名称、供应商、能力标签等,可按需添加
- `config/prompts.json`:定义默认 prompt可根据学科优化。 - `config/prompts.json`:定义默认 prompt可根据学科优化。
- `config/api_base_urls.json`:为不同模型指定默认 `base_url`。
- `static/` 与 `templates/`:自定义网页 UI 样式、增加功能按钮。
- 更新项目:如果是 Git 克隆,执行 `git pull`;压缩包用户可重新下载覆盖。 - 更新项目:如果是 Git 克隆,执行 `git pull`;压缩包用户可重新下载覆盖。
完成以上步骤后,你已经具备运行和日常使用 Snap-Solver 的全部基础。如果你有新的需求或遇到无法解决的问题,可以先查看 README 或在 Issues 中搜索 / 提问。祝你学习顺利,刷题提效! 完成以上步骤后,你已经具备运行和日常使用 Snap-Solver 的全部基础。如果你有新的需求或遇到无法解决的问题,可以先查看 README 或在 Issues 中搜索 / 提问。祝你学习顺利,刷题提效!