
    dh                    2   S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	  S SK
JrJrJrJrJrJrJrJrJrJr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S S	KJ r J!r!  S S
K"J#r#  S SK$J%r%J&r&  \RN                  " \(5      r)SSS jjr*SS jr+ " S S\5      r,g)    )annotationsN)Path)
AnyCallableDictIterableListOptionalSequenceSizedTupleUnion)Document)
Embeddings)run_in_executor)VectorStore)AddableMixinDocstore)InMemoryDocstore)DistanceStrategymaximal_marginal_relevancec                    U c3  S[         R                  ;   a  [        [         R                  " S5      5      n  U (       a  SSKJn  U$ SSKn U$ ! [         a    [        S5      ef = f)a1  
Import faiss if available, otherwise raise error.
If FAISS_NO_AVX2 environment variable is set, it will be considered
to load FAISS with no AVX2 optimization.

Args:
    no_avx2: Load FAISS strictly with no AVX2 optimization
        so that the vectorstore is portable and compatible with other devices.
NFAISS_NO_AVX2r   )	swigfaisszCould not import faiss python package. Please install it with `pip install faiss-gpu` (for CUDA supported GPU) or `pip install faiss-cpu` (depending on Python version).)osenvironboolgetenvfaissr   ImportError)no_avx2r   s     ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/faiss.pydependable_faiss_importr#   '   si     ?bjj8ryy12

0 L  L  
H
 	

