From ed46c2b274e245947dc8b55ffb9154011731c1f5 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Wed, 8 Mar 2023 04:25:20 +0800 Subject: [PATCH 1/3] feat: adjust prompt for translation --- botcmd_translate.go | 2 ++ openai/prompts/prompts.go | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/botcmd_translate.go b/botcmd_translate.go index 82a38b2..5c45e89 100644 --- a/botcmd_translate.go +++ b/botcmd_translate.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" + "github.com/samber/lo" tele "gopkg.in/telebot.v3" "git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/openai" @@ -91,6 +92,7 @@ func handleTranslateBtn(c tele.Context) error { Content: payload, }, }, + Temperature: lo.ToPtr(0.6), } resp, err := ai.ChatCompletion(req) diff --git a/openai/prompts/prompts.go b/openai/prompts/prompts.go index 43f5ef5..d0e74df 100644 --- a/openai/prompts/prompts.go +++ b/openai/prompts/prompts.go @@ -1,6 +1,9 @@ package prompts -import "fmt" +import ( + "fmt" + "strings" +) func General() string { return "You are a helpful assistant." @@ -8,7 +11,14 @@ func General() string { func Translate(targetLang string) string { return fmt.Sprintf( - "You are a helpful assistant. Your task is to help translate the following text to %s. You should not interpret the text. You should structure the translated text to look natural in native %s, while keeping the meaning unchanged.", - targetLang, targetLang, + strings.Join([]string{ + "You are a helpful assistant.", + "Your task is to help translate the text sent by the user into %s.", + "You should never interpret the user's text, but only translate it.", + "You should structure the translated text to sound natural in native %s while keeping the meanings unchanged.", + "If the user's text contains only a single word, please also add a brief explanation in %s, of the meaning of the original word.", + "The first text to be translated is:", + }, " \n"), + targetLang, targetLang, targetLang, ) } From 89ddf89052f4f72af2331cbec99e61c786e30b76 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Wed, 8 Mar 2023 04:46:00 +0800 Subject: [PATCH 2/3] feat: adjustable log level --- botcmd_translate.go | 1 - cfg.go | 1 + main.go | 13 ++++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/botcmd_translate.go b/botcmd_translate.go index 5c45e89..e83e49f 100644 --- a/botcmd_translate.go +++ b/botcmd_translate.go @@ -49,7 +49,6 @@ func handleTranslateCmd(c tele.Context) error { tele.Silent, ) } - logger.Infof("trimmed message: %q", payload) _, err := c.Bot().Reply(msg, "Sure. To what language?", tele.Silent, translateMenu) return err diff --git a/cfg.go b/cfg.go index 15a34e4..ce9efca 100644 --- a/cfg.go +++ b/cfg.go @@ -10,6 +10,7 @@ import ( ) type ConfigDef struct { + LogLevel string `env:"TG_LOG_LEVEL" env-default:"info"` AdminUIDs []int64 `env:"TG_ADMIN_UIDS"` TGBotToken string `env:"TG_TOKEN" env-required:""` TGAnnounceCommands bool `env:"TG_ANNOUNCE_CMDS"` diff --git a/main.go b/main.go index d8fab53..94df370 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "time" "go.uber.org/zap" + "go.uber.org/zap/zapcore" tele "gopkg.in/telebot.v3" "git.gensokyo.cafe/kkyy/mycurrencynet" @@ -14,9 +15,13 @@ import ( ) var logger *zap.SugaredLogger +var loglvl zap.AtomicLevel func initLogger() { - l, err := zap.NewProduction() + logCfg := zap.NewProductionConfig() + loglvl = logCfg.Level + + l, err := logCfg.Build() if err != nil { panic(err) } @@ -89,6 +94,12 @@ func main() { if err := LoadCfg(); err != nil { logger.Fatalw("Failed to load config", "err", err) } + parsedLvl, err := zapcore.ParseLevel(config.LogLevel) + if err != nil { + logger.Warnf("Invalid log level %q", config.LogLevel) + } else { + loglvl.SetLevel(parsedLvl) + } runBot() } From 3af6f0a18ddd0ba6fcb7ebac3e33196fc52d8c1e Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Wed, 8 Mar 2023 04:49:26 +0800 Subject: [PATCH 3/3] chore: adjust log level --- bot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.go b/bot.go index a808480..1d128ec 100644 --- a/bot.go +++ b/bot.go @@ -85,7 +85,7 @@ func logMiddleware(next tele.HandlerFunc) tele.HandlerFunc { return func(c tele.Context) error { upd := c.Update() defer func() { - logger.Infow("Log middleware", "update", upd) + logger.Debugw("Log middleware", "update", upd) }() return next(c)