From 5b4ca264f6838c30118bc1fd392df0bc7ad2c7e1 Mon Sep 17 00:00:00 2001 From: wmw64 <4693125+wmw64@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:54:31 +0400 Subject: [PATCH] ContainsBannedWords func added --- go.mod | 2 +- go.sum | 4 ++-- internal/app/app.go | 2 +- internal/config/config.go | 1 + internal/{const/const.go => consts/consts.go} | 0 internal/infrastructure/gpt/gpt.go | 6 ++++++ internal/service/moderation.go | 19 +++++++++++++++++++ internal/service/service.go | 6 +++++- internal/tmi/command.go | 10 +++++++--- 9 files changed, 42 insertions(+), 8 deletions(-) rename internal/{const/const.go => consts/consts.go} (100%) create mode 100644 internal/service/moderation.go diff --git a/go.mod b/go.mod index ed9bcbf..896403f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/gempir/go-twitch-irc/v4 v4.0.0 github.com/ilyakaznacheev/cleanenv v1.5.0 - github.com/sashabaranov/go-openai v1.15.3 + github.com/sashabaranov/go-openai v1.17.5 github.com/stretchr/testify v1.8.4 github.com/wmw64/twitchpl v0.5.0 ) diff --git a/go.sum b/go.sum index b82ff15..95017ae 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sashabaranov/go-openai v1.15.3 h1:rzoNK9n+Cak+PM6OQ9puxDmFllxfnVea9StlmhglXqA= -github.com/sashabaranov/go-openai v1.15.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= +github.com/sashabaranov/go-openai v1.17.5 h1:ItBzlrrfTtkFWOFlgfOhk3y/xRBC4PJol4gdbiK7hgg= +github.com/sashabaranov/go-openai v1.17.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/wmw64/twitchpl v0.5.0 h1:0CgkH2ld2X0W24ttLbhJ2lGOOEk1k+gwLor1zKeBkC4= diff --git a/internal/app/app.go b/internal/app/app.go index 5363753..00cfeb8 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -26,7 +26,7 @@ func Run(cfg *config.Config) { log.Info("channels to connect", cfg.Channel) - svc := service.New(log, ffmpeg.New(), imgur.New(), twitch.New(log), youtube.New(), gpt.New(cfg, log)) + svc := service.New(log, cfg, ffmpeg.New(), imgur.New(), twitch.New(log), youtube.New(), gpt.New(cfg, log)) chat := tmi.New(log, cfg, svc, cfg.Channel...) defer chat.Close() diff --git a/internal/config/config.go b/internal/config/config.go index fdc43e2..438525c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,6 +19,7 @@ type ( Channel []string `env-required:"true" yaml:"channel" env:"BLOSSOM_TW_CHANNEL" env-delim:","` IgnoreChannels []string `env-required:"true" yaml:"ignore_channels" env:"BLOSSOM_TW_IGNORE_CHANNELS" env-delim:","` CommandsEnabled []string `env-required:"true" yaml:"commands_enabled" env:"BLOSSOM_TW_COMMANDS_ENABLED" env-delim:","` + BannedWords []string `env-required:"true" yaml:"banned_words" env:"BLOSSOM_TW_BANNED_WORDS" env-delim:","` CmdTimeout time.Duration `env-required:"false" env-default:"20s" yaml:"cmd_timeout" env:"BLOSSOM_TW_CMD_TIMEOUT"` } diff --git a/internal/const/const.go b/internal/consts/consts.go similarity index 100% rename from internal/const/const.go rename to internal/consts/consts.go diff --git a/internal/infrastructure/gpt/gpt.go b/internal/infrastructure/gpt/gpt.go index ecea102..6f0ea99 100644 --- a/internal/infrastructure/gpt/gpt.go +++ b/internal/infrastructure/gpt/gpt.go @@ -37,6 +37,9 @@ func (g *GPT) Ask(ctx context.Context, prompt string) (string, error) { Content: prompt, }, }, + ResponseFormat: &openai.ChatCompletionResponseFormat{ + Type: openai.ChatCompletionResponseFormatTypeText, + }, } res, err := g.client.CreateChatCompletion(ctx, req) @@ -58,6 +61,9 @@ func (g *GPT) AskStream(ctx context.Context, prompt string) (stream *openai.Chat Content: prompt, }, }, + ResponseFormat: &openai.ChatCompletionResponseFormat{ + Type: openai.ChatCompletionResponseFormatTypeText, + }, Stream: true, } diff --git a/internal/service/moderation.go b/internal/service/moderation.go new file mode 100644 index 0000000..de28925 --- /dev/null +++ b/internal/service/moderation.go @@ -0,0 +1,19 @@ +package service + +import "strings" + +type Moderator interface { + ContainsBannedWords(text string) bool +} + +func (svc *service) ContainsBannedWords(text string) bool { + text = strings.ToLower(text) + + for _, word := range svc.cfg.BannedWords { + if strings.Contains(text, word) { + return true + } + } + + return false +} diff --git a/internal/service/service.go b/internal/service/service.go index 7bc07bf..f107258 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,6 +1,7 @@ package service import ( + "blossom/internal/config" "blossom/internal/infrastructure/gpt" "blossom/pkg/ffmpeg" "blossom/pkg/imgur" @@ -12,12 +13,14 @@ import ( //go:generate mockery --name Servicer type Servicer interface { AIer + Moderator Screenshot(channel string) (imgURL string, err error) PreviewLink(URL string) (description string, linkType Link, err error) } type service struct { log logger.Logger + cfg *config.Config FFMpeg ffmpeg.FFMpeger Imgur imgur.Imgurer Twitch twitch.Twitcher @@ -25,9 +28,10 @@ type service struct { gpt gpt.GPTer } -func New(log logger.Logger, ffmpeg ffmpeg.FFMpeger, imgur imgur.Imgurer, twitch twitch.Twitcher, youtube youtube.Youtuber, gpt gpt.GPTer) Servicer { +func New(log logger.Logger, cfg *config.Config, ffmpeg ffmpeg.FFMpeger, imgur imgur.Imgurer, twitch twitch.Twitcher, youtube youtube.Youtuber, gpt gpt.GPTer) Servicer { return &service{ log: log, + cfg: cfg, FFMpeg: ffmpeg, Imgur: imgur, Twitch: twitch, diff --git a/internal/tmi/command.go b/internal/tmi/command.go index 846d391..fcce79f 100644 --- a/internal/tmi/command.go +++ b/internal/tmi/command.go @@ -8,7 +8,7 @@ import ( "time" "unicode/utf8" - consts "blossom/internal/const" + "blossom/internal/consts" "blossom/internal/service" "blossom/pkg/link" @@ -95,8 +95,6 @@ func (c *chat) CommandGPT(msg twitch.PrivateMessage) (ok bool) { return true } - c.log.Debug("CutPrefix") - if after, ok := strings.CutPrefix(msg.Message, "!gpt "); ok && after != "" { ctx, cancel := context.WithTimeout(context.Background(), c.Cfg.Bot.CmdTimeout) defer cancel() @@ -125,6 +123,12 @@ func (c *chat) CommandGPT(msg twitch.PrivateMessage) (ok bool) { c.log.Debug("reply", slog.String("answer", answer)) + if c.svc.ContainsBannedWords(answer) { + c.log.Debug("answer contains banned words, skip", slog.String("answer", answer)) + + return false + } + c.TMI.Reply(msg.Channel, msg.ID, answer) return true