From f016692f20313300f2c4ed4ee7cedc4aaf77cf53 Mon Sep 17 00:00:00 2001 From: Will Date: Tue, 14 Feb 2023 00:01:23 +0800 Subject: [PATCH] update --- openai/chatgpt.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/openai/chatgpt.go b/openai/chatgpt.go index 8a58fc5..46141bb 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -107,15 +107,11 @@ func Completions(msg string) (*string, error) { } gptResponseBody := &ChatGPTResponseBody{} - gptErrorBody := &ChatGPTErrorBody{} log.Println(string(body)) err = json.Unmarshal(body, gptResponseBody) if err != nil { - err = json.Unmarshal(body, gptErrorBody) - if err != nil { - log.Println(err) - return nil, err - } + log.Println(err) + return nil, err } var reply string if len(gptResponseBody.Choices) > 0 { @@ -123,9 +119,19 @@ func Completions(msg string) (*string, error) { reply = v["text"].(string) break } - } else { + } + + gptErrorBody := &ChatGPTErrorBody{} + err = json.Unmarshal(body, gptErrorBody) + if err != nil { + log.Println(err) + return nil, err + } + + if (len(reply) == 0) { reply = gptErrorBody.Error["message"].(string) } + log.Printf("gpt response full text: %s \n", reply) result := strings.TrimSpace(reply) return &result, nil