From ef57d7179c53707b391f3c25b0ff79f9b34010fc Mon Sep 17 00:00:00 2001 From: bincooo Date: Fri, 6 Sep 2024 00:37:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(lmsys):=20=E5=AE=98=E7=BD=91=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=A8=A1=E5=BC=8F=E4=BF=AE=E6=94=B9=E5=90=8C=E6=AD=A5?= =?UTF-8?q?(#70)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 +- go.sum | 6 +- internal/plugin/llm/lmsys/fetch.go | 89 ++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 96742b1..5f7c96a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b1a1507..ebca635 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/plugin/llm/lmsys/fetch.go b/internal/plugin/llm/lmsys/fetch.go index 461d853..be0770d 100644 --- a/internal/plugin/llm/lmsys/fetch.go +++ b/internal/plugin/llm/lmsys/fetch.go @@ -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{}{ @@ -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, }, }