Source code for smarter.apps.vectorstore.serializers

# pylint: disable=C0115
"""
Serializers of the vectorstore app.

Field names are camelCase in JSON. Related objects are given by name, and a Secret's value is
never serialized.
"""

from django.urls import reverse
from rest_framework import serializers

from smarter.apps.account.serializers import MetaDataWithOwnershipModelSerializer
from smarter.apps.connection.models import ApiConnection
from smarter.apps.provider.models import Provider
from smarter.apps.secret.models import Secret
from smarter.apps.vectorstore.manifest.models.vectorstore.const import (
    MANIFEST_KIND as VECTORSTORE_KIND,
)
from smarter.lib.drf.serializers import SmarterCamelCaseSerializer

from .models import VectorstoreDocument, VectorstoreMeta, VectorstoreSnapshot


[docs] class VectorstoreSerializer(MetaDataWithOwnershipModelSerializer): """A vectorstore, as the web console and the REST API show it. Read only. """ connection: "serializers.SlugRelatedField[ApiConnection]" = serializers.SlugRelatedField( slug_field="name", read_only=True ) embeddings_provider: "serializers.SlugRelatedField[Provider]" = serializers.SlugRelatedField( slug_field="name", read_only=True ) api_key_secret: "serializers.SlugRelatedField[Secret]" = serializers.SlugRelatedField( slug_field="name", read_only=True ) document_count = serializers.SerializerMethodField() snapshot_count = serializers.SerializerMethodField() manifest_url = serializers.SerializerMethodField() class Meta: model = VectorstoreMeta kind = VECTORSTORE_KIND # the deploy's progress is shown, modeled, by the REST api and the manifest's status. exclude = ["deployment"]
[docs] def get_fields(self): """Return the serializer's fields, all read-only.""" fields = super().get_fields() for field in fields.values(): field.read_only = True return fields
[docs] def get_document_count(self, obj: VectorstoreMeta) -> int: """ Return the number of the vectorstore's documents. :param obj: The vectorstore. """ return obj.documents.count() # type: ignore[attr-defined]
[docs] def get_snapshot_count(self, obj: VectorstoreMeta) -> int: """ Return the number of the vectorstore's snapshots. :param obj: The vectorstore. """ return obj.snapshots.count() # type: ignore[attr-defined]
[docs] def get_manifest_url(self, obj: VectorstoreMeta) -> str: """The URL of the vectorstore's detail view, which renders its manifest.""" # pylint: disable=C0415 from .urls import VectorstoreReverseNames return reverse( f"{VectorstoreReverseNames.namespace}:{VectorstoreReverseNames.detailview}", kwargs={"hashed_id": obj.hashed_id}, )
[docs] class VectorstoreDocumentSerializer(SmarterCamelCaseSerializer): """A document of a vectorstore, without its content.""" hashed_id = serializers.CharField(read_only=True) class Meta: model = VectorstoreDocument exclude = ["content", "vectorstore"] read_only_fields = [f.name for f in VectorstoreDocument._meta.fields] # pylint: disable=W0212
[docs] class VectorstoreSnapshotSerializer(SmarterCamelCaseSerializer): """A Qdrant snapshot, or Pinecone backup, of a vectorstore.""" hashed_id = serializers.CharField(read_only=True) class Meta: model = VectorstoreSnapshot exclude = ["vectorstore"] read_only_fields = [f.name for f in VectorstoreSnapshot._meta.fields] # pylint: disable=W0212
__all__ = ["VectorstoreDocumentSerializer", "VectorstoreSerializer", "VectorstoreSnapshotSerializer"]