Skip to content

Commit

Permalink
Beta45 (#46)
Browse files Browse the repository at this point in the history
* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38

* beta39

* beta40

* beta41

* beta42

* beta43

* beta44

* beta45

* beta45
  • Loading branch information
Hoshinonyaruko authored Apr 4, 2024
1 parent 4455b14 commit 7930408
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 187 deletions.
151 changes: 25 additions & 126 deletions applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,29 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
lastSelectedVectorID int // 用于存储最后选取的相似文本的ID
)

//如果使用向量缓存 或者使用 向量安全词
// 进行字数拦截
if config.GetQuestionMaxLenth() != 0 {
if utils.LengthIntercept(newmsg, message) {
fmtf.Printf("字数过长,可在questionMaxLenth配置项修改,Q: %v", newmsg)
// 发送响应
w.WriteHeader(http.StatusOK)
w.Write([]byte("question too long"))
return
}
}

// 进行语言判断拦截
if len(config.GetAllowedLanguages()) > 0 {
if utils.LanguageIntercept(newmsg, message) {
fmtf.Printf("不安全!不支持的语言,可在config.yml设置允许的语言,allowedLanguages配置项,Q: %v", newmsg)
// 发送响应
w.WriteHeader(http.StatusOK)
w.Write([]byte("language not support"))
return
}
}

// 如果使用向量缓存 或者使用 向量安全词
if config.GetUseCache() || config.GetVectorSensitiveFilter() {
// 计算文本向量
vector, err = app.CalculateTextEmbedding(newmsg)
Expand Down Expand Up @@ -265,7 +287,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
if !config.GetUsePrivateSSE() {
utils.SendPrivateMessage(message.UserID, responseText)
} else {
SendSSEPrivateMessage(message.UserID, responseText)
utils.SendSSEPrivateMessage(message.UserID, responseText)
}
} else {
utils.SendGroupMessage(message.GroupID, responseText)
Expand Down Expand Up @@ -310,7 +332,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
if !config.GetUsePrivateSSE() {
utils.SendPrivateMessage(message.UserID, saveresponse)
} else {
SendSSEPrivateSafeMessage(message.UserID, saveresponse)
utils.SendSSEPrivateSafeMessage(message.UserID, saveresponse)
}
} else {
utils.SendGroupMessage(message.GroupID, saveresponse)
Expand Down Expand Up @@ -615,126 +637,3 @@ func processMessage(response string, msg structs.OnebotGroupMessage, newmesssage
}
}
}

// SendSSEPrivateMessage 分割并发送消息的核心逻辑,直接遍历字符串
func SendSSEPrivateMessage(userID int64, content string) {
punctuations := []rune{'。', '!', '?', ',', ',', '.', '!', '?'}
splitProbability := config.GetSplitByPuntuations()

var parts []string
var currentPart strings.Builder

for _, runeValue := range content {
currentPart.WriteRune(runeValue)
if strings.ContainsRune(string(punctuations), runeValue) {
// 根据概率决定是否分割
if rand.Intn(100) < splitProbability {
parts = append(parts, currentPart.String())
currentPart.Reset()
}
}
}
// 添加最后一部分(如果有的话)
if currentPart.Len() > 0 {
parts = append(parts, currentPart.String())
}

// 根据parts长度处理状态
for i, part := range parts {
state := 1
if i == len(parts)-2 { // 倒数第二部分
state = 11
} else if i == len(parts)-1 { // 最后一部分
state = 20
}

// 构造消息体并发送
messageSSE := structs.InterfaceBody{
Content: part,
State: state,
}

if state == 20 { // 对最后一部分特殊处理
RestoreResponses := config.GetRestoreCommand()
promptKeyboard := config.GetPromptkeyboard()

if len(RestoreResponses) > 0 {
selectedRestoreResponse := RestoreResponses[rand.Intn(len(RestoreResponses))]
if len(promptKeyboard) > 0 {
promptKeyboard[0] = selectedRestoreResponse
}
}

messageSSE.PromptKeyboard = promptKeyboard
}

// 发送SSE消息函数
utils.SendPrivateMessageSSE(userID, messageSSE)
}
}

