
    dhR                       S SK Jr  S SKrS SKrS SKrS SK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Jr  \(       a  S SKJr  S S	KJrJr  \R>                  " \ 5      r!SS
 jr"\" SSS9r# " S S\5      r$g)    )annotationsN)	TYPE_CHECKINGAnyCallableIterableListOptionalTupleTypeVarUnion)Document)
Embeddings)VectorStore)DistanceStrategymaximal_marginal_relevanceClient)NeighborVectorDistanceMetricc                 P     SSK Jn   U $ ! [         a  n[        S5      UeS nAff = f)Nr   r   zoCould not import aerospike_vector_search python package. Please install it with `pip install aerospike_vector`.)aerospike_vector_searchr   ImportError)r   es     b/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/aerospike.py_import_aerospiker   #   s;    2 M  E
 	s   
 
% %AVST	Aerospike)boundc                     \ rS rSrSrSSSSS\R                  4                 SS jjr\SS j5       r	SS	 jr
SS
 jr\    SS j5       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 jr\S'S j5       r     S(               S)S jjr     S(               S*S jjr\       S+                     S,S jj5       rSrg)-r   1   zm`Aerospike` vector store.

To use, you should have the ``aerospike_vector_search`` python package installed.
N_vector_text_idc
                H   [        5       n
[        U[        5      (       d  [        R                  " S5        [        X5      (       d  [        S[        U5       35      eXl        X l        X`l	        XPl
        Xpl        X@l        X0l        Xl        U R                  U	5      U l        g)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)selfclient	embedding	namespace
index_name
vector_keytext_keyid_keyset_namedistance_strategy	aerospikes              r   __init__Aerospike.__init__7   s    < &'	)Z00MM.
 &,,F|n& 
 #!%%#!"&"@"@AR"S    c                Z    [        U R                  [        5      (       a  U R                  $ g)z/Access the query embedding object if available.N)r%   r+   r   r4   s    r   
embeddingsAerospike.embeddingsm   s"     dooz22??"rA   c                    [        U R                  [        5      (       a$  U R                  R                  [	        U5      5      $ U Vs/ sH  o R                  U5      PM     sn$ s  snf )zEmbed search docs.)r%   r+   r   embed_documentslist)r4   textsts      r   _embed_documentsAerospike._embed_documentst   sK    dooz22??224;??,12Eq"E222s   A$c                    [        U R                  [        5      (       a  U R                  R                  U5      $ U R                  U5      $ )zEmbed query text.)r%   r+   r   embed_query)r4   texts     r   _embed_queryAerospike._embed_queryz   s7    dooz22??..t44t$$rA   c                   SSK Jn  [        U [        5      (       a  U $ XR                  :X  a  [        R                  $ XR
                  :X  a  [        R
                  $ XR                  :X  a  [        R                  $ [        S5      e)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   r%   r   COSINEDOT_PRODUCTSQUARED_EUCLIDEANEUCLIDEAN_DISTANCEr(   )r=   r   s     r   r2   #Aerospike.convert_distance_strategy   su     	G')9::$$ ; ;;#*** @ @@#/// F FF#666R
 	
rA   c           
     n   Uc  U R                   nUc  U R                  nU(       a  Uc  [        S5      e[        U5      nU=(       d.    U V	s/ sH!  n	[	        [
        R                  " 5       5      PM#     sn	nU(       a  U V
s/ sH  oR                  5       PM     nn
OU=(       d    U V	s/ sH  n	0 PM     sn	n[        S[        U5      U5       H  nXX-    nX;X-    nX+X-    nU R                  U5      n[        XU5       H%  u  nnnUUU R                  '   UUU R                  '   M'     [        X5       H@  u  nnUUU R                  '   U R                  R                   " SU R"                  UUUS.UD6  MB     M     U(       a$  U R                  R%                  U R"                  US9  U$ s  sn	f s  sn
f s  sn	f )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 metadata 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.

z6if wait_for_index is True, index_name must be providedr   )r7   keyr<   record_data)r7   name )r1   r/   r(   rH   struuiduuid4copyrangelenrK   zipr-   r,   r.   r*   upsertr0   wait_for_index_completion)r4   rI   	metadatasidsr<   embedding_chunk_sizer8   wait_for_indexkwargs_michunk_texts	chunk_idschunk_metadatasrD   metadatar6   rO   ids                       r   	add_textsAerospike.add_texts   s   @ ~~H))Jj0UVVU77Ac$**,'7 +459a9I5I!8%%8%Qb%%8Iq#e*&:;AA$<=K 89I'A,DEO..{;J-0[.))T .7))*+/(	. !$I ?H)+&## "oo% (	
  !@ <, LL22// 3 
 
I 8 6%8s   'F(F-)F2c                    SSK Jn  U(       a4  U H.  n U R                  R                  " SU R                  UUS.UD6  M0     g! U a       gf = f)zDelete 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)r7   r[   r<   FTr^   )r   rx   r*   deleter0   )r4   ri   r<   rl   rx   rt   s         r   ry   Aerospike.delete   s`      	;!LL'' "&//!) !	   & ! !s   *AAAc                N    U R                   " U R                  U5      4UUUS.UD6$ )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.
kmetadata_keysr8   )&similarity_search_by_vector_with_scorerP   )r4   queryr}   r~   r8   rl   s         r   similarity_search_with_score&Aerospike.similarity_search_with_score  s=    . ::e$
'!	

 
 	
rA   c           	         / nU(       a   U R                   U;  a  U R                   /U-   nUc  U R                  nUc  [        S5      eU R                  R                  " SUU R
                  UUUS.UD6nU H  nUR                  n	U R                   U	;   aC  U	R                  U R                   5      n
UR                  nUR                  [        XS9U45        Mb  [        R                  SU R                    S35        M     U$ )aS  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.

zindex_name must be provided)r8   r7   r   limitfield_names)page_contentrs   zFound document with no `z` key. Skipping.r^   )r,   r/   r(   r*   vector_searchr0   fieldspopdistanceappendr   loggerwarning)r4   r6   r}   r~   r8   rl   docsresultsresultrs   rO   scores               r   r   0Aerospike.similarity_search_by_vector_with_score*  s    2 T^^=@!^^,}<M))J:;;"&,,"<"< #
!oo%#
 #
 F}}H~~)||DNN3X4KUST.t~~.>>NO   rA   c                d    U R                   " U4UUUS.UD6 VVs/ sH  u  pgU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.
    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.
