From 8cd4de5fe36dc8116bc3168a5d581d23bbca3e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KevinZ=C3=B8nda?= <33132228+KevinZonda@users.noreply.github.com> Date: Sat, 20 Jan 2024 16:03:27 +0000 Subject: [PATCH] agent --- backend/rag/agent.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 backend/rag/agent.go diff --git a/backend/rag/agent.go b/backend/rag/agent.go new file mode 100644 index 0000000..225bc5f --- /dev/null +++ b/backend/rag/agent.go @@ -0,0 +1,48 @@ +package rag + +import ( + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" + "os" +) + +type IAgent interface { + Search(query string) ([]string, error) +} + +func RunAgent(a IAgent) { + godotenv.Load(".env") + gin.New() + g := gin.New() + g.Use(gin.Logger(), gin.Recovery()) + cs := cors.DefaultConfig() + cs.AllowAllOrigins = true + g.Use(cors.New(cs)) + g.POST("/query", func(c *gin.Context) { + var req AgentRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + return + } + rst, err := a.Search(req.Query) + if err != nil { + c.JSON(400, gin.H{"error": err.Error()}) + return + } + c.JSON(200, AgentResponse{Rst: rst}) + }) + listenAddr := os.Getenv("AGENT_LISTEN_ADDR") + if listenAddr == "" { + listenAddr = ":8080" + } + g.Run(listenAddr) +} + +type AgentRequest struct { + Query string `json:"query"` +} + +type AgentResponse struct { + Rst []string `json:"rst"` +}