Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta45 #46

Merged
merged 47 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ea4bcc6
beta1
Hoshinonyaruko Jan 21, 2024
5002a28
beta2
Hoshinonyaruko Jan 21, 2024
969841e
beta3
Hoshinonyaruko Jan 21, 2024
a2989c2
beta4
Hoshinonyaruko Jan 21, 2024
611cd8b
beta5
Hoshinonyaruko Jan 21, 2024
dbad42b
beta6
Hoshinonyaruko Jan 21, 2024
4f8895e
beta7
Hoshinonyaruko Jan 21, 2024
30a9cab
beta8
Hoshinonyaruko Jan 21, 2024
461d879
beta9
Hoshinonyaruko Jan 21, 2024
c90c2ee
beta10
Hoshinonyaruko Jan 21, 2024
5227c32
beta11
Hoshinonyaruko Jan 21, 2024
8e14e56
beta12
Hoshinonyaruko Jan 21, 2024
6adfb7f
beta13
Hoshinonyaruko Jan 21, 2024
b04f6c0
beta14
Hoshinonyaruko Jan 21, 2024
649e740
beta15
Hoshinonyaruko Jan 21, 2024
d55cf29
beta16
Hoshinonyaruko Jan 21, 2024
1cc351e
beta16
Hoshinonyaruko Jan 21, 2024
f1a373e
beta19
Hoshinonyaruko Jan 21, 2024
cbf2fe1
beta20
Hoshinonyaruko Jan 21, 2024
3485bfc
beta21
Hoshinonyaruko Jan 21, 2024
3e3bb1a
beta22
Hoshinonyaruko Jan 21, 2024
2e4f3fa
beta23
Hoshinonyaruko Mar 29, 2024
f74075d
beta24
Hoshinonyaruko Mar 29, 2024
f3913cf
beta25
Hoshinonyaruko Mar 29, 2024
4cfb51a
beta27
Hoshinonyaruko Mar 30, 2024
d9b26c0
beta28
Hoshinonyaruko Mar 31, 2024
19da709
beta29
Hoshinonyaruko Mar 31, 2024
e06dcbf
merge
Hoshinonyaruko Mar 31, 2024
c76923e
beta30
Hoshinonyaruko Mar 31, 2024
9ae21da
beta31
Hoshinonyaruko Apr 1, 2024
4f333bc
merge
Hoshinonyaruko Apr 1, 2024
5acba8f
beta33
Hoshinonyaruko Apr 1, 2024
c637fcd
beta34
Hoshinonyaruko Apr 1, 2024
4e85b37
beta35
Hoshinonyaruko Apr 1, 2024
970948e
beta36
Hoshinonyaruko Apr 1, 2024
14271c3
beta37
Hoshinonyaruko Apr 1, 2024
6154d1a
beta38
Hoshinonyaruko Apr 1, 2024
d5be5e6
beta39
Hoshinonyaruko Apr 3, 2024
54afe18
beta40
Hoshinonyaruko Apr 3, 2024
0b71855
beta41
Hoshinonyaruko Apr 3, 2024
8c5f9a3
merge
Hoshinonyaruko Apr 3, 2024
cf752d4
beta42
Hoshinonyaruko Apr 3, 2024
3698824
beta43
Hoshinonyaruko Apr 3, 2024
1e9c189
beta44
Hoshinonyaruko Apr 3, 2024
2adb4ac
beta45
Hoshinonyaruko Apr 4, 2024
79eb713
beta45
Hoshinonyaruko Apr 4, 2024
064f0d0
beta45
Hoshinonyaruko Apr 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading