forked from ZhuLinsen/daily_stock_analysis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_daily_analysis_workflow_llm_env.py
More file actions
122 lines (97 loc) · 4.01 KB
/
Copy pathtest_daily_analysis_workflow_llm_env.py
File metadata and controls
122 lines (97 loc) · 4.01 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
"""Static checks for LLM provider channel mappings in daily_analysis.yml."""
from __future__ import annotations
import re
from pathlib import Path
import yaml
ROOT_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_PATH = ROOT_DIR / "apps/dsa-web/src/components/settings/llmProviderTemplates.ts"
WORKFLOW_PATH = ROOT_DIR / ".github/workflows/daily_analysis.yml"
ENV_EXAMPLE_PATH = ROOT_DIR / ".env.example"
EXPECTED_TEMPLATE_CHANNELS = {
"aihubmix",
"deepseek",
"dashscope",
"zhipu",
"moonshot",
"minimax",
"volcengine",
"siliconflow",
"openrouter",
"gemini",
"anthropic",
"openai",
"ollama",
"copilot",
}
# Channels whose runtime authentication does not rely on a static API key
# (Ollama runs locally without auth, GitHub Copilot uses LiteLLM's OAuth
# device flow). They are still wired through the channel env conventions
# but ``LLM_<CHANNEL>_API_KEY=`` is intentionally absent from the example
# `.env`.
CHANNELS_WITHOUT_API_KEY_IN_EXAMPLE = {"ollama", "copilot"}
def _extract_provider_templates() -> dict[str, str]:
content = TEMPLATE_PATH.read_text(encoding="utf-8")
matches = re.findall(
r"channelId:\s*'(?P<channel>[^']+)'.*?baseUrl:\s*'(?P<base_url>[^']*)'",
content,
flags=re.DOTALL,
)
assert matches, "No provider channelId entries were found in llmProviderTemplates.ts"
templates = {channel: base_url for channel, base_url in matches if channel != "custom"}
assert EXPECTED_TEMPLATE_CHANNELS.issubset(templates.keys())
assert "ark" not in templates
return templates
def _load_daily_analysis_env() -> dict[str, str]:
workflow = yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8"))
steps = workflow["jobs"]["analyze"]["steps"]
analyze_step = next((step for step in steps if step.get("name") == "执行股票分析"), None)
available_step_names = [step.get("name", "<unnamed>") for step in steps]
assert analyze_step is not None, (
"Expected daily_analysis.yml job analyze to include a step named "
f"'执行股票分析'; available step names: {available_step_names}"
)
return analyze_step["env"]
def test_daily_analysis_maps_all_provider_template_channels() -> None:
templates = _extract_provider_templates()
env = _load_daily_analysis_env()
for channel in templates:
prefix = f"LLM_{channel.upper()}_"
for suffix in (
"PROTOCOL",
"BASE_URL",
"API_KEY",
"API_KEYS",
"MODELS",
"ENABLED",
"EXTRA_HEADERS",
):
assert f"{prefix}{suffix}" in env
assert not any(key.startswith("LLM_ARK_") for key in env)
def test_daily_analysis_keeps_channel_secrets_in_secrets_context() -> None:
templates = _extract_provider_templates()
env = _load_daily_analysis_env()
for channel in templates:
upper = channel.upper()
for suffix in ("API_KEY", "API_KEYS"):
key = f"LLM_{upper}_{suffix}"
assert env[key] == f"${{{{ secrets.{key} }}}}"
for suffix in ("PROTOCOL", "BASE_URL", "MODELS", "ENABLED", "EXTRA_HEADERS"):
key = f"LLM_{upper}_{suffix}"
assert f"vars.{key}" in env[key]
assert f"secrets.{key}" in env[key]
def test_env_example_includes_provider_template_channel_examples() -> None:
templates = _extract_provider_templates()
env_example = ENV_EXAMPLE_PATH.read_text(encoding="utf-8")
for channel, base_url in templates.items():
upper = channel.upper()
assert f"LLM_CHANNELS={channel}" in env_example
assert f"LLM_{upper}_MODELS=" in env_example
if channel not in CHANNELS_WITHOUT_API_KEY_IN_EXAMPLE:
assert f"LLM_{upper}_API_KEY=" in env_example
if base_url:
assert f"LLM_{upper}_BASE_URL=" in env_example
if channel != "ollama":
assert f"LLM_{upper}_PROTOCOL=" in env_example
assert "LLM_CHANNELS=ark" not in env_example
assert "LLM_ARK_" not in env_example