From bcd700ac13cf8f59bc5db1fb5d51125d46e0268d Mon Sep 17 00:00:00 2001 From: Zhou Zhiqiang Date: Mon, 25 Mar 2024 19:22:15 +0800 Subject: [PATCH] feat: dump response body when failed Signed-off-by: Zhou Zhiqiang --- provider/Baichuan/stream_chat_completion.go | 8 ++++++++ provider/ChatGLM/chat_completion.go | 16 ++++++++++++++-- provider/ChatGLM/embedding.go | 14 +++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/provider/Baichuan/stream_chat_completion.go b/provider/Baichuan/stream_chat_completion.go index 67f7b80..55ef48f 100644 --- a/provider/Baichuan/stream_chat_completion.go +++ b/provider/Baichuan/stream_chat_completion.go @@ -3,7 +3,10 @@ package Baichuan import ( "bytes" "encoding/json" + "fmt" + "io" "log" + "log/slog" "net/http" "strings" @@ -29,6 +32,11 @@ func (c *Client) ChatCompletionStreamingRequest(body ChatCompletionRequest) (cha return nil, err } + if resp.StatusCode != 200 { + resposne, _ := io.ReadAll(resp.Body) + slog.Error("unexpected status code: %d", resp.StatusCode, "response body", string(resposne)) + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } reader := utils.NewEventStreamReader(resp.Body, 4096) diff --git a/provider/ChatGLM/chat_completion.go b/provider/ChatGLM/chat_completion.go index 454c0f5..4febe81 100644 --- a/provider/ChatGLM/chat_completion.go +++ b/provider/ChatGLM/chat_completion.go @@ -2,7 +2,10 @@ package ChatGLM import ( "encoding/json" + "fmt" + "io" "log" + "log/slog" "time" ) @@ -23,8 +26,17 @@ func (c *Client) ChatCompletion(body ChatCompletionRequest) (result ChatCompleti return ChatCompletionResponse{}, err } defer resp.Body.Close() - log.Println("ChatGLM response status: ", resp.Status) - err = json.NewDecoder(resp.Body).Decode(&result) + responseBodyString, err := io.ReadAll(resp.Body) + if err != nil { + return ChatCompletionResponse{}, err + } + if resp.StatusCode != 200 { + slog.Error("unexpected status code: %d", resp.StatusCode, "response body", string(responseBodyString)) + return ChatCompletionResponse{}, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + log.Println("ChatGLM response status: ", resp.Status) + err = json.Unmarshal(responseBodyString, &result) return result, err } diff --git a/provider/ChatGLM/embedding.go b/provider/ChatGLM/embedding.go index 119ae01..3426b9f 100644 --- a/provider/ChatGLM/embedding.go +++ b/provider/ChatGLM/embedding.go @@ -2,7 +2,10 @@ package ChatGLM import ( "encoding/json" + "fmt" + "io" "log" + "log/slog" ) func (c *Client) EmbeddingRequest(body EmbeddingRequest) (result EmbeddingResponse, err error) { @@ -17,7 +20,16 @@ func (c *Client) EmbeddingRequest(body EmbeddingRequest) (result EmbeddingRespon } defer resp.Body.Close() log.Println("ChatGLM response status: ", resp.Status) - err = json.NewDecoder(resp.Body).Decode(&result) + + responseBodyString, err := io.ReadAll(resp.Body) + if err != nil { + return EmbeddingResponse{}, err + } + if resp.StatusCode != 200 { + slog.Error("unexpected status code: %d", resp.StatusCode, "response body", string(responseBodyString)) + return EmbeddingResponse{}, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + json.Unmarshal(responseBodyString, &result) if err != nil { return EmbeddingResponse{}, err }