11package aichat
22
33import (
4+ "errors"
45 "fmt"
56 "strconv"
67 "strings"
@@ -18,19 +19,92 @@ 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+ // ModelType 支持打印 string 并生产 protocal
32+ type ModelType int
33+
34+ func newModelType (typ string ) (ModelType , error ) {
35+ t , ok := apitypes [typ ]
36+ if ! ok {
37+ return 0 , errors .New ("未知类型 " + typ )
38+ }
39+ return ModelType (t ), nil
40+ }
41+
42+ func (mt ModelType ) String () string {
43+ return apilist [mt ]
44+ }
45+
46+ func (mt ModelType ) protocol (modn string , temp float32 , topp float32 , maxn uint ) (mod model.Protocol , err error ) {
47+ switch cfg .Type {
48+ case 0 :
49+ mod = model .NewOpenAI (
50+ modn , cfg .Separator ,
51+ temp , topp , maxn ,
52+ )
53+ case 1 :
54+ mod = model .NewOLLaMA (
55+ modn , cfg .Separator ,
56+ temp , topp , maxn ,
57+ )
58+ case 2 :
59+ mod = model .NewGenAI (
60+ modn ,
61+ temp , topp , maxn ,
62+ )
63+ default :
64+ err = errors .New ("unsupported model type " + strconv .Itoa (int (cfg .Type )))
65+ }
66+ return
67+ }
68+
69+ // ModelBool 支持打印成 "是/否"
70+ type ModelBool bool
71+
72+ func (mb ModelBool ) String () string {
73+ if mb {
74+ return "是"
75+ }
76+ return "否"
77+ }
78+
79+ // ModelKey 支持隐藏密钥
80+ type ModelKey string
81+
82+ func (mk ModelKey ) String () string {
83+ if len (mk ) == 0 {
84+ return "未设置"
85+ }
86+ if len (mk ) <= 4 {
87+ return "****"
88+ }
89+ key := string (mk )
90+ return key [:2 ] + strings .Repeat ("*" , len (key )- 4 ) + key [len (key )- 2 :]
91+ }
92+
2193type 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
94+ ModelName string
95+ ImageModelName string
96+ Type ModelType
97+ ImageType ModelType
98+ MaxN uint
99+ TopP float32
100+ SystemP string
101+ API string
102+ ImageAPI string
103+ Key ModelKey
104+ ImageKey ModelKey
105+ Separator string
106+ NoReplyAT ModelBool
107+ NoSystemP ModelBool
34108}
35109
36110func newconfig () config {
@@ -41,10 +115,47 @@ func newconfig() config {
41115 }
42116}
43117
118+ func (c * config ) String () string {
119+ topp , maxn := c .mparams ()
120+ sb := strings.Builder {}
121+ sb .WriteString (fmt .Sprintf ("• 模型名:%s\n " , c .ModelName ))
122+ sb .WriteString (fmt .Sprintf ("• 图像模型名:%s\n " , c .ImageModelName ))
123+ sb .WriteString (fmt .Sprintf ("• 接口类型:%v\n " , c .Type ))
124+ sb .WriteString (fmt .Sprintf ("• 图像接口类型:%v\n " , c .ImageType ))
125+ sb .WriteString (fmt .Sprintf ("• 最大长度:%d\n " , maxn ))
126+ sb .WriteString (fmt .Sprintf ("• TopP:%.1f\n " , topp ))
127+ sb .WriteString (fmt .Sprintf ("• 系统提示词:%s\n " , c .SystemP ))
128+ sb .WriteString (fmt .Sprintf ("• 接口地址:%s\n " , c .API ))
129+ sb .WriteString (fmt .Sprintf ("• 图像接口地址:%s\n " , c .ImageAPI ))
130+ sb .WriteString (fmt .Sprintf ("• 密钥:%v\n " , c .Key ))
131+ sb .WriteString (fmt .Sprintf ("• 图像密钥:%v\n " , c .ImageKey ))
132+ sb .WriteString (fmt .Sprintf ("• 分隔符:%s\n " , c .Separator ))
133+ sb .WriteString (fmt .Sprintf ("• 响应@:%v\n " , ! c .NoReplyAT ))
134+ sb .WriteString (fmt .Sprintf ("• 支持系统提示词:%v\n " , ! c .NoSystemP ))
135+ return sb .String ()
136+ }
137+
44138func (c * config ) isvalid () bool {
45139 return c .ModelName != "" && c .API != "" && c .Key != ""
46140}
47141
142+ // 获取全局模型参数:TopP和最大长度
143+ func (c * config ) mparams () (topp float32 , maxn uint ) {
144+ // 处理TopP参数
145+ topp = c .TopP
146+ if topp == 0 {
147+ topp = 0.9
148+ }
149+
150+ // 处理最大长度参数
151+ maxn = c .MaxN
152+ if maxn == 0 {
153+ maxn = 4096
154+ }
155+
156+ return topp , maxn
157+ }
158+
48159func ensureconfig (ctx * zero.Ctx ) bool {
49160 c , ok := ctx .State ["manager" ].(* ctrl.Control [* zero.Ctx ])
50161 if ! ok {
@@ -62,7 +173,7 @@ func ensureconfig(ctx *zero.Ctx) bool {
62173 return true
63174}
64175
65- func newextrasetstr (ptr * string ) func (ctx * zero.Ctx ) {
176+ func newextrasetstr [ T ~ string ] (ptr * T ) func (ctx * zero.Ctx ) {
66177 return func (ctx * zero.Ctx ) {
67178 args := strings .TrimSpace (ctx .State ["args" ].(string ))
68179 if args == "" {
@@ -74,7 +185,7 @@ func newextrasetstr(ptr *string) func(ctx *zero.Ctx) {
74185 ctx .SendChain (message .Text ("ERROR: no such plugin" ))
75186 return
76187 }
77- * ptr = args
188+ * ptr = T ( args )
78189 err := c .SetExtra (& cfg )
79190 if err != nil {
80191 ctx .SendChain (message .Text ("ERROR: set extra err: " , err ))
@@ -84,7 +195,7 @@ func newextrasetstr(ptr *string) func(ctx *zero.Ctx) {
84195 }
85196}
86197
87- func newextrasetbool (ptr * bool ) func (ctx * zero.Ctx ) {
198+ func newextrasetbool [ T ~ bool ] (ptr * T ) func (ctx * zero.Ctx ) {
88199 return func (ctx * zero.Ctx ) {
89200 args := ctx .State ["regex_matched" ].([]string )
90201 isno := args [1 ] == "不"
@@ -93,7 +204,7 @@ func newextrasetbool(ptr *bool) func(ctx *zero.Ctx) {
93204 ctx .SendChain (message .Text ("ERROR: no such plugin" ))
94205 return
95206 }
96- * ptr = isno
207+ * ptr = T ( isno )
97208 err := c .SetExtra (& cfg )
98209 if err != nil {
99210 ctx .SendChain (message .Text ("ERROR: set extra err: " , err ))
@@ -156,44 +267,3 @@ func newextrasetfloat32(ptr *float32) func(ctx *zero.Ctx) {
156267 ctx .SendChain (message .Text ("成功" ))
157268 }
158269}
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