hongze 1 rok temu
rodzic
commit
4c11cf04e9
2 zmienionych plików z 121 dodań i 9 usunięć
  1. 107 9
      controllers/ai/ai_file.go
  2. 14 0
      models/aimod/ai.go

+ 107 - 9
controllers/ai/ai_file.go

@@ -22,6 +22,7 @@ type AiFileController struct {
 // @Title 文件上传
 // @Description 文件上传接口
 // @Param   File   query   file  true       "文件"
+// @Param   AiChatTopicId   query   int  true       "主题id"
 // @Success 200 {object} models.ResourceResp
 // @router /file/upload [post]
 func (this *AiFileController) FileUpload() {
@@ -30,12 +31,16 @@ func (this *AiFileController) FileUpload() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
+
 	f, h, err := this.GetFile("File")
 	if err != nil {
 		br.Msg = "获取资源信息失败"
 		br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
 		return
 	}
+
+	aiChatTopicId, _ := this.GetInt("AiChatTopicId")
+
 	uploadFileName := h.Filename //上传的文件名
 	ext := path.Ext(h.Filename)
 	dateDir := time.Now().Format("20060102")
@@ -96,7 +101,86 @@ func (this *AiFileController) FileUpload() {
 		br.ErrMsg = "文件上传失败,Err:" + err.Error()
 		return
 	}
-	if uploadResult != nil {
+
+	var assistantId, threadId string
+	if aiChatTopicId > 0 {
+		aiChatTopicObj := new(aimod.AiChatTopic)
+		aiChatTopicObj.AiChatTopicId = aiChatTopicId
+		topic, err := aiChatTopicObj.GetAiChatTopicById()
+		if err != nil {
+			if err.Error() == utils.ErrNoRow() {
+				br.Msg = "获取数据失败!"
+				br.ErrMsg = "获取数据失败,主题不存在,Err:" + err.Error()
+				return
+			}
+			br.Msg = "获取数据失败!"
+			br.ErrMsg = "获取数据失败,GetAiChatTopicById,Err:" + err.Error()
+			return
+		}
+		assistantId = topic.AssistantId
+		threadId = topic.ThreadId
+	}
+
+	if aiChatTopicId <= 0 { //新增
+		topic := new(aimod.AiChatTopic)
+		topic.TopicName = fileName
+		topic.SysUserId = this.SysUser.AdminId
+		topic.SysUserRealName = this.SysUser.RealName
+		topic.CreateTime = time.Now()
+		topic.ModifyTime = time.Now()
+		topic.AssistantId = assistantId
+		topic.ThreadId = threadId
+		topicId, err := aimod.AddAiChatTopic(topic)
+		if err != nil {
+			br.Msg = "获取数据失败!"
+			br.ErrMsg = "生成话题失败,Err:" + err.Error()
+			return
+		}
+		aiChatTopicId = int(topicId)
+		chatItem := new(aimod.AiChat)
+		chatItem.AiChatTopicId = aiChatTopicId
+		chatItem.Ask = fileName
+		chatItem.AskUuid = utils.MD5(fileName)
+		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.SysUserId = this.SysUser.AdminId
+		chatItem.SysUserRealName = this.SysUser.RealName
+		if uploadResult != nil && uploadResult.Data != nil {
+			chatItem.OpenaiFileId = uploadResult.Data.ID
+			chatItem.OpenaiFileName = uploadFileName
+		}
+		chatItem.OpenaiFilePath = resourceUrl
+		chatItem.CreateTime = time.Now()
+		chatItem.ModifyTime = time.Now()
+		_, err = aimod.AddAiChat(chatItem)
+		if err != nil {
+			br.Msg = "获取数据失败!"
+			br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
+			return
+		}
+	} else {
+		chatItem := new(aimod.AiChat)
+		chatItem.AiChatTopicId = aiChatTopicId
+		chatItem.Ask = fileName
+		chatItem.AskUuid = utils.MD5(fileName)
+		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.SysUserId = this.SysUser.AdminId
+		chatItem.SysUserRealName = this.SysUser.RealName
+		if uploadResult != nil && uploadResult.Data != nil {
+			chatItem.OpenaiFileId = uploadResult.Data.ID
+			chatItem.OpenaiFileName = uploadFileName
+		}
+		chatItem.OpenaiFilePath = resourceUrl
+		chatItem.CreateTime = time.Now()
+		chatItem.ModifyTime = time.Now()
+		_, err = aimod.AddAiChat(chatItem)
+		if err != nil {
+			br.Msg = "获取数据失败!"
+			br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
+			return
+		}
+	}
+
+	if uploadResult != nil && uploadResult.Data != nil && uploadResult.Data.ID != "" {
 		uploadObj := new(aimod.FileUploadRecord)
 		uploadObj.AdminId = this.SysUser.AdminId
 		uploadObj.FileUrl = resourceUrl
@@ -246,8 +330,6 @@ func (this *AiFileController) FileRetrieve() {
 		threadId = fileRetrieveResp.Data.ThreadId
 		answer = fileRetrieveResp.Data.Answer
 	}
-	resp.Ask = req.Ask
-	resp.Answer = answer
 
 	if req.AiChatTopicId <= 0 { //新增
 		topic := new(aimod.AiChatTopic)
@@ -264,12 +346,10 @@ func (this *AiFileController) FileRetrieve() {
 			br.ErrMsg = "生成话题失败,Err:" + err.Error()
 			return
 		}
-		resp.AiChatTopicId = int(topicId)
 		chatItem := new(aimod.AiChat)
-		chatItem.AiChatTopicId = resp.AiChatTopicId
+		chatItem.AiChatTopicId = int(topicId)
 		chatItem.Ask = req.Ask
 		chatItem.AskUuid = utils.MD5(req.Ask)
-		chatItem.Answer = answer
 		chatItem.Model = "gpt-4-1106-preview"
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
@@ -282,12 +362,10 @@ func (this *AiFileController) FileRetrieve() {
 			return
 		}
 	} else {
-		resp.AiChatTopicId = req.AiChatTopicId
 		chatItem := new(aimod.AiChat)
-		chatItem.AiChatTopicId = resp.AiChatTopicId
+		chatItem.AiChatTopicId = req.AiChatTopicId
 		chatItem.Ask = req.Ask
 		chatItem.AskUuid = utils.MD5(req.Ask)
-		chatItem.Answer = answer
 		chatItem.Model = "gpt-4-1106-preview"
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
@@ -299,7 +377,27 @@ func (this *AiFileController) FileRetrieve() {
 			br.ErrMsg = "生成话题记录失败,Err:" + err.Error()
 			return
 		}
+		//更新	assistantId,threadId
+		aiChatTopicObj := new(aimod.AiChatTopic)
+
+		updateParams := make(map[string]interface{})
+		updateParams["assistant_id"] = assistantId
+		updateParams["thread_id"] = threadId
+		updateParams["modify_time"] = time.Now()
+
+		whereParam := make(map[string]interface{})
+		whereParam["ai_chat_topic_id"] = req.AiChatTopicId
+
+		err = aiChatTopicObj.Update(updateParams, whereParam)
+		if err != nil {
+			br.Msg = "获取失败!"
+			br.ErrMsg = "修改助手标识失败,Err:" + err.Error()
+			return
+		}
 	}
+
+	resp.Ask = req.Ask
+	resp.Answer = answer
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"

+ 14 - 0
models/aimod/ai.go

@@ -27,6 +27,7 @@ type AiChat struct {
 	SysUserRealName string `description:"提问人名称"`
 	OpenaiFileId    string `description:"openai返回的文件id"`
 	OpenaiFileName  string `description:"文件名称"`
+	OpenaiFilePath  string `description:"文件路径"`
 	CreateTime      time.Time
 	ModifyTime      time.Time
 }
@@ -164,3 +165,16 @@ type HistoryChat struct {
 	Ask    string
 	Answer string
 }
+
+// 修改
+func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
+	to := orm.NewOrm()
+	ptrStructOrTableName := "ai_chat_topic"
+
+	qs := to.QueryTable(ptrStructOrTableName)
+	for expr, exprV := range whereParam {
+		qs = qs.Filter(expr, exprV)
+	}
+	_, err = qs.Update(updateParams)
+	return
+}