
    dh|a                        S SK Jr  S SK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  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	rSS
 jrSS jr " S S\5      rg)    )annotationsN)AnyCallableDictIterableListOptionalType)Document)
Embeddingsguard_import)VectorStore)maximal_marginal_relevance   c                     [        S5      $ )zImport lancedb package.lancedbr        `/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/lancedb.pyimport_lancedbr      s    	""r   c           
         SR                  U R                  5        VVs/ sH  u  pU SU S3PM     snn5      $ s  snnf )z2Converts a dict filter to a LanceDB filter string.z AND z = '')joinitems)filterkvs      r   to_lance_filterr      s7    <<FLLNCNDAA3d1#QNCDDCs   :
c                  &   \ rS rSrSrSSSSSSSSSS	SS
SS\4                             SS jjrSS S j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 jr  S"         S)S jj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/             S0S jjr    S1             S2S jjr    S1             S3S jjr\            S4                                 S5S jj5       r     S6             S7S jjrSrg)8LanceDB   a)  `LanceDB` vector store.

To use, you should have ``lancedb`` python package installed.
You can install it with ``pip install lancedb``.

Args:
    connection: LanceDB connection to use. If not provided, a new connection
                will be created.
    embedding: Embedding to use for the vectorstore.
    vector_key: Key to use for the vector in the database. Defaults to ``vector``.
    id_key: Key to use for the id in the database. Defaults to ``id``.
    text_key: Key to use for the text in the database. Defaults to ``text``.
    table_name: Name of the table to use. Defaults to ``vectorstore``.
    api_key: API key to use for LanceDB cloud database.
    region: Region to use for LanceDB cloud database.
    mode: Mode to use for adding data to the table. Valid values are
          ``append`` and ``overwrite``. Defaults to ``overwrite``.



Example:
    .. code-block:: python
        vectorstore = LanceDB(uri='/lancedb', embedding_function)
        vectorstore.add_texts(['text1', 'text2'])
        result = vectorstore.similarity_search('text1')
Nz/tmp/lancedbvectoridtextvectorstore	overwritel2c                   [        S5      n[        S5      UR                  l        X l        X@l        XPl        X`l        US:w  a  U=(       d    [        R                  " S5      OSU l	        Xl
        Xl        Xl        Xl        Xl        SU l        [!        UUR"                  R$                  5      (       a  Xl        OUc  SU l        O[)        S5      e[!        U[*        5      (       a.  U R                  c!  UR-                  S5      (       a  [)        S5      eU R                  c  [)        S	5      e[!        UUR.                  R0                  5      (       a  Xl        O[!        U[*        UR.                  R4                  45      (       a  [)        S
5      eU R                  c  UR7                  U5      U l        O[!        U[*        5      (       al  UR-                  S5      (       a*  UR7                  X0R                  U R                  S9U l        O,UR7                  U5      U l        [8        R:                  " S5        Ubs   [!        UUR.                  R4                  UR                  R                  R<                  45      (       d   eXl        [A        US5      (       a  URB                  OSU l"        gU RI                  USS9U l        g! [F         a    [)        S5      ef = f)z$Initialize with Lance DB vectorstorer   zlancedb.remote.table LANCE_API_KEYNz9`reranker` has to be a lancedb.rerankers.Reranker object.zdb://z&API key is required for LanceDB cloud.z#embedding object should be providedzs`connection` has to be a lancedb.db.LanceDBConnection object.                `lancedb.db.LanceTable` is deprecated.)api_keyregionz[api key provided with local uri.                            The data will be stored locallynameremote_tablezj`table` has to be a lancedb.db.LanceTable or 
                    lancedb.remote.table.RemoteTable object.T)set_default)%r   remotetable
_embedding_vector_key_id_key	_text_keyosgetenvr,   r-   modedistanceoverride_relevance_score_fnlimit
_fts_index
isinstance	rerankersReranker	_reranker
ValueErrorstr
startswithdbLanceDBConnection_connection
LanceTableconnectwarningswarnRemoteTable_tablehasattrr.   _table_nameAssertionError	get_table)self
connection	embeddinguri
vector_keyid_keytext_key
table_namer,   r-   r9   r2   r:   rerankerrelevance_score_fnr<   r   s                    r   __init__LanceDB.__init__:   sj   & y)+,BC#%!@G2w<"))O"<SW	 +=(
h 1 1 : :;;%N!DNK  c3DLL$8~~g&& !IJJ??"BCCj'**">">??)
S'***?*?$@AA8 
 ||##*??3#7 c3''~~g..+2??dkk ,; ,( ,3??3+?( = !GJJ117>>3G3G3S3ST    $")%"8"8EJJn   ...FDK "  @ s   7A1J? ?Kc                j   UR                   R                  nSU;   a  SnOSU;   a  SnOS nSU;   nUb  U(       df  [        [        U5      5       Vs/ sHG  n[	        XR
                     U   R                  5       U(       a  US   U   R                  5       O0 S9PMI     sn$ U(       a  U(       a{  [        [        U5      5       Vs/ sH\  n[	        XR
                     U   R                  5       U(       a  US   U   R                  5       O0 S9X   U   R                  5       4PM^     sn$ g g s  snf s  snf )N	_distance_relevance_scoremetadata)page_contentra   )schemanamesrangelenr   r6   as_py)rR   resultsscorecolumns	score_colhas_metadataidxs          r   results_to_docsLanceDB.results_to_docs   sF   ..&&'!#I7**II!W,E !W.
 /C	 !(!8!=!C!C!EAMWZ05;;=SU /  5 !W. /C %,^^%<S%A%G%G%I' ")!4S!9!?!?!A	 &s+113 /  !Ys   AD+A"D0c                    U R                   $ )N)r3   rR   s    r   
embeddingsLanceDB.embeddings   s    r   c                   / nU=(       d.    U Vs/ sH!  n[        [        R                  " 5       5      PM#     snnU R                  R	                  [        U5      5      n[        U5       HS  u  pXx   n
U(       a  X(   OSX8   0nUR                  U R                  U
U R                  X8   U R                  U	SU05        MU     U R                  5       nUc+  U R                  R                  U R                  US9nXl        O8U R                   c  UR#                  XPR$                  S9  OUR#                  U5        SU l        U$ s  snf )a_  Turn texts into embedding and add it to the database

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.
    ids: Optional list of ids to associate with the texts.

Returns:
    List of ids of the added texts.
r$   ra   Ndata)r9   )rC   uuiduuid4r3   embed_documentslist	enumerateappendr4   r5   r6   rQ   rG   create_tablerO   rM   r,   addr9   r=   )rR   texts	metadatasidskwargsdocs_rr   rm   r%   rT   ra   tbls                r   	add_textsLanceDB.add_texts   s   $ 77Ac$**,'7__44T%[A
"5)IC"I)2y~sx8HHKK$$iLL#(NND	 * nn;""//0@0@t/LCK||#99-
7 8s   'D?c                    Ub  U(       a  Xl         U R                   nOUnOU R                   n U R                  R                  U5      $ ! [         a     gf = f)a  
Fetches a table object from the database.

Args:
    name (str, optional): The name of the table to fetch. Defaults to None
                            and fetches current table object.
    set_default (bool, optional): Sets fetched table as the default table.
                                Defaults to False.

Returns:
    Any: The fetched table object.

Raises:
    ValueError: If the specified table is not found in the database.

N)rO   rG   
open_table	Exception)rR   r.   r0   _names       r   rQ   LanceDB.get_table   s\    & #' (($$E	##..u55 		s   A	 	
AAc                    U R                  U5      nU(       a  UR                  UUUUUS9  gU(       a  UR                  U5        g[        S5      e)a  
Create a scalar(for non-vector cols) or a vector index on a table.
Make sure your vector column has enough data before creating an index on it.

Args:
    vector_col: Provide if you want to create index on a vector column.
    col_name: Provide if you want to create index on a non-vector column.
    metric: Provide the metric to use for vector index. Defaults to 'L2'
            choice of metrics: 'L2', 'dot', 'cosine'
    num_partitions: Number of partitions to use for the index. Defaults to 256.
    num_sub_vectors: Number of sub-vectors to use for the index. Defaults to 96.
    index_cache_size: Size of the index cache. Defaults to None.
    name: Name of the table to create index on. Defaults to None.

Returns:
    None
)metricvector_column_namenum_partitionsnum_sub_vectorsindex_cache_sizez%Provide either vector_col or col_nameN)rQ   create_indexcreate_scalar_indexrB   )	rR   col_name
vector_colr   r   r   r   r.   r   s	            r   r   LanceDB.create_index	  sV    6 nnT"#-- /!1   ##H-DEEr   c                    [        US5       n[        R                  " UR                  5       5      R	                  S5      sSSS5        $ ! , (       d  f       g= f)z!Get base64 string from image URI.rbzutf-8N)openbase64	b64encodereaddecode)rR   rU   
image_files      r   encode_imageLanceDB.encode_image3  s7    #t_
##JOO$56==gF __s   3A


Ac                   U R                  5       nU Vs/ sH  o`R                  US9PM     nnUc-  U Vs/ sH!  n[        [        R                  " 5       5      PM#     nnSn	U R
                  b5  [        U R
                  S5      (       a  U R
                  R                  US9n	O[        S5      e/ n
[        U	5       HQ  u  pU(       a  X+   OSX;   0nU
R                  U R                  UU R                  X;   U R                  X{   SU05        MS     Uc,  U R                  R                  U R                   U
S9nXPl        U$ UR%                  U
5        U$ s  snf s  snf )	a;  Run more images through the embeddings and add to the vectorstore.

Args:
    uris List[str]: File path to the image.
    metadatas (Optional[List[dict]], optional): Optional list of metadatas.
    ids (Optional[List[str]], optional): Optional list of IDs.

Returns:
    List[str]: List of IDs of the added images.
)rU   Nembed_image)uriszEembedding object should be provided and must have embed_image method.r$   ra   ru   )rQ   r   rC   rw   rx   r3   rN   r   rB   r{   r|   r4   r5   r6   rG   r}   rO   rM   r~   )rR   r   r   r   r   r   rU   	b64_textsr   rr   rv   rm   embra   s                 r   
add_imagesLanceDB.add_images8  sO   " nn <@@4C&&3&/4	@;.23d3tzz|$dC3
??&74??M+R+R44$4?JW  !*-HC)2y~sx8HHKK$$cLL#(NNIN	 . ;""//0@0@t/LCK 
 GGDM
= A 4s
   E'Ec                   Uc  U R                   nU R                  U5      n[        U[        5      (       a  [	        U5      nUR                  SS5      nUR                  SS5      nUR                  S5      =n	(       aE  UR                  XR                  S9R                  U5      R                  U	5      R                  X7S9n
O5UR                  XR                  S9R                  U5      R                  X7S9n
US:X  a&  U R                  b  U
R                  U R                  S	9  U
R                  5       n[        U5      S
:X  a  [        R                  " S5        U$ )N	prefilterF
query_typer#   metrics)queryr   )r   hybrid)rZ   r   zNo results found for the query.)r<   rQ   r>   dictr   getsearchr4   r   whererA   rerankto_arrowrf   rJ   rK   )rR   r   r   r   r.   r   r   r   r   r   lance_queryr   s               r   _queryLanceDB._queryl  s%    9

AnnT"fd##$V,FJJ{E2	ZZh7
jj++7+

;K;K
Lqv3	  

;K;K
Lqv3 
 !dnn&@7##%t9>MM;<r   c                   U R                   (       a  U R                   $ U R                  S:X  a  U R                  $ U R                  S:X  a  U R                  $ U R                  S:X  a  U R                  $ [        SU R                   S3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.
cosiner(   ipzANo supported normalization function for distance metric of type: z=.Consider providing relevance_score_fn to Chroma constructor.)r;   r:   _cosine_relevance_score_fn_euclidean_relevance_score_fn%_max_inner_product_relevance_score_fnrB   rq   s    r   _select_relevance_score_fn"LanceDB._select_relevance_score_fn  s     ++333==H$222]]d"555]]d"===115 @OO r   c                    Uc  U R                   nU R                  " X4X4S.UD6nU R                  XeR                  SS5      S9$ )z4
Return documents most similar to the query vector.
r   r.   ri   Fri   )r<   r   rn   pop)rR   rT   r   r   r.   r   ress          r   similarity_search_by_vector#LanceDB.similarity_search_by_vector  sI     9

Akk)KvKFK##Czz'5/I#JJr   c           
         Uc  U R                   nU R                  5       nU R                  " X4SS0UD6nU VV	s/ sH  u  pX" [        U	5      5      4PM     sn	n$ s  sn	nf )zJ
Return documents most similar to the query vector with relevance scores.
ri   T)r<   r   r   float)
rR   rT   r   r   r.   r   r[   docs_and_scoresdocri   s
             r   1similarity_search_by_vector_with_relevance_scores9LanceDB.similarity_search_by_vector_with_relevance_scores  sz     9

A!<<>::
 $
(.
 GV
FU
S$U5\23o
 	
 
s   Ac                   Uc  U R                   nUR                  SS5      nUR                  SS5      nUR                  SS5      nU R                  c  [        S5      eUS:X  d  US	:X  a  U R                  c  U R
                  c{  U R                  U5      nUR                  U R                  SS
9U l        US	:X  a  U R                  R                  U5      n	X4n
OUn
U R                  " X4X6S.UD6nU R                  XS9$ [        S5      eU R                  R                  U5      n	U R                  " X4SU0UD6nU R                  XS9$ )zAReturn documents most similar to the query with relevance scores.Nri   Tr.   r   r#   z4search needs an emmbedding function to be specified.ftsr   )replacer   r   z?Full text/ Hybrid search is not supported in LanceDB Cloud yet.r   )r<   r   r3   rB   r,   r=   rQ   create_fts_indexr6   embed_queryr   rn   NotImplementedError)rR   r   r   r   r   ri   r.   r   r   rT   r   r   s               r   similarity_search_with_score$LanceDB.similarity_search_with_score  sJ    9

A

7D)zz&$'ZZh7
??"STT*"8||#(?nnT*"%"6"6t~~t"6"T) $ ; ;E BI'/F"Fkk&PFPP++C+==)U  33E:I++iD6DVDC'''99r   c           
     4    U R                   " SXX4USS.UD6nU$ )a  Return documents most similar to the query

Args:
    query: String to query the vectorstore with.
    k: Number of documents to return.
    filter (Optional[Dict]): Optional filter arguments
        sql_filter(Optional[string]): SQL filter to apply to the query.
        prefilter(Optional[bool]): Whether to apply the filter prior
                                     to the vector search.
Raises:
    ValueError: If the specified table is not found in the database.

Returns:
    List of documents most similar to the query.
F)r   r   r.   r   r   ri   r   )r   )rR   r   r   r.   r   r   r   r   s           r   similarity_searchLanceDB.similarity_search  s1    0 // 
4Cu
PV
 
r   c                    Uc  U R                   nU R                  c  [        S5      eU R                  R                  U5      nU R	                  UUUUUS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.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List of Documents selected by maximal marginal relevance.
zBFor MMR search, you must specify an embedding function oncreation.)lambda_multr   )r<   r3   rB   r   'max_marginal_relevance_search_by_vector)	rR   r   r   fetch_kr   r   r   rT   r   s	            r   max_marginal_relevance_search%LanceDB.max_marginal_relevance_search  sm    4 9

A??"T  OO//6	;;# < 
 r   c                L   U R                   " SUUUS.UD6n[        [        R                  " U[        R                  S9US   R                  5       U=(       d    U R                  US9nU R                  U5      n	[        U	5       V
Vs/ sH  u  pX;   d  M  UPM     nn
nU$ 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 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.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List of Documents selected by maximal marginal relevance.
)r   r   r   )dtyper#   )r   r   r   )	r   r   nparrayfloat32	to_pylistr<   rn   r{   )rR   rT   r   r   r   r   r   rh   mmr_selected
candidatesirselected_resultss                r   r   /LanceDB.max_marginal_relevance_search_by_vectorC  s    6 ++ 

 	
 2HHYbjj1H'')o4::#	
 ))'2
*3J*?U*?$!1CTA*?U Vs   
B B c                T    [        SUUUUUUU	U
UUUUS.UD6nUR                  XS9  U$ )N)rS   rT   rV   rW   rX   rY   r,   r-   r9   r:   rZ   r[   )r   r   )r!   r   )clsr   rT   r   rS   rV   rW   rX   rY   r,   r-   r9   r:   rZ   r[   r   instances                    r   
from_textsLanceDB.from_textsp  sY    &  
!!!1
 
 	56r   c                   U R                  U5      nU(       a  UR                  U5        gU(       a=  UR                  U R                   S3R                  SR	                  U5      5      5        gU(       a*  U R
                  b  [        S5      eUR                  U5        gU(       a  UR                  S5        g[        S5      e)aP  
Allows deleting rows by filtering, by ids or drop columns from the table.

Args:
    filter: Provide a string SQL expression -  "{col} {operation} {value}".
    ids: Provide list of ids to delete from the table.
    drop_columns: Provide list of columns to drop from the table.
    delete_all: If True, delete all rows from the table.
z
 in ('{}'),Nz;Column operations currently not supported in LanceDB Cloud.truez6Provide either filter, ids, drop_columns or delete_all)	rQ   deleter5   formatr   r,   r   drop_columnsrB   )rR   r   
delete_allr   r   r.   r   r   s           r   r   LanceDB.delete  s    $ nnT"JJvJJ$,,|4;;CHHSMJK||')Q    .JJvUVVr   )rG   r3   r=   r5   rA   rM   rO   r6   r4   r,   r:   r<   r9   r;   r-   )rS   Optional[Any]rT   Optional[Embeddings]rU   Optional[str]rV   r   rW   r   rX   r   rY   r   r,   r   r-   r   r9   r   r2   r   r:   r   rZ   r   r[   "Optional[Callable[[float], float]]r<   int)F)rh   r   ri   boolreturnr   )r   r   )NN)
r   zIterable[str]r   Optional[List[dict]]r   Optional[List[str]]r   r   r   	List[str])NF)r.   r   r0   Optional[bool]r   r   )NN   `   NL2N)r   r   r   r   r   Optional[int]r   r  r   r  r   r   r.   r   r   None)rU   rC   r   rC   )
r   r   r   r   r   r   r   r   r   r   )NNN)r   r   r   r  r   r   r.   r   r   r   r   r   )r   zCallable[[float], float])rT   List[float]r   r  r   Optional[Dict[str, str]]r.   r   r   r   r   r   )
r   rC   r   r  r   r  r   r   r   r   )NNNF)r   rC   r   r  r.   r   r   r   r   r  r   r   r   List[Document])N   g      ?N)r   rC   r   r  r   r   r   r   r   r  r   r   r   r	  )rT   r  r   r  r   r   r   r   r   r  r   r   r   r	  )NNr#   r$   r%   r&   NNr'   r(   NN)"r   zType[LanceDB]r   r   rT   r   r   r   rS   r   rV   r   rW   r   rX   r   rY   r   r,   r   r-   r   r9   r   r:   r   rZ   r   r[   r   r   r   r   r!   )NNNNN)r   r   r   r  r   r   r   r   r.   r   r   r   r   r  )__name__
__module____qualname____firstlineno____doc__	DEFAULT_Kr\   rn   propertyrr   r   rQ   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   __static_attributes__r   r   r   r!   r!      s   : %)*.+$, $"($1!% $)#"&"&AE!VG!VG (VG 	VG
 "VG VG  VG "VG VG VG VG VG  VG  VG ?VG  !VGp D   +/#'	.. (. !	.
 . 
.b IN!7E	F #'$((+)+*. $"(F(F "(F &	(F
 '(F ((F (F (F 
(FTG +/#'	22 (2 !	2
 2 
2n   $"$$ $ 	$
 $ $ 
$L8  +/"KK K )	K
 K K 
K(  +/"

 
 )	

 
 
 

2  +/	&:&: &: )	&:
 &: 
&:V  " $#  	
    
@   +/** * 	*
 * )* * 
*^   +/+ +  +  	+ 
 +  )+  +  
+ Z 
 +/$($, $"($1!% $)"&"&AE### # (	#
 "# "# #  # "# # # #  #  # ?#  !#" 
## #N $(%) $,0"!W !W #!W 	!W
 *!W !W !W 
!W !Wr   r!   )r   r   )r   zDict[str, str]r   rC   )
__future__r   r   r7   rw   rJ   typingr   r   r   r   r   r	   r
   numpyr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   r  r   r   r!   r   r   r   <module>r     sO    "  	   F F F  - 0 - 3 M	#
E
Y
Wk Y
Wr   