feat: 百科知识库存入向量
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package plant
|
||||
|
||||
import (
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/commom/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AiChatApi struct{}
|
||||
|
||||
// ChatStreamPlant 接收用户输入,基于植物百科 RAG 产生流式 SSE 回答
|
||||
// @Summary 植物助手聊天(SSE 流式输出)
|
||||
// @Tags Plant-AiChat
|
||||
// @Param query query string true "用户提问内容"
|
||||
// @Produce text/event-stream
|
||||
// @Router /plant/chat/stream [get]
|
||||
func (a *AiChatApi) ChatStreamPlant(c *gin.Context) {
|
||||
query := c.Query("query")
|
||||
if query == "" {
|
||||
response.FailWithMsg("参数 query 不能为空", c)
|
||||
return
|
||||
}
|
||||
|
||||
// SSE Headers(微信小程序通过 enableChunked: true 配合实现打字机效果)
|
||||
w := c.Writer
|
||||
header := w.Header()
|
||||
header.Set("Content-Type", "text/event-stream")
|
||||
header.Set("Cache-Control", "no-cache")
|
||||
header.Set("Connection", "keep-alive")
|
||||
header.Set("Transfer-Encoding", "chunked")
|
||||
w.WriteHeader(200)
|
||||
|
||||
err := aiRagService.PlantChatStreamRAG(c.Request.Context(), query, func(chunk string) error {
|
||||
_, writeErr := w.WriteString("data: " + chunk + "\n\n")
|
||||
w.Flush()
|
||||
return writeErr
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
global.Logger.Error("PlantChatStreamRAG error", zap.Error(err))
|
||||
_, _ = w.WriteString("data: [ERROR]" + err.Error() + "\n\n")
|
||||
w.Flush()
|
||||
} else {
|
||||
// 流结束标志
|
||||
_, _ = w.WriteString("data: [DONE]\n\n")
|
||||
w.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// SyncWikiToQdrant 手动触发全量植物百科同步到 Qdrant
|
||||
// @Summary 同步植物百科数据到 Qdrant 向量库
|
||||
// @Tags System-SysAiConfig
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.Response
|
||||
// @Router /plant/chat/sync [post]
|
||||
func (a *AiChatApi) SyncWikiToQdrant(c *gin.Context) {
|
||||
if err := aiRagService.SyncWikiToQdrant(); err != nil {
|
||||
global.Logger.Error("SyncWikiToQdrant error", zap.Error(err))
|
||||
response.FailWithMsg("同步失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("同步成功", c)
|
||||
}
|
||||
@@ -14,6 +14,7 @@ type ApiGroup struct {
|
||||
BadgeConfigApi
|
||||
CallbackApi
|
||||
ExchangeApi
|
||||
AiChatApi
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -28,4 +29,5 @@ var (
|
||||
badgeConfigService = service.GroupApp.PlantServiceGroup.BadgeConfigService
|
||||
callbackService = service.GroupApp.PlantServiceGroup.CallbackService
|
||||
exchangeService = service.GroupApp.PlantServiceGroup.ExchangeService
|
||||
aiRagService = service.GroupApp.PlantServiceGroup.AiRagService
|
||||
)
|
||||
|
||||
@@ -139,6 +139,48 @@ func (a *WikiApi) DeleteWiki(c *gin.Context) {
|
||||
response.OkWithMsg("删除成功", c)
|
||||
}
|
||||
|
||||
// SyncWikiQdrant 单条百科同步到 Qdrant
|
||||
// @Tags 百科
|
||||
// @Summary 百科同步到Qdrant
|
||||
// @Security BearerAuth
|
||||
// @Produce application/json
|
||||
// @Param data body common.GetById true "单条百科"
|
||||
// @Router /wiki/sync-qdrant [post]
|
||||
func (a *WikiApi) SyncWikiQdrant(c *gin.Context) {
|
||||
var req common.GetById
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMsg("请求参数错误", c)
|
||||
return
|
||||
}
|
||||
if err := aiRagService.SyncSingleWiki(req.ID); err != nil {
|
||||
global.Logger.Error("同步 Qdrant 失败", zap.Error(err))
|
||||
response.FailWithMsg("同步失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("同步成功", c)
|
||||
}
|
||||
|
||||
// DeleteWikiQdrant 从 Qdrant 移除单条百科向量
|
||||
// @Tags 百科
|
||||
// @Summary 百科移除Qdrant
|
||||
// @Security BearerAuth
|
||||
// @Produce application/json
|
||||
// @Param data body common.GetById true "单条百科"
|
||||
// @Router /wiki/delete-qdrant [post]
|
||||
func (a *WikiApi) DeleteWikiQdrant(c *gin.Context) {
|
||||
var req common.GetById
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMsg("请求参数错误", c)
|
||||
return
|
||||
}
|
||||
if err := aiRagService.DeleteFromQdrant(req.ID); err != nil {
|
||||
global.Logger.Error("从 Qdrant 删除向量失败", zap.Error(err))
|
||||
response.FailWithMsg("移除失败: "+err.Error(), c)
|
||||
return
|
||||
}
|
||||
response.OkWithMsg("移除成功", c)
|
||||
}
|
||||
|
||||
// StarWiki 收藏百科
|
||||
// @Tags 百科
|
||||
// @Summary 收藏百科
|
||||
|
||||
Reference in New Issue
Block a user