Skip to content

Commit 1d15607

Browse files
committed
refactor(config): standardize API key configuration field naming
- Rename `api_key_env` to `api_key` for env auth type - Rename `api_key_key` to `api_key` for dotenv auth type - Update default config path from root to `config/ai_providers.toml` - Update default output path to `outputs/output.apkg` - Update all provider examples in ai_providers.toml.example - Update documentation in README.md to reflect new paths BREAKING CHANGE: Configuration field names have changed - Users must update their ai_providers.toml files - `api_key_env` → `api_key` (for env auth) - `api_key_key` → `api_key` (for dotenv auth) This change improves consistency by using a single field name `api_key` across different authentication types, making the configuration more intuitive and easier to understand.
1 parent 8e680b4 commit 1d15607

5 files changed

Lines changed: 22 additions & 22 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ uv sync
1919

2020
## 配置
2121

22-
在项目根目录创建 `ai_providers.toml` 文件,配置语言模型提供商:
22+
`config/ai_providers.toml` 文件中配置语言模型提供商:
2323

2424
```toml
2525
[provider_name]
2626
enable = true
2727
auth_type = "env"
28-
api_key_env = "YOUR_API_KEY_ENV_VAR"
28+
api_key = "YOUR_API_KEY_ENV_VAR"
2929
default_base_url = "https://api.example.com/v1"
3030
default_model = "model-name"
3131
```

config/ai_providers.toml.example

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ enable = false
1313
auth_type = "env"
1414
base_url_env = "OPENAI_BASE_URL"
1515
model_env = "OPENAI_MODEL"
16-
api_key_env = "OPENAI_API_KEY"
16+
api_key = "OPENAI_API_KEY"
1717
default_base_url = "https://api.openai.com/v1"
1818
default_model = "gpt-4o"
1919

@@ -23,14 +23,14 @@ auth_type = "dotenv"
2323
dotenv_path = ".env.d/anthropic.env"
2424
base_url_key = "ANTHROPIC_BASE_URL"
2525
model_key = "ANTHROPIC_MODEL"
26-
api_key_key = "ANTHROPIC_API_KEY"
26+
api_key = "ANTHROPIC_API_KEY"
2727
default_base_url = "https://api.anthropic.com"
2828
default_model = "claude-sonnet-4-20250514"
2929

3030
[google]
3131
enable = false
3232
auth_type = "env"
33-
api_key_env = "GOOGLE_API_KEY"
33+
api_key = "GOOGLE_API_KEY"
3434
model_env = "GOOGLE_MODEL"
3535
default_base_url = "https://generativelanguage.googleapis.com/v1beta/openai"
3636
default_model = "gemini-1.5-pro"
@@ -40,7 +40,7 @@ enable = false
4040
auth_type = "env"
4141
base_url_env = "ZHIPU_BASE_URL"
4242
model_env = "ZHIPU_MODEL"
43-
api_key_env = "ZHIPU_API_KEY"
43+
api_key = "ZHIPU_API_KEY"
4444
default_base_url = "https://open.bigmodel.cn/api/paas/v4"
4545
default_model = "glm-4-plus"
4646

@@ -49,7 +49,7 @@ enable = false
4949
auth_type = "env"
5050
base_url_env = "MOONSHOT_BASE_URL"
5151
model_env = "MOONSHOT_MODEL"
52-
api_key_env = "MOONSHOT_API_KEY"
52+
api_key = "MOONSHOT_API_KEY"
5353
default_base_url = "https://api.moonshot.cn/v1"
5454
default_model = "moonshot-v1-8k"
5555

@@ -58,7 +58,7 @@ enable = false
5858
auth_type = "env"
5959
base_url_env = "QWEN_BASE_URL"
6060
model_env = "QWEN_MODEL"
61-
api_key_env = "DASHSCOPE_API_KEY"
61+
api_key = "DASHSCOPE_API_KEY"
6262
default_base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
6363
default_model = "qwen-plus"
6464

@@ -67,7 +67,7 @@ enable = false
6767
auth_type = "env"
6868
base_url_env = "DOUBAO_BASE_URL"
6969
model_env = "DOUBAO_MODEL"
70-
api_key_env = "DOUBAO_API_KEY"
70+
api_key = "DOUBAO_API_KEY"
7171
default_base_url = "https://ark.cn-beijing.volces.com/api/v3"
7272
default_model = "doubao-pro-4k"
7373

