From 304093260e28ccee19d031215f8355a55925861b Mon Sep 17 00:00:00 2001 From: Will Date: Mon, 13 Feb 2023 23:03:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BD=93chatGPT=E8=BF=94=E5=9B=9E=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E6=97=B6=E5=80=99=E6=8A=8A=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E8=BF=94=E5=9B=9E=E7=BB=99IM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openai/chatgpt.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/openai/chatgpt.go b/openai/chatgpt.go index 84f4219..34f7fdb 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -115,6 +115,8 @@ func Completions(msg string) (*string, error) { reply = v["text"].(string) break } + } else { + replay = v["error"]["message"].(string) } log.Printf("gpt response text: %s \n", reply) result := strings.TrimSpace(reply) From 32b8f3552030e41bdfd60db5b46fbc7a5d25c6ae Mon Sep 17 00:00:00 2001 From: Will Date: Mon, 13 Feb 2023 23:18:08 +0800 Subject: [PATCH 2/2] update --- openai/chatgpt.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/openai/chatgpt.go b/openai/chatgpt.go index 34f7fdb..04d1a43 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -23,6 +23,10 @@ type ChatGPTResponseBody struct { Usage map[string]interface{} `json:"usage"` } +type ChatGPTErrorBody struct { + Error map[string]interface{} `json:"error"` +} + // ChatGPTRequestBody 响应体 type ChatGPTRequestBody struct { Model string `json:"model"` @@ -103,11 +107,15 @@ func Completions(msg string) (*string, error) { } gptResponseBody := &ChatGPTResponseBody{} + gptErrorBody := &ChatGPTErrorBodyP{} log.Println(string(body)) err = json.Unmarshal(body, gptResponseBody) if err != nil { - log.Println(err) - return nil, err + err = json.Unmarshal(body, gptErrorBody) + if err != nil { + log.Println(err) + return nil, err + } } var reply string if len(gptResponseBody.Choices) > 0 { @@ -116,7 +124,7 @@ func Completions(msg string) (*string, error) { break } } else { - replay = v["error"]["message"].(string) + reply = gptErrorBody.Error["message"].(string) } log.Printf("gpt response text: %s \n", reply) result := strings.TrimSpace(reply)