"""Utility functions for working with vectors and vectorstores."""fromenumimportEnumfromtypingimportList,Tuple,Typeimportnumpyasnpfromlangchain_core.documentsimportDocumentfromlangchain_community.utils.mathimportcosine_similarity
[docs]classDistanceStrategy(str,Enum):"""Enumerator of the Distance strategies for calculating distances between vectors."""EUCLIDEAN_DISTANCE="EUCLIDEAN_DISTANCE"MAX_INNER_PRODUCT="MAX_INNER_PRODUCT"DOT_PRODUCT="DOT_PRODUCT"JACCARD="JACCARD"COSINE="COSINE"
[docs]deffilter_complex_metadata(documents:List[Document],*,allowed_types:Tuple[Type,...]=(str,bool,int,float),)->List[Document]:"""Filter out metadata types that are not supported for a vector store."""updated_documents=[]fordocumentindocuments:filtered_metadata={}forkey,valueindocument.metadata.items():ifnotisinstance(value,allowed_types):continuefiltered_metadata[key]=valuedocument.metadata=filtered_metadataupdated_documents.append(document)returnupdated_documents