Source code for langchain.vectorstores.sklearn
""" Wrapper around scikit-learn NearestNeighbors implementation.
The vector store can be persisted in json, bson or parquet format.
"""
import json
import math
import os
from abc import ABC, abstractmethod
from typing import Any, Dict, Iterable, List, Literal, Optional, Tuple, Type
from uuid import uuid4
from langchain.docstore.document import Document
from langchain.schema.embeddings import Embeddings
from langchain.schema.vectorstore import VectorStore
from langchain.utils import guard_import
from langchain.vectorstores.utils import maximal_marginal_relevance
DEFAULT_K = 4  # Number of Documents to return.
DEFAULT_FETCH_K = 20  # Number of Documents to initially fetch during MMR search.
[docs]class BaseSerializer(ABC):
    """Base class for serializing data."""
[docs]    def __init__(self, persist_path: str) -> None:
        self.persist_path = persist_path
[docs]    @classmethod
    @abstractmethod
    def extension(cls) -> str:
        """The file extension suggested by this serializer (without dot)."""
[docs]    @abstractmethod
    def save(self, data: Any) -> None:
        """Saves the data to the persist_path"""
[docs]    @abstractmethod
    def load(self) -> Any:
        """Loads the data from the persist_path"""
[docs]class JsonSerializer(BaseSerializer):
    """Serializes data in json using the json package from python standard library."""
[docs]    @classmethod
    def extension(cls) -> str:
        return "json"
[docs]    def save(self, data: Any) -> None:
        with open(self.persist_path, "w") as fp:
            json.dump(data, fp)