Skip to content

Commit

Permalink
feat: dump response body when failed
Browse files Browse the repository at this point in the history
Signed-off-by: Zhou Zhiqiang <[email protected]>
  • Loading branch information
STRRL committed Apr 2, 2024
1 parent e47d9fe commit bcd700a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions provider/Baichuan/stream_chat_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package Baichuan
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"strings"

Expand All @@ -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)

Expand Down
16 changes: 14 additions & 2 deletions provider/ChatGLM/chat_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package ChatGLM

import (
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"time"
)

Expand All @@ -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
}
14 changes: 13 additions & 1 deletion provider/ChatGLM/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package ChatGLM

import (
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
)

func (c *Client) EmbeddingRequest(body EmbeddingRequest) (result EmbeddingResponse, err error) {
Expand All @@ -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
}
Expand Down

0 comments on commit bcd700a

Please sign in to comment.