9
9
"github.com/bincooo/chatgpt-adapter/logger"
10
10
"github.com/bincooo/chatgpt-adapter/pkg"
11
11
"github.com/bincooo/coze-api"
12
+ "github.com/bincooo/emit.io"
12
13
"github.com/gin-gonic/gin"
13
14
"net/http"
14
15
"strconv"
@@ -31,12 +32,14 @@ var (
31
32
version8k = "9527"
32
33
scene8k = 2
33
34
o8k = false
35
+ w8k = false
34
36
35
37
// 128k
36
38
botId128k = "114514"
37
39
version128k = "9527"
38
40
scene128k = 2
39
41
o128k = false
42
+ w128k = false
40
43
41
44
mu sync.Mutex
42
45
rwMus = make (map [string ]* common.ExpireLock )
@@ -56,6 +59,7 @@ func init() {
56
59
version8k = fmt .Sprintf ("%v" , config ["version" ])
57
60
scene8k = config ["scene" ].(int )
58
61
o8k = config ["iso" ].(bool )
62
+ w8k = config ["isw" ].(bool )
59
63
}
60
64
}
61
65
@@ -67,6 +71,7 @@ func init() {
67
71
version128k = fmt .Sprintf ("%v" , config ["version" ])
68
72
scene128k = config ["scene" ].(int )
69
73
o128k = config ["iso" ].(bool )
74
+ w128k = config ["isw" ].(bool )
70
75
}
71
76
}
72
77
@@ -120,7 +125,6 @@ func (API) Completion(ctx *gin.Context) {
120
125
var (
121
126
cookie = ctx .GetString ("token" )
122
127
proxies = ctx .GetString ("proxies" )
123
- notebook = ctx .GetBool ("notebook" )
124
128
completion = common .GetGinCompletion (ctx )
125
129
matchers = common .GetGinMatchers (ctx )
126
130
)
@@ -139,7 +143,7 @@ func (API) Completion(ctx *gin.Context) {
139
143
}
140
144
141
145
ctx .Set (ginTokens , tokens )
142
- options , isO , err := newOptions (proxies , completion .Model , pMessages )
146
+ options , mode , err := newOptions (proxies , completion .Model , pMessages )
143
147
if err != nil {
144
148
response .Error (ctx , - 1 , err )
145
149
return
@@ -149,54 +153,19 @@ func (API) Completion(ctx *gin.Context) {
149
153
chat := coze .New (co , msToken , options )
150
154
151
155
var lock * common.ExpireLock
152
- if isO {
153
- var system string
154
- message := pMessages [0 ]
155
- if message .Role == "system" {
156
- system = message .Content
157
- }
158
-
159
- var value map [string ]interface {}
160
- value , err = chat .BotInfo (ctx .Request .Context ())
161
- if err != nil {
162
- logger .Error (err )
163
- response .Error (ctx , - 1 , err )
164
- return
165
- }
166
-
167
- // 加锁
168
- botId := customBotId (completion .Model )
169
- lock = newLock (botId )
170
- if ! lock .Lock (ctx .Request .Context ()) {
171
- // 上锁失败
172
- logger .Errorf ("上锁失败:%s" , botId )
173
- response .Error (ctx , http .StatusTooManyRequests , "Too Many Requests" )
174
- return
175
- }
176
-
177
- logger .Infof ("上锁成功:%s" , botId )
178
- if err = chat .DraftBot (ctx .Request .Context (), coze.DraftInfo {
179
- Model : value ["model" ].(string ),
180
- TopP : completion .TopP ,
181
- Temperature : completion .Temperature ,
182
- MaxTokens : completion .MaxTokens ,
183
- FrequencyPenalty : 0 ,
184
- PresencePenalty : 0 ,
185
- ResponseFormat : 0 ,
186
- }, system ); err != nil {
187
- // 全局配置修改失败,解锁
188
- lock .Unlock ()
189
- rmLock (botId )
190
- logger .Error (fmt .Errorf ("全局配置修改失败,解锁:%s, %v" , botId , err ))
191
- response .Error (ctx , - 1 , err )
156
+ if mode == 'o' {
157
+ l , eer := draftBot (ctx , pMessages , chat , completion )
158
+ if eer != nil {
159
+ response .Error (ctx , eer .Code , eer .Err )
192
160
return
193
161
}
162
+ lock = l
194
163
}
195
164
196
165
query := ""
197
- if notebook && len ( pMessages ) > 0 {
198
- // notebook 模式只取第一条 content
199
- query = pMessages [0 ]. Content
166
+ if mode == 'w' {
167
+ query = pMessages [ len ( pMessages ) - 1 ]. Content
168
+ chat . WebSdk ( chat . TransferMessages ( pMessages [: len ( pMessages ) - 1 ]))
200
169
} else {
201
170
query = coze .MergeMessages (pMessages )
202
171
}
@@ -226,6 +195,49 @@ func (API) Completion(ctx *gin.Context) {
226
195
}
227
196
}
228
197
198
+ // return true 终止
199
+ func draftBot (ctx * gin.Context , pMessages []coze.Message , chat coze.Chat , completion pkg.ChatCompletion ) (eLock * common.ExpireLock , emitErr * emit.Error ) {
200
+ var system string
201
+ message := pMessages [0 ]
202
+ if message .Role == "system" {
203
+ system = message .Content
204
+ }
205
+
206
+ var value map [string ]interface {}
207
+ value , err := chat .BotInfo (ctx .Request .Context ())
208
+ if err != nil {
209
+ logger .Error (err )
210
+ return nil , & emit.Error {Code : - 1 , Err : err }
211
+ }
212
+
213
+ // 加锁
214
+ botId := customBotId (completion .Model )
215
+ eLock = newLock (botId )
216
+ if ! eLock .Lock (ctx .Request .Context ()) {
217
+ // 上锁失败
218
+ logger .Errorf ("上锁失败:%s" , botId )
219
+ return nil , & emit.Error {Code : http .StatusTooManyRequests , Err : errors .New ("too Many Requests" )}
220
+ }
221
+
222
+ logger .Infof ("上锁成功:%s" , botId )
223
+ if err = chat .DraftBot (ctx .Request .Context (), coze.DraftInfo {
224
+ Model : value ["model" ].(string ),
225
+ TopP : completion .TopP ,
226
+ Temperature : completion .Temperature ,
227
+ MaxTokens : completion .MaxTokens ,
228
+ FrequencyPenalty : 0 ,
229
+ PresencePenalty : 0 ,
230
+ ResponseFormat : 0 ,
231
+ }, system ); err != nil {
232
+ // 全局配置修改失败,解锁
233
+ eLock .Unlock ()
234
+ rmLock (botId )
235
+ logger .Error (fmt .Errorf ("全局配置修改失败,解锁:%s, %v" , botId , err ))
236
+ return nil , & emit.Error {Code : - 1 , Err : err }
237
+ }
238
+ return eLock , nil
239
+ }
240
+
229
241
func (API ) Generation (ctx * gin.Context ) {
230
242
var (
231
243
cookie = ctx .GetString ("token" )
@@ -242,6 +254,7 @@ func (API) Generation(ctx *gin.Context) {
242
254
options := coze .NewDefaultOptions (botId35_16k , version35_16k , scene35_16k , false , proxies )
243
255
co , msToken := extCookie (cookie )
244
256
chat := coze .New (co , msToken , options )
257
+
245
258
image , err := chat .Images (ctx .Request .Context (), generation .Message )
246
259
if err != nil {
247
260
logger .Error (err )
@@ -297,12 +310,17 @@ func customBotId(model string) string {
297
310
return ""
298
311
}
299
312
300
- func newOptions (proxies string , model string , pMessages []coze.Message ) (options coze.Options , isO bool , err error ) {
313
+ func newOptions (proxies string , model string , pMessages []coze.Message ) (options coze.Options , mode byte , err error ) {
301
314
if strings .HasPrefix (model , "coze/" ) {
302
315
values := strings .Split (model [5 :], "-" )
303
316
scene , e := strconv .Atoi (values [2 ])
304
317
if e == nil {
305
- isO = isOwner (model )
318
+ isO := isOwner (model )
319
+ if isO {
320
+ mode = 'o'
321
+ } else if isWebSdk (model ) {
322
+ mode = 'w'
323
+ }
306
324
options = coze .NewDefaultOptions (values [0 ], values [1 ], scene , isO , proxies )
307
325
logger .Infof ("using custom coze options: botId = %s, version = %s, scene = %d" , values [0 ], values [1 ], scene )
308
326
return
@@ -317,7 +335,11 @@ func newOptions(proxies string, model string, pMessages []coze.Message) (options
317
335
return
318
336
}
319
337
320
- isO = o8k
338
+ if o8k {
339
+ mode = 'o'
340
+ } else if w8k {
341
+ mode = 'w'
342
+ }
321
343
options = coze .NewDefaultOptions (botId8k , version8k , scene8k , o8k , proxies )
322
344
// 大于7k token 使用 gpt-128k
323
345
if token := calcTokens (pMessages ); token > 7000 {
@@ -326,7 +348,11 @@ func newOptions(proxies string, model string, pMessages []coze.Message) (options
326
348
return
327
349
}
328
350
329
- isO = o128k
351
+ if o128k {
352
+ mode = 'o'
353
+ } else if w128k {
354
+ mode = 'w'
355
+ }
330
356
options = coze .NewDefaultOptions (botId128k , version128k , scene128k , o128k , proxies )
331
357
}
332
358
@@ -347,8 +373,9 @@ func extCookie(co string) (cookie, msToken string) {
347
373
}
348
374
349
375
func isOwner (model string ) bool {
350
- if model == Model {
351
-
352
- }
353
376
return strings .HasSuffix (model , "-o" )
354
377
}
378
+
379
+ func isWebSdk (model string ) bool {
380
+ return strings .HasSuffix (model , "-w" )
381
+ }
0 commit comments