瀏覽代碼

wip:增加黑名单及历史记录查询修改

wangkaiyue 2 年之前
父節點
當前提交
274f8ab50d

+ 1 - 1
internal/controller/chatHistory.go

@@ -25,7 +25,7 @@ func (c *cChatHistory) Method(ctx context.Context, req *v1.ChatHistoryReq) (res
 	if req.PrevId != "" {
 		req.PrevId = encrypt.SE.Decode4Hex(req.PrevId) //id解密
 	}
-	history, hasNext, err := model.ChatHistroy.GetMessage(model.SessionCtx.Get(ctx).JSession.AccountId, req.PageNum, req.PageSize, req.PrevId)
+	history, hasNext, err := model.ChatHistory.GetMessage(model.SessionCtx.Get(ctx).JSession.AccountId, req.PageNum, req.PageSize, req.PrevId)
 	if err != nil {
 		glog.Error(ctx, "%d查询聊天记录异常,error:%s", model.SessionCtx.Get(ctx).JSession.AccountId, err)
 		res.ErrorCode = -1

+ 1 - 1
internal/controller/evaluate.go

@@ -22,7 +22,7 @@ func (c *cEvaluate) Method(ctx context.Context, req *v1.EvaluateReq) (res *v1.Ev
 		return nil, fmt.Errorf("无用户身份")
 	}
 
-	err = model.ChatHistroy.Evaluate(session.AccountId, id, req.Evaluate)
+	err = model.ChatHistory.Evaluate(session.AccountId, id, req.Evaluate)
 	if err != nil {
 		res.Data = false
 		return

+ 6 - 1
internal/model/blackList.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/os/gctx"
 )
 
 var (
@@ -13,9 +14,13 @@ var (
 type cUserBlackList struct {
 }
 
+func init() {
+	g.Redis("black").Set(gctx.New(), "aiChat_black_14891", 1)
+}
+
 // CheckBlackList 校验黑名单
 func (l *cUserBlackList) CheckBlackList(ctx context.Context, accountId int64) bool {
-	v, e := g.Redis().Get(ctx, fmt.Sprintf("aiChat_black_%d", accountId))
+	v, e := g.Redis("black").Get(ctx, fmt.Sprintf("aiChat_black_%d", accountId))
 	if e != nil {
 		return false
 	}

+ 3 - 2
internal/model/chatApi.go

@@ -42,7 +42,6 @@ func (c *cChatGpt) SimpleDo(ctx context.Context, qReq *QuestionReq) (res *Simple
 		BaseQuestion: qReq.BaseQuestion,
 		Identity:     g.Config().MustGet(ctx, "chat.api.identity", "剑鱼chat").String(),
 	}
-	//g.Dump(gReq)
 	var gRes *gclient.Response
 	gRes, err = g.Client().Header(consts.RequestJsonHeader).Post(ctx, g.Config().MustGet(ctx, "chat.api.addr_simple", "").String(), gReq)
 	if err != nil {
@@ -50,6 +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)
 	if err != nil {
 		return nil, err
 	}
@@ -61,7 +61,7 @@ func (c *cChatGpt) GPTDo(ctx context.Context, qReq *QuestionReq) (res *GPTRes, e
 		BaseQuestion: qReq.BaseQuestion,
 		Identity:     g.Config().MustGet(ctx, "chat.api.identity", "剑鱼chat").String(),
 	}
-	//g.Dump(gReq)
+
 	var gRes *gclient.Response
 	gRes, err = g.Client().Header(consts.RequestJsonHeader).Post(ctx, g.Config().MustGet(ctx, "chat.api.addr_answer", "").String(), gReq)
 	if err != nil {
@@ -69,6 +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)
 	if err != nil {
 		return nil, err
 	}

+ 8 - 8
internal/model/chatHistory.go

@@ -11,12 +11,12 @@ import (
 )
 
 var (
-	ChatHistroy = &cChatHistroy{
+	ChatHistory = &cChatHistory{
 		Arr: make([]*ChatRecord, 0, 100),
 	}
 )
 
-type cChatHistroy struct {
+type cChatHistory struct {
 	Arr []*ChatRecord
 }
 
@@ -35,14 +35,14 @@ type ChatRecord struct {
 	Useful     int    `json:"useful" dc:"1:有用 -1:无用 0:暂无评价"`
 	Actions    int    `json:"actions" dc:"1:有功能菜单 0:无功能菜单"`
 	Item       int    `json:"item" dc:"1:常见问题 2:业务意图 3:chatGpt"`
-	AnswerId   int64  `json:"answer_id" dc:"回答问题的id"`
+	QuestionId int64  `json:"question_id" dc:"回答问题的id"`
 	Refer      string `json:"refer" dc:"会话来源地址"`
 	PersonId   int64  `json:"person_id" dc:"自然人id"`
 	CreateTime string `json:"create_time" dc:"时间"`
 }
 
 // CacheSave 保存聊天信息
-func (m *cChatHistroy) CacheSave(msg *ChatRecord) {
+func (m *cChatHistory) CacheSave(msg *ChatRecord) {
 	m.Arr = append(m.Arr, msg)
 	if len(m.Arr) > 100 {
 		tmp, ctx := m.Arr, gctx.New()
@@ -65,7 +65,7 @@ func (m *cChatHistroy) CacheSave(msg *ChatRecord) {
 }
 
 // Save 保存聊天记录 返回最后一条记录id
-func (m *cChatHistroy) Save(ctx context.Context, msgs ...*ChatRecord) (id int64) {
+func (m *cChatHistory) Save(ctx context.Context, msgs ...*ChatRecord) (id int64) {
 	val := gconv.Maps(msgs)
 	r, err := g.Model("ai_message_history").Data(val).Insert()
 
@@ -82,10 +82,10 @@ func (m *cChatHistroy) Save(ctx context.Context, msgs ...*ChatRecord) (id int64)
 }
 
 // GetMessage 查询聊天信息
-func (m *cChatHistroy) GetMessage(userId int64, pageNum, pageSize int, prevId string) (h []ResHistory, hasNext bool, err error) {
+func (m *cChatHistory) GetMessage(userId int64, pageNum, pageSize int, prevId string) (h []ResHistory, hasNext bool, err error) {
 	var hTmp []ResHistory
 	if prevId != "" {
-		err = g.Model("ai_message_history").Where("person_id = ? and id < ? ", userId, prevId).OrderDesc("id").Limit(pageNum*pageSize, pageSize).Scan(&hTmp)
+		err = g.Model("ai_message_history").Where("person_id = ? and id < ? ", userId, prevId).OrderDesc("id").Limit(pageSize).Scan(&hTmp)
 	} else {
 		err = g.Model("ai_message_history").Where("person_id = ?", userId).OrderDesc("id").Limit(pageNum*pageSize, pageSize).Scan(&hTmp)
 	}
@@ -103,7 +103,7 @@ func (m *cChatHistroy) GetMessage(userId int64, pageNum, pageSize int, prevId st
 }
 
 // Evaluate 评价
-func (m *cChatHistroy) Evaluate(userId int64, megId string, value int) error {
+func (m *cChatHistory) Evaluate(userId int64, megId string, value int) error {
 	res, err := g.Model("ai_message_history").One("id =? and person_id =? and type=2", megId, userId)
 	if err != nil {
 		return err

+ 4 - 7
internal/model/ws.go

@@ -1,7 +1,6 @@
 package model
 
 import (
-	"aiChat/utility"
 	"aiChat/utility/fsw"
 	. "app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/date"
@@ -38,7 +37,7 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 	}
 
 	reply, replyId, errMsg := func() (string, int64, error) {
-		questionId := ChatHistroy.Save(m.Ctx, &ChatRecord{
+		questionId := ChatHistory.Save(m.Ctx, &ChatRecord{
 			Content:    req.Prompt,
 			Type:       1,
 			Refer:      req.Href,
@@ -75,11 +74,11 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 		}
 
 		// 记录问答
-		replyId := ChatHistroy.Save(m.Ctx, &ChatRecord{
+		replyId := ChatHistory.Save(m.Ctx, &ChatRecord{
 			Content:    reply,
 			Type:       2,
 			Actions:    gconv.Int(If(errReply == "", 1, 0)),
-			AnswerId:   questionId,
+			QuestionId: questionId,
 			PersonId:   jSession.AccountId,
 			Item:       from,
 			CreateTime: time.Now().Format(date.Date_Full_Layout),
@@ -87,9 +86,7 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 		if replyId <= 0 {
 			g.Log().Error(m.Ctx, "问答存储存储异常")
 		}
-		if err = utility.ResourcePowerDeduct(m.Ctx, jSession.AccountId, jSession.EntAccountId, fmt.Sprintf("%d", replyId)); err != nil {
-			g.Log().Error(m.Ctx, "扣减异常", err)
-		}
+
 		return reply, replyId, nil
 	}()
 

+ 3 - 6
manifest/config/config.yaml

@@ -28,17 +28,14 @@ etcd:
       - 192.168.3.206:2379
     powerCheckCenterKey: "powercheck.rpc" #权益校验中台
     userCenterKey: "usercenter.rpc" #用户中台rpc
-  # 资源中台配置
-  resourceCenter:
-    key: resource.rpc
-    address:
-      - 192.168.3.206:2379
 
 redis:
   session:
     address: 192.168.3.149:1713
   main:
     address: 192.168.3.149:1712
+  black:
+    address: 192.168.3.149:1712
 
 database:
   default:
@@ -47,7 +44,7 @@ database:
     maxOpen: "5"
 
 limit:
-  rateMinute: 3 #每分钟速率
+  rateMinute: 10 #每分钟速率
   exceedMsg: "您的提问过于频繁,请等待一会儿再试" # 超出提示语句
   fswMsg: "您的问题可能包含敏感词汇,请修改后再提问!"# 敏感词回复
 

+ 0 - 59
utility/resource.go

@@ -1,59 +0,0 @@
-package utility
-
-import (
-	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
-	"bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/resource"
-	"fmt"
-	"github.com/gogf/gf/v2/frame/g"
-	"github.com/gogf/gf/v2/net/gtrace"
-	"github.com/gogf/gf/v2/os/gctx"
-	"github.com/zeromicro/go-zero/core/discov"
-	"github.com/zeromicro/go-zero/zrpc"
-)
-
-var ResourceCenterRpc resource.Resource
-
-func init() {
-	ctx := gctx.New()
-	ResourceCenterRpc = resource.NewResource(zrpc.MustNewClient(zrpc.RpcClientConf{
-		Etcd: discov.EtcdConf{
-			Key:   g.Cfg().MustGet(ctx, "etcd.resourceCenter.key", nil).String(),
-			Hosts: g.Cfg().MustGet(ctx, "etcd.resourceCenter.address", nil).Strings(),
-		},
-	}))
-}
-
-// ResourcePowerDeduct 资源中心消费
-// accountId 账户id
-// entAccountId 企业账户id
-// funcCode 业务代码
-// deductNum 扣除数量
-// ids 扣除详情id;例如数据导出信息id等数据
-func ResourcePowerDeduct(ctx g.Ctx, accountId, entAccountId int64, id string) error {
-	ctx, span := gtrace.NewSpan(ctx, "ResourcePowerDeduct")
-	defer span.End()
-	res, err := ResourceCenterRpc.Deduction(ctx, &pb.DeductionReq{
-		Appid:        g.Config().MustGet(ctx, "chat.appId").String(),
-		AccountId:    accountId,
-		EntAccountId: entAccountId,
-		FunctionCode: g.Config().MustGet(ctx, "chat.resourceCode").String(),
-		Count:        1,
-		Ids:          []string{id},
-	})
-
-	if err != nil {
-		return fmt.Errorf("资源异常")
-	}
-	//0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
-	switch res.Status {
-	case 0:
-		return fmt.Errorf("资源异常")
-	case -1:
-		return fmt.Errorf("权益已过期")
-	case -2:
-		return fmt.Errorf("对不起,当日提问已达上限,请明日再来吧~")
-	case -3:
-		return fmt.Errorf("资源权限异常")
-	}
-	return nil
-}