1
1
# 随机用户名生成器
2
2
import random
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
+ from pathlib import Path
3
7
4
- def generate_username ():
5
- "生成一个长度为 5-10 的, 由大小写字母和数字组成的随机用户名"
6
- username = ''
7
- for i in range (random .randint (5 , 10 )):
8
- username += random .choice ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' )
9
- return username
8
+ CURRENT_DIR = Path (__file__ ).resolve ().parent
9
+ logger = init_and_get_logger (CURRENT_DIR , "generate_username" )
10
10
11
- if __name__ == '__main__' :
12
- print (generate_username ())
11
+
12
+ def generate_username (style = "default" ):
13
+ """生成一个更加真实的随机用户名
14
+
15
+ 参数:
16
+ style: 用户名风格,可选值包括 "default"、"gaming"、"professional"、"social"
17
+ """
18
+ adjectives = [
19
+ "cool" ,
20
+ "super" ,
21
+ "awesome" ,
22
+ "happy" ,
23
+ "clever" ,
24
+ "bright" ,
25
+ "swift" ,
26
+ "quick" ,
27
+ "brave" ,
28
+ "mighty" ,
29
+ "eager" ,
30
+ "calm" ,
31
+ "wise" ,
32
+ "gentle" ,
33
+ "lucky" ,
34
+ "wild" ,
35
+ ]
36
+
37
+ nouns = [
38
+ "fox" ,
39
+ "tiger" ,
40
+ "eagle" ,
41
+ "panda" ,
42
+ "wolf" ,
43
+ "robot" ,
44
+ "ninja" ,
45
+ "hero" ,
46
+ "phoenix" ,
47
+ "dragon" ,
48
+ "coder" ,
49
+ "gamer" ,
50
+ "star" ,
51
+ "spark" ,
52
+ "shadow" ,
53
+ "runner" ,
54
+ "master" ,
55
+ ]
56
+
57
+ verbs = [
58
+ "run" ,
59
+ "jump" ,
60
+ "play" ,
61
+ "code" ,
62
+ "read" ,
63
+ "build" ,
64
+ "create" ,
65
+ "design" ,
66
+ "dream" ,
67
+ "think" ,
68
+ ]
69
+
70
+ tech_terms = [
71
+ "dev" ,
72
+ "tech" ,
73
+ "byte" ,
74
+ "pixel" ,
75
+ "data" ,
76
+ "cyber" ,
77
+ "code" ,
78
+ "web" ,
79
+ "net" ,
80
+ "cloud" ,
81
+ ]
82
+
83
+ gaming_prefixes = [
84
+ "pro" ,
85
+ "epic" ,
86
+ "mega" ,
87
+ "ultra" ,
88
+ "hyper" ,
89
+ "elite" ,
90
+ "legend" ,
91
+ "master" ,
92
+ ]
93
+
94
+ # 风格特定的生成逻辑
95
+ if style == "gaming" :
96
+ # 游戏风格: Elite_Dragon99, MegaGamer_2023
97
+ pattern = random .choice (
98
+ [
99
+ f"{ random .choice (gaming_prefixes )} { random .choice (nouns ).capitalize ()} { random .randint (1 , 999 )} " ,
100
+ f"{ random .choice (adjectives ).capitalize ()} { random .choice (nouns ).capitalize ()} { random .randint (1 , 99 )} " ,
101
+ f"{ random .choice (gaming_prefixes )} _{ random .choice (nouns )} { random .randint (1 , 99 )} " ,
102
+ ]
103
+ )
104
+
105
+ elif style == "professional" :
106
+ # 专业风格: j_smith_dev, tech_master, clever_coder
107
+ pattern = random .choice (
108
+ [
109
+ f"{ random .choice ('abcdefghijklmnopqrstuvwxyz' )} _{ random .choice (nouns )} _{ random .choice (tech_terms )} " ,
110
+ f"{ random .choice (tech_terms )} _{ random .choice (nouns )} " ,
111
+ f"{ random .choice (adjectives )} _{ random .choice (tech_terms )} " ,
112
+ ]
113
+ )
114
+
115
+ elif style == "social" :
116
+ # 社交媒体风格: happy.fox, cool_tiger22, real.ninja.2023
117
+ pattern = random .choice (
118
+ [
119
+ f"{ random .choice (adjectives )} .{ random .choice (nouns )} " ,
120
+ f"{ random .choice (adjectives )} _{ random .choice (nouns )} { random .randint (10 , 99 )} " ,
121
+ f"real.{ random .choice (nouns )} .{ random .randint (2020 , 2025 )} " ,
122
+ ]
123
+ )
124
+
125
+ else : # default
126
+ # 默认风格: 多种组合
127
+ patterns = [
128
+ f"{ random .choice (adjectives )} { random .choice (nouns )} { random .randint (1 , 99 )} " ,
129
+ f"{ random .choice (verbs )} { random .choice (nouns ).capitalize ()} " ,
130
+ f"{ random .choice (adjectives )} _{ random .choice (nouns )} " ,
131
+ f"{ random .choice (nouns )} { random .randint (100 , 999 )} " ,
132
+ ]
133
+ pattern = random .choice (patterns )
134
+
135
+ return pattern
136
+
137
+
138
+ def generate_username_with_deepseek (
139
+ style : str = "default" , count : int = 1
140
+ ) -> List [str ]:
141
+ """通过 DeepSeek API 生成创意用户名
142
+
143
+ 参数:
144
+ style: 用户名风格,可选值包括 "default"、"gaming"、"professional"、"social"、"creative"
145
+ count: 要生成的用户名数量
146
+
147
+ 返回:
148
+ 生成的用户名列表,如果 API 调用失败则返回使用本地方法生成的用户名
149
+ """
150
+ # 构建适合不同风格的提示词
151
+ style_prompts = {
152
+ "default" : "创建普通但有趣的用户名,适合一般网站使用" ,
153
+ "gaming" : "创建酷炫的游戏玩家用户名,可包含游戏元素、战斗词汇或英雄主题" ,
154
+ "professional" : "创建专业的用户名,适合职场和专业平台,保持简洁和专业性" ,
155
+ "social" : "创建有趣、吸引人的社交媒体用户名,能反映个性特点" ,
156
+ "creative" : "创建非常有创意的用户名,可使用文字游戏、双关语或独特组合" ,
157
+ }
158
+
159
+ prompt_text = style_prompts .get (style , style_prompts ["default" ])
160
+
161
+ system_prompt = (
162
+ "你是一个专业的用户名生成器。只输出用户名本身,不要有额外说明或引号。"
163
+ )
164
+
165
+ user_prompt = f"""请生成{ count } 个{ style } 风格的用户名。{ prompt_text } 。
166
+ 要求:
167
+ 1. 每个用户名一行
168
+ 2. 长度控制在5-16个字符之间
169
+ 3. 可以适当使用数字、下划线或点
170
+ 4. 不要有引号或解释
171
+ 5. 用户名应该看起来自然、有创意且易于记忆
172
+ """
173
+
174
+ try :
175
+ response = client .chat .completions .create (
176
+ model = "deepseek-chat" ,
177
+ messages = [
178
+ {"role" : "system" , "content" : system_prompt },
179
+ {"role" : "user" , "content" : user_prompt },
180
+ ],
181
+ max_tokens = 30 ,
182
+ temperature = 0.3 ,
183
+ n = 1 ,
184
+ stop = None ,
185
+ )
186
+ content = response .choices [0 ].message .content
187
+ # 将返回的用户名列表拆分成单个用户名
188
+ usernames = content .split ("\n " )
189
+ return usernames
190
+
191
+ except Exception as e :
192
+ logger .error (f"DeepSeek API 生成用户名时发生未预期出错: { str (e )} " )
193
+ logger .info ("使用本地方法生成用户名" )
194
+ return [generate_username (style ) for _ in range (count )]
195
+
196
+
197
+ def test_generate_username ():
198
+ logger .info ("---本地生成的用户名, 每个风格3个---" )
199
+ for style in ["default" , "gaming" , "professional" , "social" ]:
200
+ usernames = [generate_username (style ) for _ in range (3 )]
201
+ logger .info (f"{ style } 风格: { ', ' .join (usernames )} " )
202
+
203
+
204
+ def test_generate_username_with_deepseek ():
205
+ # 测试 DeepSeek API 生成
206
+ logger .info ("\n ---DeepSeek API 生成的用户名---" )
207
+ try :
208
+ # 每个风格生成 3 个用户名
209
+ for style in ["default" , "gaming" , "professional" , "social" ]:
210
+ usernames = generate_username_with_deepseek (style , count = 3 )
211
+ logger .info (f"{ style } 风格: { ', ' .join (usernames )} " )
212
+ except Exception as e :
213
+ logger .info (f"测试 DeepSeek API 时出错: { str (e )} " )
214
+
215
+
216
+ if __name__ == "__main__" :
217
+ test_generate_username ()
218
+ test_generate_username_with_deepseek ()
0 commit comments