
    dh F                        S SK Jr  S SKrS SKrS SKrS SKJr  S SKJr  S SK	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  S SKJr  S SKJ r   \!" / SQ5      r"Sr#SS jr$ " S S\5      r%g)    )annotationsN)ConfigParser)Path)AnyCallableDictIterableListOptionalTuple)Document)
Embeddingsguard_import)VectorStore)Docstore)InMemoryDocstore)maximal_marginal_relevance)angular	euclidean	manhattanhammingdotr   c                     [        S5      $ )z1Import annoy if available, otherwise raise error.annoyr        ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/annoy.pydependable_annoy_importr      s      r   c                     \ rS rSrSr          SS jr\SS j5       r S       SS jjr      SS 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4                 S)S jj5       r\S\SS4               S*S jj5       r\S\SS4               S+S jj5       rS,S-S jjr\SS.       S.S jj5       rSrg)/Annoy   a  `Annoy` vector store.

To use, you should have the ``annoy`` python package installed.

Example:
    .. code-block:: python

        from langchain_community.vectorstores import Annoy
        db = Annoy(embedding_function, index, docstore, index_to_docstore_id)

c                @    Xl         X l        X0l        X@l        XPl        g)z%Initialize with necessary components.N)embedding_functionindexmetricdocstoreindex_to_docstore_id)selfr$   r%   r&   r'   r(   s         r   __init__Annoy.__init__*   s     #5
 $8!r   c                    g Nr   )r)   s    r   
embeddingsAnnoy.embeddings9   s     r   Nc                    [        S5      e)Nz=Annoy does not allow to add new data once the index is build.)NotImplementedError)r)   texts	metadataskwargss       r   	add_textsAnnoy.add_texts>   s     "K
 	
r   c                    / n[        X5       Hg  u  pEU R                  U   nU R                  R                  U5      n[	        U[
        5      (       d  [        SU SU 35      eUR                  Xu45        Mi     U$ )zTurns annoy results into a list of documents and scores.

Args:
    idxs: List of indices of the documents in the index.
    dists: List of distances of the documents in the index.
Returns:
    List of Documents and scores.
Could not find document for id , got )zipr(   r'   search
isinstancer   
ValueErrorappend)r)   idxsdistsdocsidxdist_iddocs           r   process_index_resultsAnnoy.process_index_resultsH   sy     T)IC++C0C--&&s+Cc8,, #B3%vcU!STTKK$ * r   c                ^    U R                   R                  XUSS9u  pEU R                  XE5      $ 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.
    search_k: inspect up to search_k nodes which defaults
        to n_trees * n if not provided
Returns:
    List of Documents most similar to the query and score for each
Tsearch_kinclude_distances)r%   get_nns_by_vectorrF   )r)   	embeddingkrL   r?   r@   s         r   &similarity_search_with_score_by_vector,Annoy.similarity_search_with_score_by_vector\   s9     jj228t 3 
 ))$66r   c                ^    U R                   R                  XUSS9u  pEU R                  XE5      $ rJ   )r%   get_nns_by_itemrF   )r)   docstore_indexrP   rL   r?   r@   s         r   %similarity_search_with_score_by_index+Annoy.similarity_search_with_score_by_indexn   s9     jj00D 1 
 ))$66r   c                L    U R                  U5      nU R                  XBU5      nU$ )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.
    search_k: inspect up to search_k nodes which defaults
        to n_trees * n if not provided

Returns:
    List of Documents most similar to the query and score for each
)r$   rQ   )r)   queryrP   rL   rO   rA   s         r   similarity_search_with_score"Annoy.similarity_search_with_score   s+     ++E2	::9Rr   c                ^    U R                  XU5      nU VVs/ sH  u  pgUPM	     snn$ s  snnf )aD  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.
    search_k: inspect up to search_k nodes which defaults
        to n_trees * n if not provided

Returns:
    List of Documents most similar to the embedding.
)rQ   )r)   rO   rP   rL   r4   docs_and_scoresrE   _s           r   similarity_search_by_vector!Annoy.similarity_search_by_vector   s5     EE(
 #22//222   )c                ^    U R                  XU5      nU VVs/ sH  u  pgUPM	     snn$ s  snnf )a:  Return docs most similar to docstore_index.

Args:
    docstore_index: Index of document in docstore
    k: Number of Documents to return. Defaults to 4.
    search_k: inspect up to search_k nodes which defaults
        to n_trees * n if not provided

Returns:
    List of Documents most similar to the embedding.
)rV   )r)   rU   rP   rL   r4   r]   rE   r^   s           r   similarity_search_by_index Annoy.similarity_search_by_index   s5     DDx
 #22//222ra   c                ^    U R                  XU5      nU VVs/ sH  u  pgU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.
    search_k: inspect up to search_k nodes which defaults
        to n_trees * n if not provided

