kefu/controller/ai/traning.go

66 lines
1.8 KiB
Go
Raw Normal View History

2024-12-10 02:50:12 +00:00
package ai
import (
"fmt"
"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
"kefu/models"
"kefu/tools"
"unicode/utf8"
)
// 训练素材文本
func PostTraning(c *gin.Context) {
title := c.PostForm("title")
content := c.PostForm("content")
id := c.PostForm("id")
oldFileId := c.PostForm("fileId")
collectName, _ := c.Get("collect_name")
openaiUrl, _ := c.Get("openai_url")
openaiKey, _ := c.Get("openai_key")
pointId := uuid.NewV4().String()
if id != "" {
pointId = id
res, _ := Train(openaiUrl.(string), openaiKey.(string), pointId, collectName.(string), content, oldFileId, title, "")
c.Writer.Write([]byte(res))
return
}
if oldFileId != "" {
fileModel := models.FindAiFileRow("id = ?", oldFileId)
fileModel.FileSize = tools.Int2Str(tools.Str2Int(fileModel.FileSize) + utf8.RuneCountInString(content))
fileModel.SaveAiFile("id = ?", oldFileId)
res, err := Train(openaiUrl.(string), openaiKey.(string), pointId, collectName.(string), content, oldFileId, title, "")
//入库
if err == nil {
aiFilePoint := &models.AiFilePoints{
FileId: fmt.Sprintf("%s", oldFileId),
CollectName: collectName.(string),
PointsId: pointId,
}
aiFilePoint.AddAiFilePoint()
}
c.Writer.Write([]byte(res))
return
}
//入库
files := &models.AiFile{
FileName: title,
CollectName: collectName.(string),
FileSize: tools.Int2Str(utf8.RuneCountInString(content)),
}
fileId := files.AddAiFile()
res, err := Train(openaiUrl.(string), openaiKey.(string), pointId, collectName.(string), content, tools.Int2Str(fileId), title, "")
//入库
if err == nil {
aiFilePoint := &models.AiFilePoints{
FileId: fmt.Sprintf("%d", fileId),
CollectName: collectName.(string),
PointsId: pointId,
}
aiFilePoint.AddAiFilePoint()
}
c.Writer.Write([]byte(res))
return
}