Skip to content

Commit

Permalink
fix(lmsys): 官网请求模式修改同步(#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed Sep 5, 2024
1 parent d2af642 commit ef57d71
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 7 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/bincooo/claude-api v1.0.5-0.20240814184031-56ad31d90db8
github.com/bincooo/cohere-api v0.0.0-20240620172202-4b7697448b46
github.com/bincooo/coze-api v1.0.2-0.20240814183726-b9eb08bb183c
github.com/bincooo/coze-api v1.0.2-0.20240828163652-7f48a7682de1
github.com/bincooo/edge-api v1.0.4-0.20240801160633-4131c13986a5
github.com/bincooo/emit.io v1.0.1-0.20240814182208-c5236f8f202c
github.com/bincooo/vecmul.com v0.0.0-20240806095224-6e571cbe7b7e
github.com/bincooo/you.com v0.0.0-20240819002641-ea605dd2c07c
github.com/bogdanfinn/tls-client v1.7.7
github.com/dlclark/regexp2 v1.7.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ github.com/bincooo/claude-api v1.0.5-0.20240814184031-56ad31d90db8 h1:YTa3k9RjFS
github.com/bincooo/claude-api v1.0.5-0.20240814184031-56ad31d90db8/go.mod h1:1C9dP0HFj9CjYUfz4l3gyoNGXKafPlXDTfBaIrh8ZLY=
github.com/bincooo/cohere-api v0.0.0-20240620172202-4b7697448b46 h1:12sHtk9r99icBCTBnsHaYep5DjH4gu6AzceFNSf216U=
github.com/bincooo/cohere-api v0.0.0-20240620172202-4b7697448b46/go.mod h1:X2PunEb/JOvEnkKkQnlm27UZnn96TR53hGH5TZ55Ak4=
github.com/bincooo/coze-api v1.0.2-0.20240814183726-b9eb08bb183c h1:6sPwC9XiRQ2WKlwH3VJr8OCttKTJplSMpmwThXiXYzE=
github.com/bincooo/coze-api v1.0.2-0.20240814183726-b9eb08bb183c/go.mod h1:DbHf5dv8h0bGrQoWoCyVTFJzsfqTOb7asqzn3gTfTas=
github.com/bincooo/coze-api v1.0.2-0.20240828163652-7f48a7682de1 h1:nLgQQD6i4+NXRVGrSyvPsDG3/mF+tPcV2PStKEKubdc=
github.com/bincooo/coze-api v1.0.2-0.20240828163652-7f48a7682de1/go.mod h1:DbHf5dv8h0bGrQoWoCyVTFJzsfqTOb7asqzn3gTfTas=
github.com/bincooo/edge-api v1.0.4-0.20240801160633-4131c13986a5 h1:LXwjKFfwyOSinPSKMRI8mRNUkkLhJoexl8jGn/t9ZOk=
github.com/bincooo/edge-api v1.0.4-0.20240801160633-4131c13986a5/go.mod h1:OBBrqmW8rUMTsF/sPH1GsPQT5K9QKJCZkHdYkTIR09Q=
github.com/bincooo/emit.io v1.0.1-0.20240814182208-c5236f8f202c h1:+E1m0U39B0AA54+/p75+yz+GoN1yAFQ99SCuRs9tRsM=
github.com/bincooo/emit.io v1.0.1-0.20240814182208-c5236f8f202c/go.mod h1:OJbKJoZ6x6vSpCC+JNtfcaXo3ilpvQscWrcGEmtmrZI=
github.com/bincooo/vecmul.com v0.0.0-20240806095224-6e571cbe7b7e h1:NEPtfaeaLrY5fSAykG9gopRovHwIvORJGGCSRhVyHTQ=
github.com/bincooo/vecmul.com v0.0.0-20240806095224-6e571cbe7b7e/go.mod h1:YqrsM+ORzIuR/2guKHE89xHiJ1+tvHm+aPV7pVbvFO4=
github.com/bincooo/you.com v0.0.0-20240819002641-ea605dd2c07c h1:gaQBt/jOQhKIWMSCcjzprivs2BBESYd/fdFbk1EA0AI=
github.com/bincooo/you.com v0.0.0-20240819002641-ea605dd2c07c/go.mod h1:pCSXDSfILyPKttpOOM3yBlzl99mNEzmw5bfrGVzXCaA=
github.com/bogdanfinn/fhttp v0.5.28 h1:G6thT8s8v6z1IuvXMUsX9QKy3ZHseTQTzxuIhSiaaAw=
Expand Down
89 changes: 85 additions & 4 deletions internal/plugin/llm/lmsys/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,90 @@ func fetch(ctx context.Context, proxies, token, messages string, opts options) (
)
}