@@ -76,6 +76,6 @@ enable = false
7676
auth_type = "env"
7777
base_url_env = "SILICONFLOW_BASE_URL"
7878
model_env = "SILICONFLOW_MODEL"
79-
api_key_env = "SILICONFLOW_API_KEY"
79+
api_key = "SILICONFLOW_API_KEY"
8080
default_base_url = "https://api.siliconflow.cn/v1"
8181
default_model = "Qwen/Qwen2.5-72B-Instruct"

src/doc2anki/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222
console = Console()
2323

24-
DEFAULT_CONFIG_PATH = Path("ai_providers.toml")
24+
DEFAULT_CONFIG_PATH = Path("config/ai_providers.toml")
2525

2626

2727
@app.command("list")
@@ -143,7 +143,7 @@ def generate_cmd(
143143
help="Input file or directory path",
144144
),
145145
output: Path = typer.Option(
146-
Path("output.apkg"),
146+
Path("outputs/output.apkg"),
147147
"-o",
148148
"--output",
149149
help="Output APKG file path",

src/doc2anki/config/loader.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def _resolve_direct_auth(provider_name: str, config: dict[str, Any]) -> Provider
7878

7979
def _resolve_env_auth(provider_name: str, config: dict[str, Any]) -> ProviderConfig:
8080
"""Resolve environment variable authentication config."""
81-
if "api_key_env" not in config:
81+
if "api_key" not in config:
8282
raise ConfigError(
83-
f"Provider '{provider_name}' with auth_type=env missing 'api_key_env'"
83+
f"Provider '{provider_name}' with auth_type=env missing 'api_key'"
8484
)
8585

86-
api_key = os.getenv(config["api_key_env"])
86+
api_key = os.getenv(config["api_key"])
8787
if not api_key:
8888
raise ConfigError(
8989
f"Provider '{provider_name}': environment variable "
90-
f"'{config['api_key_env']}' not set or empty"
90+
f"'{config['api_key']}' not set or empty"
9191
)
9292

9393
base_url = None
@@ -121,9 +121,9 @@ def _resolve_dotenv_auth(provider_name: str, config: dict[str, Any]) -> Provider
121121
raise ConfigError(
122122
f"Provider '{provider_name}' with auth_type=dotenv missing 'dotenv_path'"
123123
)
124-
if "api_key_key" not in config:
124+
if "api_key" not in config:
125125
raise ConfigError(
126-
f"Provider '{provider_name}' with auth_type=dotenv missing 'api_key_key'"
126+
f"Provider '{provider_name}' with auth_type=dotenv missing 'api_key'"
127127
)
128128

129129
dotenv_path = Path(config["dotenv_path"])
@@ -135,10 +135,10 @@ def _resolve_dotenv_auth(provider_name: str, config: dict[str, Any]) -> Provider
135135
# Load the dotenv file
136136
load_dotenv(dotenv_path, override=True)
137137

138-
api_key = os.getenv(config["api_key_key"])
138+
api_key = os.getenv(config["api_key"])
139139
if not api_key:
140140
raise ConfigError(
141-
f"Provider '{provider_name}': key '{config['api_key_key']}' "
141+
f"Provider '{provider_name}': key '{config['api_key']}' "
142142
f"not found in {dotenv_path}"
143143
)
144144

src/doc2anki/config/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EnvAuthConfig(BaseModel):
2929
auth_type: Literal["env"]
3030
base_url_env: Optional[str] = None
3131
model_env: Optional[str] = None
32-
api_key_env: str
32+
api_key: str # Environment variable name containing the API key
3333
default_base_url: Optional[str] = None
3434
default_model: Optional[str] = None
3535

@@ -42,7 +42,7 @@ class DotenvAuthConfig(BaseModel):
4242
dotenv_path: str
4343
base_url_key: Optional[str] = None
4444
model_key: Optional[str] = None
45-
api_key_key: str
45+
api_key: str # Key name in the dotenv file containing the API key
4646
default_base_url: Optional[str] = None
4747
default_model: Optional[str] = None
4848

0 commit comments

Comments
 (0)