Integrate with LocalRAG, drop RAG functionalities
This commit is contained in:
140
webui/app.go
140
webui/app.go
@@ -49,146 +49,6 @@ func NewApp(opts ...Option) *App {
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *App) KnowledgeBaseReset(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
db := pool.GetAgentMemory(c.Params("name"))
|
||||
db.Reset()
|
||||
return c.Redirect("/knowledgebase/" + c.Params("name"))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) KnowledgeBaseExport(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
db := pool.GetAgentMemory(c.Params("name"))
|
||||
knowledgeBase := db.Data()
|
||||
|
||||
c.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.knowledgebase.json", c.Params("name")))
|
||||
return c.JSON(knowledgeBase)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) KnowledgeBaseImport(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
// Handle error
|
||||
return err
|
||||
}
|
||||
|
||||
os.MkdirAll("./uploads", os.ModePerm)
|
||||
|
||||
destination := fmt.Sprintf("./uploads/%s", file.Filename)
|
||||
if err := c.SaveFile(file, destination); err != nil {
|
||||
// Handle error
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
knowledge := []string{}
|
||||
if err := json.Unmarshal(data, &knowledge); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(knowledge) > 0 {
|
||||
xlog.Info("Importing agent KB")
|
||||
db := pool.GetAgentMemory(c.Params("name"))
|
||||
db.Reset()
|
||||
|
||||
for _, k := range knowledge {
|
||||
db.Store(k)
|
||||
}
|
||||
|
||||
} else {
|
||||
return fmt.Errorf("Empty knowledge base")
|
||||
}
|
||||
|
||||
return c.Redirect("/agents")
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) KnowledgeBaseFile(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
agent := pool.GetAgent(c.Params("name"))
|
||||
db := agent.Memory()
|
||||
|
||||
// https://golang.withcodeexample.com/blog/file-upload-handling-golang-fiber-guide/
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
// Handle error
|
||||
return err
|
||||
}
|
||||
|
||||
payload := struct {
|
||||
ChunkSize int `form:"chunk_size"`
|
||||
}{}
|
||||
|
||||
if err := c.BodyParser(&payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
os.MkdirAll("./uploads", os.ModePerm)
|
||||
|
||||
destination := fmt.Sprintf("./uploads/%s", file.Filename)
|
||||
if err := c.SaveFile(file, destination); err != nil {
|
||||
// Handle error
|
||||
return err
|
||||
}
|
||||
|
||||
xlog.Info("File uploaded to: " + destination)
|
||||
fmt.Printf("Payload: %+v\n", payload)
|
||||
|
||||
content, err := readPdf(destination) // Read local pdf file
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
xlog.Info("Content is", content)
|
||||
chunkSize := a.config.DefaultChunkSize
|
||||
if payload.ChunkSize > 0 {
|
||||
chunkSize = payload.ChunkSize
|
||||
}
|
||||
|
||||
go state.StringsToKB(db, chunkSize, content)
|
||||
|
||||
_, err = c.WriteString(chatDiv("File uploaded", "gray"))
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) KnowledgeBase(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
agent := pool.GetAgent(c.Params("name"))
|
||||
db := agent.Memory()
|
||||
|
||||
payload := struct {
|
||||
URL string `form:"url"`
|
||||
ChunkSize int `form:"chunk_size"`
|
||||
}{}
|
||||
|
||||
if err := c.BodyParser(&payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
website := payload.URL
|
||||
if website == "" {
|
||||
return fmt.Errorf("please enter a URL")
|
||||
}
|
||||
chunkSize := a.config.DefaultChunkSize
|
||||
if payload.ChunkSize > 0 {
|
||||
chunkSize = payload.ChunkSize
|
||||
}
|
||||
|
||||
go state.WebsiteToKB(website, chunkSize, db)
|
||||
|
||||
return c.Redirect("/knowledgebase/" + c.Params("name"))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) Notify(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||
return func(c *fiber.Ctx) error {
|
||||
payload := struct {
|
||||
|
||||
Reference in New Issue
Block a user