feat: 百科rag
This commit is contained in:
@@ -3,6 +3,7 @@ package plant
|
||||
import (
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/commom/response"
|
||||
"sundynix-go/utils/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
@@ -32,7 +33,9 @@ func (a *AiChatApi) ChatStreamPlant(c *gin.Context) {
|
||||
header.Set("Transfer-Encoding", "chunked")
|
||||
w.WriteHeader(200)
|
||||
|
||||
var fullAnswer string
|
||||
err := aiRagService.PlantChatStreamRAG(c.Request.Context(), query, func(chunk string) error {
|
||||
fullAnswer += chunk
|
||||
_, writeErr := w.WriteString("data: " + chunk + "\n\n")
|
||||
w.Flush()
|
||||
return writeErr
|
||||
@@ -47,6 +50,16 @@ func (a *AiChatApi) ChatStreamPlant(c *gin.Context) {
|
||||
_, _ = w.WriteString("data: [DONE]\n\n")
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
// 异步保存问答历史
|
||||
if fullAnswer != "" {
|
||||
userId := auth.GetUserId(c)
|
||||
go func() {
|
||||
if saveErr := chatHistoryService.SaveHistory(userId, query, fullAnswer); saveErr != nil {
|
||||
global.Logger.Error("Save chat history failed", zap.Error(saveErr))
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// SyncWikiToQdrant 手动触发全量植物百科同步到 Qdrant
|
||||
@@ -63,3 +76,74 @@ func (a *AiChatApi) SyncWikiToQdrant(c *gin.Context) {
|
||||
}
|
||||
response.OkWithMsg("同步成功", c)
|
||||
}
|
||||
|
||||
// GetChatHistory 获取AI问答历史列表
|
||||
// @Tags Plant-AiChat
|
||||
// @Param current query int false "页码"
|
||||
// @Param pageSize query int false "每页条数"
|
||||
// @Router /plant/chat/history [get]
|
||||
func (a *AiChatApi) GetChatHistory(c *gin.Context) {
|
||||
userId := auth.GetUserId(c)
|
||||
current := 1
|
||||
pageSize := 20
|
||||
if v, ok := c.GetQuery("current"); ok {
|
||||
if n := parseInt(v); n > 0 {
|
||||
current = n
|
||||
}
|
||||
}
|
||||
if v, ok := c.GetQuery("pageSize"); ok {
|
||||
if n := parseInt(v); n > 0 {
|
||||
pageSize = n
|
||||
}
|
||||
}
|
||||
list, total, err := chatHistoryService.GetHistoryList(userId, current, pageSize)
|
||||
if err != nil {
|
||||
response.FailWithMsg("查询失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithData(map[string]interface{}{
|
||||
"list": list,
|
||||
"total": total,
|
||||
}, c)
|
||||
}
|
||||
|
||||
// DeleteChatHistory 删除单条历史
|
||||
// @Tags Plant-AiChat
|
||||
// @Router /plant/chat/history/delete [post]
|
||||
func (a *AiChatApi) DeleteChatHistory(c *gin.Context) {
|
||||
var req struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.Id == "" {
|
||||
response.FailWithMsg("参数错误", c)
|
||||
return
|
||||
}
|
||||
userId := auth.GetUserId(c)
|
||||
if err := chatHistoryService.DeleteHistory(userId, req.Id); err != nil {
|
||||
response.FailWithMsg("删除失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("删除成功", c)
|
||||
}
|
||||
|
||||
// ClearChatHistory 清空所有历史
|
||||
// @Tags Plant-AiChat
|
||||
// @Router /plant/chat/history/clear [post]
|
||||
func (a *AiChatApi) ClearChatHistory(c *gin.Context) {
|
||||
userId := auth.GetUserId(c)
|
||||
if err := chatHistoryService.ClearHistory(userId); err != nil {
|
||||
response.FailWithMsg("清空失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("已清空", c)
|
||||
}
|
||||
|
||||
func parseInt(s string) int {
|
||||
n := 0
|
||||
for _, c := range s {
|
||||
if c >= '0' && c <= '9' {
|
||||
n = n*10 + int(c-'0')
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
@@ -30,4 +30,5 @@ var (
|
||||
callbackService = service.GroupApp.PlantServiceGroup.CallbackService
|
||||
exchangeService = service.GroupApp.PlantServiceGroup.ExchangeService
|
||||
aiRagService = service.GroupApp.PlantServiceGroup.AiRagService
|
||||
chatHistoryService = service.GroupApp.PlantServiceGroup.AiChatHistoryService
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user