Returns:
    List of Documents most similar to the query.
)rZ   )r)   rY   rP   rL   r4   r]   rE   r^   s           r   similarity_searchAnnoy.similarity_search   s0     ;;EhO"12//222ra   c                
   U R                   R                  XSSS9nU Vs/ sH  opR                   R                  U5      PM     nn[        [        R
                  " U/[        R                  S9UUUS9n	U	 Vs/ sH  owS:w  d  M
  Xg   PM     n
n/ nU
 Hd  nU R                  U   nU R                  R                  U5      n[        U[        5      (       d  [        SU SU 35      eUR                  U5        Mf     U$ s  snf s  s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.
    fetch_k: Number of Documents to fetch to pass to MMR algorithm.
    k: Number of Documents to return. Defaults to 4.
    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.
rH   FrK   )dtype)rP   lambda_multr8   r9   )r%   rN   get_item_vectorr   nparrayfloat32r(   r'   r;   r<   r   r=   r>   )r)   rO   rP   fetch_krj   r4   r?   ir.   mmr_selectedselected_indicesrA   rD   rE   s                 r   'max_marginal_relevance_search_by_vector-Annoy.max_marginal_relevance_search_by_vector   s   2 zz++u , 
 >BBTjj003T
B1HHi[

3#	
 .:E\"WGDG\E!A++A.C--&&s+Cc8,, #B3%vcU!STTKK " # C Fs   #C;7D D c                H    U R                  U5      nU R                  XbX4S9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 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.
)rj   )r$   rs   )r)   rY   rP   ro   rj   r4   rO   rA   s           r   max_marginal_relevance_search#Annoy.max_marginal_relevance_search   s5    0 ++E2	;;' < 
 r   d   c                   U[         ;  a  [        SU S[        [         5       35      e[        S5      n	U(       d  [        S5      e[	        US   5      n
U	R                  XS9n[        U5       H  u  pUR                  X5        M     UR                  XgS9  / n[        U5       H+  u  pU(       a  XL   O0 nUR                  [        UUS95        M-     [        [	        U5      5       Vs0 sH!  o[        [        R                  " 5       5      _M#     nn[        [        U5       VVs0 sH  u  nnUU   U_M     snn5      nU " UR                   XUU5      $ s  snf s  snnf )	NzUnsupported distance metric: z. Expected one of r   z/embeddings must be provided to build AnnoyIndexr   r&   )n_jobs)page_contentmetadata)INDEX_METRICSr=   listr   len
AnnoyIndex	enumerateadd_itembuildr>   r   rangestruuiduuid4r   embed_query)clsr2   r.   rO   r3   r&   treesr{   r4   r   fr%   rp   emb	documentstextr}   index_to_idrE   r'   s                       r   __fromAnnoy.__from  sS    &3F8 <''+M':&;=  W%NOO
1   2
+FANN1" ,E)	 'GA'0y|bHX4(KL ( 6;3y>5JK5J#djjl++5JK#/8/CD/CVQ[^S /CD
 9((%;OO	 LDs   )'E%E
c           	     P    UR                  U5      nU R                  " XX#XEU40 UD6$ )aP  Construct Annoy wrapper from raw documents.

Args:
    texts: List of documents to index.
    embedding: Embedding function to use.
    metadatas: List of metadata dictionaries to associate with documents.
    metric: Metric to use for indexing. Defaults to "angular".
    trees: Number of trees to use for indexing. Defaults to 100.
    n_jobs: Number of jobs to use for indexing. Defaults to -1.

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

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

Example:
    .. code-block:: python

        from langchain_community.vectorstores import Annoy
        from langchain_community.embeddings import OpenAIEmbeddings
        embeddings = OpenAIEmbeddings()
        index = Annoy.from_texts(texts, embeddings)
)embed_documents_Annoy__from)	r   r2   rO   r3   r&   r   r{   r4   r.   s	            r   
from_textsAnnoy.from_texts<  s7    H ..u5
zzyVF
NT
 	
r   c           	         U Vs/ sH  oS   PM	     n	nU Vs/ sH  oS   PM	     n
nU R                   " XX#XEU40 UD6$ s  snf s  snf )a  Construct Annoy wrapper from embeddings.

Args:
    text_embeddings: List of tuples of (text, embedding)
    embedding: Embedding function to use.
    metadatas: List of metadata dictionaries to associate with documents.
    metric: Metric to use for indexing. Defaults to "angular".
    trees: Number of trees to use for indexing. Defaults to 100.
    n_jobs: Number of jobs to use for indexing. Defaults to -1

This is a user friendly interface that:
    1. Creates an in memory docstore with provided embeddings
    2. Initializes the Annoy database

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

Example:
    .. code-block:: python

        from langchain_community.vectorstores import Annoy
        from langchain_community.embeddings import OpenAIEmbeddings
        embeddings = OpenAIEmbeddings()
        text_embeddings = embeddings.embed_documents(texts)
        text_embedding_pairs = list(zip(texts, text_embeddings))
        db = Annoy.from_embeddings(text_embedding_pairs, embeddings)
r      )r   )r   text_embeddingsrO   r3   r&   r   r{   r4   tr2   r.   s              r   from_embeddingsAnnoy.from_embeddingse  s[    J  //!1/$34OqdO
4zzyVF
NT
 	
 04s	   =AFc                   [        U5      n[        R                  " USS9  [        5       nU R                  R
                  U R                  S.US'   U R                  R                  [        US-  5      US9  [        US-  S5       n[        R                  " U R                  U R                  U4U5        S	S	S	5        g	! , (       d  f       g	= f)
zSave Annoy index, docstore, and index_to_docstore_id to disk.

Args:
    folder_path: folder path to save index, docstore,
        and index_to_docstore_id to.
    prefault: Whether to pre-load the index into memory.
T)exist_ok)r   r&   ANNOYindex.annoy)prefault	index.pklwbN)r   osmakedirsr   r%   r   r&   saver   openpickledumpr'   r(   )r)   folder_pathr   pathconfig_objectfiles         r   
save_localAnnoy.save_local  s     K 
D4($kk"
g 	

D=01HE$$d+tKK(A(A=QSWX ,++s   /B>>
C)allow_dangerous_deserializationc                  U(       d  [        S5      e[        U5      n[        S5      n[        US-  S5       n[        R
                  " U5      u  nnn	SSS5        [        W	S   S   5      n
U	S   S   nUR                  XS	9nUR                  [        US
-  5      5        U " UR                  XWW5      $ ! , (       d  f       Nh= f)a  Load Annoy index, docstore, and index_to_docstore_id to disk.

Args:
    folder_path: folder path to load index, docstore,
        and index_to_docstore_id from.
    embeddings: Embeddings to use when generating queries.
    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.
aB  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   rbNr   r   r&   rz   r   )
r=   r   r   r   r   loadintr   r   r   )r   r   r.   r   r   r   r   r'   r(   r   r   r&   r%   s                r   
load_localAnnoy.load_local  s    ( /	"  K W%$$d+t 	$ , g&s+,w'1   2

3tm+,-""E8=Q
 	
! ,+s   B55
C)r'   r$   r%   r(   r&   )
r$   r   r%   r   r&   r   r'   r   r(   zDict[int, str])returnzOptional[Embeddings]r-   )r2   zIterable[str]r3   Optional[List[dict]]r4   r   r   	List[str])r?   z	List[int]r@   List[float]r   List[Tuple[Document, float]])   rH   )rO   r   rP   r   rL   r   r   r   )rU   r   rP   r   rL   r   r   r   )rY   r   rP   r   rL   r   r   r   )
rO   r   rP   r   rL   r   r4   r   r   List[Document])
rU   r   rP   r   rL   r   r4   r   r   r   )
rY   r   rP   r   rL   r   r4   r   r   r   )r      g      ?)rO   r   rP   r   ro   r   rj   floatr4   r   r   r   )rY   r   rP   r   ro   r   rj   r   r4   r   r   r   )r2   r   r.   zList[List[float]]rO   r   r3   r   r&   r   r   r   r{   r   r4   r   r   r!   )r2   r   rO   r   r3   r   r&   r   r   r   r{   r   r4   r   r   r!   )r   zList[Tuple[str, List[float]]]rO   r   r3   r   r&   r   r   r   r{   r   r4   r   r   r!   )F)r   r   r   boolr   None)r   r   r.   r   r   r   r   r!   )__name__
__module____qualname____firstlineno____doc__r*   propertyr.   r5   rF   rQ   rV   rZ   r_   rc   rf   rs   rv   classmethodDEFAULT_METRICr   r   r   r   r   __static_attributes__r   r   r   r!   r!      s   
9$9 9 	9
 9 -9   +/

 (
 	

 

&1	%* CE7$7),7<?7	%7& @B7!7&)79<7	%7& 79 03	%& CE3$3),3<?3PS3	3( @B3!3&)39<3MP3	3( 7933 3033DG3	3(  -- - 	-
 - - 
-d    	
   
<  +/$#P#P &#P 	#P
 (#P #P #P #P #P 
#P #PJ 
 +/$&
&
 &
 (	&

 &
 &
 &
 &
 
&
 &
P 
 +/$)
6)
 )
 (	)

 )
 )
 )
 )
 
)
 )
VY(  166
6
 6

 *.6
 
6
 6
r   r!   )r   r   )&
__future__r   r   r   r   configparserr   pathlibr   typingr   r   r   r	   r
   r   r   numpyrl   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   !langchain_community.docstore.baser   &langchain_community.docstore.in_memoryr   &langchain_community.vectorstores.utilsr   	frozensetr~   r   r   r!   r   r   r   <module>r      s[    " 	   %  G G G  - 0 - 3 6 C MQR!

K 
r   