-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_manager.sh
executable file
·561 lines (493 loc) · 19 KB
/
config_manager.sh
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/bin/bash
CONFIG_FILE="config.json"
# 检查 jq 是否安装
if ! command -v jq &> /dev/null; then
echo "错误:需要安装 jq 来处理 JSON 文件"
echo "请运行:brew install jq"
exit 1
fi
# 读取配置文件并处理兼容性
function read_config() {
local config=""
# 如果配置文件不存在或为空,创建新的
if [[ ! -f "$CONFIG_FILE" ]] || [[ ! -s "$CONFIG_FILE" ]]; then
echo "{\"services\": {}, \"default_service\": null, \"defaults\": {\"max_context_length\": 4096, \"max_output_length\": 2048}}" > "$CONFIG_FILE"
fi
# 检查 JSON 格式是否有效
if ! jq '.' "$CONFIG_FILE" > /dev/null 2>&1; then
echo "错误:配置文件格式无效"
echo "正在重置配置文件..."
echo "{\"services\": {}, \"default_service\": null, \"defaults\": {\"max_context_length\": 4096, \"max_output_length\": 2048}}" > "$CONFIG_FILE"
fi
# 读取配置文件
config=$(cat "$CONFIG_FILE")
# 检查是否需要升级配置
local needs_upgrade=false
# 检查是否存在 defaults 部分
if ! echo "$config" | jq -e '.defaults' > /dev/null 2>&1; then
config=$(echo "$config" | jq '. += {"defaults": {"max_context_length": 4096, "max_output_length": 2048}}')
needs_upgrade=true
fi
# 检查每个服务的配置
if echo "$config" | jq -e '.services' > /dev/null 2>&1; then
local services=($(echo "$config" | jq -r '.services | keys[]'))
for service in "${services[@]}"; do
# 检查是否缺少上下文长度设置
if ! echo "$config" | jq -e ".services.\"$service\".max_context_length" > /dev/null 2>&1; then
echo "注意:服务 '$service' 缺少上下文长度设置,将使用默认值"
config=$(echo "$config" | jq ".services.\"$service\".max_context_length = 4096")
needs_upgrade=true
fi
# 检查是否缺少输出长度设置
if ! echo "$config" | jq -e ".services.\"$service\".max_output_length" > /dev/null 2>&1; then
echo "注意:服务 '$service' 缺少输出长度设置,将使用默认值"
config=$(echo "$config" | jq ".services.\"$service\".max_output_length = 2048")
needs_upgrade=true
fi
done
fi
# 如果配置有更新,保存并提示用户
if [[ "$needs_upgrade" == "true" ]]; then
echo "配置文件已更新,添加了缺失的参数设置"
echo "您可以使用 '更新服务配置' 选项来调整这些参数"
echo
save_config "$config"
fi
echo "$config"
}
# 保存配置文件
function save_config() {
# 验证 JSON 格式
if ! echo "$1" | jq '.' > /dev/null 2>&1; then
echo "错误:无效的配置数据"
return 1
fi
# 保存配置
echo "$1" | jq '.' > "$CONFIG_FILE"
}
# 交互式添加服务
function interactive_add_service() {
echo "=== 添加新的翻译服务 ==="
# 输入服务名称
while true; do
read -p "请输入服务名称(如 openai, deepseek): " service_name
if [[ -z "$service_name" ]]; then
echo "服务名称不能为空"
continue
fi
# 检查服务是否已存在
if echo "$(read_config)" | jq -e ".services.\"$service_name\"" > /dev/null; then
echo "服务 '$service_name' 已存在"
continue
fi
break
done
# 输入 API 密钥
while true; do
read -p "请输入 API 密钥: " api_key
if [[ -z "$api_key" ]]; then
echo "API 密钥不能为空"
continue
fi
break
done
# 选择预设 API 地址或自定义
echo "选择 API 地址:"
echo "1) OpenAI (https://api.openai.com/v1/chat/completions)"
echo "2) DeepSeek (https://api.deepseek.com/v1/chat/completions)"
echo "3) 自定义"
while true; do
read -p "请选择 [1-3]: " url_choice
case $url_choice in
1) url="https://api.openai.com/v1/chat/completions"; break;;
2) url="https://api.deepseek.com/v1/chat/completions"; break;;
3) read -p "请输入自定义 API 地址: " url;
if [[ -n "$url" ]]; then break; fi;;
*) echo "请选择有效的选项";;
esac
done
# 选择预设模型或自定义
echo "选择模型:"
echo "1) GPT-4"
echo "2) GPT-3.5-turbo"
echo "3) DeepSeek-chat"
echo "4) 自定义"
while true; do
read -p "请选择 [1-4]: " model_choice
case $model_choice in
1) model="gpt-4"; break;;
2) model="gpt-3.5-turbo"; break;;
3) model="deepseek-chat"; break;;
4) read -p "请输入自定义模型名称: " model;
if [[ -n "$model" ]]; then break; fi;;
*) echo "请选择有效的选项";;
esac
done
# 添加上下文长度设置
echo "设置上下文长度(字符数):"
echo "1) 4K (4096)"
echo "2) 8K (8192)"
echo "3) 32K (32768)"
echo "4) 64K (65536)"
echo "5) 自定义"
while true; do
read -p "请选择 [1-5]: " length_choice
case $length_choice in
1) context_length=4096; break;;
2) context_length=8192; break;;
3) context_length=32768; break;;
4) context_length=65536; break;;
5) read -p "请输入自定义长度: " context_length
if [[ "$context_length" =~ ^[0-9]+$ ]]; then break; fi
echo "请输入有效的数字";;
*) echo "请选择有效的选项";;
esac
done
# 添加输出长度设置
echo "设置最大输出长度(字符数):"
echo "1) 2K (2048)"
echo "2) 4K (4096)"
echo "3) 8K (8192)"
echo "4) 自定义"
while true; do
read -p "请选择 [1-4]: " output_choice
case $output_choice in
1) output_length=2048; break;;
2) output_length=4096; break;;
3) output_length=8192; break;;
4) read -p "请输入自定义长度: " output_length
if [[ "$output_length" =~ ^[0-9]+$ ]]; then break; fi
echo "请输入有效的数字";;
*) echo "请选择有效的选项";;
esac
done
# 更新确认信息显示
echo
echo "请确认以下信息:"
echo "服务名称: $service_name"
echo "API 密钥: $api_key"
echo "API 地址: $url"
echo "模型名称: $model"
echo "上下文长度: $context_length"
echo "输出长度: $output_length"
read -p "是否确认添加?[y/N] " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "已取消添加服务"
return 1
fi
# 更新配置保存
local config=$(read_config)
config=$(echo "$config" | jq ".services.\"$service_name\" = {
\"service\": \"$service_name\",
\"api_key\": \"$api_key\",
\"url\": \"$url\",
\"model\": \"$model\",
\"language\": \"zh-CN\",
\"max_context_length\": $context_length,
\"max_output_length\": $output_length
}")
# 如果是第一个服务,设为默认
if [[ $(echo "$config" | jq '.default_service') == "null" ]]; then
config=$(echo "$config" | jq ".default_service = \"$service_name\"")
fi
save_config "$config"
echo "服务 '$service_name' 添加成功"
# 询问是否设为默认服务
if [[ $(echo "$config" | jq -r '.default_service') != "$service_name" ]]; then
read -p "是否将此服务设为默认?[y/N] " set_default
if [[ "$set_default" == "y" || "$set_default" == "Y" ]]; then
config=$(echo "$config" | jq ".default_service = \"$service_name\"")
save_config "$config"
echo "已将 '$service_name' 设为默认服务"
fi
fi
}
# 交互式删除服务
function interactive_remove_service() {
echo "=== 删除翻译服务 ==="
# 获取现有服务列表
local config=$(read_config)
local services=($(echo "$config" | jq -r '.services | keys[]'))
if [[ ${#services[@]} -eq 0 ]]; then
echo "当前没有配置任何服务"
return 1
fi
# 显示服务列表
echo "现有服务:"
for i in "${!services[@]}"; do
echo "$((i+1))) ${services[$i]}"
done
# 选择要删除的服务
while true; do
read -p "请选择要删除的服务 [1-${#services[@]}]: " choice
if [[ "$choice" =~ ^[0-9]+$ && "$choice" -ge 1 && "$choice" -le "${#services[@]}" ]]; then
service_name="${services[$((choice-1))]}"
break
fi
echo "请输入有效的选项"
done
# 确认删除
read -p "确定要删除服务 '$service_name' 吗?[y/N] " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "已取消删除"
return 1
fi
# 删除服务
if [[ $(echo "$config" | jq -r ".default_service") == "$service_name" ]]; then
config=$(echo "$config" | jq '.default_service = null')
fi
config=$(echo "$config" | jq "del(.services.\"$service_name\")")
save_config "$config"
echo "服务 '$service_name' 已删除"
}
# 交互式更新服务
function interactive_update_service() {
echo "=== 更新服务配置 ==="
# 获取现有服务列表
local config=$(read_config)
local services=($(echo "$config" | jq -r '.services | keys[]'))
if [[ ${#services[@]} -eq 0 ]]; then
echo "当前没有配置任何服务"
return 1
fi
# 显示服务列表
echo "选择要更新的服务:"
for i in "${!services[@]}"; do
echo "$((i+1))) ${services[$i]}"
done
# 选择服务
while true; do
read -p "请选择 [1-${#services[@]}]: " choice
if [[ "$choice" =~ ^[0-9]+$ && "$choice" -ge 1 && "$choice" -le "${#services[@]}" ]]; then
service_name="${services[$((choice-1))]}"
break
fi
echo "请输入有效的选项"
done
# 选择要更新的字段
echo "选择要更新的配置项:"
echo "1) API 密钥"
echo "2) API 地址"
echo "3) 模型名称"
echo "4) 语言设置"
echo "5) 上下文长度"
echo "6) 输出长度"
while true; do
read -p "请选择 [1-6]: " field_choice
case $field_choice in
1) field="api_key"
read -p "请输入新的 API 密钥: " value
;;
2) field="url"
echo "选择 API 地址:"
echo "1) OpenAI (https://api.openai.com/v1/chat/completions)"
echo "2) DeepSeek (https://api.deepseek.com/v1/chat/completions)"
echo "3) 自定义"
while true; do
read -p "请选择 [1-3]: " url_choice
case $url_choice in
1) value="https://api.openai.com/v1/chat/completions"; break;;
2) value="https://api.deepseek.com/v1/chat/completions"; break;;
3) read -p "请输入自定义 API 地址: " value;
if [[ -n "$value" ]]; then break; fi;;
*) echo "请选择有效的选项";;
esac
done
;;
3) field="model"
echo "选择模型:"
echo "1) GPT-4"
echo "2) GPT-3.5-turbo"
echo "3) DeepSeek-chat"
echo "4) 自定义"
while true; do
read -p "请选择 [1-4]: " model_choice
case $model_choice in
1) value="gpt-4"; break;;
2) value="gpt-3.5-turbo"; break;;
3) value="deepseek-chat"; break;;
4) read -p "请输入自定义模型名称: " value;
if [[ -n "$value" ]]; then break; fi;;
*) echo "请选择有效的选项";;
esac
done
;;
4) field="language"
read -p "请输入语言代码 (默认 zh-CN): " value
value=${value:-zh-CN}
;;
5) field="max_context_length"
echo "设置上下文长度(字符数):"
echo "1) 4K (4096)"
echo "2) 8K (8192)"
echo "3) 32K (32768)"
echo "4) 64K (65536)"
echo "5) 自定义"
while true; do
read -p "请选择 [1-5]: " length_choice
case $length_choice in
1) value=4096; break;;
2) value=8192; break;;
3) value=32768; break;;
4) value=65536; break;;
5) read -p "请输入自定义长度: " value
if [[ "$value" =~ ^[0-9]+$ ]]; then break; fi
echo "请输入有效的数字";;
*) echo "请选择有效的选项";;
esac
done
;;
6) field="max_output_length"
echo "设置最大输出长度(字符数):"
echo "1) 2K (2048)"
echo "2) 4K (4096)"
echo "3) 8K (8192)"
echo "4) 自定义"
while true; do
read -p "请选择 [1-4]: " output_choice
case $output_choice in
1) value=2048; break;;
2) value=4096; break;;
3) value=8192; break;;
4) read -p "请输入自定义长度: " value
if [[ "$value" =~ ^[0-9]+$ ]]; then break; fi
echo "请输入有效的数字";;
*) echo "请选择有效的选项";;
esac
done
;;
*) echo "请选择有效的选项"; continue;;
esac
break
done
# 确认更新
echo
echo "请确认更新信息:"
echo "服务名称: $service_name"
echo "更新字段: $field"
echo "新的值: $value"
read -p "是否确认更新?[y/N] " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "已取消更新"
return 1
fi
# 更新配置
if [[ "$field" == "max_context_length" || "$field" == "max_output_length" ]]; then
config=$(echo "$config" | jq ".services.\"$service_name\".\"$field\" = $value")
else
config=$(echo "$config" | jq ".services.\"$service_name\".\"$field\" = \"$value\"")
fi
save_config "$config"
echo "服务 '$service_name' 的 '$field' 已更新"
}
# 交互式设置默认服务
function interactive_set_default() {
echo "=== 设置默认服务 ==="
# 获取现有服务列表
local config=$(read_config)
local services=($(echo "$config" | jq -r '.services | keys[]'))
local current_default=$(echo "$config" | jq -r '.default_service')
if [[ ${#services[@]} -eq 0 ]]; then
echo "当前没有配置任何服务"
return 1
fi
# 显示服务列表
echo "选择默认服务:"
for i in "${!services[@]}"; do
if [[ "${services[$i]}" == "$current_default" ]]; then
echo "$((i+1))) ${services[$i]} (当前默认)"
else
echo "$((i+1))) ${services[$i]}"
fi
done
# 选择服务
while true; do
read -p "请选择 [1-${#services[@]}]: " choice
if [[ "$choice" =~ ^[0-9]+$ && "$choice" -ge 1 && "$choice" -le "${#services[@]}" ]]; then
service_name="${services[$((choice-1))]}"
break
fi
echo "请输入有效的选项"
done
# 设置默认服务
config=$(echo "$config" | jq ".default_service = \"$service_name\"")
save_config "$config"
echo "已将 '$service_name' 设为默认服务"
}
# 列出所有服务
function list_services() {
echo "=== 已配置的服务 ==="
local config=$(read_config)
local default_service=$(echo "$config" | jq -r '.default_service')
local services=($(echo "$config" | jq -r '.services | keys[]'))
if [[ ${#services[@]} -eq 0 ]]; then
echo "当前没有配置任何服务"
return 1
fi
# 显示服务列表
for service in "${services[@]}"; do
local api_key=$(echo "$config" | jq -r ".services.\"$service\".api_key")
local url=$(echo "$config" | jq -r ".services.\"$service\".url")
local model=$(echo "$config" | jq -r ".services.\"$service\".model")
local context_length=$(echo "$config" | jq -r ".services.\"$service\".max_context_length")
local output_length=$(echo "$config" | jq -r ".services.\"$service\".max_output_length")
if [[ "$service" == "$default_service" ]]; then
echo "* $service (默认服务)"
else
echo " $service"
fi
echo " API 地址: $url"
echo " 模型: $model"
echo " API 密钥: ${api_key:0:8}..."
echo " 上下文长度: $context_length"
echo " 输出长度: $output_length"
echo
done
}
# 获取当前默认服务
function get_default_service() {
echo "=== 当前默认服务 ==="
local config=$(read_config)
local default_service=$(echo "$config" | jq -r '.default_service')
if [[ "$default_service" == "null" ]]; then
echo "当前没有设置默认服务"
return 1
fi
local service_config=$(echo "$config" | jq -r ".services.\"$default_service\"")
echo "服务名称: $default_service"
echo "API 地址: $(echo "$service_config" | jq -r '.url')"
echo "模型: $(echo "$service_config" | jq -r '.model')"
echo "API 密钥: $(echo "$service_config" | jq -r '.api_key' | cut -c1-8)..."
echo "语言: $(echo "$service_config" | jq -r '.language')"
echo "上下文长度: $(echo "$service_config" | jq -r '.max_context_length')"
echo "输出长度: $(echo "$service_config" | jq -r '.max_output_length')"
}
# 主菜单
function show_menu() {
while true; do
echo
echo "=== 翻译服务配置管理 ==="
echo "1) 查看所有服务"
echo "2) 添加新服务"
echo "3) 删除服务"
echo "4) 更新服务配置"
echo "5) 设置默认服务"
echo "6) 查看当前默认服务"
echo "0) 退出"
echo
read -p "请选择操作 [0-6]: " choice
echo
case $choice in
1) list_services;;
2) interactive_add_service;;
3) interactive_remove_service;;
4) interactive_update_service;;
5) interactive_set_default;;
6) get_default_service;;
0) exit 0;;
*) echo "请选择有效的选项";;
esac
done
}
# 启动主菜单
show_menu