Skip to content

Commit 7164a9e

Browse files
committed
feat: coze 新增webSdk模式
1 parent bee1705 commit 7164a9e

File tree

5 files changed

+108
-63
lines changed

5 files changed

+108
-63
lines changed

config.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ llm:
1515
baseUrl: "http://127.0.0.1:8080"
1616
model: "bing"
1717
token: "xxx"
18-
# coze 默认配置;;;内置配置经常变动,难以维护改为配置化
18+
19+
# coze 默认配置;;;内置配置经常变动,难以维护改为配置化。新增webSdk模式,但还未得知速率是否有限制???
1920
#coze:
2021
# 8k:
2122
# botId: 114514
2223
# version: 9527
2324
# scene: 2
24-
# isO: true
25+
# isO: false
26+
# isW: false
2527
# 128k:
2628
# botId: 114514
2729
# version: 9527
2830
# scene: 2
29-
# isO: true
31+
# isO: false
32+
# isW: false
3033
# images:
3134
# botId: 114514
3235
# version: 9527

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/antonfisher/nested-logrus-formatter v1.3.1
77
github.com/bincooo/claude-api v1.0.4-0.20240528113818-a9ce288f9fe6
88
github.com/bincooo/cohere-api v0.0.0-20240528114307-7566acc0f00c
9-
github.com/bincooo/coze-api v1.0.2-0.20240606225815-85aeed60c707
9+
github.com/bincooo/coze-api v1.0.2-0.20240608014519-965b46c0b685
1010
github.com/bincooo/edge-api v1.0.4-0.20240528170427-1e84d9c6ae62
1111
github.com/bincooo/emit.io v0.0.0-20240530174536-ed3f9ef9eaa9
1212
github.com/dlclark/regexp2 v1.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ github.com/bincooo/claude-api v1.0.4-0.20240528113818-a9ce288f9fe6 h1:gGSvsqVtgv
5656
github.com/bincooo/claude-api v1.0.4-0.20240528113818-a9ce288f9fe6/go.mod h1:qoD2FHwGq2+Ohl5xi+jFFYjAGxP0V1ImeIg6R05uqPQ=
5757
github.com/bincooo/cohere-api v0.0.0-20240528114307-7566acc0f00c h1:6uiML4R5LeAxigqK0yKdoHbqabSzR4EO5NEoTUh2FzA=
5858
github.com/bincooo/cohere-api v0.0.0-20240528114307-7566acc0f00c/go.mod h1:/mR16bJb7+z329/Al+TOCQ+NFkWWnggCrr0Hqdfo5cQ=
59-
github.com/bincooo/coze-api v1.0.2-0.20240606225815-85aeed60c707 h1:zWRmkTs6OIP5rPwHTAoRknMeUUo9Y/t9L2gGh+z8raE=
60-
github.com/bincooo/coze-api v1.0.2-0.20240606225815-85aeed60c707/go.mod h1:9aui20MScducmz8FFwcvKmzCGr6yugEnqE8hLtBiVkQ=
59+
github.com/bincooo/coze-api v1.0.2-0.20240608014519-965b46c0b685 h1:4m1ndn48a0Bc5NpjdrSd/N+LJAckLeAOd0HJEK7GM2A=
60+
github.com/bincooo/coze-api v1.0.2-0.20240608014519-965b46c0b685/go.mod h1:9aui20MScducmz8FFwcvKmzCGr6yugEnqE8hLtBiVkQ=
6161
github.com/bincooo/edge-api v1.0.4-0.20240528170427-1e84d9c6ae62 h1:qkqz9DtAOmX6Zipm+LRT3ORBkGwTITb4lDVXwcNVxc8=
6262
github.com/bincooo/edge-api v1.0.4-0.20240528170427-1e84d9c6ae62/go.mod h1:vs2s7Jx1x/ot/JcgfHlp1QttqvdSz0HUr0QEux6COGk=
6363
github.com/bincooo/emit.io v0.0.0-20240530174536-ed3f9ef9eaa9 h1:cDgPKdKIHJNdSEfXFpz2C8EzOdSfdGty5mS+72B31zI=

internal/plugin/llm/coze/adapter.go

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/bincooo/chatgpt-adapter/logger"
1010
"github.com/bincooo/chatgpt-adapter/pkg"
1111
"github.com/bincooo/coze-api"
12+
"github.com/bincooo/emit.io"
1213
"github.com/gin-gonic/gin"
1314
"net/http"
1415
"strconv"
@@ -31,12 +32,14 @@ var (
3132
version8k = "9527"
3233
scene8k = 2
3334
o8k = false
35+
w8k = false
3436

3537
// 128k
3638
botId128k = "114514"
3739
version128k = "9527"
3840
scene128k = 2
3941
o128k = false
42+
w128k = false
4043

4144
mu sync.Mutex
4245
rwMus = make(map[string]*common.ExpireLock)
@@ -56,6 +59,7 @@ func init() {
5659
version8k = fmt.Sprintf("%v", config["version"])
5760
scene8k = config["scene"].(int)
5861
o8k = config["iso"].(bool)
62+
w8k = config["isw"].(bool)
5963
}
6064
}
6165

@@ -67,6 +71,7 @@ func init() {
6771
version128k = fmt.Sprintf("%v", config["version"])
6872
scene128k = config["scene"].(int)
6973
o128k = config["iso"].(bool)
74+
w128k = config["isw"].(bool)
7075
}
7176
}
7277