// SendSSEPrivateSafeMessage 分割并发送安全消息的核心逻辑,直接遍历字符串
func SendSSEPrivateSafeMessage(userID int64, saveresponse string) {
// 将字符串转换为rune切片,以正确处理多字节字符
runes := []rune(saveresponse)

// 计算每部分应该包含的rune数量
partLength := len(runes) / 3

// 初始化用于存储分割结果的切片
parts := make([]string, 3)

// 按字符分割字符串
for i := 0; i < 3; i++ {
if i < 2 { // 前两部分
start := i * partLength
end := start + partLength
parts[i] = string(runes[start:end])
} else { // 最后一部分,包含所有剩余的字符
start := i * partLength
parts[i] = string(runes[start:])
}
}
// 开头
messageSSE := structs.InterfaceBody{
Content: parts[0],
State: 1,
}

utils.SendPrivateMessageSSE(userID, messageSSE)

// 中间
messageSSE = structs.InterfaceBody{
Content: parts[1],
State: 11,
}
utils.SendPrivateMessageSSE(userID, messageSSE)

// 从配置中获取恢复响应数组
RestoreResponses := config.GetRestoreCommand()

var selectedRestoreResponse string
// 如果RestoreResponses至少有一个成员,则随机选择一个
if len(RestoreResponses) > 0 {
selectedRestoreResponse = RestoreResponses[rand.Intn(len(RestoreResponses))]
}

// 从配置中获取promptkeyboard
promptkeyboard := config.GetPromptkeyboard()

// 确保promptkeyboard至少有一个成员
if len(promptkeyboard) > 0 {
// 使用随机选中的RestoreResponse替换promptkeyboard的第一个成员
promptkeyboard[0] = selectedRestoreResponse
}

// 创建InterfaceBody结构体实例
messageSSE = structs.InterfaceBody{
Content: parts[2], // 假设空格字符串是期望的内容
State: 20, // 假设的状态码
PromptKeyboard: promptkeyboard, // 使用更新后的promptkeyboard
}

// 发送SSE私人消息
utils.SendPrivateMessageSSE(userID, messageSSE)
}
2 changes: 1 addition & 1 deletion applogic/vectorsensitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (app *App) InterceptSensitiveContent(vector []float64, message structs.Oneb
if !config.GetUsePrivateSSE() {
utils.SendPrivateMessage(message.UserID, saveresponse)
} else {
SendSSEPrivateSafeMessage(message.UserID, saveresponse)
utils.SendSSEPrivateSafeMessage(message.UserID, saveresponse)
}
} else {
utils.SendGroupMessage(message.GroupID, saveresponse)
Expand Down
172 changes: 114 additions & 58 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,68 @@ type Config struct {
}

type Settings struct {
SecretId string `yaml:"secretId"`
SecretKey string `yaml:"secretKey"`
Region string `yaml:"region"`
UseSse bool `yaml:"useSse"`
Port int `yaml:"port"`
HttpPath string `yaml:"path"`
SystemPrompt []string `yaml:"systemPrompt"`
IPWhiteList []string `yaml:"iPWhiteList"`
MaxTokensHunyuan int `yaml:"maxTokensHunyuan"`
ApiType int `yaml:"apiType"`
WenxinAccessToken string `yaml:"wenxinAccessToken"`
WenxinApiPath string `yaml:"wenxinApiPath"`
MaxTokenWenxin int `yaml:"maxTokenWenxin"`
GptModel string `yaml:"gptModel"`
GptApiPath string `yaml:"gptApiPath"`
GptToken string `yaml:"gptToken"`
MaxTokenGpt int `yaml:"maxTokenGpt"`
GptSafeMode bool `yaml:"gptSafeMode"`
GptSseType int `yaml:"gptSseType"`
Groupmessage bool `yaml:"groupMessage"`
SplitByPuntuations int `yaml:"splitByPuntuations"`
HunyuanType int `yaml:"hunyuanType"`
FirstQ []string `yaml:"firstQ"`
FirstA []string `yaml:"firstA"`
SecondQ []string `yaml:"secondQ"`
SecondA []string `yaml:"secondA"`
ThirdQ []string `yaml:"thirdQ"`
ThirdA []string `yaml:"thirdA"`
SensitiveMode bool `yaml:"sensitiveMode"`
SensitiveModeType int `yaml:"sensitiveModeType"`
DefaultChangeWord string `yaml:"defaultChangeWord"`
AntiPromptAttackPath string `yaml:"antiPromptAttackPath"`
ReverseUserPrompt bool `yaml:"reverseUserPrompt"`
IgnoreExtraTips bool `yaml:"ignoreExtraTips"`
SaveResponses []string `yaml:"saveResponses"`
RestoreCommand []string `yaml:"restoreCommand"`
RestoreResponses []string `yaml:"restoreResponses"`
UsePrivateSSE bool `yaml:"usePrivateSSE"`
Promptkeyboard []string `yaml:"promptkeyboard"`
Savelogs bool `yaml:"savelogs"`
AntiPromptLimit float64 `yaml:"antiPromptLimit"`
UseCache bool `yaml:"useCache"`
CacheThreshold int `yaml:"cacheThreshold"`
CacheChance int `yaml:"cacheChance"`
EmbeddingType int `yaml:"embeddingType"`
WenxinEmbeddingUrl string `yaml:"wenxinEmbeddingUrl"`
GptEmbeddingUrl string `yaml:"gptEmbeddingUrl"`
PrintHanming bool `yaml:"printHanming"`
CacheK float64 `yaml:"cacheK"`
CacheN int `yaml:"cacheN"`
PrintVector bool `yaml:"printVector"`
VToBThreshold float64 `yaml:"vToBThreshold"`
GptModeration bool `yaml:"gptModeration"`
WenxinTopp float64 `yaml:"wenxinTopp"`
WnxinPenaltyScore float64 `yaml:"wenxinPenaltyScore"`
WenxinMaxOutputTokens int `yaml:"wenxinMaxOutputTokens"`
VectorSensitiveFilter bool `yaml:"vectorSensitiveFilter"`
VertorSensitiveThreshold int `yaml:"vertorSensitiveThreshold"`
SecretId string `yaml:"secretId"`
SecretKey string `yaml:"secretKey"`
Region string `yaml:"region"`
UseSse bool `yaml:"useSse"`
Port int `yaml:"port"`
HttpPath string `yaml:"path"`
SystemPrompt []string `yaml:"systemPrompt"`
IPWhiteList []string `yaml:"iPWhiteList"`
MaxTokensHunyuan int `yaml:"maxTokensHunyuan"`
ApiType int `yaml:"apiType"`
WenxinAccessToken string `yaml:"wenxinAccessToken"`
WenxinApiPath string `yaml:"wenxinApiPath"`
MaxTokenWenxin int `yaml:"maxTokenWenxin"`
GptModel string `yaml:"gptModel"`
GptApiPath string `yaml:"gptApiPath"`
GptToken string `yaml:"gptToken"`
MaxTokenGpt int `yaml:"maxTokenGpt"`
GptSafeMode bool `yaml:"gptSafeMode"`
GptSseType int `yaml:"gptSseType"`
Groupmessage bool `yaml:"groupMessage"`
SplitByPuntuations int `yaml:"splitByPuntuations"`
HunyuanType int `yaml:"hunyuanType"`
FirstQ []string `yaml:"firstQ"`
FirstA []string `yaml:"firstA"`
SecondQ []string `yaml:"secondQ"`
SecondA []string `yaml:"secondA"`
ThirdQ []string `yaml:"thirdQ"`
ThirdA []string `yaml:"thirdA"`
SensitiveMode bool `yaml:"sensitiveMode"`
SensitiveModeType int `yaml:"sensitiveModeType"`
DefaultChangeWord string `yaml:"defaultChangeWord"`
AntiPromptAttackPath string `yaml:"antiPromptAttackPath"`
ReverseUserPrompt bool `yaml:"reverseUserPrompt"`
IgnoreExtraTips bool `yaml:"ignoreExtraTips"`
SaveResponses []string `yaml:"saveResponses"`
RestoreCommand []string `yaml:"restoreCommand"`
RestoreResponses []string `yaml:"restoreResponses"`
UsePrivateSSE bool `yaml:"usePrivateSSE"`
Promptkeyboard []string `yaml:"promptkeyboard"`
Savelogs bool `yaml:"savelogs"`
AntiPromptLimit float64 `yaml:"antiPromptLimit"`
UseCache bool `yaml:"useCache"`
CacheThreshold int `yaml:"cacheThreshold"`
CacheChance int `yaml:"cacheChance"`
EmbeddingType int `yaml:"embeddingType"`
WenxinEmbeddingUrl string `yaml:"wenxinEmbeddingUrl"`
GptEmbeddingUrl string `yaml:"gptEmbeddingUrl"`
PrintHanming bool `yaml:"printHanming"`
CacheK float64 `yaml:"cacheK"`
CacheN int `yaml:"cacheN"`
PrintVector bool `yaml:"printVector"`
VToBThreshold float64 `yaml:"vToBThreshold"`
GptModeration bool `yaml:"gptModeration"`
WenxinTopp float64 `yaml:"wenxinTopp"`
WnxinPenaltyScore float64 `yaml:"wenxinPenaltyScore"`
WenxinMaxOutputTokens int `yaml:"wenxinMaxOutputTokens"`
VectorSensitiveFilter bool `yaml:"vectorSensitiveFilter"`
VertorSensitiveThreshold int `yaml:"vertorSensitiveThreshold"`
AllowedLanguages []string `yaml:"allowedLanguages"`
LanguagesResponseMessages []string `yaml:"langResponseMessages"`
QuestionMaxLenth int `yaml:"questionMaxLenth"`
QmlResponseMessages []string `yaml:"qmlResponseMessages"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -794,3 +798,55 @@ func GetVertorSensitiveThreshold() int {
}
return 0
}

// GetAllowedLanguages 返回允许的语言列表
func GetAllowedLanguages() []string {
mu.Lock()
defer mu.Unlock()
if instance != nil {
return instance.Settings.AllowedLanguages
}
return nil // 或返回一个默认的语言列表
}

// GetLanguagesResponseMessages 返回语言拦截响应消息列表
func GetLanguagesResponseMessages() string {
mu.Lock()
defer mu.Unlock()
if instance != nil && len(instance.Settings.LanguagesResponseMessages) > 0 {
// 如果列表中只有一个消息,直接返回这个消息
if len(instance.Settings.LanguagesResponseMessages) == 1 {
return instance.Settings.LanguagesResponseMessages[0]
}
// 如果有多个消息,随机选择一个返回
index := rand.Intn(len(instance.Settings.LanguagesResponseMessages))
return instance.Settings.LanguagesResponseMessages[index]
}
return "" // 如果列表为空,返回空字符串
}

// 获取QuestionMaxLenth
func GetQuestionMaxLenth() int {
mu.Lock()
defer mu.Unlock()
if instance != nil {
return instance.Settings.QuestionMaxLenth
}
return 0
}

// GetQmlResponseMessages 返回语言拦截响应消息列表
func GetQmlResponseMessages() string {
mu.Lock()
defer mu.Unlock()
if instance != nil && len(instance.Settings.QmlResponseMessages) > 0 {
// 如果列表中只有一个消息,直接返回这个消息
if len(instance.Settings.QmlResponseMessages) == 1 {
return instance.Settings.QmlResponseMessages[0]
}
// 如果有多个消息,随机选择一个返回
index := rand.Intn(len(instance.Settings.QmlResponseMessages))
return instance.Settings.QmlResponseMessages[index]
}
return "" // 如果列表为空,返回空字符串
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.839
gopkg.in/yaml.v3 v3.0.1
)

require github.com/abadojack/whatlanggo v1.0.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4=
github.com/abadojack/whatlanggo v1.0.1/go.mod h1:66WiQbSbJBIlOZMsvbKe5m6pzQovxCH9B/K8tQB2uoc=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
Expand Down
Loading

0 comments on commit 7930408

Please sign in to comment.