From 304093260e28ccee19d031215f8355a55925861b Mon Sep 17 00:00:00 2001 From: Will Date: Mon, 13 Feb 2023 23:03:47 +0800 Subject: [PATCH 1/6] =?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 52b066524f67c90fd8ba74a36e4a43ce9ac12700 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 13 Feb 2023 23:06:35 +0800 Subject: [PATCH 2/6] fix bug --- 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 3/6] 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) From 9cf74df5441aae9636e4151db4c3a45a974ab8a1 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 13 Feb 2023 23:22:43 +0800 Subject: [PATCH 4/6] done --- openai/chatgpt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openai/chatgpt.go b/openai/chatgpt.go index 04d1a43..c6d165f 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -107,7 +107,7 @@ func Completions(msg string) (*string, error) { } gptResponseBody := &ChatGPTResponseBody{} - gptErrorBody := &ChatGPTErrorBodyP{} + gptErrorBody := &ChatGPTErrorBody{} log.Println(string(body)) err = json.Unmarshal(body, gptResponseBody) if err != nil { From a4f9af7e7670b8c51c6d8d3f0be567afc3d0230e Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 13 Feb 2023 23:39:18 +0800 Subject: [PATCH 5/6] update --- openai/chatgpt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openai/chatgpt.go b/openai/chatgpt.go index c6d165f..8a58fc5 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -126,7 +126,7 @@ func Completions(msg string) (*string, error) { } else { reply = gptErrorBody.Error["message"].(string) } - log.Printf("gpt response text: %s \n", reply) + log.Printf("gpt response full text: %s \n", reply) result := strings.TrimSpace(reply) return &result, nil } From f016692f20313300f2c4ed4ee7cedc4aaf77cf53 Mon Sep 17 00:00:00 2001 From: Will Date: Tue, 14 Feb 2023 00:01:23 +0800 Subject: [PATCH 6/6] 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