|
@@ -1,13 +1,20 @@
|
|
|
package facade
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "eta/eta_api/models/rag"
|
|
|
+ localService "eta/eta_api/services/llm"
|
|
|
"eta/eta_api/services/llm/facade/bus_response"
|
|
|
+ "eta/eta_api/utils"
|
|
|
"eta/eta_api/utils/llm"
|
|
|
"eta/eta_api/utils/llm/eta_llm/eta_llm_http"
|
|
|
"eta/eta_api/utils/ws"
|
|
|
"fmt"
|
|
|
"github.com/gorilla/websocket"
|
|
|
"github.com/rdlucklib/rdluck_tools/uuid"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "os"
|
|
|
)
|
|
|
|
|
|
var (
|
|
@@ -38,7 +45,132 @@ func LLMKnowledgeBaseSearchDocs(search LLMKnowledgeSearch) (resp bus_response.Se
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// AIGCBaseOnPromote aigc 生成内容
|
|
|
+func AIGCBaseOnPromote(aigc AIGC) (resp bus_response.AIGCEtaResponse, err error) {
|
|
|
+ mapping, queryErr := rag.GetArticleKbMapping(aigc.ArticleId)
|
|
|
+ if queryErr != nil && !errors.Is(queryErr, gorm.ErrRecordNotFound) {
|
|
|
+ utils.FileLog.Error("获取文章知识库信息失败,err: %v", queryErr)
|
|
|
+ err = fmt.Errorf("获取文章知识库信息失败,err: %v", queryErr)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ var kbId string
|
|
|
+ var file *os.File
|
|
|
+ var params map[string]interface{}
|
|
|
+ if mapping.Id == 0 || mapping.KbId == "" {
|
|
|
+ article, fileErr := rag.GetArticleById(aigc.ArticleId)
|
|
|
+ if fileErr != nil {
|
|
|
+ // 找不到就处理失败
|
|
|
+ utils.FileLog.Error("公众号文章不存在")
|
|
|
+ err = fmt.Errorf("公众号文章不存在")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 文章加入到知识库
|
|
|
+ path, fileErr := localService.CreateArticleFile(article)
|
|
|
+ if fileErr != nil {
|
|
|
+ utils.FileLog.Error("创建文章文件失败,err: %v", fileErr)
|
|
|
+ err = fmt.Errorf("创建文章文件失败,err: %v", fileErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = os.Remove(path)
|
|
|
+ }()
|
|
|
+ file, err = os.Open(path)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("打开文件失败,err:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ uploadResp, httpErr := llmService.UploadFileToTemplate([]*os.File{file}, params)
|
|
|
+ if httpErr != nil {
|
|
|
+ utils.FileLog.Error("上传文件失败,err:", err.Error())
|
|
|
+ err = fmt.Errorf("上传文件失败,err:%v", httpErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ data := uploadResp.(eta_llm_http.UploadDocsResponse)
|
|
|
+ //保存映射关系到数据库
|
|
|
+ if data.Id == "" {
|
|
|
+ utils.FileLog.Error("上传文件失败,向量库Id获取失败")
|
|
|
+ err = fmt.Errorf("上传文件失败,向量库Id获取失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = rag.CreateArticleKbMapping(rag.ArticleKbMapping{
|
|
|
+ WechatArticleId: aigc.ArticleId,
|
|
|
+ KbId: data.Id,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Warn("创建文章知识库映射关系失败,err:", err.Error())
|
|
|
+ }
|
|
|
+ kbId = data.Id
|
|
|
+ } else {
|
|
|
+ kbId = mapping.KbId
|
|
|
+ }
|
|
|
+ //知识库对话
|
|
|
+ response, httpErr := llmService.FileChat(aigc.Promote, kbId, nil)
|
|
|
+ if httpErr != nil {
|
|
|
+ utils.FileLog.Error("内容生成失败,err:", err.Error())
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", httpErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !response.Success {
|
|
|
+ utils.FileLog.Error("内容生成失败,code:%v,msg:%v", response.Ret, response.Msg)
|
|
|
+ err = fmt.Errorf("内容生成失败,code:%v,msg:%v", response.Ret, response.Msg)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ var baseResp eta_llm_http.RagBaseResponse
|
|
|
+ parseErr := json.Unmarshal(response.Data, &baseResp)
|
|
|
+ if parseErr != nil {
|
|
|
+ utils.FileLog.Error("内容生成失败,code:%v,msg:%v", parseErr)
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", parseErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if baseResp.Code != 200 {
|
|
|
+ if baseResp.Code == 404 {
|
|
|
+ params["PrevId"] = kbId
|
|
|
+ _, putErr := llmService.UploadFileToTemplate([]*os.File{file}, params)
|
|
|
+ if putErr != nil {
|
|
|
+ utils.FileLog.Error("内容生成失败,err:", err.Error())
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", httpErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ utils.FileLog.Error("内容生成失败,code:%v,msg:%v", baseResp.Code, baseResp.Msg)
|
|
|
+ err = fmt.Errorf("内容生成失败,code:%v,msg:%v", baseResp.Code, baseResp.Msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ gcResp, gcErr := llmService.FileChat(aigc.Promote, kbId, nil)
|
|
|
+ if gcErr != nil {
|
|
|
+ utils.FileLog.Error("内容生成失败,err:%v", gcErr.Error())
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", gcErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !gcResp.Success {
|
|
|
+ utils.FileLog.Error("内容生成失败,code:%v,msg:%v", gcResp.Ret, gcResp.Msg)
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", gcResp.Msg)
|
|
|
+ }
|
|
|
+ var steamResp eta_llm_http.ContentResponse
|
|
|
+ parseErr = json.Unmarshal(gcResp.Data, &steamResp)
|
|
|
+ if parseErr != nil {
|
|
|
+ utils.FileLog.Error("内容生成失败,code:%v,msg:%v", parseErr)
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", parseErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ parseErr = json.Unmarshal(steamResp.Data, &resp)
|
|
|
+ if parseErr != nil {
|
|
|
+ utils.FileLog.Error("内容生成失败,code:%v,msg:%v", parseErr)
|
|
|
+ err = fmt.Errorf("内容生成失败,err:%v", parseErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
type LLMKnowledgeSearch struct {
|
|
|
Query string `json:"Query"`
|
|
|
KnowledgeBaseName string `json:"KnowledgeBaseName"`
|
|
|
}
|
|
|
+
|
|
|
+type AIGC struct {
|
|
|
+ Promote string
|
|
|
+ ArticleId int
|
|
|
+}
|