Browse Source

fix:ai topic字数限制

zqbao 6 months ago
parent
commit
9428b55c85
4 changed files with 64 additions and 6 deletions
  1. 7 1
      controllers/ai/ai.go
  2. 7 1
      controllers/ai/ai_file.go
  3. 17 4
      controllers/ai/ai_summary.go
  4. 33 0
      services/aiser/ai.go

+ 7 - 1
controllers/ai/ai.go

@@ -114,8 +114,14 @@ func (this *AiController) List() {
 	resp.Model = req.Model
 
 	if req.AiChatTopicId <= 0 { //新增
+		name, err := aiser.QueryAskContent(req.Ask)
+		if err != nil {
+			br.Msg = "获取数据失败!"
+			br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error()
+			return
+		}
 		topic := new(aimod.AiChatTopic)
-		topic.TopicName = req.Ask
+		topic.TopicName = name
 		topic.SysUserId = this.SysUser.AdminId
 		topic.SysUserRealName = this.SysUser.RealName
 		topic.CreateTime = time.Now()

+ 7 - 1
controllers/ai/ai_file.go

@@ -352,8 +352,14 @@ func (this *AiFileController) FileRetrieve() {
 	}
 
 	if req.AiChatTopicId <= 0 { //新增
+		name, err := aiser.QueryAskContent(req.Ask)
+		if err != nil {
+			br.Msg = "获取数据失败!"
+			br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error()
+			return
+		}
 		topic := new(aimod.AiChatTopic)
-		topic.TopicName = req.Ask
+		topic.TopicName = name
 		topic.SysUserId = this.SysUser.AdminId
 		topic.SysUserRealName = this.SysUser.RealName
 		topic.CreateTime = time.Now()

+ 17 - 4
controllers/ai/ai_summary.go

@@ -11,9 +11,10 @@ import (
 	"eta_gn/eta_api/services/aiser"
 	"eta_gn/eta_api/utils"
 	"fmt"
-	"github.com/rdlucklib/rdluck_tools/paging"
 	"strconv"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
 // AiSummaryClassifyItems
@@ -1059,7 +1060,7 @@ func (this *AiController) GenerateAiSummary() {
 		return
 	}
 	if req.OriginContent == "" && len(req.OpenaiFileId) == 0 && req.SaDocId <= 0 {
-		br.Msg = "参数错误"
+		br.Msg = "请选择文档或输入内容"
 		br.IsSendEmail = false
 		return
 	}
@@ -1155,8 +1156,14 @@ func (this *AiController) GenerateAiSummary() {
 		resp.Model = req.Model
 
 		if req.AiChatTopicId <= 0 { //新增
+			name, err := aiser.QueryAskContent(ask)
+			if err != nil {
+				br.Msg = "获取数据失败!"
+				br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error()
+				return
+			}
 			topic := new(aimod.AiChatTopic)
-			topic.TopicName = ask
+			topic.TopicName = name
 			topic.SysUserId = this.SysUser.AdminId
 			topic.SysUserRealName = this.SysUser.RealName
 			topic.CreateTime = time.Now()
@@ -1285,8 +1292,14 @@ func (this *AiController) GenerateAiSummary() {
 		}
 
 		if req.AiChatTopicId <= 0 { //新增
+			name, err := aiser.QueryAskContent(ask)
+			if err != nil {
+				br.Msg = "获取数据失败!"
+				br.ErrMsg = "获取数据失败,QueryAskContent,Err:" + err.Error()
+				return
+			}
 			topic := new(aimod.AiChatTopic)
-			topic.TopicName = ask
+			topic.TopicName = name
 			topic.SysUserId = this.SysUser.AdminId
 			topic.SysUserRealName = this.SysUser.RealName
 			topic.CreateTime = time.Now()

+ 33 - 0
services/aiser/ai.go

@@ -9,8 +9,41 @@ import (
 	"net/http"
 	"strings"
 	"time"
+
+	"github.com/PuerkitoBio/goquery"
 )
 
+func QueryAskContent(ask string) (result string, err error) {
+	if ask == "" {
+		return "", errors.New("ask is empty")
+	}
+	doc, err := goquery.NewDocumentFromReader(strings.NewReader(ask))
+	if err != nil {
+		return
+	}
+	var text string
+	doc.Find("p").Each(func(i int, s *goquery.Selection) {
+		if _, ok := s.Attr("data-f-id"); !ok {
+			text += s.Text()
+		}
+	})
+	tmpText := []rune(text)
+	if len(tmpText) > 80 {
+		result = string(tmpText[:80])
+	} else {
+		result = text
+	}
+	if result == "" {
+		askLen := len([]rune(ask))
+		if askLen > 80 {
+			result = string([]rune(ask))
+		} else {
+			result = ask
+		}
+	}
+	return
+}
+
 func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat, model string) (result string, err error) {
 	chatUrl := utils.EtaAiUrl + `chat/auto_msg`