11package aichat
22
33import (
4+ "errors"
45 "fmt"
56 "strconv"
67 "strings"
@@ -18,19 +19,89 @@ var (
1819 cfg = newconfig ()
1920)
2021
22+ var (
23+ apitypes = map [string ]uint8 {
24+ "OpenAI" : 0 ,
25+ "OLLaMA" : 1 ,
26+ "GenAI" : 2 ,
27+ }
28+ apilist = [3 ]string {"OpenAI" , "OLLaMA" , "GenAI" }
29+ )
30+
31+ type ModelType int
32+
33+ func newModelType (typ string ) (ModelType , error ) {
34+ t , ok := apitypes [typ ]
35+ if ! ok {
36+ return 0 , errors .New ("未知类型 " + typ )
37+ }
38+ return ModelType (t ), nil
39+ }
40+
41+ func (mt ModelType ) String () string {
42+ return apilist [mt ]
43+ }
44+
45+ func (mt ModelType ) protocol (modn string , temp float32 , topp float32 , maxn uint ) (mod model.Protocol , err error ) {
46+ switch cfg .Type {
47+ case 0 :
48+ mod = model .NewOpenAI (
49+ modn , cfg .Separator ,
50+ temp , topp , maxn ,
51+ )
52+ case 1 :
53+ mod = model .NewOLLaMA (
54+ modn , cfg .Separator ,
55+ temp , topp , maxn ,
56+ )
57+ case 2 :
58+ mod = model .NewGenAI (
59+ modn ,
60+ temp , topp , maxn ,
61+ )
62+ default :
63+ err = errors .New ("unsupported model type " + strconv .Itoa (int (cfg .Type )))
64+ }
65+ return
66+ }
67+
68+ type ModelBool bool
69+
70+ func (mb ModelBool ) String () string {
71+ if mb {
72+ return "是"
73+ }
74+ return "否"
75+ }
76+
77+ type ModelKey string
78+
79+ func (mk ModelKey ) String () string {
80+ if len (mk ) == 0 {
81+ return "未设置"
82+ }
83+ if len (mk ) <= 4 {
84+ return "****"
85+ }
86+ key := string (mk )
87+ return key [:2 ] + strings .Repeat ("*" , len (key )- 4 ) + key [len (key )- 2 :]
88+ }
89+
2190type config struct {
22- ModelName string
23- Type int
24- MaxN uint
25- TopP float32
26- SystemP string
27- API string
28- Key string
29- Separator string
30- NoReplyAT bool
31- NoSystemP bool
32- NoRecord bool
33- NoAgent bool
91+ ModelName string
92+ ImageModelName string
93+ Type ModelType
94+ ImageType ModelType
95+ MaxN uint
96+ TopP float32
97+ SystemP string
98+ API string
99+ ImageAPI string
100+ Key ModelKey
101+ ImageKey ModelKey
102+ Separator string
103+ NoReplyAT ModelBool
104+ NoSystemP ModelBool
34105}
35106
36107func newconfig () config {
@@ -41,10 +112,47 @@ func newconfig() config {
41112 }
42113}
43114
115+ func (c * config ) String () string {
116+ topp , maxn := c .mparams ()
117+ sb := strings.Builder {}
118+ sb .WriteString (fmt .Sprintf ("• 模型名:%s\n " , c .ModelName ))
119+ sb .WriteString (fmt .Sprintf ("• 图像模型名:%s\n " , c .ImageModelName ))
120+ sb .WriteString (fmt .Sprintf ("• 接口类型:%v\n " , c .Type ))
121+ sb .WriteString (fmt .Sprintf ("• 图像接口类型:%v\n " , c .ImageType ))
122+ sb .WriteString (fmt .Sprintf ("• 最大长度:%d\n " , maxn ))
123+ sb .WriteString (fmt .Sprintf ("• TopP:%.1f\n " , topp ))
124+ sb .WriteString (fmt .Sprintf ("• 系统提示词:%s\n " , c .SystemP ))
125+ sb .WriteString (fmt .Sprintf ("• 接口地址:%s\n " , c .API ))
126+ sb .WriteString (fmt .Sprintf ("• 图像接口地址:%s\n " , c .ImageAPI ))
127+ sb .WriteString (fmt .Sprintf ("• 密钥:%v\n " , c .Key ))
128+ sb .WriteString (fmt .Sprintf ("• 图像密钥:%v\n " , c .ImageKey ))
129+ sb .WriteString (fmt .Sprintf ("• 分隔符:%s\n " , c .Separator ))
130+ sb .WriteString (fmt .Sprintf ("• 响应@:%v\n " , ! c .NoReplyAT ))
131+ sb .WriteString (fmt .Sprintf ("• 支持系统提示词:%v\n " , ! c .NoSystemP ))
132+ return sb .String ()
133+ }
134+
44135func (c * config ) isvalid () bool {
45136 return c .ModelName != "" && c .API != "" && c .Key != ""
46137}
47138
139+ // 获取全局模型参数:TopP和最大长度
140+ func (c * config ) mparams () (topp float32 , maxn uint ) {
141+ // 处理TopP参数
142+ topp = c .TopP
143+ if topp == 0 {
144+ topp = 0.9
145+ }
146+
147+ // 处理最大长度参数
148+ maxn = c .MaxN
149+ if maxn == 0 {
150+ maxn = 4096
151+ }
152+
153+ return topp , maxn
154+ }
155+
48156func ensureconfig (ctx * zero.Ctx ) bool {
49157 c , ok := ctx .State ["manager" ].(* ctrl.Control [* zero.Ctx ])
50158 if ! ok {
@@ -62,7 +170,7 @@ func ensureconfig(ctx *zero.Ctx) bool {
62170 return true
63171}
64172
65- func newextrasetstr (ptr * string ) func (ctx * zero.Ctx ) {
173+ func newextrasetstr [ T ~ string ] (ptr * T ) func (ctx * zero.Ctx ) {
66174 return func (ctx * zero.Ctx ) {
67175 args := strings .TrimSpace (ctx .State ["args" ].(string ))
68176 if args == "" {
@@ -74,7 +182,7 @@ func newextrasetstr(ptr *string) func(ctx *zero.Ctx) {
74182 ctx .SendChain (message .Text ("ERROR: no such plugin" ))
75183 return
76184 }
77- * ptr = args
185+ * ptr = T ( args )
78186 err := c .SetExtra (& cfg )
79187 if err != nil {
80188 ctx .SendChain (message .Text ("ERROR: set extra err: " , err ))
@@ -84,7 +192,7 @@ func newextrasetstr(ptr *string) func(ctx *zero.Ctx) {
84192 }
85193}
86194
87- func newextrasetbool (ptr * bool ) func (ctx * zero.Ctx ) {
195+ func newextrasetbool [ T ~ bool ] (ptr * T ) func (ctx * zero.Ctx ) {
88196 return func (ctx * zero.Ctx ) {
89197 args := ctx .State ["regex_matched" ].([]string )
90198 isno := args [1 ] == "不"
@@ -93,7 +201,7 @@ func newextrasetbool(ptr *bool) func(ctx *zero.Ctx) {
93201 ctx .SendChain (message .Text ("ERROR: no such plugin" ))
94202 return
95203 }
96- * ptr = isno
204+ * ptr = T ( isno )
97205 err := c .SetExtra (& cfg )
98206 if err != nil {
99207 ctx .SendChain (message .Text ("ERROR: set extra err: " , err ))
@@ -156,44 +264,3 @@ func newextrasetfloat32(ptr *float32) func(ctx *zero.Ctx) {
156264 ctx .SendChain (message .Text ("成功" ))
157265 }
158266}
159-
160- func printConfig (rate int64 , temperature int64 , cfg config ) string {
161- maxn := cfg .MaxN
162- if maxn == 0 {
163- maxn = 4096
164- }
165- topp := cfg .TopP
166- if topp == 0 {
167- topp = 0.9
168- }
169- var builder strings.Builder
170- builder .WriteString ("当前AI聊天配置:\n " )
171- builder .WriteString (fmt .Sprintf ("• 模型名:%s\n " , cfg .ModelName ))
172- builder .WriteString (fmt .Sprintf ("• 接口类型:%d(%s)\n " , cfg .Type , apilist [cfg .Type ]))
173- builder .WriteString (fmt .Sprintf ("• 触发概率:%d%%\n " , rate ))
174- builder .WriteString (fmt .Sprintf ("• 温度:%.2f\n " , float32 (temperature )/ 100 ))
175- builder .WriteString (fmt .Sprintf ("• 最大长度:%d\n " , maxn ))
176- builder .WriteString (fmt .Sprintf ("• TopP:%.1f\n " , topp ))
177- builder .WriteString (fmt .Sprintf ("• 系统提示词:%s\n " , cfg .SystemP ))
178- builder .WriteString (fmt .Sprintf ("• 接口地址:%s\n " , cfg .API ))
179- builder .WriteString (fmt .Sprintf ("• 密钥:%s\n " , maskKey (cfg .Key )))
180- builder .WriteString (fmt .Sprintf ("• 分隔符:%s\n " , cfg .Separator ))
181- builder .WriteString (fmt .Sprintf ("• 响应@:%s\n " , yesNo (! cfg .NoReplyAT )))
182- builder .WriteString (fmt .Sprintf ("• 支持系统提示词:%s\n " , yesNo (! cfg .NoSystemP )))
183- builder .WriteString (fmt .Sprintf ("• 以AI语音输出:%s\n " , yesNo (! cfg .NoRecord )))
184- return builder .String ()
185- }
186-
187- func maskKey (key string ) string {
188- if len (key ) <= 4 {
189- return "****"
190- }
191- return key [:2 ] + strings .Repeat ("*" , len (key )- 4 ) + key [len (key )- 2 :]
192- }
193-
194- func yesNo (b bool ) string {
195- if b {
196- return "是"
197- }
198- return "否"
199- }
0 commit comments