return partTwo(ctx, proxies, cookies, hash, opts)
err = partTwo(ctx, proxies, cookies, hash, opts)
if err != nil {
return nil, logger.WarpError(err)
}

return partThree(ctx, proxies, cookies, hash, opts)
}

func partTwo(ctx context.Context, proxies, cookies, hash string, opts options) error {
obj := map[string]interface{}{
"event_data": nil,
"session_hash": hash,
"data": make([]interface{}, 0),
}

var response *http.Response
var err error
obj["fn_index"] = opts.fn[0] + 1
obj["trigger_id"] = opts.fn[1]
response, err = emit.ClientBuilder(plugin.HTTPClient).
Context(ctx).
Proxies(proxies).
POST(baseUrl+"/queue/join").
JHeader().
Ja3("yes").
Header("User-Agent", ua).
Header("Cookie", cookies).
Header("Origin", baseUrl).
Header("Referer", baseUrl+"/").
Header("Accept-Language", "en-US,en;q=0.9").
Header("Cache-Control", "no-cache").
Header("Priority", "u=1, i").
Body(obj).
DoS(http.StatusOK)
if err != nil {
ver = ""
return logger.WarpError(err)
}

obj, err = emit.ToMap(response)
if err != nil {
return logger.WarpError(err)
}

if eventId, ok := obj["event_id"]; ok {
logger.Infof("lmsys eventId: %s", eventId)
} else {
return errors.New("fetch failed")
}

cookies = emit.MergeCookies(cookies, emit.GetCookies(response))
_ = response.Body.Close()

response, err = emit.ClientBuilder(plugin.HTTPClient).
Context(ctx).
Proxies(proxies).
Ja3("yes").
GET(baseUrl+"/queue/data").
Query("session_hash", hash).
Header("User-Agent", ua).
Header("Cookie", cookies).
Header("Origin", baseUrl).
Header("Referer", baseUrl+"/").
Header("Accept-Language", "en-US,en;q=0.9").
Header("Cache-Control", "no-cache").
Header("Priority", "u=1, i").
DoS(http.StatusOK)
if err != nil {
return logger.WarpError(err)
}

defer response.Body.Close()
cookies = emit.MergeCookies(cookies, emit.GetCookies(response))
e, err := emit.NewGio(ctx, response)
if err != nil {
return logger.WarpError(err)
}

return e.Do()
}

func partTwo(ctx context.Context, proxies, cookies, hash string, opts options) (chan string, error) {
func partThree(ctx context.Context, proxies, cookies, hash string, opts options) (chan string, error) {
obj := map[string]interface{}{
"fn_index": opts.fn[0] + 1,
"fn_index": opts.fn[0] + 2,
"trigger_id": opts.fn[1],
"session_hash": hash,
"data": []interface{}{
Expand Down Expand Up @@ -203,7 +281,10 @@ func partOne(ctx context.Context, proxies, token string, opts *options, messages
"data": []interface{}{
nil,
opts.model,
messages,
map[string]string{
"text": messages,
// TODO - image
},
nil,
},
}
Expand Down

0 comments on commit ef57d71

Please sign in to comment.