-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathconfig.py
More file actions
47 lines (43 loc) · 1.68 KB
/
config.py
File metadata and controls
47 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import json
from utils.constants import DELIMITER, LOG_DIR, LLM_API_URL, LLM_API_KEY, LLM_MODEL_TYPE, VLM_API_URL, VLM_API_KEY, VLM_MODEL_TYPE, EMBEDDING_API_URL, EMBEDDING_API_KEY, EMBEDDING_MODEL_TYPE
# 全局配置字典,用于存储用户设置的API配置
api_config = {
"llm_api_key": LLM_API_KEY,
"llm_api_url": LLM_API_URL,
"llm_model": LLM_MODEL_TYPE,
"vlm_api_key": VLM_API_KEY,
"vlm_api_url": VLM_API_URL,
"vlm_model": VLM_MODEL_TYPE,
"embedding_api_key": EMBEDDING_API_KEY,
"embedding_api_url": EMBEDDING_API_URL,
"embedding_model": EMBEDDING_MODEL_TYPE
}
# 配置文件路径
CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "api_config.json")
# 从文件加载配置
def load_api_config():
global api_config
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
loaded_config = json.load(f)
for k, v in loaded_config.items():
# 空字符串不覆盖默认配置,避免把默认 URL/模型清空
if isinstance(v, str) and not v.strip():
continue
api_config[k] = v
except Exception as e:
print(f"加载API配置失败: {e}")
# 保存配置到文件
def save_api_config(config):
global api_config
for k, v in config.items():
if isinstance(v, str) and not v.strip():
continue
api_config[k] = v
try:
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(api_config, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"保存API配置失败: {e}")