-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_key.py
More file actions
executable file
·53 lines (40 loc) · 1.55 KB
/
gen_key.py
File metadata and controls
executable file
·53 lines (40 loc) · 1.55 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
#!/usr/bin/env python3
"""Generate an API key for the LLM serving nginx auth and update .env."""
import secrets
import string
import sys
import os
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
def generate_key(prefix: str = "sk-lm", part1_len: int = 10, part2_len: int = 20) -> str:
"""Generate key like: sk-lm-XXXXXXXXXX:YYYYYYYYYYYYYYYYYYYY"""
chars = string.ascii_letters + string.digits
part1 = "".join(secrets.choice(chars) for _ in range(part1_len))
part2 = "".join(secrets.choice(chars) for _ in range(part2_len))
return f"{prefix}-{part1}:{part2}"
def update_env(key: str) -> None:
"""Update DPI_FACTORY_API_KEY in .env file."""
env_path = os.path.join(SCRIPT_DIR, ".env")
if os.path.exists(env_path):
with open(env_path) as f:
lines = f.readlines()
with open(env_path, "w") as f:
for line in lines:
if line.startswith("DPI_FACTORY_API_KEY="):
f.write(f"DPI_FACTORY_API_KEY={key}\n")
else:
f.write(line)
else:
with open(env_path, "w") as f:
f.write(f"DPI_FACTORY_API_KEY={key}\n")
f.write("DPI_FACTORY_API_BASE=http://125.188.35.185:19101/v1\n")
def main() -> None:
key = generate_key()
print(f"Generated API key:\n {key}")
if "--no-save" in sys.argv:
print("\nDry run — .env not updated.")
return
update_env(key)
print(f"\n✓ Saved to .env")
print(f" Run ./reload_nginx.sh to apply the new key.")
if __name__ == "__main__":
main()