Skip to content

Commit

Permalink
ContainsBannedWords func added
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-sm committed Nov 11, 2023
1 parent 03ea26d commit 5b4ca26
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions internal/infrastructure/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
}

Expand Down
19 changes: 19 additions & 0 deletions internal/service/moderation.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"blossom/internal/config"
"blossom/internal/infrastructure/gpt"
"blossom/pkg/ffmpeg"
"blossom/pkg/imgur"
Expand All @@ -12,22 +13,25 @@ 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
Youtube youtube.Youtuber
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,
Expand Down
10 changes: 7 additions & 3 deletions internal/tmi/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
"unicode/utf8"

consts "blossom/internal/const"
"blossom/internal/consts"
"blossom/internal/service"
"blossom/pkg/link"

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5b4ca26

Please sign in to comment.