Skip to content

Commit dd1381d

Browse files
committed
✨ 添加设置AI语音模型
1 parent 9aaa448 commit dd1381d

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

plugin/aichat/cfg.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aichat
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67

@@ -151,3 +152,33 @@ func newextrasetfloat32(ptr *float32) func(ctx *zero.Ctx) {
151152
ctx.SendChain(message.Text("成功"))
152153
}
153154
}
155+
156+
func printConfig(cfg config) string {
157+
var builder strings.Builder
158+
builder.WriteString("当前AI聊天配置:\n")
159+
builder.WriteString(fmt.Sprintf("• 模型名:%s\n", cfg.ModelName))
160+
builder.WriteString(fmt.Sprintf("• 触发概率:%d%%\n", cfg.Type))
161+
builder.WriteString(fmt.Sprintf("• 最大生成长度:%d\n", cfg.MaxN))
162+
builder.WriteString(fmt.Sprintf("• TopP采样值:%.1f\n", cfg.TopP))
163+
builder.WriteString(fmt.Sprintf("• 系统提示词:%s\n", cfg.SystemP))
164+
builder.WriteString(fmt.Sprintf("• 接口地址:%s\n", cfg.API))
165+
builder.WriteString(fmt.Sprintf("• 密钥:%s\n", maskKey(cfg.Key)))
166+
builder.WriteString(fmt.Sprintf("• 分隔符:%s\n", cfg.Separator))
167+
builder.WriteString(fmt.Sprintf("• 响应@:%s\n", yesNo(!cfg.NoReplyAT)))
168+
builder.WriteString(fmt.Sprintf("• 支持系统提示词:%s\n", yesNo(!cfg.NoSystemP)))
169+
return builder.String()
170+
}
171+
172+
func maskKey(key string) string {
173+
if len(key) <= 4 {
174+
return "****"
175+
}
176+
return key[:2] + strings.Repeat("*", len(key)-4) + key[len(key)-2:]
177+
}
178+
179+
func yesNo(b bool) string {
180+
if b {
181+
return "是"
182+
}
183+
return "否"
184+
}

plugin/aichat/main.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math/rand"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/fumiama/deepinfra"
1011
"github.com/fumiama/deepinfra/model"
@@ -40,8 +41,9 @@ var (
4041
"- 设置AI聊天(不)响应AT\n" +
4142
"- 设置AI聊天最大长度4096\n" +
4243
"- 设置AI聊天TopP 0.9\n" +
43-
"- 设置AI语音群号1048452984 (tips:群里必须有AI声聊应用)\n" +
44+
"- 查看AI聊天配置\n" +
4445
"- [启用|禁用]AI语音\n" +
46+
"- 设置AI语音群号1048452984 (tips:群里必须有AI声聊应用)\n" +
4547
"- 设置AI语音模型\n" +
4648
"- 发送AI语音xxx",
4749
PrivateDataFolder: "aichat",
@@ -278,6 +280,10 @@ func init() {
278280
Handle(newextrasetuint(&cfg.MaxN))
279281
en.OnPrefix("设置AI聊天TopP", ensureconfig, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).
280282
Handle(newextrasetfloat32(&cfg.TopP))
283+
en.OnFullMatch("查看AI聊天配置", ensureconfig, zero.SuperUserPermission).SetBlock(true).
284+
Handle(func(ctx *zero.Ctx) {
285+
ctx.SendChain(message.Text(printConfig(cfg)))
286+
})
281287
en.OnPrefix("设置AI语音群号", zero.SuperUserPermission).SetBlock(true).
282288
Handle(func(ctx *zero.Ctx) {
283289
u := strings.TrimSpace(ctx.State["args"].(string))
@@ -291,36 +297,39 @@ func init() {
291297
})
292298
en.OnFullMatch("设置AI语音模型", zero.SuperUserPermission).SetBlock(true).
293299
Handle(func(ctx *zero.Ctx) {
300+
next := zero.NewFutureEvent("message", 999, false, ctx.CheckSession())
301+
recv, cancel := next.Repeat()
302+
defer cancel()
294303
jsonData := ctx.GetAICharacters(customgid, 1)
295304

296305
// 转换为字符串数组
297306
var names []string
298307
// 初始化两个映射表
299308
nameToID := make(map[string]string)
300309
nameToURL := make(map[string]string)
301-
characters := jsonData.Get("#.characters.#")
310+
characters := jsonData.Get("#.characters")
302311

303312
// 遍历每个角色对象
304-
characters.ForEach(func(_, character gjson.Result) bool {
305-
// 提取当前角色的三个字段
306-
name := character.Get("character_name").String()
307-
names = append(names, name)
308-
id := character.Get("character_id").String()
309-
url := character.Get("preview_url").String()
310-
311-
// 存入映射表(重复名称会覆盖,保留最后出现的条目)
312-
nameToID[name] = id
313-
nameToURL[name] = url
313+
characters.ForEach(func(_, group gjson.Result) bool {
314+
group.ForEach(func(_, character gjson.Result) bool {
315+
// 提取当前角色的三个字段
316+
name := character.Get("character_name").String()
317+
names = append(names, name)
318+
// 存入映射表(重复名称会覆盖,保留最后出现的条目)
319+
nameToID[name] = character.Get("character_id").String()
320+
nameToURL[name] = character.Get("preview_url").String()
321+
return true // 继续遍历
322+
})
314323
return true // 继续遍历
315324
})
316325
var builder strings.Builder
317326
// 写入开头文本
318-
builder.WriteString("请选择模型序号\n")
327+
builder.WriteString("请选择语音模型序号\n")
319328

320329
// 遍历names数组,拼接序号和名称
321330
for i, v := range names {
322331
// 将数字转换为字符串(不依赖fmt)
323-
numStr := strconv.Itoa(i + 1)
332+
numStr := strconv.Itoa(i)
324333
// 拼接格式:"序号. 名称\n"
325334
builder.WriteString(numStr)
326335
builder.WriteString(". ")
@@ -329,5 +338,27 @@ func init() {
329338
}
330339
// 获取最终字符串
331340
ctx.SendChain(message.Text(builder.String()))
341+
for {
342+
select {
343+
case <-time.After(time.Second * 120):
344+
ctx.SendChain(message.Text("设置AI语音模型指令过期"))
345+
return
346+
case c := <-recv:
347+
msg := c.Event.Message.ExtractPlainText()
348+
num, err := strconv.Atoi(msg)
349+
if err != nil {
350+
ctx.SendChain(message.Text("请输入数字!"))
351+
continue
352+
}
353+
if num < 0 || num >= len(names) {
354+
ctx.SendChain(message.Text("序号非法!"))
355+
continue
356+
}
357+
modelName = nameToID[names[num]]
358+
ctx.SendChain(message.Text("已选择语音模型: ", names[num]))
359+
ctx.SendChain(message.Record(nameToURL[names[num]]))
360+
return
361+
}
362+
}
332363
})
333364
}

0 commit comments

Comments
 (0)