From 8a49b21ace7cd3b13869427db463362a58bd072d Mon Sep 17 00:00:00 2001 From: YongXin Date: Fri, 24 Feb 2023 17:57:08 +0800 Subject: [PATCH] update --- bootstrap/telegram.go | 7 ++++--- handler/telegram/telegram.go | 2 +- main.go | 4 ++++ openai/chatgpt.go | 14 +++++++------- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bootstrap/telegram.go b/bootstrap/telegram.go index 794c656..6feed23 100644 --- a/bootstrap/telegram.go +++ b/bootstrap/telegram.go @@ -16,7 +16,7 @@ func StartTelegramBot() { log.Info("Start Telegram Bot") telegramKey := config.GetTelegram() if telegramKey == nil { - log.Info("未找到tg token,不启动tg tot") + log.Error("未找到tg token,不启动tg tot") return } @@ -79,9 +79,10 @@ func StartTelegramBot() { } requestText := strings.TrimSpace(splitItems[1]) - log.Println("问题:", requestText) + log.Info("问题:", requestText) reply = telegram.Handle(requestText) } else { + log.Info("问题:", text) reply = telegram.Handle(text) } @@ -96,7 +97,7 @@ func StartTelegramBot() { continue } - log.Println(send.Text) + log.Info("回答:", send.Text) } select {} diff --git a/handler/telegram/telegram.go b/handler/telegram/telegram.go index f4fc5ff..112e803 100644 --- a/handler/telegram/telegram.go +++ b/handler/telegram/telegram.go @@ -12,7 +12,7 @@ func Handle(msg string) *string { requestText := strings.TrimSpace(msg) reply, err := openai.Completions(requestText) if err != nil { - log.Println(err) + log.Error(err) } return reply } diff --git a/main.go b/main.go index 751b648..73e34a4 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,9 @@ import ( ) func main() { + log.SetLevel(log.DebugLevel) + //log.SetLevel(log.InfoLevel) + log.SetReportCaller(true) log.SetFormatter(&log.TextFormatter{ DisableColors: false, @@ -27,5 +30,6 @@ func main() { } else if telegramEnv != nil { bootstrap.StartTelegramBot() } + log.Info("程序退出") } diff --git a/openai/chatgpt.go b/openai/chatgpt.go index 1f91f75..42b1d97 100644 --- a/openai/chatgpt.go +++ b/openai/chatgpt.go @@ -79,14 +79,14 @@ func Completions(msg string) (*string, error) { requestData, err := json.Marshal(requestBody) if err != nil { - log.Println(err) + log.Error(err) return nil, err } - log.Printf("request openai json string : %v", string(requestData)) + log.Debugf("request openai json string : %v", string(requestData)) req, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", bytes.NewBuffer(requestData)) if err != nil { - log.Println(err) + log.Error(err) return nil, err } @@ -111,10 +111,10 @@ func Completions(msg string) (*string, error) { } gptResponseBody := &ChatGPTResponseBody{} - log.Println(string(body)) + log.Debug(string(body)) err = json.Unmarshal(body, gptResponseBody) if err != nil { - log.Println(err) + log.Error(err) return nil, err } @@ -130,13 +130,13 @@ func Completions(msg string) (*string, error) { gptErrorBody := &ChatGPTErrorBody{} err = json.Unmarshal(body, gptErrorBody) if err != nil { - log.Println(err) + log.Error(err) return nil, err } reply = gptErrorBody.Error["message"].(string) } - log.Printf("gpt response full text: %s \n", reply) + log.Debugf("gpt response full text: %s \n", reply) result := strings.TrimSpace(reply) return &result, nil }