Skip to content

Commit

Permalink
style: matcher配置化
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed Sep 13, 2024
1 parent 0426b6f commit 5f96dd7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
12 changes: 12 additions & 0 deletions example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,15 @@ toolCall:
# 目前注册没有限制,可配置多个key轮询
#magnify:
# - "xxx"

# 用于处理llm响应时的内容
# find: 开头匹配
# end: 结束匹配
# content: 正则处理
#matcher:
# - find: <!-- Attack
# end: -->
# content: "<!-- Attack [\\s\\S]+ -->:"
# - find: <thinking>
# end: </thinking>
# content: "<thinking>[\\s\\S]+<\\/thinking>:thinking..."
82 changes: 81 additions & 1 deletion internal/common/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"chatgpt-adapter/logger"
"chatgpt-adapter/pkg"
"context"
regexp "github.com/dlclark/regexp2"
"github.com/gin-gonic/gin"
"strconv"
"strings"
)

Expand All @@ -18,6 +20,8 @@ var (
"<|tool|>",
"<|end|>",
}

globalMatchers = make([]Matcher, 0)
)

// 匹配器接口
Expand All @@ -33,9 +37,85 @@ type SymbolMatcher struct {
H func(index int, content string) (state int, result string)
}

func init() {
AddInitialized(func() {
obj := pkg.Config.Get("matcher")
if obj == nil {
return
}

if slice, ok := obj.([]interface{}); ok {
initMatchers(slice)
}
})
}

func initMatchers(slice []interface{}) {
for _, it := range slice {
if m, o := it.(map[string]interface{}); o {
find, ok := m["find"]
if !ok {
continue
}

end, ok := m["end"]
if !ok {
end = ""
}

l, ok := m["len"]
if !ok {
l = "5"
}

findL, err := strconv.Atoi(l.(string))
if err != nil {
continue
}

str, ok := m["content"]
if !ok {
str = ""
}

values := split(str.(string))
if len(values) < 2 {
continue
}

c := regexp.MustCompile(strings.TrimSpace(values[0]), regexp.Compiled)
join := strings.TrimSpace(values[1])

globalMatchers = append(globalMatchers, &SymbolMatcher{
Find: find.(string),
H: func(index int, content string) (state int, result string) {
if end != "" {
if !strings.Contains(content, end.(string)) {
return vars.MatMatching, content
}
} else {
r := []rune(content)
if index+findL > len(r)-1 {
return vars.MatMatching, content
}
}

result, err = c.Replace(content, join, -1, -1)
if err != nil {
logger.Warn("compile failed: "+values[0], err)
return vars.MatMatched, content
}

return vars.MatMatched, result
},
})
}
}
}

func NewMatchers() []Matcher {
slice := make([]Matcher, 0)
// todo 内置一些过滤器
slice = append(slice, globalMatchers...)
return slice
}

Expand Down

0 comments on commit 5f96dd7

Please sign in to comment.