
    NgR                       d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZmZ erd dlmZ d d	lmZmZ  ej        e           Z!ddZ" edd          Z# G d de          Z$dS )    )annotationsN)	TYPE_CHECKINGAnyCallableIterableListOptionalTupleTypeVarUnion)Document)
Embeddings)VectorStore)DistanceStrategymaximal_marginal_relevanceClient)NeighborVectorDistanceMetricreturnr   c                 Z    	 ddl m}  n"# t          $ r}t          d          |d }~ww xY w| S )Nr   r   zoCould not import aerospike_vector_search python package. Please install it with `pip install aerospike_vector`.)aerospike_vector_searchr   ImportError)r   es     f/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/aerospike.py_import_aerospiker   #   s_    2222222   E
 
 	
 Ms   	 
(#(AVST	Aerospike)boundc                  F   e Zd ZdZdddddej        fdMdZedNd            ZdOdZ	dPdZ
edQd!            Z	 	 	 	 	 	 dRdSd/Z	 	 dTdUd1Z	 	 	 dVdWd7Z	 	 	 dVdXd8Z	 	 	 dVdYd:Z	 	 	 dVdZd;Zd[d=Zed\d@            Z	 	 	 	 	 d]d^dEZ	 	 	 	 	 d]d_dFZe	 	 	 	 	 	 	 d`dadL            ZdS )br   zu`Aerospike` vector store.

    To use, you should have the ``aerospike_vector_search`` python package installed.
    N_vector_text_idclientr   	embeddingUnion[Embeddings, Callable]	namespacestr
index_nameOptional[str]
vector_keytext_keyid_keyset_namedistance_strategy7Optional[Union[DistanceStrategy, VectorDistanceMetric]]c
                v   t                      }
t          |t                    st          j        d           t          ||
          st          dt          |                     || _        || _        || _	        || _
        || _        || _        || _        || _        |                     |	          | _        dS )a  Initialize with Aerospike client.

        Args:
            client: Aerospike client.
            embedding: Embeddings object or Callable (deprecated) to embed text.
            namespace: Namespace to use for storing vectors. This should match
            index_name: Name of the index previously created in Aerospike. This
            vector_key: Key to use for vector in metadata. This should match the
                key used during index creation.
            text_key: Key to use for text in metadata.
            id_key: Key to use for id in metadata.
            set_name: Default set name to use for storing vectors.
            distance_strategy: Distance strategy to use for similarity search
                This should match the distance strategy used during index creation.
        z`Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.zDclient should be an instance of aerospike_vector_search.Client, got N)r   
isinstancer   warningswarn
ValueErrortype_client
_embedding	_text_key_vector_key_id_key_index_name
_namespace	_set_nameconvert_distance_strategy_distance_strategy)selfr$   r%   r'   r)   r+   r,   r-   r.   r/   	aerospikes              r   __init__zAerospike.__init__7   s    < &''	)Z00 	M.  
 &),, 	&F||& &  
 #!%%#!"&"@"@AR"S"S    r   Optional[Embeddings]c                H    t          | j        t                    r| j        S dS )z/Access the query embedding object if available.N)r2   r8   r   rA   s    r   