r|   )r   )r4   r6   r}   r~   r8   rl   docrm   s           r   similarity_search_by_vector%Aerospike.similarity_search_by_vectorf  sU    2 EE+%	
 	
 	
 		
 	
s   ,c                f    U R                   " U4X#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 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|   )r   )	r4   r   r}   r~   r8   rl   docs_and_scoresr   rm   s	            r   similarity_searchAerospike.similarity_search  sE    * ;;


NT
 #22//222s   -c                   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.

0 is dissimilar, 1 is similar.

Aerospike's relevance_fn assume euclidean and dot product embeddings are
normalized to unit norm.
rS   )	r3   r   rU   _cosine_relevance_score_fnrV   %_max_inner_product_relevance_score_fnrX   _euclidean_relevance_score_fnr(   rC   s    r   _select_relevance_score_fn$Aerospike._select_relevance_score_fn  sw     ""&6&=&==222$$(8(D(DD===$$(8(K(KK555V rA   c                    SU S-  -
  $ )zWAerospike returns cosine distance scores between [0,2]

0 is dissimilar, 1 is similar.
      r^   )r   s    r   r   $Aerospike._cosine_relevance_score_fn  s     EAIrA   c                   U(       a   U R                   U;  a  U R                   /U-   nU R                  " U4UUUS.UD6n[        [        R                  " U/[        R
                  S9U V	s/ sH  oR                  U R                      PM     sn	UUS9n
U(       a@  U R                   U;   a0  U
 H*  nX   R                  R                  U R                   5        M,     U
 Vs/ sH  oU   PM	     sn$ s  sn	f s  snf )aY  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|   )dtype)r}   lambda_mult)r-   r   r   nparrayfloat32rs   r   )r4   r6   r}   fetch_kr   r~   r8   rl   r   r   mmr_selectedro   s               r   'max_marginal_relevance_search_by_vector1Aerospike.max_marginal_relevance_search_by_vector  s    < T--]B!--.>M//
'!	

 
 2HHi[

37;<t\\$**+t<#	
 T-->!  $$T%5%56 " "..AQ.. = /s   +!C.
C3c                V    U R                  U5      nU R                  " UUUU4UUS.UD6$ )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~   r8   )rP   r   )	r4   r   r}   r   r   r~   r8   rl   r6   s	            r   max_marginal_relevance_search'Aerospike.max_marginal_relevance_search  sJ    6 %%e,	;;	

 (!
 
 	
rA   c
                ^    U " UUU40 U
D6nUR                   " U4UUUUS.U	=(       d    0 D6  U$ )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,
        )
)rh   ri   r8   rj   )ru   )clsrI   r6   rh   r5   r7   r8   ri   embeddings_chunk_sizeclient_kwargsrl   r>   s               r   
from_textsAerospike.from_texts  s]    P 
 	
	 		
!!6	
 "	
 rA   )	r*   r3   r+   r.   r/   r0   r1   r,   r-   )r5   r   r6   zUnion[Embeddings, Callable]r7   r_   r8   Optional[str]r9   r_   r:   r_   r;   r_   r<   r   r=   z7Optional[Union[DistanceStrategy, VectorDistanceMetric]])returnzOptional[Embeddings])rI   Iterable[str]r   zList[List[float]])rO   r_   r   List[float])r=   z-Union[VectorDistanceMetric, DistanceStrategy]r   r   )NNN  NT)rI   r   rh   Optional[List[dict]]ri   Optional[List[str]]r<   r   rj   intr8   r   rk   boolrl   r   r   	List[str])NN)ri   r   r<   r   rl   r   r   zOptional[bool])   NN)r   r_   r}   r   r~   r   r8   r   rl   r   r   List[Tuple[Document, float]])r6   r   r}   r   r~   r   r8   r   rl   r   r   r   )r6   r   r}   r   r~   r   r8   r   rl   r   r   List[Document])r   r_   r}   r   r~   r   r8   r   rl   r   r   r   )r   zCallable[[float], float])r   floatr   r   )r      g      ?NN)r6   r   r}   r   r   r   r   r   r~   r   r8   r   rl   r   r   r   )r   r_   r}   r   r   r   r   r   r~   r   r8   r   rl   r   r   r   )NNtestNNr   N)rI   r   r6   r   rh   r   r5   r   r7   r_   r8   r   ri   r   r   r   r   zOptional[dict]rl   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   rX   r?   propertyrD   rK   rP   staticmethodr2   ru   ry   r   r   r   r   r   r   r   r   classmethodr   __static_attributes__r^   rA   r   r   r   1   s"    %)#"& //4T4T /4T 	4T
 "4T 4T 4T 4T  4T
4Tl  3% 
H
	
 
: +/#'"&$($(#NN (N !	N
  N "N "N N N 
Nd $("&    	
 
F -1$(

 
 +	

 "
 
 
&
D -1$(:: : +	:
 ": : 
&:~ -1$( 
 
  
 +	 

 " 
  
 
 
J -1$(33 3 +	3
 "3 3 
342    -1$(3/3/ 3/ 	3/
 3/ +3/ "3/ 3/ 
3/p  -1$($
$
 $
 	$

 $
 +$
 "$
 $
 
$
L 
 +/$(#'%)(,66 6 (	6
 6 6 "6 !6  #6 &6 6 
6 6rA   )r   r   )%
__future__r   loggingr`   r&   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   rT   r   r   	getLoggerr   r   r   r   r   r^   rA   r   <module>r      so    "   
 
 
  - 0 3
 .L			8	$ v[)d drA   