Browse Source

wip:接口敏感词检测调整

wangkaiyue 2 năm trước cách đây
mục cha
commit
6ff247f155

+ 2 - 0
internal/controller/chatWs.go

@@ -27,9 +27,11 @@ var ChatWs = func(r *ghttp.Request) {
 		})
 		return
 	}
+
 	for {
 		// 接受客户端信息
 		_, msg, err := ws.ReadMessage()
+
 		if err != nil {
 			glog.Info(wsChat.Ctx, session.PositionId, "接收消息出错", err)
 			_ = ws.Close()

+ 2 - 2
internal/model/chatApi.go

@@ -49,7 +49,7 @@ func (c *cChatGpt) SimpleDo(ctx context.Context, qReq *QuestionReq) (res *Simple
 	}
 	res = &SimpleRes{}
 	err = gconv.Struct(gRes.ReadAll(), res)
-	g.Dump("SimpleDo", res)
+	//g.Dump("SimpleDo", gReq, res)
 	if err != nil {
 		return nil, err
 	}
@@ -69,7 +69,7 @@ func (c *cChatGpt) GPTDo(ctx context.Context, qReq *QuestionReq) (res *GPTRes, e
 	}
 	res = &GPTRes{}
 	err = gconv.Struct(gRes.ReadAll(), res)
-	g.Dump("GPTDo", res)
+	//g.Dump("GPTDo", gReq, res)
 	if err != nil {
 		return nil, err
 	}

+ 20 - 1
internal/model/question.go

@@ -3,6 +3,7 @@ package model
 import (
 	"aiChat/internal/consts"
 	"aiChat/utility"
+	"aiChat/utility/fsw"
 	"context"
 	"fmt"
 	"github.com/gogf/gf/v2/frame/g"
@@ -35,6 +36,23 @@ type QuestionReq struct {
 	Href string `json:"href"` //咨询页面
 }
 
+// ParseHistoryFsw 过滤历史记录敏感词
+func (r *BaseQuestion) ParseHistoryFsw() {
+	var newHistory [][]string
+	for _, h := range r.History {
+		var pass bool = true
+		for _, v := range h {
+			if fsw.Match(v) {
+				pass = false
+			}
+		}
+		if pass {
+			newHistory = append(newHistory, h)
+		}
+	}
+	r.History = newHistory
+}
+
 // GetUsuallyProblem 获取常见问题
 func (q *cQuestion) GetUsuallyProblem(ctx context.Context, scenario, limit int) (list []string, err error) {
 	list = make([]string, 0, limit)
@@ -77,6 +95,7 @@ func (q *cQuestion) getIsbusinessData(ctx context.Context, code string) (bRes *B
 
 // DetailQuestion 问题处理
 func (q *cQuestion) DetailQuestion(ctx context.Context, qRes *QuestionReq) (reply string, from int, err error) {
+	qRes.ParseHistoryFsw()
 	// 语义服务
 	sRes, err := ChatGpt.SimpleDo(ctx, qRes)
 	if err != nil {
@@ -93,7 +112,7 @@ func (q *cQuestion) DetailQuestion(ctx context.Context, qRes *QuestionReq) (repl
 	// 校验是否有业务逻辑
 	matchArr := regExpSmart.FindStringSubmatch(cRes.Response)
 	if len(matchArr) == 0 {
-		return cRes.Response, Answer_ChatGPT, nil
+		return fsw.Repl(cRes.Response), Answer_ChatGPT, nil
 	}
 	// 查询业务逻辑
 	var bRes = &BusinessRes{}

+ 0 - 2
internal/model/ws.go

@@ -69,8 +69,6 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 				g.Log().Error(m.Ctx, "问答异常", err)
 				return "", 0, fmt.Errorf("问答异常")
 			}
-			// 回答敏感词过滤
-			reply = fsw.Repl(reply)
 		}
 
 		// 记录问答