You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.8 KiB

11 months ago
  1. const { Workspace } = require("../../models/workspace");
  2. const { Document } = require("../../models/documents");
  3. const { DocumentVectors } = require("../../models/vectors");
  4. const { EventLogs } = require("../../models/eventLogs");
  5. const { purgeEntireVectorCache } = require("../files");
  6. const { getVectorDbClass } = require("../helpers");
  7. /**
  8. * Resets all vector database and associated content:
  9. * - Purges the entire vector-cache folder.
  10. * - Deletes all document vectors from the database.
  11. * - Deletes all documents from the database.
  12. * - Deletes all vector db namespaces for each workspace.
  13. * - Logs an event indicating the reset.
  14. * @param {string} vectorDbKey - The _previous_ vector database provider name that we will be resetting.
  15. * @returns {Promise<boolean>} - True if successful, false otherwise.
  16. */
  17. async function resetAllVectorStores({ vectorDbKey }) {
  18. try {
  19. const workspaces = await Workspace.where();
  20. purgeEntireVectorCache(); // Purges the entire vector-cache folder.
  21. await DocumentVectors.delete(); // Deletes all document vectors from the database.
  22. await Document.delete(); // Deletes all documents from the database.
  23. await EventLogs.logEvent("workspace_vectors_reset", {
  24. reason: "System vector configuration changed",
  25. });
  26. console.log(
  27. "Resetting anythingllm managed vector namespaces for",
  28. vectorDbKey
  29. );
  30. const VectorDb = getVectorDbClass(vectorDbKey);
  31. for (const workspace of workspaces) {
  32. try {
  33. await VectorDb["delete-namespace"]({ namespace: workspace.slug });
  34. } catch (e) {
  35. console.error(e.message);
  36. }
  37. }
  38. return true;
  39. } catch (error) {
  40. console.error("Failed to reset vector stores:", error);
  41. return false;
  42. }
  43. }
  44. module.exports = { resetAllVectorStores };