
    dhm#                        S SK Jr  S SKrS SKrS SKJrJrJrJrJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr  \R$                  " \5      r " S S	\5      rg)
    )annotationsN)AnyIterableListOptionalTuple)Document)
Embeddings)VectorStore)DistanceStrategyc                  r   \ rS rSrSr\R                  4     SS jjr\SS j5       r	SS 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/ 4         SS jjrS/ S.         SS jjrS/ 4         SS jjr\ S           SS jj5       rSrg) KDBAI   a,  `KDB.AI` vector store.

See https://kdb.ai.

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

Args:
    table: kdbai_client.Table object to use as storage,
    embedding: Any embedding function implementing
        `langchain.embeddings.base.Embeddings` interface,
    distance_strategy: One option from DistanceStrategy.EUCLIDEAN_DISTANCE,
        DistanceStrategy.DOT_PRODUCT or DistanceStrategy.COSINE.

See the example [notebook](https://github.com/KxSystems/langchain/blob/KDB.AI/docs/docs/integrations/vectorstores/kdbai.ipynb).
c                d     SS K nXl        X l        X0l        g ! [         a    [        S5      ef = f)Nr   z`Could not import kdbai_client python package. Please install it with `pip install kdbai_client`.)kdbai_clientImportError_table
_embeddingdistance_strategy)selftable	embeddingr   r   s        ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/kdbai.py__init__KDBAI.__init__!   s@    	 #!2  	E 	s    /c                Z    [        U R                  [        5      (       a  U R                  $ g N)
isinstancer   r
   )r   s    r   
embeddingsKDBAI.embeddings4   s     dooz22??"    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 r   )r   r   r
   embed_documentslist)r   textsts      r   _embed_documentsKDBAI._embed_documents:   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      $ r   )r   r   r
   embed_query)r   texts     r   _embed_queryKDBAI._embed_query?   s7    dooz22??..t44t$$r!   Nc                    SS K n SS KnU R                  R	                  U5      nUR                  5       nX'S'   U Vs/ sH  oR                  S5      PM     snUS'   U V	s/ sH  oR                  U	SS9PM     sn	US	'   Ub  UR                  Xs/S
S9nU R                  R                  USS9  g ! [         a    [        S5      ef = f! [         a    [        S5      ef = fs  snf s  sn	f )Nr   zRCould not import numpy python package. Please install it with `pip install numpy`.TCould not import pandas python package. Please install it with `pip install pandas`.idzutf-8r+   float32)dtyper      )axisF)warn)numpyr   pandasr   r#   	DataFrameencodearrayconcatr   insert)
r   r%   idsmetadatanppdembedsdfr&   es
             r   _insertKDBAI._insertD   s    		 007\\^4167Ahhw'76
BHI&QHHQiH8&I<B>2B2E*+  	> 	  	? 	 8Is!   B. C C !C%.CCc                <    SSK n[        U5      nSnUb.  [        X&R                  5      (       a  UnOUR	                  U5      n/ n[        U5      S-
  U-  S-   n	[        U	5       H  n
X-  nU
S-   U-  nXU nU(       a  X;U nO?[        [        U5      5       Vs/ sH!  n[        [        R                  " 5       5      PM#     nnUb  UR                  X R                  SS9nOSnU R                  XU5        X-   nM     U$ ! [         a    [        S5      ef = fs  snf )a  Run more texts through the embeddings and add to the vectorstore.

Args:
    texts (Iterable[str]): Texts to add to the vectorstore.
    metadatas (Optional[List[dict]]): List of metadata corresponding to each
        chunk of text.
    ids (Optional[List[str]]): List of IDs corresponding to each chunk of text.
    batch_size (Optional[int]): Size of batch of chunks of text to insert at
        once.

Returns:
    List[str]: List of IDs of the added texts.
r   Nr/   r3   T)drop)r7   r   r$   r   r8   lenrangestruuiduuid4ilocreset_indexrD   )r   r%   	metadatasr=   
batch_sizekwargsr@   metadfout_idsnbatchesiistartiendbatch	batch_ids_
batch_metas                    r   	add_textsKDBAI.add_textsc   s.   ,	 U# )\\22"i0JNz1A5xA^FEZ'D&Et,	8=c%j8IJ8I1S.8I	J!#[[5AAtAL
!
LL:6)G ! ;  	? 	, Ks   D  'D Dc                     SSK nU Vs/ sH  oUR                  PM     nnUR                  U Vs/ sH  oUR                  PM     sn5      nU R                  XgUS9$ ! [         a    [        S5      ef = fs  snf s  snf )a  Run more documents through the embeddings and add to the vectorstore.

Args:
    documents (List[Document]: Documents to add to the vectorstore.
    batch_size (Optional[int]): Size of batch of documents to insert at once.

Returns:
    List[str]: List of IDs of the added texts.
r   Nr/   )r>   rP   )r7   r   page_contentr8   r>   r\   )r   	documentsrP   rQ   r@   xr%   r>   s           r   add_documentsKDBAI.add_documents   s~    	 *33A3<<Y ?YY ?@~~e:~NN  	? 	 4 ?s   A A3A8A0r3   c                J    U R                   " U R                  U5      4X#S.UD6$ )a6  Run similarity search with distance from a query string.

Args:
    query (str): Query string.
    k (Optional[int]): number of neighbors to retrieve.
    filter (Optional[List]): KDB.AI metadata filter clause: https://code.kx.com/kdbai/use/filter.html

Returns:
    List[Document]: List of similar documents.
kfilter)&similarity_search_by_vector_with_scorer,   )r   queryrf   rg   rQ   s        r   similarity_search_with_score"KDBAI.similarity_search_with_score   s5    " ::e$
()
<B
 	
r!   re   c                  SU;   a  UR                  S5      nU R                  R                  " S	U/X#S.UD6n/ n[        U[        5      (       a  US   nOU$ UR                  SS9 Hj  nUR                  S5      nUR                  S5      n	UR                  [        UUR                  5        VV
s0 sH  u  p*US:w  d  M  X*_M     sn
nS9U	45        Ml     U$ s  sn
nf )
aH  Return documents most similar to embedding, along with scores.

Args:
    embedding (List[float]): query vector.
    k (Optional[int]): number of neighbors to retrieve.
    filter (Optional[List]): KDB.AI metadata filter clause: https://code.kx.com/kdbai/use/filter.html

Returns:
    List[Document]: List of similar documents.
n)vectorsrm   rg   r   records)orientr+   __nn_distance)r_   r>    )	popr   searchr   r$   to_dictappendr	   items)r   r   rf   rg   rQ   matchesdocsrowr+   scorevs              r   rh   ,KDBAI.similarity_search_by_vector_with_score   s    $ &=

3A++$$Wi[AWPVWgt$$ajGK??)?4C776?DGGO,EKK%)3699;!N;41!v+$!$;!N  5  "Os   /C>Cc                d    U R                   " U4X#S.UD6nU VVs/ sH  u  pgUPM	     snn$ s  snnf )a(  Run similarity search from a query string.

Args:
    query (str): Query string.
    k (Optional[int]): number of neighbors to retrieve.
    filter (Optional[List]): KDB.AI metadata filter clause: https://code.kx.com/kdbai/use/filter.html

Returns:
    List[Document]: List of similar documents.
re   )rj   )r   ri   rf   rg   rQ   docs_and_scoresdocrZ   s           r   similarity_searchKDBAI.similarity_search   sC    " ;;

)/
 #22//222s   ,c                    [        S 5      e)zNot implemented.)	Exception)clsr%   r   rO   rQ   s        r   
from_textsKDBAI.from_texts  s     *++r!   )r   r   r   )r   r   r   r
   r   zOptional[DistanceStrategy])returnzOptional[Embeddings])r%   Iterable[str]r   zList[List[float]])r+   rJ   r   List[float]r   )r%   	List[str]r=   Optional[List[str]]r>   zOptional[Any]r   None)NN    )r%   r   rO   Optional[List[dict]]r=   r   rP   intrQ   r   r   r   )r   )r`   List[Document]rP   r   rQ   r   r   r   )
ri   rJ   rf   r   rg   Optional[List]rQ   r   r   List[Tuple[Document, float]])
r   r   rf   r   rg   r   rQ   r   r   r   )
ri   rJ   rf   r   rg   r   rQ   r   r   r   )r   r   r%   r   r   r
   rO   r   rQ   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   EUCLIDEAN_DISTANCEr   propertyr   r'   r,   rD   r\   rb   rj   rh   r   classmethodr   __static_attributes__rr   r!   r   r   r      s   , //33 3
	3&  
3
% #'	++ !+  	+
 
+D +/#'55 (5 !	5
 5 5 
5p <>O'O58OILO	O8 !#	

 
 	

 
 
&
2 !#&& 	&
 & & 
&&V !#	33 3 	3
 3 
3, 
 +/	,,, , (	,
 , 
, ,r!   r   )
__future__r   loggingrK   typingr   r   r   r   r   langchain_core.documentsr	   langchain_core.embeddingsr
   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   	getLoggerr   loggerr   rr   r!   r   <module>r      s>    "   7 7 - 0 3 C			8	$,K ,r!   