# pylint: disable=W0613
"""The JSON API of the React Vectorstore list in the web console: list, clone, rename and delete."""
from http import HTTPStatus
from django.db import transaction
from django.http import HttpRequest, JsonResponse
from smarter.apps.vectorstore.caching import (
invalidate_all_cached_vectorstores_for_user_profile,
)
from smarter.apps.vectorstore.models import VectorstoreMeta, VectorstoreStatus
from smarter.apps.vectorstore.serializers import VectorstoreSerializer
from smarter.apps.vectorstore.service import (
VectorstoreService,
VectorstoreServiceError,
)
from smarter.lib import logging
from smarter.lib.django.http.client_errors import (
CLIENT_ERROR_TYPES,
client_error_message,
)
from smarter.lib.django.listview import SmarterListApiView
from smarter.lib.django.pagination import DEFAULT_SORT_FIELDS
from smarter.lib.django.views import SmarterAuthenticatedNeverCachedWebView
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.typing import SmarterDictType
STATE_FIELDS: SmarterDictType = {
# what a clone does not copy: it is a new, undeployed vectorstore.
"status": VectorstoreStatus.PENDING,
"status_message": "",
"index_name": "",
"endpoint_url": "",
"api_key_secret": None,
"vector_count": 0,
"stats": {},
"deployed_at": None,
"last_checked_at": None,
"last_snapshot_at": None,
"last_maintenance_at": None,
}
logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.VECTORSTORE_LOGGING])
SORT_FIELDS = {
**DEFAULT_SORT_FIELDS,
"database": "backend",
"vectorCount": "vector_count",
}
"""The columns of the Vectorstore list that it may be sorted by, and the fields that sort them."""
[docs]
class VectorstoreListApiView(SmarterListApiView):
"""The vectorstores that the user owns, those shared with them, or both."""
model = VectorstoreMeta
serializer_class = VectorstoreSerializer
listview_cache_kind = "Vectorstore"
sort_fields = SORT_FIELDS
[docs]
class VectorstoreListApiCloneView(SmarterAuthenticatedNeverCachedWebView):
"""
Clone a vectorstore: a new, undeployed vectorstore with the same spec, owned by the user.
Its documents and database are not copied. Deploy it, and add documents, to use it.
"""
[docs]
def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse:
"""
Clone the vectorstore that the user may read, as ``new_name``, and invalidate the user's cached vectorstores.
:param request: The request.
:param kwargs: The url's ``vectorstore_id`` and ``new_name``.
:returns: The clone, 400 if ``new_name`` is empty, 404 if the vectorstore is not found, or 409 if
the user has a vectorstore named ``new_name``.
"""
user_profile = self.user_profile
if user_profile is None:
return JsonResponse({"error": "A user profile is required."}, status=HTTPStatus.FORBIDDEN)
vectorstore_id = kwargs.get("vectorstore_id")
new_name = self.to_snake_case(str(kwargs.get("new_name") or "").strip())
if not new_name:
return JsonResponse({"error": "new_name is required."}, status=HTTPStatus.BAD_REQUEST)
source = VectorstoreMeta.objects.with_read_permission_for(user_profile.user).filter(id=vectorstore_id).first()
if source is None:
return JsonResponse({"error": f"Vectorstore {vectorstore_id} not found."}, status=HTTPStatus.NOT_FOUND)
if VectorstoreMeta.objects.filter(user_profile=user_profile, name=new_name).exists():
return JsonResponse(
{"error": f"You already have a vectorstore named {new_name}."}, status=HTTPStatus.CONFLICT
)
with transaction.atomic():
clone = VectorstoreMeta.objects.get(pk=source.pk)
clone.pk = None
clone.name = new_name
clone.user_profile = user_profile
for field, value in STATE_FIELDS.items():
setattr(clone, field, value)
spec = dict(clone.spec or {})
spec.setdefault("index", {}).pop("name", None)
clone.spec = spec
clone.save()
clone.tags.set(source.tags_list)
invalidate_all_cached_vectorstores_for_user_profile(user_profile=user_profile)
return JsonResponse(VectorstoreSerializer(clone, context={"request": request}).data, status=HTTPStatus.OK)
[docs]
class VectorstoreListApiDeleteView(SmarterAuthenticatedNeverCachedWebView):
"""Destroy a vectorstore's database and data, unless deletionProtection is enabled, then delete it."""
[docs]
def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse:
"""
Destroy the database of the vectorstore that the user owns, if it was deployed, then delete it and its API key Secret.
:param request: The request.
:param kwargs: The url's ``vectorstore_id``.
:returns: A message, 404 if the vectorstore is not found, or 400 if it could not be deleted, e.g.
because of deletionProtection.
"""
vectorstore_id = kwargs.get("vectorstore_id")
vectorstore = (
VectorstoreMeta.objects.with_ownership_permission_for(self.user_profile.user) # type: ignore
.filter(id=vectorstore_id)
.first()
)
if vectorstore is None:
return JsonResponse({"error": f"Vectorstore {vectorstore_id} not found."}, status=HTTPStatus.NOT_FOUND)
try:
service = VectorstoreService(vectorstore)
if vectorstore.deployed_at or vectorstore.deletion_protection:
service.destroy()
secret = vectorstore.api_key_secret
vectorstore.delete()
if secret is not None:
secret.delete()
except VectorstoreServiceError as e:
# a refusal written for the user, e.g. deletionProtection, not a failure.
logger.warning("%s.post() refused to delete %s: %s", self.formatted_class_name, vectorstore, e.message)
return JsonResponse({"error": e.message}, status=HTTPStatus.BAD_REQUEST)
except CLIENT_ERROR_TYPES as e:
logger.warning("%s.post() could not delete %s: %s", self.formatted_class_name, vectorstore, e)
message = client_error_message(e, "An error occurred while deleting the Vectorstore.")
return JsonResponse({"error": message}, status=HTTPStatus.BAD_REQUEST)
except Exception: # pylint: disable=broad-exception-caught
logger.error("%s.post() failed to delete %s", self.formatted_class_name, vectorstore, exc_info=True)
return JsonResponse(
{"error": "An unknown error occurred while deleting the Vectorstore."},
status=HTTPStatus.INTERNAL_SERVER_ERROR,
)
invalidate_all_cached_vectorstores_for_user_profile(user_profile=self.user_profile) # type: ignore
return JsonResponse({"message": f"Vectorstore {vectorstore_id} deleted."}, status=HTTPStatus.OK)
[docs]
class VectorstoreListApiRenameView(SmarterAuthenticatedNeverCachedWebView):
"""Rename a vectorstore.
Its database keeps its index name, and its server its Kubernetes name.
"""
[docs]
def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse:
"""
Rename the vectorstore that the user owns to ``new_name``, and invalidate the user's cached vectorstores.
:param request: The request.
:param kwargs: The url's ``vectorstore_id`` and ``new_name``.
:returns: The vectorstore, 400 if ``new_name`` is empty, 404 if the vectorstore is not found, or
409 if a vectorstore named ``new_name`` exists.
"""
vectorstore_id = kwargs.get("vectorstore_id")
new_name = self.to_snake_case(str(kwargs.get("new_name") or "").strip())
if not new_name:
return JsonResponse({"error": "new_name is required."}, status=HTTPStatus.BAD_REQUEST)
vectorstore = (
VectorstoreMeta.objects.with_ownership_permission_for(self.user_profile.user) # type: ignore
.filter(id=vectorstore_id)
.first()
)
if vectorstore is None:
return JsonResponse({"error": f"Vectorstore {vectorstore_id} not found."}, status=HTTPStatus.NOT_FOUND)
if VectorstoreMeta.objects.filter(user_profile=vectorstore.user_profile, name=new_name).exists():
return JsonResponse({"error": f"A vectorstore named {new_name} exists."}, status=HTTPStatus.CONFLICT)
vectorstore.rename(new_name=new_name)
invalidate_all_cached_vectorstores_for_user_profile(user_profile=self.user_profile) # type: ignore
return JsonResponse(VectorstoreSerializer(vectorstore, context={"request": request}).data, status=HTTPStatus.OK)