Skip to content

Commit 5b4ca26

Browse files
committed
ContainsBannedWords func added
1 parent 03ea26d commit 5b4ca26

File tree

9 files changed

+42
-8
lines changed

9 files changed

+42
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21
55
require (
66
github.com/gempir/go-twitch-irc/v4 v4.0.0
77
github.com/ilyakaznacheev/cleanenv v1.5.0
8-
github.com/sashabaranov/go-openai v1.15.3
8+
github.com/sashabaranov/go-openai v1.17.5
99
github.com/stretchr/testify v1.8.4
1010
github.com/wmw64/twitchpl v0.5.0
1111
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
1313
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1414
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1515
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16-
github.com/sashabaranov/go-openai v1.15.3 h1:rzoNK9n+Cak+PM6OQ9puxDmFllxfnVea9StlmhglXqA=
17-
github.com/sashabaranov/go-openai v1.15.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
16+
github.com/sashabaranov/go-openai v1.17.5 h1:ItBzlrrfTtkFWOFlgfOhk3y/xRBC4PJol4gdbiK7hgg=
17+
github.com/sashabaranov/go-openai v1.17.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
1818
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1919
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
2020
github.com/wmw64/twitchpl v0.5.0 h1:0CgkH2ld2X0W24ttLbhJ2lGOOEk1k+gwLor1zKeBkC4=

internal/app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Run(cfg *config.Config) {
2626

2727
log.Info("channels to connect", cfg.Channel)
2828

29-
svc := service.New(log, ffmpeg.New(), imgur.New(), twitch.New(log), youtube.New(), gpt.New(cfg, log))
29+
svc := service.New(log, cfg, ffmpeg.New(), imgur.New(), twitch.New(log), youtube.New(), gpt.New(cfg, log))
3030

3131
chat := tmi.New(log, cfg, svc, cfg.Channel...)
3232
defer chat.Close()

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type (
1919
Channel []string `env-required:"true" yaml:"channel" env:"BLOSSOM_TW_CHANNEL" env-delim:","`
2020
IgnoreChannels []string `env-required:"true" yaml:"ignore_channels" env:"BLOSSOM_TW_IGNORE_CHANNELS" env-delim:","`
2121
CommandsEnabled []string `env-required:"true" yaml:"commands_enabled" env:"BLOSSOM_TW_COMMANDS_ENABLED" env-delim:","`
22+
BannedWords []string `env-required:"true" yaml:"banned_words" env:"BLOSSOM_TW_BANNED_WORDS" env-delim:","`
2223
CmdTimeout time.Duration `env-required:"false" env-default:"20s" yaml:"cmd_timeout" env:"BLOSSOM_TW_CMD_TIMEOUT"`
2324
}
2425

File renamed without changes.

internal/infrastructure/gpt/gpt.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func (g *GPT) Ask(ctx context.Context, prompt string) (string, error) {
3737
Content: prompt,
3838
},
3939
},
40+
ResponseFormat: &openai.ChatCompletionResponseFormat{
41+
Type: openai.ChatCompletionResponseFormatTypeText,
42+
},
4043
}
4144

4245
res, err := g.client.CreateChatCompletion(ctx, req)
@@ -58,6 +61,9 @@ func (g *GPT) AskStream(ctx context.Context, prompt string) (stream *openai.Chat
5861
Content: prompt,
5962
},
6063
},
64+
ResponseFormat: &openai.ChatCompletionResponseFormat{
65+
Type: openai.ChatCompletionResponseFormatTypeText,
66+
},
6167
Stream: true,
6268
}
6369

internal/service/moderation.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package service
2+
3+
import "strings"
4+
5+
type Moderator interface {
6+
ContainsBannedWords(text string) bool
7+
}
8+
9+
func (svc *service) ContainsBannedWords(text string) bool {
10+
text = strings.ToLower(text)
11+
12+
for _, word := range svc.cfg.BannedWords {
13+
if strings.Contains(text, word) {
14+
return true
15+
}
16+
}
17+
18+
return false
19+
}

internal/service/service.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"blossom/internal/config"
45
"blossom/internal/infrastructure/gpt"
56
"blossom/pkg/ffmpeg"
67
"blossom/pkg/imgur"
@@ -12,22 +13,25 @@ import (
1213
//go:generate mockery --name Servicer
1314
type Servicer interface {
1415
AIer
16+
Moderator
1517
Screenshot(channel string) (imgURL string, err error)
1618
PreviewLink(URL string) (description string, linkType Link, err error)
1719
}
1820

1921
type service struct {
2022
log logger.Logger
23+
cfg *config.Config
2124
FFMpeg ffmpeg.FFMpeger
2225
Imgur imgur.Imgurer
2326
Twitch twitch.Twitcher
2427
Youtube youtube.Youtuber
2528
gpt gpt.GPTer
2629
}
2730

28-
func New(log logger.Logger, ffmpeg ffmpeg.FFMpeger, imgur imgur.Imgurer, twitch twitch.Twitcher, youtube youtube.Youtuber, gpt gpt.GPTer) Servicer {
31+
func New(log logger.Logger, cfg *config.Config, ffmpeg ffmpeg.FFMpeger, imgur imgur.Imgurer, twitch twitch.Twitcher, youtube youtube.Youtuber, gpt gpt.GPTer) Servicer {
2932
return &service{
3033
log: log,
34+
cfg: cfg,
3135
FFMpeg: ffmpeg,
3236
Imgur: imgur,
3337
Twitch: twitch,

internal/tmi/command.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99
"unicode/utf8"
1010

11-
consts "blossom/internal/const"
11+
"blossom/internal/consts"
1212
"blossom/internal/service"
1313
"blossom/pkg/link"
1414

@@ -95,8 +95,6 @@ func (c *chat) CommandGPT(msg twitch.PrivateMessage) (ok bool) {
9595
return true
9696
}
9797

98-
c.log.Debug("CutPrefix")
99-
10098
if after, ok := strings.CutPrefix(msg.Message, "!gpt "); ok && after != "" {
10199
ctx, cancel := context.WithTimeout(context.Background(), c.Cfg.Bot.CmdTimeout)
102100
defer cancel()
@@ -125,6 +123,12 @@ func (c *chat) CommandGPT(msg twitch.PrivateMessage) (ok bool) {
125123

126124
c.log.Debug("reply", slog.String("answer", answer))
127125

126+
if c.svc.ContainsBannedWords(answer) {
127+
c.log.Debug("answer contains banned words, skip", slog.String("answer", answer))
128+
129+
return false
130+
}
131+
128132
c.TMI.Reply(msg.Channel, msg.ID, answer)
129133

130134
return true

0 commit comments

Comments
 (0)