diff --git a/core/cache/memory.go b/core/cache/memory.go index 8a96260..25bf2f8 100644 --- a/core/cache/memory.go +++ b/core/cache/memory.go @@ -21,8 +21,8 @@ type Manager[T any] struct { var ( toolTasksCacheManager *Manager[[]model.Keyv[string]] - - windsurfCacheManager *Manager[string] + windsurfCacheManager *Manager[string] + bingCacheManager *Manager[string] ) func init() { @@ -36,6 +36,11 @@ func init() { windsurfCacheManager = &Manager[string]{ cache.New[string](gocacheStore.NewGoCache(client)), } + + client = gocache.New(5*time.Minute, 5*time.Minute) + bingCacheManager = &Manager[string]{ + cache.New[string](gocacheStore.NewGoCache(client)), + } }) } @@ -47,6 +52,10 @@ func WindsurfCacheManager() *Manager[string] { return windsurfCacheManager } +func BingCacheManager() *Manager[string] { + return bingCacheManager +} + func (cacheManager *Manager[T]) SetValue(key string, value T) error { return cacheManager.SetWithExpiration(key, value, 120*time.Second) } diff --git a/go.mod b/go.mod index 58cfb50..f56f356 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.3 require ( github.com/antonfisher/nested-logrus-formatter v1.3.1 github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a - github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370 + github.com/bincooo/edge-api v1.0.4-0.20241229054502-4e2a9a08765e github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381 github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86 github.com/bogdanfinn/tls-client v1.7.7 @@ -25,10 +25,8 @@ require ( ) //github.com/iocgo/sdk v0.0.0-20241129021727-ca323c08f298 => ../sdk -replace ( - github.com/bincooo/edge-api v1.0.4-0.20241209140454-b708b5d57059 => ../edge-api - github.com/samber/do/v2 v2.0.0-beta.7 => github.com/iocgo/do/v2 v2.0.0-patch.0.20241204032939-7bbcadbc5f38 -) +//github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370 => ../edge-api +replace github.com/samber/do/v2 v2.0.0-beta.7 => github.com/iocgo/do/v2 v2.0.0-patch.0.20241204032939-7bbcadbc5f38 require ( github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 // indirect diff --git a/go.sum b/go.sum index 0dd954d..2c968af 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a h1:oBGzNQ6EjtBhNR9s629bx4NpYqoiAo4DXXnrIok2HeU= github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a/go.mod h1:ZCiekJWTzAnwJyn7qdClip40nK9Ix54cHZHXhtKK8iQ= -github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370 h1:r7Tcvb0kFlWZkeQRNFrk9SuEhw0S3alpAA0Twk3XUy0= -github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370/go.mod h1:JMD3hj34imIyW8GLD2cmhynEbG7jXuUQG2/WWtjgzTw= +github.com/bincooo/edge-api v1.0.4-0.20241229054502-4e2a9a08765e h1:ro2bwq/oznWnKUUTAeMg5UaOPI4jlkEL8GAz/+37/FI= +github.com/bincooo/edge-api v1.0.4-0.20241229054502-4e2a9a08765e/go.mod h1:sBHg6xfFnLlkYYMDtYL+MkkXUOlF1+V0xFZ30vd4ZBw= github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381 h1:28pzQ6Dsvy7J8liRQSBtaF3Pxt8ZUR2Nl/VczlXFWEI= github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381/go.mod h1:cPNK/qXkuZp+YXYnkRatPMlGUlCD0rqJ8CeAqLAe+LA= github.com/bincooo/go-annotation v0.0.0-20241210101123-2fc3053d2f16 h1:/2MZmTiHa1a+hfUsmZxvOjQh1+lfZU/9gd7QRjD+xo8= diff --git a/relay/llm/bing/adapter.go b/relay/llm/bing/adapter.go index 60a4739..0dee547 100644 --- a/relay/llm/bing/adapter.go +++ b/relay/llm/bing/adapter.go @@ -1,6 +1,7 @@ package bing import ( + "chatgpt-adapter/core/cache" "chatgpt-adapter/core/common" "chatgpt-adapter/core/common/toolcall" "chatgpt-adapter/core/common/vars" @@ -93,18 +94,25 @@ func (api *api) Completion(ctx *gin.Context) (err error) { timeout, cancel := context.WithTimeout(ctx.Request.Context(), 10*time.Second) defer cancel() - conversationId, err := edge.CreateConversation(elseOf(proxied, common.HTTPClient, common.NopHTTPClient), timeout, cookie) + accessToken, err := genToken(timeout, cookie) + if err != nil { + return + } + + timeout, cancel = context.WithTimeout(ctx.Request.Context(), 10*time.Second) + defer cancel() + conversationId, err := edge.CreateConversation(elseOf(proxied, common.HTTPClient, common.NopHTTPClient), timeout, accessToken) if err != nil { return } timeout, cancel = context.WithTimeout(context.TODO(), 10*time.Second) defer cancel() - defer edge.DeleteConversation(common.HTTPClient, timeout, conversationId, cookie) + defer edge.DeleteConversation(common.HTTPClient, timeout, conversationId, accessToken) challenge := "" label: - message, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), cookie, conversationId, challenge, request, "从[\n\nAi:]处继续回复,\n\n当前问题是: "+query) + message, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), accessToken, conversationId, challenge, request, "从[\n\nAi:]处继续回复,\n\n当前问题是: "+query) if err != nil { if challenge == "" && err.Error() == "challenge" { challenge, err = hookCloudflare() @@ -135,6 +143,22 @@ func convertRequest(ctx *gin.Context, completion model.Completion) (content stri return } +func genToken(ctx context.Context, ident string) (accessToken string, err error) { + cacheManager := cache.BingCacheManager() + accessToken, err = cacheManager.GetValue(ident) + if err != nil || accessToken != "" { + return + } + + accessToken, err = edge.RefreshToken(common.HTTPClient, ctx, ident) + if err != nil { + return + } + + err = cacheManager.SetWithExpiration(ident, accessToken, 12*time.Hour) + return +} + func elseOf[T any](condition bool, t1, t2 T) T { if condition { return t1