|
@@ -0,0 +1,86 @@
|
|
|
|
+package model
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ elastic "app.yhyue.com/moapp/jybase/esv1"
|
|
|
|
+ "context"
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ Question = &cQuestion{}
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+const (
|
|
|
|
+ answerFrom = iota
|
|
|
|
+ Answer_UsuallyProblem
|
|
|
|
+ Answer_Isbusiness
|
|
|
|
+ Answer_ChatGPT
|
|
|
|
+
|
|
|
|
+ index, itype = "aiquestion", "aiquestion"
|
|
|
|
+ GetQuestionListSql = `{"query":{"bool":{"must":[{"term":{"status":1}},{"term":{"isbusiness":%d}},{"term":{"source":"%s"}}]}},"from":0,"size":%d,"sort":[{"id":"desc"}],"_source": ["question"]}`
|
|
|
|
+ GetAnswerFromQuestion = `{"query":{"bool":{"must":[{"term":{"status":1}},{"multi_match":{"query":"%s","minimum_should_match":"%s","fields":["question"]}}]}},"from":0,"size":1,"_source":["check_member","isbusiness","question","answer","auto_url","joggle","service_id","noperm","source"]}`
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type cQuestion struct {
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// QuestionReq 用户发送过来的问题详情
|
|
|
|
+type QuestionReq struct {
|
|
|
|
+ Context string `json:"context"` //内容
|
|
|
|
+ History []struct {
|
|
|
|
+ Q string `json:"q"` //问题
|
|
|
|
+ A string `json:"a"` //答案
|
|
|
|
+ } `json:"history,omitempty"` //会话上下文,会话历史
|
|
|
|
+ Href string `json:"href,omitempty"` //咨询页面
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// QuestionEsRes 问题检索库查询结果
|
|
|
|
+type QuestionEsRes struct {
|
|
|
|
+ CheckMember int `json:"check_member" dc:"是否校验大会员权限"`
|
|
|
|
+ Isbusiness int `json:"isbusiness" dc:"业务意图 0:否 1:是"`
|
|
|
|
+ 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"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetProblem 获取常见问题及猜你想问
|
|
|
|
+func (q *cQuestion) GetProblem(scenario, isbusiness, limit int) (list []string) {
|
|
|
|
+ list = make([]string, 0, limit)
|
|
|
|
+ res := elastic.Get(index, itype, fmt.Sprintf(GetQuestionListSql, isbusiness, scenarioName[scenario], limit))
|
|
|
|
+ if res != nil && len(*res) > 0 {
|
|
|
|
+ for _, m := range *res {
|
|
|
|
+ list = append(list, gconv.MapStrStr(m)["question"])
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetAnswer 获取答案
|
|
|
|
+func (q *cQuestion) GetAnswer(ctx context.Context, question *QuestionReq) (itemFrom int, detail string, err error) {
|
|
|
|
+ res := elastic.Get(index, itype, fmt.Sprintf(GetAnswerFromQuestion, question.Context, g.Config().MustGet(ctx, "chat.match", "70%").String()))
|
|
|
|
+ if res == nil || len(*res) == 0 {
|
|
|
|
+ return getAnswerFromChatGPT()
|
|
|
|
+ }
|
|
|
|
+ qResObj := &QuestionEsRes{}
|
|
|
|
+ gconv.Struct((*res)[0], qResObj)
|
|
|
|
+
|
|
|
|
+ // 非业务问题直接返回
|
|
|
|
+ if qResObj.Isbusiness == 0 {
|
|
|
|
+ return Answer_UsuallyProblem, qResObj.Answer, nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 业务问题
|
|
|
|
+ //qResObj.CheckMember
|
|
|
|
+
|
|
|
|
+ return Answer_Isbusiness, "", nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// getChatGPT 获取ChatGPT答案
|
|
|
|
+func getAnswerFromChatGPT() (int, string, error) {
|
|
|
|
+ return Answer_ChatGPT, "", nil
|
|
|
|
+}
|