embeddingszAerospike.embeddingsm   s%     doz22 	#?"trD   textsIterable[str]List[List[float]]c                     t           j        t                    r' j                            t	          |                    S  fd|D             S )zEmbed search docs.c                :    g | ]}                     |          S  )r8   ).0trA   s     r   
<listcomp>z.Aerospike._embed_documents.<locals>.<listcomp>x   s%    222q""222rD   )r2   r8   r   embed_documentslist)rA   rI   s   ` r   _embed_documentszAerospike._embed_documentst   sN    doz22 	@?224;;???2222E2222rD   textList[float]c                    t          | j        t                    r| j                            |          S |                     |          S )zEmbed query text.)r2   r8   r   embed_query)rA   rU   s     r   _embed_queryzAerospike._embed_queryz   s>    doz22 	5?..t444t$$$rD   -Union[VectorDistanceMetric, DistanceStrategy]r   c                    ddl m} t          | t                    r| S | |j        k    rt          j        S | |j        k    rt          j        S | |j        k    rt          j        S t          d          )z
        Convert Aerospikes distance strategy to langchains DistanceStrategy
        enum. This is a convenience method to allow users to pass in the same
        distance metric used to create the index.
        r   )r   DUnknown distance strategy, must be cosine, dot_product, or euclidean)	aerospike_vector_search.typesr   r2   r   COSINEDOT_PRODUCTSQUARED_EUCLIDEANEUCLIDEAN_DISTANCEr5   )r/   r   s     r   r?   z#Aerospike.convert_distance_strategy   s     	GFFFFF')9:: 	%$$ 4 ;;;#** 4 @@@#// 4 FFF#66U
 
 	
rD     T	metadatasOptional[List[dict]]idsOptional[List[str]]embedding_chunk_sizeintwait_for_indexboolkwargsr   	List[str]c           
        || j         }|| j        }|r|t          d          t          |          }|pd |D             }|rd |D             }n|pd |D             }t	          dt          |          |          D ]}	||	|	|z            }
||	|	|z            }||	|	|z            }|                     |
          }t          |||
          D ]\  }}}||| j        <   ||| j	        <   t          ||          D ]+\  }}||| j
        <    | j        j        d	| j        |||d| ,|r!| j                            | j        |           |S )
a  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 ids to associate with the texts.
            set_name: Optional aerospike set name to add the texts to.
            batch_size: Batch size to use when adding the texts to the vectorstore.
            embedding_chunk_size: Chunk size to use when embedding the texts.
            index_name: Optional aerospike index name used for waiting for index
                completion. If not provided, the default index_name will be used.
            wait_for_index: If True, wait for the all the texts to be indexed
                before returning. Requires index_name to be provided. Defaults
                to True.
            kwargs: Additional keyword arguments to pass to the client upsert call.

        Returns:
            List of ids from adding the texts into the vectorstore.

        Nz6if wait_for_index is True, index_name must be providedc                N    g | ]"}t          t          j                              #S rN   )r(   uuiduuid4rO   _s     r   rQ   z'Aerospike.add_texts.<locals>.<listcomp>   s&    777Ac$*,,''777rD   c                6    g | ]}|                                 S rN   )copy)rO   ms     r   rQ   z'Aerospike.add_texts.<locals>.<listcomp>   s     555a555rD   c                    g | ]}i S rN   rN   rq   s     r   rQ   z'Aerospike.add_texts.<locals>.<listcomp>   s    %8%8%8Qb%8%8%8rD   r   )r'   keyr.   record_data)r'   namerN   )r>   r<   r5   rS   rangelenrT   zipr:   r9   r;   r7   upsertr=   wait_for_index_completion)rA   rI   rc   re   r.   rg   r)   ri   rk   ichunk_texts	chunk_idschunk_metadatasrH   metadatar%   rU   ids                     r   	add_textszAerospike.add_texts   s   @ ~H)J 	Wj0UVVVU777777  	9559555II!8%8%8%%8%8%8Iq#e**&:;; 	 	AA(<$< <=KA$8 889I'A0D,D(DEO..{;;J-0[. . 0 0))T .7)*+/(( #I ? ?  H)+&## "o% (	 
      	L22/ 3   
 
rD   Optional[bool]c                r    ddl m} |r.|D ]+}	  | j        j        d| j        ||d| # |$ r Y  dS w xY wdS )a7  Delete by vector ID or other criteria.

        Args:
            ids: List of ids to delete.
            **kwargs: Other keyword arguments to pass to client delete call.

        Returns:
            Optional[bool]: True if deletion is successful,
            False otherwise, None if not implemented.
        r   )AVSServerError)r'   rw   r.   FTrN   )r   r   r7   deleter=   )rA   re   r.   rk   r   r   s         r   r   zAerospike.delete   s      	;::::: 
	! 	! 	!!'DL' "&/!)  !	    & ! ! ! 555! ts   *44   querykmetadata_keysList[Tuple[Document, float]]c                L     | j         |                     |          f|||d|S )a  Return aerospike documents most similar to query, along with scores.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            metadata_keys: List of metadata keys to return with the documents.
                If None, all metadata keys will be returned. Defaults to None.
            index_name: Name of the index to search. Overrides the default
                index_name.
            kwargs: Additional keyword arguments to pass to the search method.

        Returns:
            List of Documents most similar to the query and associated scores.
        r   r   r)   )&similarity_search_by_vector_with_scorerY   )rA   r   r   r   r)   rk   s         r   similarity_search_with_scorez&Aerospike.similarity_search_with_score  sI    . ;t:e$$
'!	
 

 
 
 	
rD   c           	        g }|r| j         |vr| j         g|z   }|| j        }|t          d           | j        j        d|| j        |||d|}|D ]}}|j        }	| j         |	v rH|	                    | j                   }
|j        }|	                    t          |
|	          |f           Zt                              d| j          d           ~|S )a  Return aerospike documents most similar to embedding, along with scores.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            metadata_keys: List of metadata keys to return with the documents.
                If None, all metadata keys will be returned. Defaults to None.
            index_name: Name of the index to search. Overrides the default
                index_name.
            kwargs: Additional keyword arguments to pass to the client
                vector_search method.

        Returns:
            List of Documents most similar to the query and associated scores.

        Nzindex_name must be provided)r)   r'   r   limitfield_names)page_contentr   zFound document with no `z` key. Skipping.rN   )r9   r<   r5   r7   vector_searchr=   fieldspopdistanceappendr   loggerwarning)rA   r%   r   r   r)   rk   docsresultsresultr   rU   scores               r   r   z0Aerospike.similarity_search_by_vector_with_score*  s    2  	=T^=@@!^,}<M)J:;;;"<$,"< #
!o%#
 #
 #
 #
  	 	F}H~))||DN33X4(KKKUSTTTTOt~OOO   rD   List[Document]c                :    d  | j         |f|||d|D             S )ak  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.
            metadata_keys: List of metadata keys to return with the documents.
                If None, all metadata keys will be returned. Defaults to None.
            index_name: Name of the index to search. Overrides the default
                index_name.
            kwargs: Additional keyword arguments to pass to the search method.


        Returns:
            List of Documents most similar to the query vector.
        c                    g | ]\  }}|S rN   rN   rO   docrr   s      r   rQ   z9Aerospike.similarity_search_by_vector.<locals>.<listcomp>}  s,     	
 	
 	
Q 	
 	
 	
rD   r   )r   )rA   r%   r   r   r)   rk   s         r   similarity_search_by_vectorz%Aerospike.similarity_search_by_vectorf  sR    .	
 	
E$E+%	 
  	
 	
 	
 		
rD   c                >     | j         |f|||d|}d |D             S )a*  Return aerospike documents most similar to query.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            metadata_keys: List of metadata keys to return with the documents.
                If None, all metadata keys will be returned. Defaults to None.
            index_name: Optional name of the index to search. Overrides the
                default index_name.

        Returns:
            List of Documents most similar to the query and score for each
        r   c                    g | ]\  }}|S rN   rN   r   s      r   rQ   z/Aerospike.similarity_search.<locals>.<listcomp>  s    222Q222rD   )r   )rA   r   r   r   r)   rk   docs_and_scoress          r   similarity_searchzAerospike.similarity_search  sJ    * <$;
m

 
NT
 
 32/2222rD   Callable[[float], float]c                    | j         t          j        k    r| j        S | j         t          j        k    r| j        S | j         t          j        k    r| j        S t          d          )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.

        0 is dissimilar, 1 is similar.

        Aerospike's relevance_fn assume euclidean and dot product embeddings are
        normalized to unit norm.
        r\   )	r@   r   r^   _cosine_relevance_score_fnr_   %_max_inner_product_relevance_score_fnra   _euclidean_relevance_score_fnr5   rG   s    r   _select_relevance_score_fnz$Aerospike._select_relevance_score_fn  sl     "&6&===22$(8(DDD==$(8(KKK55!  rD   r   floatc                    d| dz  z
  S )zgAerospike returns cosine distance scores between [0,2]

        0 is dissimilar, 1 is similar.
              rN   )r   s    r   r   z$Aerospike._cosine_relevance_score_fn  s     EAIrD            ?fetch_klambda_multc                X   
 |r j         |vr j         g|z   }  j        |f|||d|
t          t          j        |gt          j                   fd
D             ||          }|r3 j         |v r*|D ]'}	
|	         j                             j                    (
fd|D             S )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 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.
            metadata_keys: List of metadata keys to return with the documents.
                If None, all metadata keys will be returned. Defaults to None.
            index_name: Optional name of the index to search. Overrides the
                default index_name.
        Returns:
            List of Documents selected by maximal marginal relevance.
        r   )dtypec                4    g | ]}|j         j                 S rN   )r   r:   )rO   r   rA   s     r   rQ   zEAerospike.max_marginal_relevance_search_by_vector.<locals>.<listcomp>  s#    <<<S\$*+<<<rD   )r   r   c                     g | ]
}|         S rN   rN   )rO   r   r   s     r   rQ   zEAerospike.max_marginal_relevance_search_by_vector.<locals>.<listcomp>  s    ...AQ...rD   )r:   r   r   nparrayfloat32r   r   )rA   r%   r   r   r   r   r)   rk   mmr_selectedr   r   s   `         @r   'max_marginal_relevance_search_by_vectorz1Aerospike.max_marginal_relevance_search_by_vector  s	   <  	?T-]BB!-.>M/t/
'!	
 

 
 
 2Hi[
333<<<<t<<<#	
 
 
  	7T->>! 7 7Q $$T%56666........rD   c                T    |                      |          } | j        ||||f||d|S )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.
            index_name: Name of the index to search.
        Returns:
            List of Documents selected by maximal marginal relevance.
        )r   r)   )rY   r   )	rA   r   r   r   r   r   r)   rk   r%   s	            r   max_marginal_relevance_searchz'Aerospike.max_marginal_relevance_search  sV    6 %%e,,	;t;	

 (!
 
 
 
 	
rD   testr   embeddings_chunk_sizeclient_kwargsOptional[dict]c
                F     | |||fi |
} |j         |f||||d|	pi  |S )a  
        This is a user friendly interface that:
            1. Embeds text.
            2. Converts the texts into documents.
            3. Adds the documents to a provided Aerospike index

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

        Example:
            .. code-block:: python

                from langchain_community.vectorstores import Aerospike
                from langchain_openai import OpenAIEmbeddings
                from aerospike_vector_search import Client, HostPort

                client = Client(seeds=HostPort(host="localhost", port=5000))
                aerospike = Aerospike.from_texts(
                    ["foo", "bar", "baz"],
                    embedder,
                    client,
                    "namespace",
                    index_name="index",
                    vector_key="vector",
                    distance_strategy=MODEL_DISTANCE_CALC,
                )
        )rc   re   r)   rg   )r   )clsrI   r%   rc   r$   r'   r)   re   r   r   rk   rB   s               r   
