Skip to content

Commit

Permalink
fix/Prompt-Tool-delete-fix (#399)
Browse files Browse the repository at this point in the history
* fix/Prompt-Tool-delete-fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix/git review comments fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
athul-rs and pre-commit-ci[bot] authored Jun 11, 2024
1 parent eae2e6b commit 39e803a
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions backend/prompt_studio/prompt_studio_index_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

from account.models import User
from django.db import connection, models
from django.db.models.signals import post_delete
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from prompt_studio.prompt_profile_manager.models import ProfileManager
from prompt_studio.prompt_studio_core.prompt_ide_base_tool import PromptIdeBaseTool
from prompt_studio.prompt_studio_document_manager.models import DocumentManager
from unstract.sdk.constants import LogLevel
from unstract.sdk.exceptions import SdkError
from unstract.sdk.vector_db import VectorDB
from utils.models.base_model import BaseModel

Expand Down Expand Up @@ -90,31 +89,38 @@ class Meta:
]


def delete_from_vector_db(index_ids_history, vector_db_instance_id):
org_schema = connection.tenant.schema_name
util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_schema)
vector_db = VectorDB(
tool=util,
adapter_instance_id=vector_db_instance_id,
)
for index_id in index_ids_history:
logger.debug(f"Deleting from VectorDB - index id: {index_id}")
try:
vector_db.delete(ref_doc_id=index_id)
except Exception as e:
# Log error and continue with the next index id
logger.error(f"Error deleting index: {index_id} - {e}")


# Function will be executed every time an instance of IndexManager is deleted.
@receiver(post_delete, sender=IndexManager)
@receiver(pre_delete, sender=IndexManager)
def perform_vector_db_cleanup(sender, instance, **kwargs):
"""Signal to perform vector db cleanup."""
logger.info("Performing vector db cleanup")
logger.debug("Document tool id: %s", instance.document_manager.tool_id)
logger.debug(f"Document tool id: {instance.document_manager.tool_id}")
try:
# Get the index_ids_history to clean up from the vector db
index_ids_history = json.loads(instance.index_ids_history)
vector_db_instance_id = str(instance.profile_manager.vector_store.id)
# Generate a run_id
org_schema = connection.tenant.schema_name
util = PromptIdeBaseTool(log_level=LogLevel.INFO, org_id=org_schema)
vector_db = VectorDB(
tool=util,
adapter_instance_id=vector_db_instance_id,
delete_from_vector_db(index_ids_history, vector_db_instance_id)
except Exception as e:
logger.warning(
"Error during vector DB cleanup for deleted document "
"in prompt studio tool %s: %s",
instance.document_manager.tool_id,
e,
exc_info=True, # For additional stack trace
)
for index_id in index_ids_history:
logger.debug(f"Deleting from VectorDB - index id: {index_id}")
try:
vector_db.delete(ref_doc_id=index_id)
except Exception as e:
# Log error and continue with the next index id
logger.error(f"Error deleting index: {index_id} - {e}")
# Not raising any exception.
# Cleanup should not fail the deletion of the index manager.
except SdkError as e:
logger.error(f"Error while performing vector db cleanup: {e}")

0 comments on commit 39e803a

Please sign in to comment.