feat: add error body info

This commit is contained in:
Zhang Minghan 2023-12-19 19:15:32 +08:00
parent 898b336afe
commit 0a31d550ce
5 changed files with 35 additions and 23 deletions

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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)
}
}