|
| 1 | +import sys |
| 2 | +import re |
| 3 | +from dynaconf import Dynaconf |
| 4 | + |
| 5 | +def is_valid_openai_api_key(api_key): |
| 6 | + """ |
| 7 | + 校验是否为有效的 OpenAI API Key 格式。 |
| 8 | +
|
| 9 | + :param api_key: 待校验的 API Key 字符串 |
| 10 | + :return: 如果格式有效返回 True,否则返回 False |
| 11 | + """ |
| 12 | + # OpenAI API Key 格式通常以 "sk-" 开头,后面是 48 个字母或数字,总长度为 51 |
| 13 | + pattern = r"^sk-[A-Za-z0-9]{48}$" |
| 14 | + return bool(re.match(pattern, api_key)) |
| 15 | + |
| 16 | +class ConfigManager: |
| 17 | + def __init__(self, default_config="default.toml", user_config="aipython.toml"): |
| 18 | + self.default_config = default_config |
| 19 | + self.user_config = user_config |
| 20 | + self.config = self._load_config() |
| 21 | + |
| 22 | + def _load_config(self): |
| 23 | + # 使用 Dynaconf 合并 default 和用户指定的配置文件 |
| 24 | + try: |
| 25 | + config = Dynaconf( |
| 26 | + settings_files=[self.default_config, self.user_config], |
| 27 | + envvar_prefix="AIPY", merge_enabled=True |
| 28 | + ) |
| 29 | + except Exception as e: |
| 30 | + print(f"加载配置时出错: {e}") |
| 31 | + return config |
| 32 | + |
| 33 | + def get_config(self): |
| 34 | + return self.config |
| 35 | + |
| 36 | + def check_config(self): |
| 37 | + if not self.config: |
| 38 | + ret = "请检查配置文件路径和格式。" |
| 39 | + print(ret) |
| 40 | + return |
| 41 | + |
| 42 | + self.check_llm() |
| 43 | + |
| 44 | + def check_llm(self): |
| 45 | + if not self.config: |
| 46 | + print("配置尚未加载。") |
| 47 | + |
| 48 | + # 检查是否存在 'llm' 节 |
| 49 | + llm = self.config.get("llm") |
| 50 | + if not llm: |
| 51 | + print("缺少 'llm' 配置。") |
| 52 | + |
| 53 | + llms = {} |
| 54 | + for name, config in self.config['llm'].items(): |
| 55 | + # 检查每个 LLM 的配置 |
| 56 | + if config.get("enable", True): |
| 57 | + llms[name] = config |
| 58 | + |
| 59 | + if not llms: |
| 60 | + self._init_llm() |
| 61 | + |
| 62 | + |
| 63 | + def _init_llm(self): |
| 64 | + print( |
| 65 | + """当前环境缺少配置文件,请注册一个trustoken账号,可以使用免费赠送的API账号。 |
| 66 | +浏览器打开https://api.trustoken.ai/register , 进行账号注册。 |
| 67 | +注册后进行登录,访问页面顶部的“令牌”页面,或者点击这个地址:https://api.trustoken.ai/token |
| 68 | +点击“复制”按钮,复制令牌到剪贴板。 |
| 69 | +在此执行粘贴。""" |
| 70 | + ) |
| 71 | + |
| 72 | + while True: |
| 73 | + user_token = input("请粘贴令牌并按 Enter 键 (输入 'exit' 退出): ").strip() |
| 74 | + if user_token.lower() == "exit": |
| 75 | + print("退出令牌输入流程。") |
| 76 | + break |
| 77 | + if not user_token: |
| 78 | + print("未检测到令牌输入。") |
| 79 | + continue |
| 80 | + # 这里假设合法令牌的条件为长度不小于10字符,可根据需要调整验证逻辑 |
| 81 | + |
| 82 | + if not is_valid_openai_api_key(user_token): |
| 83 | + print("输入的令牌不合法,请确保令牌正确,格式为‘sk-xxxxxx……’,或输入 'exit' 退出。") |
| 84 | + continue |
| 85 | + |
| 86 | + self.save_trustoken(user_token) |
| 87 | + break |
| 88 | + |
| 89 | + def save_trustoken(self, token): |
| 90 | + # 根据文件所在目录定位配置文件 |
| 91 | + config_file = self.user_config |
| 92 | + try: |
| 93 | + with open(config_file, "a") as f: |
| 94 | + # 在配置文件末尾追加新的llm信任令牌配置,采用指定的格式 |
| 95 | + f.write("\n[llm.trustoken]\n") |
| 96 | + f.write(f'api_key = "{token}"\n') |
| 97 | + f.write('base_url = "https://api.trustoken.ai/v1"\n') |
| 98 | + f.write('model = "deepseek/deepseek-chat-v3-0324"\n') |
| 99 | + f.write("default = true\n") |
| 100 | + print(f"令牌已保存到 {config_file}") |
| 101 | + print("请重新运行程序以加载新的配置。") |
| 102 | + sys.exit(0) |
| 103 | + except Exception as e: |
| 104 | + print(f"保存令牌时出错: {e}") |
| 105 | + |
0 commit comments