1
1
# 随机用户名生成器
2
2
import random
3
3
from typing import Optional , List
4
- from summer_modules .ai .deepseek import client
5
- from summer_modules .logger import init_and_get_logger
6
4
from pathlib import Path
5
+ import toml
6
+
7
+ from summer_modules .ai .deepseek import DeepseekClient
8
+ from summer_modules .logger import init_and_get_logger
7
9
8
10
CURRENT_DIR = Path (__file__ ).resolve ().parent
9
11
logger = init_and_get_logger (CURRENT_DIR , "generate_username" )
12
+ CONFIG_TOML_FILEPATH = (CURRENT_DIR / "../../../config.toml" ).resolve ()
13
+ CONFIG = toml .load (CONFIG_TOML_FILEPATH )
14
+ DEEPSEEK_APIKEY = CONFIG ["deepseek_apikey" ]
15
+ DEEPSEEK_CLIENT = DeepseekClient (api_key = DEEPSEEK_APIKEY )
10
16
11
17
12
18
def generate_username (style = "default" ):
@@ -172,7 +178,7 @@ def generate_username_with_deepseek(
172
178
"""
173
179
174
180
try :
175
- response = client .chat .completions .create (
181
+ response = DEEPSEEK_CLIENT . client .chat .completions .create (
176
182
model = "deepseek-chat" ,
177
183
messages = [
178
184
{"role" : "system" , "content" : system_prompt },
@@ -185,8 +191,12 @@ def generate_username_with_deepseek(
185
191
)
186
192
content = response .choices [0 ].message .content
187
193
# 将返回的用户名列表拆分成单个用户名
188
- usernames = content .split ("\n " )
189
- return usernames
194
+ if content :
195
+ usernames = content .split ("\n " )
196
+ return usernames
197
+ else :
198
+ logger .error ("DeepSeek API 返回的用户名为空" )
199
+ return []
190
200
191
201
except Exception as e :
192
202
logger .error (f"DeepSeek API 生成用户名时发生未预期出错: { str (e )} " )
0 commit comments