From 0a31d550ce58a712d93b78ba639fc7d5b8c239b8 Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Tue, 19 Dec 2023 19:15:32 +0800 Subject: [PATCH] feat: add error body info --- adapter/request.go | 4 ++-- channel/channel.go | 8 ++++++++ channel/worker.go | 19 ++++--------------- utils/char.go | 15 +++++++++++++++ utils/net.go | 12 ++++++------ 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/adapter/request.go b/adapter/request.go index 6f23e04..c6702d9 100644 --- a/adapter/request.go +++ b/adapter/request.go @@ -30,13 +30,13 @@ func NewChatRequest(conf globals.ChannelConfig, props *ChatProps, hook globals.H if isQPSOverLimit(props.Model, err) { // sleep for 0.5s to avoid qps limit - fmt.Println(fmt.Sprintf("qps limit for %s, sleep and retry (times: %d)", props.Model, props.Current)) + globals.Info(fmt.Sprintf("qps limit for %s, sleep and retry (times: %d)", props.Model, props.Current)) time.Sleep(500 * time.Millisecond) return NewChatRequest(conf, props, hook) } if props.Current < retries { - fmt.Println(fmt.Sprintf("retrying chat request for %s (attempt %d/%d, error: %s)", props.Model, props.Current+1, retries, err.Error())) + globals.Warn(fmt.Sprintf("retrying chat request for %s (attempt %d/%d, error: %s)", props.Model, props.Current+1, retries, err.Error())) return NewChatRequest(conf, props, hook) } } diff --git a/channel/channel.go b/channel/channel.go index c27a790..29d44c9 100644 --- a/channel/channel.go +++ b/channel/channel.go @@ -9,6 +9,10 @@ import ( ) var defaultMaxRetries = 1 +var defaultReplacer = []string{ + "openai_api", "anthropic_api", + "api2d", "closeai_api", "one_api", "new_api", +} func (c *Channel) GetId() int { return c.Id @@ -173,5 +177,9 @@ func (c *Channel) ProcessError(err error) error { content = strings.Replace(content, domain, "channel", -1) } + for _, item := range defaultReplacer { + content = strings.Replace(content, item, "chatnio_upstream", -1) + } + return errors.New(content) } diff --git a/channel/worker.go b/channel/worker.go index 74b698e..386109d 100644 --- a/channel/worker.go +++ b/channel/worker.go @@ -5,7 +5,7 @@ import ( "chat/globals" "chat/utils" "fmt" - "github.com/spf13/viper" + "github.com/cloudwego/hertz/cmd/hz/util/logs" ) func NewChatRequest(props *adapter.ChatProps, hook globals.Hook) error { @@ -15,29 +15,18 @@ func NewChatRequest(props *adapter.ChatProps, hook globals.Hook) error { ticker := ConduitInstance.GetTicker(props.Model) - debug := viper.GetBool("debug") - var err error for !ticker.IsDone() { if channel := ticker.Next(); channel != nil { - if debug { - fmt.Println(fmt.Sprintf("[channel] try channel %s for model %s", channel.GetName(), props.Model)) - } - props.MaxRetries = utils.ToPtr(channel.GetRetry()) if err = adapter.NewChatRequest(channel, props, hook); err == nil { - if debug { - fmt.Println(fmt.Sprintf("[channel] hit channel %s for model %s", channel.GetName(), props.Model)) - } - return nil } - fmt.Println(fmt.Sprintf("[channel] caught error %s for model %s at channel %s -> goto next channel", err.Error(), props.Model, channel.GetName())) + + logs.Warn(fmt.Sprintf("[channel] caught error %s for model %s at channel %s", err.Error(), props.Model, channel.GetName())) } } - if debug { - fmt.Println(fmt.Sprintf("[channel] channels are exhausted for model %s", props.Model)) - } + logs.Info(fmt.Sprintf("[channel] channels are exhausted for model %s", props.Model)) return err } diff --git a/utils/char.go b/utils/char.go index f2612b1..f3e5baf 100644 --- a/utils/char.go +++ b/utils/char.go @@ -63,6 +63,21 @@ func Marshal[T interface{}](data T) string { return string(res) } +func MarshalWithIndent[T interface{}](data T, length ...int) string { + var indent string + if len(length) > 0 { + indent = strings.Repeat(" ", length[0]) + } else { + indent = " " + } + + res, err := json.MarshalIndent(data, "", indent) + if err != nil { + return "" + } + return string(res) +} + func MapToStruct[T any](data interface{}) *T { val := Marshal(data) if form, err := Unmarshal[T]([]byte(val)); err == nil { diff --git a/utils/net.go b/utils/net.go index 824410b..c0d38a8 100644 --- a/utils/net.go +++ b/utils/net.go @@ -6,7 +6,6 @@ import ( "crypto/tls" "fmt" "github.com/goccy/go-json" - "github.com/spf13/viper" "io" "net/http" "net/url" @@ -128,8 +127,9 @@ func EventSource(method string, uri string, headers map[string]string, body inte client := newClient() req, err := http.NewRequest(method, uri, ConvertBody(body)) if err != nil { - return nil + return err } + for key, value := range headers { req.Header.Set(key, value) } @@ -142,10 +142,10 @@ func EventSource(method string, uri string, headers map[string]string, body inte defer res.Body.Close() if res.StatusCode >= 400 { - // print body - if viper.GetBool("debug") { - if content, err := io.ReadAll(res.Body); err == nil { - fmt.Println(fmt.Sprintf("request failed with status: %s, body: %s", res.Status, string(content))) + if content, err := io.ReadAll(res.Body); err == nil { + if form, err := Unmarshal[map[string]interface{}](content); err == nil { + data := MarshalWithIndent(form, 2) + return fmt.Errorf("request failed with status: %s\n```json\n%s\n```", res.Status, data) } }