Merge pull request #61 from yongxin-ms/main

把chatGPT服务的错误消息也发送给IM
This commit is contained in:
涼風羽月 2023-02-14 01:38:53 +09:00 committed by GitHub
commit c07e25d4fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,10 @@ type ChatGPTResponseBody struct {
Usage map[string]interface{} `json:"usage"` Usage map[string]interface{} `json:"usage"`
} }
type ChatGPTErrorBody struct {
Error map[string]interface{} `json:"error"`
}
// ChatGPTRequestBody 响应体 // ChatGPTRequestBody 响应体
type ChatGPTRequestBody struct { type ChatGPTRequestBody struct {
Model string `json:"model"` Model string `json:"model"`
@ -115,8 +119,20 @@ func Completions(msg string) (*string, error) {
reply = v["text"].(string) reply = v["text"].(string)
break break
} }
}
gptErrorBody := &ChatGPTErrorBody{}
err = json.Unmarshal(body, gptErrorBody)
if err != nil {
log.Println(err)
return nil, err
} }
log.Printf("gpt response text: %s \n", reply)
if (len(reply) == 0) {
reply = gptErrorBody.Error["message"].(string)
}
log.Printf("gpt response full text: %s \n", reply)
result := strings.TrimSpace(reply) result := strings.TrimSpace(reply)
return &result, nil return &result, nil
} }