123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package model
- import (
- "aiChat/internal/consts"
- "aiChat/utility"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "regexp"
- "strings"
- )
- var (
- Question = &cQuestion{}
- regExpSmart = regexp.MustCompile("smart_\\S+_smart")
- )
- const (
- Answer_UsuallyProblem = iota + 1
- Answer_Isbusiness
- Answer_ChatGPT
- )
- type cQuestion struct {
- }
- type BaseQuestion struct {
- Prompt string `json:"prompt"`
- History [][]string `json:"history"`
- }
- type QuestionReq struct {
- *BaseQuestion
- Href string `json:"href"` //咨询页面
- }
- // GetGuessQuestion 猜你想问
- func (q *cQuestion) GetGuessQuestion(ctx context.Context, limit int) (list []string, err error) {
- list = make([]string, 0, limit)
- res, err := g.Model("ai_question_list").Ctx(ctx).Fields("question").
- Where("status = 1 and question_type = ?", consts.QUESTION_TYPE_GUESS).OrderDesc("id").Limit(limit).All()
- if err != nil {
- return nil, err
- }
- for _, m := range res.List() {
- list = append(list, gconv.String(m["question"]))
- }
- return
- }
- // GetUsuallyProblem 获取常见问题
- func (q *cQuestion) GetUsuallyProblem(ctx context.Context, scenario, limit int) (list []string, err error) {
- list = make([]string, 0, limit)
- res, err := g.Model("ai_question_list").Ctx(ctx).Fields("question").
- Where("status = 1 and question_type = ? and source = ?", consts.QUESTION_TYPE_USUALLY, scenario).OrderDesc("id").Limit(limit).All()
- if err != nil {
- return nil, err
- }
- for _, m := range res.List() {
- list = append(list, gconv.String(m["question"]))
- }
- return
- }
- type BusinessRes struct {
- CheckMember int `json:"check_member" dc:"是否校验大会员权限"`
- Answer string `json:"answer" dc:"答案"`
- AutoUrl string `json:"auto_url" dc:"默认url"`
- Joggle string `json:"joggle" dc:"业务接口"`
- Noperm string `json:"noperm" dc:"无权限回复"`
- Source string `json:"source" dc:"问题所属"`
- ServiceId string `json:"service_id" dc:"大会员功能服务id"`
- }
- // getIsbusinessData 获取业务规则
- func (q *cQuestion) getIsbusinessData(ctx context.Context, code string) (bRes *BusinessRes, err error) {
- res, err := g.Model("ai_question").Ctx(ctx).Fields("check_member", "answer", "auto_url", "joggle", "noperm", "source", "service_id").Where("ai_code = ?", code).One()
- if err != nil {
- return nil, err
- }
- if res.IsEmpty() {
- return nil, fmt.Errorf("未知业务指令")
- }
- bRes = &BusinessRes{}
- if err = res.Struct(bRes); err != nil {
- return nil, err
- }
- return bRes, nil
- }
- // DetailQuestion 问题处理
- func (q *cQuestion) DetailQuestion(ctx context.Context, qRes *QuestionReq) (reply string, from int, err error) {
- cRes, err := ChatGpt.Do(ctx, qRes)
- if err != nil {
- return "", 0, err
- }
- // 校验是否有业务逻辑
- matchArr := regExpSmart.FindStringSubmatch(cRes.Response)
- if len(matchArr) == 0 {
- return cRes.Response, Answer_ChatGPT, nil
- }
- // 查询业务逻辑
- var bRes = &BusinessRes{}
- bRes, err = q.getIsbusinessData(ctx, matchArr[0])
- // 权限校验
- jSession := SessionCtx.Get(ctx).JSession
- powerPass := func() bool {
- power := utility.Middleground.PowerCheckCenter.Check(g.Config().MustGet(ctx, "chat.appId").String(), jSession.UserId, jSession.NewUid, jSession.AccountId, jSession.EntId, jSession.PositionType, jSession.PositionId)
- // 大会员权益校验
- if bRes.CheckMember == 1 && power.Member.Status <= 0 {
- return false
- }
- // 大会员权益ServiceId校验
- if bRes.ServiceId != "" {
- for _, v := range strings.Split(bRes.ServiceId, ",") {
- var ok bool = false
- for _, vv := range power.Member.MemberPowerList {
- if gconv.Int64(v) == vv {
- ok = true
- break
- }
- }
- if !ok {
- return false
- }
- }
- }
- return true
- }()
- if !powerPass {
- return bRes.Noperm, Answer_Isbusiness, nil
- }
- _, infoId := GetScenarioAndInfoId(qRes.Href)
- if bRes.Source == scenarioName[DetailPage] && infoId == "" {
- return bRes.AutoUrl, Answer_Isbusiness, nil
- }
- businessRes, err := utility.DoBusiness(ctx, bRes.Joggle, &utility.RpcParams{
- UserId: jSession.UserId,
- Answer: bRes.Answer,
- BiddingId: infoId,
- IsEnt: false,
- BaseUserId: jSession.NewUid,
- })
- if err != nil {
- return "", Answer_Isbusiness, nil
- }
- if businessRes.ErrMsg != "" {
- return "", Answer_Isbusiness, fmt.Errorf(businessRes.ErrMsg)
- }
- return businessRes.ReplyMsg, Answer_Isbusiness, nil
- }
|