new-api / service /sensitive.go
liuzhao521
Deploy New API v0.9.25+ (commit b47cf4ef) to HuggingFace Spaces
4674012
package service
import (
"errors"
"strings"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/setting"
)
func CheckSensitiveMessages(messages []dto.Message) ([]string, error) {
if len(messages) == 0 {
return nil, nil
}
for _, message := range messages {
arrayContent := message.ParseContent()
for _, m := range arrayContent {
if m.Type == "image_url" {
// TODO: check image url
continue
}
// ζ£€ζŸ₯ text 是否为空
if m.Text == "" {
continue
}
if ok, words := SensitiveWordContains(m.Text); ok {
return words, errors.New("sensitive words detected")
}
}
}
return nil, nil
}
func CheckSensitiveText(text string) (bool, []string) {
return SensitiveWordContains(text)
}
// SensitiveWordContains ζ˜―ε¦εŒ…ε«ζ•ζ„Ÿθ―οΌŒθΏ”ε›žζ˜―ε¦εŒ…ε«ζ•ζ„Ÿθ―ε’Œζ•ζ„Ÿθ―εˆ—θ‘¨
func SensitiveWordContains(text string) (bool, []string) {
if len(setting.SensitiveWords) == 0 {
return false, nil
}
if len(text) == 0 {
return false, nil
}
checkText := strings.ToLower(text)
return AcSearch(checkText, setting.SensitiveWords, true)
}
// SensitiveWordReplace ζ•ζ„Ÿθ―ζ›Ώζ’οΌŒθΏ”ε›žζ˜―ε¦εŒ…ε«ζ•ζ„Ÿθ―ε’Œζ›Ώζ’εŽηš„ζ–‡ζœ¬
func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string, string) {
if len(setting.SensitiveWords) == 0 {
return false, nil, text
}
checkText := strings.ToLower(text)
m := getOrBuildAC(setting.SensitiveWords)
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
if len(hits) > 0 {
words := make([]string, 0, len(hits))
var builder strings.Builder
builder.Grow(len(text))
lastPos := 0
for _, hit := range hits {
pos := hit.Pos
word := string(hit.Word)
builder.WriteString(text[lastPos:pos])
builder.WriteString("**###**")
lastPos = pos + len(word)
words = append(words, word)
}
builder.WriteString(text[lastPos:])
return true, words, builder.String()
}
return false, nil, text
}