Skip to content

Commit

Permalink
update (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo authored and bincooo committed Jan 1, 2025
1 parent f57eaee commit 078329d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/bincooo/coze-api v1.0.2-0.20241222100337-a88cc26a1a5a
github.com/bincooo/edge-api v1.0.4-0.20241229152313-75901c4f8730
github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381
github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86
github.com/bincooo/you.com v0.0.0-20250101212728-8baa0b98edea
github.com/bogdanfinn/tls-client v1.7.7
github.com/dlclark/regexp2 v1.11.4
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/bincooo/emit.io v1.0.1-0.20241222074906-3b397f33e381 h1:28pzQ6Dsvy7J8
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=
github.com/bincooo/go-annotation v0.0.0-20241210101123-2fc3053d2f16/go.mod h1:3ZV3/eOQJ/28O5jINzNi98+K+8WY/pOvDYidT7WWaFs=
github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86 h1:RD/R5pW0ARZy+WMEYXeJ5jUtl+Zd2Og0lL9EfvRvQPs=
github.com/bincooo/you.com v0.0.0-20241226004948-53c5f0da9b86/go.mod h1:BmBo0qPN22FWFtyByP+3XfRr4lvlpGrOGWKyLoDB4rU=
github.com/bincooo/you.com v0.0.0-20250101212728-8baa0b98edea h1:BCC9FBk1MGmTdRzsBfSHBahoFMFypBp0pjPDGMW1O8E=
github.com/bincooo/you.com v0.0.0-20250101212728-8baa0b98edea/go.mod h1:M7kpyfEvvZ6hzhUtizkSSvnzzhA+RT1HcYDucPQQLLY=
github.com/bogdanfinn/fhttp v0.5.28 h1:G6thT8s8v6z1IuvXMUsX9QKy3ZHseTQTzxuIhSiaaAw=
github.com/bogdanfinn/fhttp v0.5.28/go.mod h1:oJiYPG3jQTKzk/VFmogH8jxjH5yiv2rrOH48Xso2lrE=
github.com/bogdanfinn/tls-client v1.7.7 h1:c3mf6LX6bxEsunJhP2BJeJE7qN/7BniWUpIpBc9Igu8=
Expand Down
42 changes: 42 additions & 0 deletions relay/llm/bing/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,54 @@ import (
"chatgpt-adapter/core/gin/inter"
"chatgpt-adapter/core/gin/model"
"chatgpt-adapter/core/gin/response"
"chatgpt-adapter/core/logger"
"context"
"errors"
"github.com/bincooo/edge-api"
"github.com/bincooo/emit.io"
"github.com/gin-gonic/gin"
"github.com/iocgo/sdk/env"
"github.com/iocgo/sdk/stream"
"slices"
"strings"
"sync"
"time"
)

var (
Model = "bing"

mu sync.Mutex
scheduleKeys []string
)

func init() { go timer() }

// 保活
func timer() {
t := time.NewTimer(time.Hour)
cacheManager := cache.BingCacheManager()
for {
select {
case <-t.C:
for _, k := range scheduleKeys[:] {
ident, err := cacheManager.GetValue(k)
if err != nil {
logger.Error(err)
continue
}

timeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
_, err = genToken(timeout, ident, true)
if err != nil {
logger.Error(err)
}
cancel()
}
}
}
}

type api struct {
inter.BaseAdapter

Expand Down Expand Up @@ -191,9 +224,18 @@ func genToken(ctx context.Context, ident string, new bool) (accessToken string,

err = cacheManager.SetWithExpiration(ident, accessToken, 48*time.Hour)
accessToken = strings.Split(accessToken, "|")[1]
setTokenTimer(ident)
return
}

func setTokenTimer(ident string) {
mu.Lock()
defer mu.Unlock()
if !slices.Contains(scheduleKeys, ident) {
scheduleKeys = append(scheduleKeys, ident)
}
}

func elseOf[T any](condition bool, t1, t2 T) T {
if condition {
return t1
Expand Down

0 comments on commit 078329d

Please sign in to comment.