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) }