from_textszAerospike.from_texts  sw    P C
 
 	
 
	 			
!!6	
 	
 "	
 	
 	
 rD   )r$   r   r%   r&   r'   r(   r)   r*   r+   r(   r,   r(   r-   r(   r.   r*   r/   r0   )r   rE   )rI   rJ   r   rK   )rU   r(   r   rV   )r/   rZ   r   r   )NNNrb   NT)rI   rJ   rc   rd   re   rf   r.   r*   rg   rh   r)   r*   ri   rj   rk   r   r   rl   )NN)re   rf   r.   r*   rk   r   r   r   )r   NN)r   r(   r   rh   r   rf   r)   r*   rk   r   r   r   )r%   rV   r   rh   r   rf   r)   r*   rk   r   r   r   )r%   rV   r   rh   r   rf   r)   r*   rk   r   r   r   )r   r(   r   rh   r   rf   r)   r*   rk   r   r   r   )r   r   )r   r   r   r   )r   r   r   NN)r%   rV   r   rh   r   rh   r   r   r   rf   r)   r*   rk   r   r   r   )r   r(   r   rh   r   rh   r   r   r   rf   r)   r*   rk   r   r   r   )NNr   NNrb   N)rI   rl   r%   r   rc   rd   r$   r   r'   r(   r)   r*   re   rf   r   rh   r   r   rk   r   r   r   )__name__
__module____qualname____doc__r   ra   rC   propertyrH   rT   rY   staticmethodr?   r   r   r   r   r   r   r   r   r   r   classmethodr   rN   rD   r   r   r   1   sl         %)#"& /4T 4T 4T 4T 4Tl    X3 3 3 3% % % % 
 
 
 \
: +/#'"&$($(#N N N N Nd $("&    F -1$(
 
 
 
 
D -1$(: : : : :~ -1$( 
  
  
  
  
J -1$(3 3 3 3 34   4    \  -1$(3/ 3/ 3/ 3/ 3/p  -1$($
 $
 $
 $
 $
L 
 +/$(#'%)(,6 6 6 6 [6 6 6rD   )r   r   )%
__future__r   loggingro   r3   typingr   r   r   r   r   r	   r
   r   r   numpyr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   r   r   r   r]   r   r   	getLoggerr   r   r   r   r   rN   rD   r   <module>r      s   " " " " " "   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3       
  M......LLLLLLLL		8	$	$    wv[)))e e e e e e e e e erD   