@@ -120,7 +125,6 @@ func (API) Completion(ctx *gin.Context) {
120125
var (
121126
cookie = ctx.GetString("token")
122127
proxies = ctx.GetString("proxies")
123-
notebook = ctx.GetBool("notebook")
124128
completion = common.GetGinCompletion(ctx)
125129
matchers = common.GetGinMatchers(ctx)
126130
)
@@ -139,7 +143,7 @@ func (API) Completion(ctx *gin.Context) {
139143
}
140144

141145
ctx.Set(ginTokens, tokens)
142-
options, isO, err := newOptions(proxies, completion.Model, pMessages)
146+
options, mode, err := newOptions(proxies, completion.Model, pMessages)
143147
if err != nil {
144148
response.Error(ctx, -1, err)
145149
return
@@ -149,54 +153,19 @@ func (API) Completion(ctx *gin.Context) {
149153
chat := coze.New(co, msToken, options)
150154

151155
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)
192160
return
193161
}
162+
lock = l
194163
}
195164

196165
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]))
200169
} else {
201170
query = coze.MergeMessages(pMessages)
202171
}
@@ -226,6 +195,49 @@ func (API) Completion(ctx *gin.Context) {
226195
}
227196
}
228197

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+
229241
func (API) Generation(ctx *gin.Context) {
230242
var (
231243
cookie = ctx.GetString("token")
@@ -242,6 +254,7 @@ func (API) Generation(ctx *gin.Context) {
242254
options := coze.NewDefaultOptions(botId35_16k, version35_16k, scene35_16k, false, proxies)
243255
co, msToken := extCookie(cookie)
244256
chat := coze.New(co, msToken, options)
257+
245258
image, err := chat.Images(ctx.Request.Context(), generation.Message)
246259
if err != nil {
247260
logger.Error(err)
@@ -297,12 +310,17 @@ func customBotId(model string) string {
297310
return ""
298311
}
299312

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) {
301314
if strings.HasPrefix(model, "coze/") {
302315
values := strings.Split(model[5:], "-")
303316
scene, e := strconv.Atoi(values[2])
304317
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+
}
306324
options = coze.NewDefaultOptions(values[0], values[1], scene, isO, proxies)
307325
logger.Infof("using custom coze options: botId = %s, version = %s, scene = %d", values[0], values[1], scene)
308326
return
@@ -317,7 +335,11 @@ func newOptions(proxies string, model string, pMessages []coze.Message) (options
317335
return
318336
}
319337

320-
isO = o8k
338+
if o8k {
339+
mode = 'o'
340+
} else if w8k {
341+
mode = 'w'
342+
}
321343
options = coze.NewDefaultOptions(botId8k, version8k, scene8k, o8k, proxies)
322344
// 大于7k token 使用 gpt-128k
323345
if token := calcTokens(pMessages); token > 7000 {
@@ -326,7 +348,11 @@ func newOptions(proxies string, model string, pMessages []coze.Message) (options
326348
return
327349
}
328350

329-
isO = o128k
351+
if o128k {
352+
mode = 'o'
353+
} else if w128k {
354+
mode = 'w'
355+
}
330356
options = coze.NewDefaultOptions(botId128k, version128k, scene128k, o128k, proxies)
331357
}
332358

@@ -347,8 +373,9 @@ func extCookie(co string) (cookie, msToken string) {
347373
}
348374

349375
func isOwner(model string) bool {
350-
if model == Model {
351-
352-
}
353376
return strings.HasSuffix(model, "-o")
354377
}
378+
379+
func isWebSdk(model string) bool {
380+
return strings.HasSuffix(model, "-w")
381+
}

internal/plugin/llm/coze/toolcall.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package coze
22

33
import (
4+
"github.com/bincooo/chatgpt-adapter/internal/common"
45
"github.com/bincooo/chatgpt-adapter/internal/gin.handler/response"
56
"github.com/bincooo/chatgpt-adapter/internal/plugin"
67
"github.com/bincooo/chatgpt-adapter/logger"
@@ -14,7 +15,6 @@ import (
1415
func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.ChatCompletion) bool {
1516
logger.Info("completeTools ...")
1617
exec, err := plugin.CompleteToolCalls(ctx, completion, func(message string) (string, error) {
17-
var notebook = ctx.GetBool("notebook")
1818
pMessages := []coze.Message{
1919
{
2020
Role: "system",
@@ -23,22 +23,37 @@ func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.
2323
}
2424

2525
co, msToken := extCookie(cookie)
26-
options, _, err := newOptions(proxies, "", pMessages)
26+
options, mode, err := newOptions(proxies, completion.Model, pMessages)
2727
if err != nil {
2828
return "", err
2929
}
3030

3131
chat := coze.New(co, msToken, options)
32+
var lock *common.ExpireLock
33+
if mode == 'o' {
34+
lock, err = draftBot(ctx, pMessages, chat, completion)
35+
if err != nil {
36+
return "", err
37+
}
38+
}
3239

3340
query := ""
34-
if notebook && len(pMessages) > 0 {
35-
// notebook 模式只取第一条 content
36-
query = pMessages[0].Content
41+
if mode == 'w' {
42+
query = pMessages[len(pMessages)-1].Content
43+
chat.WebSdk(chat.TransferMessages(pMessages[:len(pMessages)-1]))
3744
} else {
3845
query = coze.MergeMessages(pMessages)
3946
}
4047

4148
chatResponse, err := chat.Reply(ctx.Request.Context(), coze.Text, query)
49+
// 构建完请求即可解锁
50+
if lock != nil {
51+
lock.Unlock()
52+
botId := customBotId(completion.Model)
53+
rmLock(botId)
54+
logger.Infof("构建完成解锁:%s", botId)
55+
}
56+
4257
if err != nil {
4358
return "", err
4459
}

0 commit comments

Comments
 (0)