Skip to content

Commit

Permalink
update improve blocked word regexp when environment variable for addi…
Browse files Browse the repository at this point in the history
…tional blocked word is empty; improve global error message as response
  • Loading branch information
dimaskiddo committed Mar 11, 2023
1 parent bdf8675 commit a468ef8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 24 additions & 11 deletions pkg/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (

var OAIClient *OpenAI.Client

var OAIGPTModelName string
var (
OAIGPTModelName,
OAIGPTBlockedWord string
)

var OAIGPTModelToken int

var (
Expand All @@ -26,7 +30,12 @@ var (
OAIGPTModelPenaltyFreq float32
)

var OAIGPTBlockedWord string
var OAIGPTBlockedWordRegex *regexp.Regexp

const listBlockedWord string = "" +
"lgbt|lesbian|gay|homosexual|homoseksual|bisexual|biseksual|transgender|" +
"fuck|sex|ngentot|entot|ngewe|ewe|masturbate|masturbasi|coli|colmek|jilmek|" +
"cock|penis|kontol|vagina|memek|porn|porno|bokep"

func init() {
var err error
Expand Down Expand Up @@ -66,24 +75,24 @@ func init() {
log.Println(log.LogLevelFatal, "Error Parse Environment Variable for OpenAI GPT Model Penalty Frequency")
}

OAIGPTBlockedWord = "lgbt|lesbian|gay|homosexual|homoseksual|bisexual|biseksual|transgender|fuck|sex|ngentot|entot|ngewe|ewe|masturbate|masturbasi|coli|colmek|jilmek|cock|penis|kontol|vagina|memek|porn|porno|bokep"
envBlockedWord := strings.TrimSpace(os.Getenv("OPENAI_GPT_BLOCKED_WORD"))
if len(envBlockedWord) > 0 {
OAIGPTBlockedWord = "\\b(?i)(" + OAIGPTBlockedWord + "|" + envBlockedWord + ")"
OAIGPTBlockedWord = strings.TrimSpace(os.Getenv("OPENAI_GPT_BLOCKED_WORD"))
if len(OAIGPTBlockedWord) > 0 {
OAIGPTBlockedWordRegex = regexp.MustCompile("\\b(?i)(" + listBlockedWord + "|" + OAIGPTBlockedWord + ")")
} else {
OAIGPTBlockedWordRegex = regexp.MustCompile("\\b(?i)(" + listBlockedWord + ")")
}

OAIClient = OpenAI.NewClient(OAIAPIKey)
}

func GPT3Response(question string) (response string, err error) {
gptResponseBuilder := strings.Builder{}
gptIsFirstWordFound := false

gptBlockedWord := regexp.MustCompile(OAIGPTBlockedWord)
if bool(gptBlockedWord.MatchString(question)) {
if bool(OAIGPTBlockedWordRegex.MatchString(question)) {
return "Sorry, the AI can not response due to it is containing some blocked word 🥺", nil
}

gptResponseBuilder := strings.Builder{}
gptIsFirstWordFound := false

gptChatMode := regexp.MustCompile("\\b(?i)(" + "gpt-3\\.5" + ")")
if bool(gptChatMode.MatchString(OAIGPTModelName)) {
gptRequest := OpenAI.ChatCompletionRequest{
Expand Down Expand Up @@ -180,5 +189,9 @@ func GPT3Response(question string) (response string, err error) {
gptResponseBuffer = strings.TrimLeft(gptResponseBuffer, ".\n")
gptResponseBuffer = strings.TrimLeft(gptResponseBuffer, "\n")

if bool(OAIGPTBlockedWordRegex.MatchString(gptResponseBuffer)) {
return "Sorry, the AI can not response due to it is containing some blocked word 🥺", nil
}

return gptResponseBuffer, nil
}
2 changes: 1 addition & 1 deletion pkg/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func WhatsAppHandler(event interface{}) {

response, err := gpt.GPT3Response(question)
if err != nil {
response = "Sorry, the AI can not response for this time. Please try again after a few moment. Thank you ! 🙈"
response = "Sorry, the AI can not response for this time. Please try again after a few moment 🥺"
}

_, err = WhatsAppSendGPTResponse(context.Background(), evt, response)
Expand Down

0 comments on commit a468ef8

Please sign in to comment.