package main import ( "encoding/json" "fmt" "strconv" "github.com/edgedb/edgedb-go" "github.com/gofiber/fiber/v2" ) // LLM stuff func deleteLLM(c *fiber.Ctx) error { var selectedLLMIds []string err := json.Unmarshal([]byte(c.FormValue("selectedLLMIds")), &selectedLLMIds) if err != nil { fmt.Print("Error unmarshalling selected LLM IDs") panic(err) } for _, id := range selectedLLMIds { idUUID, _ := edgedb.ParseUUID(id) err := edgeClient.Execute(edgeCtx, ` UPDATE LLM FILTER .id = $0 AND .user = global currentUser SET { to_delete := true }; `, idUUID) if err != nil { fmt.Print("Error deleting LLM") panic(err) } } deleteLLMtoDelete() return c.SendString(GenerateModelPopoverHTML(true)) } func deleteLLMtoDelete() { err := edgeClient.Execute(edgeCtx, ` delete LLM filter .to_delete = true and not exists( select Message filter .llm = LLM ); `) if err != nil { panic(err) } } func createLLM(c *fiber.Ctx) error { name := c.FormValue("model-name-input") modelID := c.FormValue("selectedLLMId") temperature := c.FormValue("temperature-slider") f, _ := strconv.ParseFloat(temperature, 32) temperatureFloat := float32(f) systemPrompt := c.FormValue("model-prompt-input") url := c.FormValue("model-url-input") token := c.FormValue("model-key-input") customID := c.FormValue("model-cid-input") if modelID == "custom" { err := edgeClient.Execute(edgeCtx, ` WITH countLLM := count((SELECT LLM FILTER .user = global currentUser)) INSERT LLM { name := $0, context := $1, temperature := $2, position := countLLM + 1, modelInfo := (INSERT ModelInfo { name := $0, modelID := $5, inputPrice := 0.0, outputPrice := 0.0, maxToken := 500, company := (SELECT Company FILTER .name = "huggingface" LIMIT 1), }), custom_endpoint := (INSERT CustomEndpoint { endpoint := $3, key := $4, }), user := global currentUser }; `, name, systemPrompt, temperatureFloat, url, token, customID) // TODO Add real max token if err != nil { fmt.Print("Error creating LLM") panic(err) } } else { err := edgeClient.Execute(edgeCtx, ` WITH countLLM := count((SELECT LLM FILTER .user = global currentUser)) INSERT LLM { name := $0, context := $1, temperature := $2, position := countLLM + 1, modelInfo := (SELECT ModelInfo FILTER .modelID = $3 LIMIT 1), user := global currentUser } `, name, systemPrompt, temperatureFloat, modelID) if err != nil { fmt.Print("Error creating LLM") panic(err) } } return c.SendString(GenerateModelPopoverHTML(true)) } type PositionUpdate struct { Position int `json:"position"` ID string `json:"id"` } func updateLLMPositionBatch(c *fiber.Ctx) error { var positionUpdates []PositionUpdate if err := c.BodyParser(&positionUpdates); err != nil { return err } for _, update := range positionUpdates { idUUID, err := edgedb.ParseUUID(update.ID) if err != nil { fmt.Print("Error parsing UUID") panic(err) } err = edgeClient.Execute(edgeCtx, ` UPDATE LLM FILTER .id = $0 AND .user = global currentUser SET { position := $1 }; `, idUUID, int32(update.Position)) if err != nil { fmt.Print("Error updating LLM position") panic(err) } } return nil } // When we reorder the LLM list func updateConversationPositionBatch(c *fiber.Ctx) error { var positionUpdates []PositionUpdate if err := c.BodyParser(&positionUpdates); err != nil { return err } for _, update := range positionUpdates { fmt.Println(update.ID) idUUID, err := edgedb.ParseUUID(update.ID) if err != nil { fmt.Print("Error parsing UUID") panic(err) } err = edgeClient.Execute(edgeCtx, ` UPDATE Conversation FILTER .id = $0 AND .user = global currentUser SET { position := $1 }; `, idUUID, int32(update.Position)) if err != nil { fmt.Print("Error updating conversation position") panic(err) } } return nil }