-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathapi_utils.py
More file actions
34 lines (27 loc) · 932 Bytes
/
api_utils.py
File metadata and controls
34 lines (27 loc) · 932 Bytes
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
#!/usr/bin/env python3
"""
API 工具函数 - DashScope / Gemini 等 API 调用的公共逻辑
"""
import os
from typing import Optional, Tuple
from config import DASHSCOPE_BASE_URL
def resolve_api_keys(
api_key: Optional[str] = None,
qwen_api_key: Optional[str] = None,
) -> Tuple[str, str]:
"""
统一解析 Gemini 和 DashScope/Qwen API Key。
优先级:参数 > 环境变量
Returns:
(gemini_key, qwen_key)
"""
gemini = (api_key or "").strip() or os.getenv("GEMINI_API_KEY", "")
qwen = (qwen_api_key or "").strip() or os.getenv("DASHSCOPE_API_KEY", "")
return gemini, qwen
def setup_dashscope_url(base_url: Optional[str] = None) -> None:
"""
设置 dashscope 的 base_http_api_url。
参数 base_url 优先,否则使用 config.DASHSCOPE_BASE_URL。
"""
import dashscope
dashscope.base_http_api_url = (base_url or DASHSCOPE_BASE_URL).rstrip('/')