Skip to content

Commit

Permalink
feat(bing): 自动过盾(#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo authored and bincooo committed Dec 28, 2024
1 parent 31208d9 commit 5a71b4e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 12 deletions.
4 changes: 2 additions & 2 deletions core/gin/response/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ var (
func bingFormater(role string) string {
switch role {
case "user":
return "Hu: "
return "Human: "
case "assistant":
return "Ai: "
default:
return "Sys: "
return "Instructions: "
}
}

Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20241209140454-b708b5d57059
github.com/bincooo/edge-api v1.0.4-0.20241228201804-a3b98723b370
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
Expand All @@ -25,7 +25,10 @@ require (
)

//github.com/iocgo/sdk v0.0.0-20241129021727-ca323c08f298 => ../sdk
replace github.com/samber/do/v2 v2.0.0-beta.7 => github.com/iocgo/do/v2 v2.0.0-patch.0.20241204032939-7bbcadbc5f38
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
)

require (
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.20241209140454-b708b5d57059 h1:3TcNsTaRspXSGqz0aMHuf8gSEKUkZ4yxmmSsFuJCRxU=
github.com/bincooo/edge-api v1.0.4-0.20241209140454-b708b5d57059/go.mod h1:JMD3hj34imIyW8GLD2cmhynEbG7jXuUQG2/WWtjgzTw=
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/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=
Expand Down
18 changes: 16 additions & 2 deletions relay/llm/bing/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (api *api) Completion(ctx *gin.Context) (err error) {
return
}

query := ""
if i := len(completion.Messages) - 1; completion.Messages[i].Is("role", "user") {
query = completion.Messages[i].GetString("content")
completion.Messages = completion.Messages[:i]
}
request := convertRequest(ctx, completion)

timeout, cancel := context.WithTimeout(ctx.Request.Context(), 10*time.Second)
Expand All @@ -95,10 +100,19 @@ func (api *api) Completion(ctx *gin.Context) (err error) {

timeout, cancel = context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
defer edge.DeleteConversation(elseOf(proxied, common.HTTPClient, common.NopHTTPClient), timeout, conversationId, cookie)
defer edge.DeleteConversation(common.HTTPClient, timeout, conversationId, cookie)

message, err := edge.Chat(elseOf(proxied, common.HTTPClient, common.NopHTTPClient), ctx.Request.Context(), cookie, conversationId, request)
challenge := ""
label:
message, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), cookie, conversationId, challenge, request, "从[\n\nAi:]处继续回复,\n\n当前问题是: "+query)
if err != nil {
if challenge == "" && err.Error() == "challenge" {
challenge, err = hookCloudflare()
if err != nil {
return
}
goto label
}
return
}

Expand Down
61 changes: 59 additions & 2 deletions relay/llm/bing/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"chatgpt-adapter/core/common/toolcall"
"chatgpt-adapter/core/gin/model"
"encoding/json"
"errors"
"fmt"
"github.com/bincooo/emit.io"
"github.com/iocgo/sdk/env"
"net/http"
"time"

"chatgpt-adapter/core/common"
Expand All @@ -18,7 +22,7 @@ const (
ginTokens = "__tokens__"
)

func waitMessage(message chan []byte, cancel func(str string) bool) (content string, err error) {
func waitMessage(message chan []byte, cb func(string), cancel func(str string) bool) (content string, err error) {
for {
chunk, ok := <-message
if !ok {
Expand All @@ -28,6 +32,18 @@ func waitMessage(message chan []byte, cancel func(str string) bool) (content str
magic := chunk[0]
chunk = chunk[1:]
if magic == 1 {
//if string(chunk) == "challenge" {
// if challenge != "" {
// cb(challenge)
// challenge = ""
// continue
// }
//
// challenge, err = hookCloudflare()
// if err != nil {
// break
// }
//}
err = fmt.Errorf("%s", chunk)
break
}
Expand Down Expand Up @@ -151,9 +167,50 @@ func asError(ctx *gin.Context, msg interface{}) {
return
}
logger.Error(msg)

if response.NotSSEHeader(ctx) {
response.Error(ctx, -1, msg)
}
return
}

func hookCloudflare() (challenge string, err error) {
baseUrl := env.Env.GetString("browser-less.reversal")
if !env.Env.GetBool("browser-less.enabled") && baseUrl == "" {
return "", errors.New("trying cloudflare failed, please setting `browser-less.enabled` or `browser-less.reversal`")
}

logger.Info("trying cloudflare ...")
if baseUrl == "" {
baseUrl = "http://127.0.0.1:" + env.Env.GetString("browser-less.port")
}

r, err := emit.ClientBuilder(common.HTTPClient).
GET(baseUrl+"/bing/clearance").
DoC(emit.Status(http.StatusOK), emit.IsJSON)
if err != nil {
logger.Error(err)
if emit.IsJSON(r) == nil {
logger.Error(emit.TextResponse(r))
}
return
}

defer r.Body.Close()
obj, err := emit.ToMap(r)
if err != nil {
logger.Error(err)
return
}

if data, ok := obj["data"].(string); ok {
challenge = data
return
}

msg := "challenge failed"
if data, ok := obj["msg"].(string); ok {
msg = data
}
err = errors.New(msg)
return
}
14 changes: 12 additions & 2 deletions relay/llm/bing/toolcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func toolChoice(ctx *gin.Context, completion model.Completion) bool {
cookie := ctx.GetString("token")

exec, err := toolcall.ToolChoice(ctx, completion, func(message string) (string, error) {
message += "\n\nAi:"
if echo {
logger.Infof("toolCall message: \n%s", message)
return "", nil
Expand All @@ -31,12 +32,21 @@ func toolChoice(ctx *gin.Context, completion model.Completion) bool {
return "", err
}

buffer, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), cookie, conversationId, message)
challenge := ""
label:
buffer, err := edge.Chat(common.HTTPClient, ctx.Request.Context(), cookie, conversationId, challenge, message, "从[\n\nAi:]处继续回复")
if err != nil {
if challenge == "" && err.Error() == "challenge" {
challenge, err = hookCloudflare()
if err != nil {
return "", err
}
goto label
}
return "", err
}

return waitMessage(buffer, toolcall.Cancel)
return waitMessage(buffer, nil, toolcall.Cancel)
})

if err != nil {
Expand Down

0 comments on commit 5a71b4e

Please sign in to comment.