s   A A A$c                    [        U [        5      (       a[  [        U[        5      (       aF  [        U 5      [        U5      :w  a.  [        U SU SU S[        U 5       SU S[        U5       35      eg )Nz and z% expected to be equal length but len(z)=z	 and len()
isinstancer   len
ValueError)xyx_namey_names       r"   _len_check_if_sizedr,   B   sq    !U
1e 4 4Q3q69IheF8 $("SVHIfXRAxA
 	
     c                     \ rS rSrSrSS\R                  4             S3S jjr\S4S j5       r	S5S jr
S5S jrS6S	 jrS6S
 jr  S7         S8S jjr  S7         S9S jjr  S7         S9S jjr  S7         S:S jjr   S;           S<S jjr   S;           S<S jjr   S;           S=S jjr   S;           S=S jjr   S;           S>S jjr   S;           S?S jjr   S;           S@S jjr   S;           S@S jjrSSSSS.           SAS jjrSSSSS.           SAS jjr    SB             SCS jjr    SB             SCS jjr    SB             SDS jjr    SB             SDS  jjrSESFS! jjr SGS" jr!\"SSS\R                  4                 SHS# jj5       r#\"  S7           SIS$ jj5       r$\"  S7           SJS% jj5       r%\"  S7           SKS& jj5       r&\"  S7           SKS' jj5       r'SLSMS( jjr(\" SLSS).           SNS* jjj5       r)SOS+ jr*\"SS).         SPS, jj5       r+SQS- jr,   S;           S=S. jjr-   S;           S=S/ jjr.\/    SRS0 j5       r0SSS1 jr1S2r2g)TFAISSK   us  FAISS vector store integration.

See [The FAISS Library](https://arxiv.org/pdf/2401.08281) paper.

Setup:
    Install ``langchain_community`` and ``faiss-cpu`` python packages.

    .. code-block:: bash

        pip install -qU langchain_community faiss-cpu

Key init args — indexing params:
    embedding_function: Embeddings
        Embedding function to use.

Key init args — client params:
    index: Any
        FAISS index to use.
    docstore: Docstore
        Docstore to use.
    index_to_docstore_id: Dict[int, str]
        Mapping of index to docstore id.

Instantiate:
    .. code-block:: python

        import faiss
        from langchain_community.vectorstores import FAISS
        from langchain_community.docstore.in_memory import InMemoryDocstore
        from langchain_openai import OpenAIEmbeddings

        index = faiss.IndexFlatL2(len(OpenAIEmbeddings().embed_query("hello world")))

        vector_store = FAISS(
            embedding_function=OpenAIEmbeddings(),
            index=index,
            docstore= InMemoryDocstore(),
            index_to_docstore_id={}
        )

Add Documents:
    .. code-block:: python

        from langchain_core.documents import Document

        document_1 = Document(page_content="foo", metadata={"baz": "bar"})
        document_2 = Document(page_content="thud", metadata={"bar": "baz"})
        document_3 = Document(page_content="i will be deleted :(")

        documents = [document_1, document_2, document_3]
        ids = ["1", "2", "3"]
        vector_store.add_documents(documents=documents, ids=ids)

Delete Documents:
    .. code-block:: python

        vector_store.delete(ids=["3"])

Search:
    .. code-block:: python

        results = vector_store.similarity_search(query="thud",k=1)
        for doc in results:
            print(f"* {doc.page_content} [{doc.metadata}]")

    .. code-block:: python

        * thud [{'bar': 'baz'}]

Search with filter:
    .. code-block:: python

        results = vector_store.similarity_search(query="thud",k=1,filter={"bar": "baz"})
        for doc in results:
            print(f"* {doc.page_content} [{doc.metadata}]")

    .. code-block:: python

        * thud [{'bar': 'baz'}]

Search with score:
    .. code-block:: python

        results = vector_store.similarity_search_with_score(query="qux",k=1)
        for doc, score in results:
            print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")

    .. code-block:: python

        * [SIM=0.335304] foo [{'baz': 'bar'}]

Async:
    .. code-block:: python

        # add documents
        # await vector_store.aadd_documents(documents=documents, ids=ids)

        # delete documents
        # await vector_store.adelete(ids=["3"])

        # search
        # results = vector_store.asimilarity_search(query="thud",k=1)

        # search with score
        results = await vector_store.asimilarity_search_with_score(query="qux",k=1)
        for doc,score in results:
            print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")

    .. code-block:: python

        * [SIM=0.335304] foo [{'baz': 'bar'}]

Use as Retriever:
    .. code-block:: python

        retriever = vector_store.as_retriever(
            search_type="mmr",
            search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
        )
        retriever.invoke("thud")

    .. code-block:: python

        [Document(metadata={'bar': 'baz'}, page_content='thud')]

NFc                T   [        U[        5      (       d  [        R                  S5        Xl        X l        X0l        X@l        Xpl        XPl	        X`l
        U R                  [        R                  :w  a6  U R                  (       a$  [        R                  " SU R                   35        ggg)z%Initialize with necessary components.t`embedding_function` is expected to be an Embeddings object, support for passing in a function will soon be removed.z2Normalizing L2 is not applicable for metric type: N)r%   r   loggerwarningembedding_functionindexdocstoreindex_to_docstore_iddistance_strategyoverride_relevance_score_fn_normalize_L2r   EUCLIDEAN_DISTANCEwarningswarn)selfr5   r6   r7   r8   relevance_score_fnnormalize_L2r9   s           r"   __init__FAISS.__init__   s     ,j99NNB #5
 $8!!2+=()""&6&I&II""MM  $ 6 679 # Jr-   c                \    [        U R                  [        5      (       a  U R                  $ S $ N)r%   r5   r   r?   s    r"   
embeddingsFAISS.embeddings   s1     $11:>> ##	
 	
r-   c                    [        U R                  [        5      (       a  U R                  R                  U5      $ U Vs/ sH  o R                  U5      PM     sn$ s  snf rE   )r%   r5   r   embed_documents)r?   textstexts      r"   _embed_documentsFAISS._embed_documents   sM    d--z::**::5AA>CDed++D1eDDDs   Ac                   #    [        U R                  [        5      (       a#  U R                  R                  U5      I S h  vN $ [	        S5      e N7fNr2   )r%   r5   r   aembed_documents	Exception)r?   rK   s     r"   _aembed_documentsFAISS._aembed_documents   sM     d--z::00AA%HHH
 B  I   >A AAc                    [        U R                  [        5      (       a  U R                  R                  U5      $ U R                  U5      $ rE   )r%   r5   r   embed_queryr?   rL   s     r"   _embed_queryFAISS._embed_query  s=    d--z::**66t<<**400r-   c                   #    [        U R                  [        5      (       a#  U R                  R                  U5      I S h  vN $ [	        S5      e N7frP   )r%   r5   r   aembed_queryrR   rX   s     r"   _aembed_queryFAISS._aembed_query  sM     d--z::00==dCCC B  DrU   c                &   [        5       n[        U R                  [        5      (       d  [	        SU R                   S35      e[        XSS5        U=(       d.    U Vs/ sH!  n[        [        R                  " 5       5      PM#     snn[        XSS5        U=(       d	    S U 5       n[        XAU5       VV	V
s/ sH  u  pn
[        XU
S9PM     nn	nn
[        XSS	5        U(       a,  [        U5      [        [        U5      5      :w  a  [	        S
5      e[        R                  " U[        R                  S9nU R                   (       a  UR#                  U5        U R$                  R'                  U5        U R                  R'                  [        XK5       VVs0 sH  u  pX_M	     snn5        [        U R(                  5      n[+        U5       VVs0 sH
  u  pX-   U_M     nnnU R(                  R-                  U5        U$ s  snf s  sn
n	nf s  snnf s  snnf )NzSIf trying to add texts, the underlying docstore should support adding items, which z	 does notrK   	metadatasidsc              3  $   #    U H  n0 v   M	     g 7frE    ).0_s     r"   	<genexpr>FAISS.__add.<locals>.<genexpr>+  s     "5u!2us   )idpage_contentmetadata	documentsrG   z$Duplicate ids found in the ids list.dtype)r#   r%   r7   r   r'   r,   struuiduuid4zipr   r&   setnparrayfloat32r;   rA   r6   addr8   	enumerateupdate)r?   rK   rG   r`   ra   r   re   
_metadatasid_tmrk   vectordocstarting_lenjindex_to_ids                    r"   __addFAISS.__add  s    ()$--66''+}}oY@ 
 	Eg{C77Ac$**,'7E75"5u"5
 !Z8
8	 a88 	 

 	I;M3s8s3s8},CDD*BJJ7v&

v 	C4GH4G384GHI4445;DS>J>|',>J!!((5
1 8
  IJs   'G;6H H
Hc                Z    [        U5      nU R                  U5      nU R                  XX#S9$ )a4  Run more texts through the embeddings and add to the vectorstore.

Args:
    texts: Iterable of strings to add to the vectorstore.
    metadatas: Optional list of metadatas associated with the texts.
    ids: Optional list of unique IDs.

Returns:
    List of ids from adding the texts into the vectorstore.
r`   ra   )listrM   _FAISS__addr?   rK   r`   ra   kwargsrG   s         r"   	add_textsFAISS.add_textsB  s0    " U**51
zz%yzJJr-   c                v   #    [        U5      nU R                  U5      I Sh  vN nU R                  XX#S9$  N7f)aG  Run more texts through the embeddings and add to the vectorstore
    asynchronously.

Args:
    texts: Iterable of strings to add to the vectorstore.
    metadatas: Optional list of metadatas associated with the texts.
    ids: Optional list of unique IDs.

Returns:
    List of ids from adding the texts into the vectorstore.
Nr   )r   rS   r   r   s         r"   
aadd_textsFAISS.aadd_textsW  s;     $ U11%88
zz%yzJJ 9s    979c                6    [        U6 u  pVU R                  XVX#S9$ )aN  Add the given texts and embeddings to the vectorstore.

Args:
    text_embeddings: Iterable pairs of string and embedding to
        add to the vectorstore.
    metadatas: Optional list of metadatas associated with the texts.
    ids: Optional list of unique IDs.

Returns:
    List of ids from adding the texts into the vectorstore.
r   )rq   r   )r?   text_embeddingsr`   ra   r   rK   rG   s          r"   add_embeddingsFAISS.add_embeddingsm  s$    &  1zz%yzJJr-         c                   [        5       n[        R                  " U/[        R                  S9nU R                  (       a  UR                  U5        U R                  R                  Xsc  UOU5      u  p/ n
Ub  U R                  U5      n[        U	S   5       H  u  pUS:X  a  M  U R                  U   nU R                  R                  U5      n[        U[        5      (       d  [        SU SU 35      eUb3  W" UR                  5      (       a  U
R!                  XS   U   45        M  M  U
R!                  XS   U   45        M     UR#                  S5      nUbv  U R$                  [&        R(                  [&        R*                  4;   a  [,        R.                  O[,        R0                  nU
 VVs/ sH  u  nnU" UU5      (       d  M  UU4PM     n
nnU
SU $ s  snnf )a$  Return docs most similar to query.

Args:
    embedding: Embedding vector to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Union[Callable, Dict[str, Any]]]): Filter by metadata.
        Defaults to None. If a callable, it must take as input the
        metadata dict of Document and return a bool.
    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.
    **kwargs: kwargs to be passed to similarity search. Can include:
        score_threshold: Optional, a floating point value between 0 to 1 to
            filter the resulting set of retrieved docs

Returns:
    List of documents most similar to the query text and L2 distance
    in float for each. Lower score represents more similarity.
rl   Nr   Could not find document for id , got score_threshold)r#   rs   rt   ru   r;   rA   r6   search_create_filter_funcrw   r8   r7   r%   r   r'   rj   appendgetr9   r   MAX_INNER_PRODUCTJACCARDoperatorgele)r?   	embeddingkfilterfetch_kr   r   r}   scoresindicesdocsfilter_funcr   i_idr~   r   cmp
similaritys                      r"   &similarity_search_with_score_by_vector,FAISS.similarity_search_with_score_by_vector  s   4 ()9+RZZ8v&**++FAWU226:Kgaj)DABw++A.C--&&s+Cc8,, #B3%vcU!STT!s||,,KKQil 34 - S)A,/0 * !**%67& ))$668H8P8PQR  [[	  (,'+OCz?3 "j!'+  
 BQxs   -GGc                V   #    [        SU R                  U4UUUS.UD6I Sh  vN $  N7f)a#  Return docs most similar to query asynchronously.

Args:
    embedding: Embedding vector to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, Any]]): Filter by metadata.
        Defaults to None. If a callable, it must take as input the
        metadata dict of Document and return a bool.

    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.
    **kwargs: kwargs to be passed to similarity search. Can include:
        score_threshold: Optional, a floating point value between 0 to 1 to
            filter the resulting set of retrieved docs

Returns:
    List of documents most similar to the query text and L2 distance
    in float for each. Lower score represents more similarity.
Nr   r   r   )r   r   )r?   r   r   r   r   r   s         r"   'asimilarity_search_with_score_by_vector-FAISS.asimilarity_search_with_score_by_vector  sG     : %77
 
 
 
 	
 
s    )')c                V    U R                  U5      nU R                  " UU4UUS.UD6nU$ )a4  Return docs most similar to query.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata.
        Defaults to None. If a callable, it must take as input the
        metadata dict of Document and return a bool.

    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.

Returns:
    List of documents most similar to the query text with
    L2 distance in float. Lower score represents more similarity.
r   r   )rY   r   r?   queryr   r   r   r   r   r   s           r"   similarity_search_with_score"FAISS.similarity_search_with_score  sH    0 %%e,	::
 	

 
 r-   c                   #    U R                  U5      I Sh  vN nU R                  " UU4UUS.UD6I Sh  vN nU$  N% N7f)aC  Return docs most similar to query asynchronously.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata.
        Defaults to None. If a callable, it must take as input the
        metadata dict of Document and return a bool.

    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.

Returns:
    List of documents most similar to the query text with
    L2 distance in float. Lower score represents more similarity.
Nr   )r]   r   r   s           r"   asimilarity_search_with_score#FAISS.asimilarity_search_with_score  s]     0 ,,U33	AA
 	

 
 
  4
s   A=A?AAc                h    U R                   " UU4UUS.UD6nU VVs/ sH  u  pxUPM	     snn$ s  snnf )a  Return docs most similar to embedding vector.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata.
        Defaults to None. If a callable, it must take as input the
        metadata dict of Document and return a bool.

    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.

Returns:
    List of Documents most similar to the embedding.
r   )r   	r?   r   r   r   r   r   docs_and_scoresr~   re   s	            r"   similarity_search_by_vector!FAISS.similarity_search_by_vector/  sM    . EE
 	

 
 #22//222s   .c                   #    U R                   " UU4UUS.UD6I Sh  vN nU VVs/ sH  u  pxUPM	     snn$  Ns  snnf 7f)a  Return docs most similar to embedding vector asynchronously.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter (Optional[Dict[str, str]]): Filter by metadata.
        Defaults to None. If a callable, it must take as input the
        metadata dict of Document and return a bool.

    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.

Returns:
    List of Documents most similar to the embedding.
r   N)r   r   s	            r"   asimilarity_search_by_vector"FAISS.asimilarity_search_by_vectorO  s\     . !% L L!
 	!

 !
 
 #22//22
 3s   A 8	A :A A c                d    U R                   " X4X4S.UD6nU VVs/ sH  u  pxUPM	     snn$ s  snnf )a  Return docs most similar to query.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter: (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.
    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.

Returns:
    List of Documents most similar to the query.
r   )r   	r?   r   r   r   r   r   r   r~   re   s	            r"   similarity_searchFAISS.similarity_searcho  sC    ( ;;
#
8>
 #22//222s   ,c                   #    U R                   " X4X4S.UD6I Sh  vN nU VVs/ sH  u  pxUPM	     snn$  Ns  snnf 7f)a  Return docs most similar to query asynchronously.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    filter: (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.
    fetch_k: (Optional[int]) Number of Documents to fetch before filtering.
              Defaults to 20.

Returns:
    List of Documents most similar to the query.
r   N)r   r   s	            r"   asimilarity_searchFAISS.asimilarity_search  sR     ( !% B B!
#!
8>!
 
 #22//22
 3s   >6	>8>>      ?r   r   lambda_multr   c                  U R                   R                  [        R                  " U/[        R                  S9Uc  UOUS-  5      u  pgUb  U R                  U5      n/ n	US    H  n
U
S:X  a  M  U R                  U
   nU R                  R                  U5      n[        U[        5      (       d  [        SU SU 35      eU" UR                  5      (       d  Mt  U	R                  U
5        M     [        R                  " U	/5      nUS    V
s/ sH.  oS:w  d  M
  U R                   R                  [        U
5      5      PM0     nn
[        [        R                  " U/[        R                  S9UUUS9n/ nU H  n
US   U
   S:X  a  M  U R                  US   U
      nU R                  R                  U5      n[        U[        5      (       d  [        SU SU 35      eUR                  XS   U
   45        M     U$ s  sn
f )a  Return docs and their similarity scores selected using the maximal marginal
    relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    fetch_k: Number of Documents to fetch before filtering to
             pass to MMR algorithm.
    lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
Returns:
    List of Documents and similarity scores selected by maximal marginal
        relevance and score for each.
rl      r   r   r   r   )r   r   )r6   r   rs   rt   ru   r   r8   r7   r%   r   r'   rj   r   reconstructintr   )r?   r   r   r   r   r   r   r   r   filtered_indicesr   r   r~   rG   mmr_selectedr   s                   r"   2max_marginal_relevance_search_with_score_by_vector8FAISS.max_marginal_relevance_search_with_score_by_vector  s   8 **++HHi[

3~G7Q;
 226:K!QZ7//2mm**3/!#x00$'Fse6RUQV%WXXs||,,$++A.   hh 012G>EajTjQSG4djj,,SV4j
T1HHi[

3#	
 Aqz!}"++GAJqM:C--&&s+Cc8,, #B3%vcU!STT""C1#67  ' Us   G8(G8c          
     P   #    [        SU R                  UUUUUS9I Sh  vN $  N7f)a	  Return docs and their similarity scores selected using the maximal marginal
    relevance asynchronously.

Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    fetch_k: Number of Documents to fetch before filtering to
             pass to MMR algorithm.
    lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
Returns:
    List of Documents and similarity scores selected by maximal marginal
        relevance and score for each.
Nr   )r   r   )r?   r   r   r   r   r   s         r"   3amax_marginal_relevance_search_with_score_by_vector9FAISS.amax_marginal_relevance_search_with_score_by_vector  s9     : %CC#
 
 	
 
s   &$&c                \    U R                  XX4US9nU VV	s/ sH  u  pUPM	     sn	n$ s  sn	nf )a  Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    fetch_k: Number of Documents to fetch before filtering to
             pass to MMR algorithm.
    lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
Returns:
    List of Documents selected by maximal marginal relevance.
r   )r   
r?   r   r   r   r   r   r   r   r~   re   s
             r"   'max_marginal_relevance_search_by_vector-FAISS.max_marginal_relevance_search_by_vector  s=    4 QQGV R 
 #22//222s   (c                x   #    U R                  XX4US9I Sh  vN nU VV	s/ sH  u  pUPM	     sn	n$  Ns  sn	nf 7f)a  Return docs selected using the maximal marginal relevance asynchronously.

Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    fetch_k: Number of Documents to fetch before filtering to
             pass to MMR algorithm.
    lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
Returns:
    List of Documents selected by maximal marginal relevance.
r   N)r   r   s
             r"   (amax_marginal_relevance_search_by_vector.FAISS.amax_marginal_relevance_search_by_vector+  sP     6 JJQW K   	
 #22//22	 3s   :2	:4::c                X    U R                  U5      nU R                  " U4UUUUS.UD6nU$ )a  Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    fetch_k: Number of Documents to fetch before filtering (if needed) to
             pass to MMR algorithm.
    lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
Returns:
    List of Documents selected by maximal marginal relevance.
r   )rY   r   	r?   r   r   r   r   r   r   r   r   s	            r"   max_marginal_relevance_search#FAISS.max_marginal_relevance_searchL  sI    4 %%e,	;;
#
 
 r-   c                   #    U R                  U5      I Sh  vN nU R                  " U4UUUUS.UD6I Sh  vN nU$  N& N7f)a  Return docs selected using the maximal marginal relevance asynchronously.

Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    fetch_k: Number of Documents to fetch before filtering (if needed) to
             pass to MMR algorithm.
    lambda_mult: Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
Returns:
    List of Documents selected by maximal marginal relevance.
Nr   )r]   r   r   s	            r"   amax_marginal_relevance_search$FAISS.amax_marginal_relevance_searchq  s^     4 ,,U33	BB
#
 
 
  4
s   A>AA A Ac                   Uc  [        S5      e[        U5      R                  U R                  R	                  5       5      nU(       a  [        SU 35      eU R                  R                  5        VVs0 sH  u  pEXT_M	     nnnU Vs1 sH  oVU   iM	     nnU R                  R                  [        R                  " U[        R                  S95        U R                  R                  U5        [        U R                  R                  5       5       VVs/ sH  u  pX;  d  M  UPM     n	nn[        U	5       VVs0 sH  u  pX_M	     snnU l        gs  snnf s  snf s  snnf s  snnf )zDelete by ID. These are the IDs in the vectorstore.

Args:
    ids: List of ids to delete.

Returns:
    Optional[bool]: True if deletion is successful,
    False otherwise, None if not implemented.
zNo ids provided to delete.zESome specified ids do not exist in the current store. Ids not found: rl   T)r'   rr   
differencer8   valuesitemsr6   
remove_idsrs   fromiterint64r7   deletesortedrw   )
r?   ra   r   missing_idsidxrz   reversed_indexindex_to_deleter   remaining_idss
             r"   r   FAISS.delete  sD    ;9::#h))$*C*C*J*J*LMW-! 
 483L3L3R3R3TU3Txs#(3TU:=>#3#.#>

bkk/JKS! !!:!:!@!@!BC
C' C 	 

 ;DM:R$S:RQV:R$S! V>


 %Ts   3EE
E!E8E c           
        [        U R                  [        5      (       d  [        S5      e[	        U R
                  5      nU R                  R                  UR                  5        / nUR
                  R                  5        HU  u  pEUR                  R                  U5      n[        U[        5      (       d  [        S5      eUR                  X$-   XV45        MW     U R                  R                  U VVVs0 sH  u  pxohU_M
     snnn5        U V	VVs0 sH  u  poyU_M
     n
nn	nU R
                  R                  U
5        gs  snnnf s  snnn	f )zMerge another FAISS object with the current one.

Add the target FAISS to the current one.

Args:
    target: FAISS object you wish to merge into the current one

Returns:
    None.
z'Cannot merge with this type of docstorezDocument should be returnedN)r%   r7   r   r'   r&   r8   r6   
merge_fromr   r   r   r   rv   rx   )r?   targetr   	full_infor   	target_idr~   re   r   r6   r   s              r"   r   FAISS.merge_from  s    $--66FGG4445 	

fll+ 	"77==?LA//((3Cc8,, !>??l.	?@	 @ 	yAy8yAB7@Ayme!czyA!!((5 BAs   5E Ec                N   [        5       n	U[        R                  :X  a  U	R                  [	        US   5      5      n
OU	R                  [	        US   5      5      n
UR                  S[        5       5      nUR                  S0 5      nU " UU
UU4UUS.UD6nUR                  XXES9  U$ )Nr   r7   r8   )rA   r9   r   )	r#   r   r   IndexFlatIPr&   IndexFlatL2popr   r   )clsrK   rG   r   r`   ra   rA   r9   r   r   r6   r7   r8   vecstores                 r"   __fromFAISS.__from  s     () 0 B BB%%c*Q-&89E %%c*Q-&89E::j*:*<=%zz*@"E 	

 &/
 
 	uIGr-   c                T    UR                  U5      nU R                  " UUU4UUS.UD6$ )a  Construct FAISS wrapper from raw documents.

This is a user friendly interface that:
    1. Embeds documents.
    2. Creates an in memory docstore
    3. Initializes the FAISS database

This is intended to be a quick way to get started.

Example:
    .. code-block:: python

        from langchain_community.vectorstores import FAISS
        from langchain_community.embeddings import OpenAIEmbeddings

        embeddings = OpenAIEmbeddings()
        faiss = FAISS.from_texts(texts, embeddings)
r   )rJ   _FAISS__fromr  rK   r   r`   ra   r   rG   s          r"   
from_textsFAISS.from_texts  sE    6 ..u5
zz
  
 
 	
r-   c                p   #    UR                  U5      I Sh  vN nU R                  " UUU4UUS.UD6$  N7f)a  Construct FAISS wrapper from raw documents asynchronously.

This is a user friendly interface that:
    1. Embeds documents.
    2. Creates an in memory docstore
    3. Initializes the FAISS database

This is intended to be a quick way to get started.

Example:
    .. code-block:: python

        from langchain_community.vectorstores import FAISS
        from langchain_community.embeddings import OpenAIEmbeddings

        embeddings = OpenAIEmbeddings()
        faiss = await FAISS.afrom_texts(texts, embeddings)
Nr   )rQ   r  r  s          r"   afrom_textsFAISS.afrom_texts  sP     6 %55e<<
zz
  
 
 	
 =s   646c                j    [        U6 u  pgU R                  " [        U5      [        U5      U4UUS.UD6$ )az  Construct FAISS wrapper from raw documents.

This is a user friendly interface that:
    1. Embeds documents.
    2. Creates an in memory docstore
    3. Initializes the FAISS database

This is intended to be a quick way to get started.

Example:
    .. code-block:: python

        from langchain_community.vectorstores import FAISS
        from langchain_community.embeddings import OpenAIEmbeddings

        embeddings = OpenAIEmbeddings()
        text_embeddings = embeddings.embed_documents(texts)
        text_embedding_pairs = zip(texts, text_embeddings)
        faiss = FAISS.from_embeddings(text_embedding_pairs, embeddings)
r   )rq   r  r   )r  r   r   r`   ra   r   rK   rG   s           r"   from_embeddingsFAISS.from_embeddingsB  sK    :  1zzK
  
 
 	
r-   c                8   #    U R                   " UU4UUS.UD6$ 7f)z:Construct FAISS wrapper from raw documents asynchronously.r   )r  )r  r   r   r`   ra   r   s         r"   afrom_embeddingsFAISS.afrom_embeddingsi  s7      ""
  	

 
 	
s   c                R   [        U5      nUR                  SSS9  [        5       nUR                  U R                  [        X2 S3-  5      5        [        X2 S3-  S5       n[        R                  " U R                  U R                  4U5        SSS5        g! , (       d  f       g= f)zSave FAISS index, docstore, and index_to_docstore_id to disk.

Args:
    folder_path: folder path to save index, docstore,
        and index_to_docstore_id to.
    index_name: for saving with a specific index file name
T)exist_okparents.faiss.pklwbN)r   mkdirr#   write_indexr6   rn   openpickledumpr7   r8   )r?   folder_path
index_namepathr   fs         r"   
save_localFAISS.save_local{  s     K 

D$
/ ()$**c$<v1F*F&GH $<t,,d3qKK(A(ABAF 433s   !.B
B&)allow_dangerous_deserializationc               ,   U(       d  [        S5      e[        U5      n[        5       nUR                  [	        Xc S3-  5      5      n[        Xc S3-  S5       n	[        R                  " U	5      u  n
nSSS5        U " X(W
W40 UD6$ ! , (       d  f       N= f)a>  Load FAISS index, docstore, and index_to_docstore_id from disk.

Args:
    folder_path: folder path to load index, docstore,
        and index_to_docstore_id from.
    embeddings: Embeddings to use when generating queries
    index_name: for saving with a specific index file name
    allow_dangerous_deserialization: whether to allow deserialization
        of the data which involves loading a pickle file.
        Pickle files can be modified by malicious actors to deliver a
        malicious payload that results in execution of
        arbitrary code on your machine.
B  The de-serialization relies loading a pickle file. Pickle files can be modified to deliver a malicious payload that results in execution of arbitrary code on your machine.You will need to set `allow_dangerous_deserialization` to `True` to enable deserialization. If you do this, make sure that you trust the source of the data. For example, if you are loading a file that you created, and know that no one else has modified the file, then this is safe to do. Do not set this to `True` if you are loading a file from an untrusted source (e.g., some random site on the internet.).r  r  rbN)r'   r   r#   
read_indexrn   r  r  load)r  r  rG   r   r%  r   r!  r   r6   r"  r7   r8   s               r"   
load_localFAISS.load_local  s    . /	"  K ')  Tl&,A%A!BC $<t,,d3q $ 4 :h0DOOO 43s   B
Bc                p    [         R                  " U R                  U R                  U R                  45      $ )zCSerialize FAISS index, docstore, and index_to_docstore_id to bytes.)r  dumpsr6   r7   r8   rF   s    r"   serialize_to_bytesFAISS.serialize_to_bytes  s&    ||TZZ8Q8QRSSr-   c               n    U(       d  [        S5      e[        R                  " U5      u  nnnU " X%Xg40 UD6$ )zGDeserialize FAISS index, docstore, and index_to_docstore_id from bytes.r'  )r'   r  loads)r  
serializedrG   r%  r   r6   r7   r8   s           r"   deserialize_from_bytesFAISS.deserialize_from_bytes  sL     /	"   LL
		
  :hOOOr-   c                F   U R                   b  U R                   $ U R                  [        R                  :X  a  U R                  $ U R                  [        R
                  :X  a  U R                  $ U R                  [        R                  :X  a  U R                  $ [        S5      e)a   
The 'correct' relevance function
may differ depending on a few things, including:
- the distance / similarity metric used by the VectorStore
- the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
- embedding dimensionality
- etc.
zJUnknown distance strategy, must be cosine, max_inner_product, or euclidean)
r:   r9   r   r   %_max_inner_product_relevance_score_fnr<   _euclidean_relevance_score_fnCOSINE_cosine_relevance_score_fnr'   rF   s    r"   _select_relevance_score_fn FAISS._select_relevance_score_fn  s     ++7333 !!%5%G%GG===##'7'J'JJ555##'7'>'>>222  r-   c                    U R                  5       nUc  [        S5      eU R                  " U4UUUS.UD6nU VV	s/ sH  u  pX" U	5      4PM     n
nn	U
$ s  sn	nf )?Return docs and their similarity scores on a scale from 0 to 1.Lrelevance_score_fn must be provided to FAISS constructor to normalize scoresr   )r;  r'   r   r?   r   r   r   r   r   r@   r   r~   scoredocs_and_rel_scoress              r"   (_similarity_search_with_relevance_scores.FAISS._similarity_search_with_relevance_scores  s     "<<>%9  ;;
	

 
 @O
?NS$U+, 	 
 #"
s   Ac                   #    U R                  5       nUc  [        S5      eU R                  " U4UUUS.UD6I Sh  vN nU VV	s/ sH  u  pX" U	5      4PM     n
nn	U
$  N"s  sn	nf 7f)r>  Nr?  r   )r;  r'   r   r@  s              r"   )_asimilarity_search_with_relevance_scores/FAISS._asimilarity_search_with_relevance_scores  s      "<<>%9  !% B B!
	!

 !
 
 @O
?NS$U+, 	 
 #"

s!   9A&A	A&A A& A&c                  ^^^^ [        U 5      (       a  U $ [        U [        5      (       d  [        S[	        U 5       35      eSSKJnJnJnJ	nJ
nJn  UUUUUUS.nS S S.nXx-  m[        [        T5      / SQ-   5      n	S	mU  H7  n
U
(       d  M  U
R                  S
5      (       d  M$  X;  d  M+  [        SU
 35      e         SUU4S jjmSUU4S jjmT" U 5      $ )av  
Create a filter function based on the provided filter.

Args:
    filter: A callable or a dictionary representing the filter
    conditions for documents.

Returns:
    A function that takes Document's metadata and returns True if it
    satisfies the filter conditions, otherwise False.

Raises:
    ValueError: If the filter is invalid or contains unsupported operators.
z5filter must be a dict of metadata or a callable, not r   )eqr   gtr   ltne)z$eqz$neqz$gtz$ltz$gtez$ltec                
    X;   $ rE   rc   abs     r"   <lambda>+FAISS._create_filter_func.<locals>.<lambda>`  s    r-   c                
    X;  $ rE   rc   rN  s     r"   rQ  rR  a  s    r-   )z$inz$nin)$and$or$not
   $&filter contains unsupported operator: c                ^  >^ ^^^ [        T[        5      (       aP  / mTR                  5        H/  u  p#UT;  a  [        SU 35      eTR	                  TU   U45        M1     SU U4S jjnU$ [        T[
        5      (       a(  [        T5      T:  a  [        T5      mUU 4S j$ UU 4S j$ UU 4S j$ )a$  
Creates a filter function based on field and condition.

Args:
    field: The document field to filter on
    condition: Filter condition (dict for operators, list for in,
               or direct value for equality)

Returns:
    A filter function that takes a document and returns boolean
rY  c                R   >^ U R                  T5      m[        U4S jT 5       5      $ )a?  
Evaluates a document against a set of predefined operators
and their values. This function applies multiple
comparison/sequence operators to a specific field value
from the document. All conditions must be satisfied for the
function to return True.

Args:
    doc (Dict[str, Any]): The document to evaluate, containing
    key-value pairs where keys are field names and values
    are the field values. The document must contain the field
    being filtered.

Returns:
    bool: True if the document's field value satisfies all
        operator conditions, False otherwise.
c              3  8   >#    U H  u  pU" TU5      v   M     g 7frE   rc   )rd   opvalue	doc_values      r"   rf   YFAISS._create_filter_func.<locals>.filter_func_cond.<locals>.filter_fn.<locals>.<genexpr>  s     OY	r)U33Ys   )r   all)r~   r_  field	operatorss    @r"   	filter_fnFFAISS._create_filter_func.<locals>.filter_func_cond.<locals>.filter_fn  s#    $ !$IOYOOOr-   c                ,   > U R                  T5      T;   $ rE   r   )r~   condition_setrb  s    r"   rQ  EFAISS._create_filter_func.<locals>.filter_func_cond.<locals>.<lambda>  s    swwu~'Fr-   c                ,   > U R                  T5      T;   $ rE   rg  r~   	conditionrb  s    r"   rQ  ri    s    3775>Y#>r-   c                ,   > U R                  T5      T:H  $ rE   rg  rk  s    r"   rQ  ri    s    swwu~:r-   )r~   Dict[str, Any]returnr   )r%   dictr   r'   r   r   r&   	frozenset)	rb  rl  r]  r^  rd  rh  rc  
OPERATIONSSET_CONVERT_THRESHOLDs	   ``   @@r"   filter_func_cond3FAISS._create_filter_func.<locals>.filter_func_condl  s     )T**	!*!2IB+(+QRTQU)VWW$$jne%<= "3
P P* ! )T**y>$99$-i$8MFF>>::r-   c                T  >^^^ SU ;   a!  U S    Vs/ sH  nT" U5      PM     snmU4S j$ SU ;   a!  U S    Vs/ sH  nT" U5      PM     snmU4S j$ SU ;   a  T" U S   5      mU4S j$ U R                  5        VVs/ sH  u  p#T" X#5      PM     snnmU4S j$ s  snf s  snf s  snnf )a  
Creates a filter function that evaluates documents against specified
filter conditions.

This function processes a dictionary of filter conditions and returns
a callable that can evaluate documents against these conditions. It
supports logical operators ($and, $or, $not) and field-level filtering.

Args:
    filter (Dict[str, Any]): A dictionary containing filter conditions.
    Can include:
        - Logical operators ($and, $or, $not) with lists of sub-filters
        - Field-level conditions with comparison or sequence operators
        - Direct field-value mappings for equality comparison

Returns:
    Callable[[Dict[str, Any]], bool]: A function that takes a document
    (as a dictionary) and returns True if the document matches all
    filter conditions, False otherwise.
rT  c                0   >^  [        U 4S jT 5       5      $ )Nc              3  0   >#    U H  o" T5      v   M     g 7frE   rc   rd   r"  r~   s     r"   rf   SFAISS._create_filter_func.<locals>.filter_func.<locals>.<lambda>.<locals>.<genexpr>       &?w!qvvw   ra  r~   filterss   `r"   rQ  @FAISS._create_filter_func.<locals>.filter_func.<locals>.<lambda>      3&?w&?#?r-   rU  c                0   >^  [        U 4S jT 5       5      $ )Nc              3  0   >#    U H  o" T5      v   M     g 7frE   rc   ry  s     r"   rf   rz    r{  r|  )anyr~  s   `r"   rQ  r    r  r-   rV  c                   > T" U 5      (       + $ rE   rc   )r~   conds    r"   rQ  r    s    tCy=r-   c                0   >^  [        U 4S jT 5       5      $ )Nc              3  0   >#    U H  o" T5      v   M     g 7frE   rc   )rd   rl  r~   s     r"   rf   rz    s     "N:i9S>>:r|  r}  )r~   
conditionss   `r"   rQ  r    s    s"N:"NNr-   )r   )	r   
sub_filterrb  rl  r  r  r  r   rt  s	       @@@r"   r   .FAISS._create_filter_func.<locals>.filter_func  s    * EKF^T^z;z2^T??EKE]S]z;z2]S??"6&>200 )/(6$E !2(6J ON U Ts   BB=B$)rb  rn   rl  z%Union[Dict[str, Any], List[Any], Any]ro   Callable[[Dict[str, Any]], bool])r   rn  ro  r  )callabler%   rp  r'   typer   rI  r   rJ  r   rK  rL  rq  r   
startswith)r   rI  r   rJ  r   rK  rL  COMPARISON_OPERATORSSEQUENCE_OPERATORSVALID_OPERATORSr]  rr  rs  r   rt  s              @@@@r"   r   FAISS._create_filter_func;  s
   $ FM&$''GV~V  	43  
 '+
 *>
#D$47N$NO " BrbmmC((R-F #I"!NOO 2	;2	;#H2	;-2	; 2	;h%	O %	ON 6""r-   c                   U Vs/ sH  o R                   R                  U5      PM     nnU Vs/ sH  n[        U[        5      (       d  M  UPM     sn$ s  snf s  snf rE   )r7   r   r%   r   )r?   ra   rz   r   r~   s        r"   
get_by_idsFAISS.get_by_ids  sI    589Sc$$S)S9#Atz#x'@tAA :As   #AA
A)r;   r9   r7   r5   r6   r8   r:   )r5   z/Union[Callable[[str], List[float]], Embeddings]r6   r   r7   r   r8   zDict[int, str]r@   z"Optional[Callable[[float], float]]rA   r   r9   r   )ro  zOptional[Embeddings])rK   	List[str]ro  List[List[float]])rL   rn   ro  List[float])NN)
rK   Iterable[str]rG   zIterable[List[float]]r`   Optional[Iterable[dict]]ra   Optional[List[str]]ro  r  )
rK   r  r`   Optional[List[dict]]ra   r  r   r   ro  r  )
r   !Iterable[Tuple[str, List[float]]]r`   r  ra   r  r   r   ro  r  )r   Nr   )r   r  r   r   r   )Optional[Union[Callable, Dict[str, Any]]]r   r   r   r   ro  List[Tuple[Document, float]])r   rn   r   r   r   r  r   r   r   r   ro  r  )r   r  r   r   r   zOptional[Dict[str, Any]]r   r   r   r   ro  List[Document])r   r  r   r   r   r  r   r   r   r   ro  r  )r   rn   r   r   r   r  r   r   r   r   ro  r  )r   r  r   r   r   r   r   floatr   r  ro  r  )r   r   r   N)r   r  r   r   r   r   r   r  r   r  r   r   ro  r  )r   rn   r   r   r   r   r   r  r   r  r   r   ro  r  rE   )ra   r  r   r   ro  Optional[bool])r   r/   ro  None)rK   r  rG   r  r   r   r`   r  ra   r  rA   r   r9   r   r   r   ro  r/   )rK   r  r   r   r`   r  ra   r  r   r   ro  r/   )rK   z	list[str]r   r   r`   r  ra   r  r   r   ro  r/   )r   r  r   r   r`   r  ra   r  r   r   ro  r/   )r6   )r  rn   r   rn   ro  r  )r  rn   rG   r   r   rn   r%  r   r   r   ro  r/   )ro  bytes)
r3  r  rG   r   r%  r   r   r   ro  r/   )ro  zCallable[[float], float])r   r  ro  r  )ra   zSequence[str]ro  zlist[Document])3__name__
__module____qualname____firstlineno____doc__r   r<   rB   propertyrG   rM   rS   rY   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr  r	  r  r  r  r#  r+  r/  r4  r;  rC  rF  staticmethodr   r  __static_attributes__rc   r-   r"   r/   r/   K   sd	   }P BF".>.Q.Q!
! ! ! -! ?! ! ,!F 
 
E
1 /3#'(( *( ,	(
 !( 
(Z +/#'	KK (K !	K
 K 
K0 +/#'	KK (K !	K
 K 
K2 +/#'	K:K (K !	K
 K 
K2 <@?? ? :	?
 ? ? 
&?H <@%
%
 %
 :	%

 %
 %
 
&%
T <@     :	 
     
& J <@     :	 
     
& J +/33 3 )	3
 3 3 
3F <@33 3 :	3
 3 3 
3F <@33 3 :	3
 3 3 
38 <@33 3 :	3
 3 3 
3:  <@BB 	B
 B B :B 
&BP  <@%
%
 	%

 %
 %
 :%
 
&%
T  <@33 3 	3
 3 :3 3 
3D  <@33 3 	3
 3 :3 3 
3H  <@## # 	#
 # :# # 
#P  <@## # 	#
 # :# # 
#J D6@  /3#'".>.Q.Q & 	
 , !  ,  
 > 
 +/#'"
"
 "
 (	"

 !"
 "
 
"
 "
H 
 +/#'"
"
 "
 (	"

 !"
 "
 
"
 "
H 
 /3#'$
:$
 $
 ,	$

 !$
 $
 
$
 $
L 
 /3#'
:
 
 ,	

 !
 
 

 
"G& 
 "	1P 161P1P 1P 	1P *.1P 1P 
1P 1PfT  16PP P
 *.P P 
P P>< <@## # :	#
 # # 
&#@ <@## # :	#
 # # 
&#: K#9K#	)K# K#ZBr-   r/   rE   )r!   r  ro  r   )
r(   r   r)   r   r*   rn   r+   rn   ro  r  )-
__future__r   loggingr   r   r  ro   r=   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   numpyrs   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.runnables.configr   langchain_core.vectorstoresr   !langchain_community.docstore.baser   r   &langchain_community.docstore.in_memoryr   &langchain_community.vectorstores.utilsr   r   	getLoggerr  r3   r#   r,   r/   rc   r-   r"   <module>r     st    "   	        - 0 ; 3 D C
 
		8	$6@BK @Br-   