
    dhK                       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	J
r
JrJr  S SK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  \(       a  S SKrSS
 jr  S       SS jjrSS jrSS jr\" SSSS9 " S S\5      5       r g)    )annotationsN)TYPE_CHECKINGAnyCallableDictIterableListOptionalTuple)uuid4)
deprecated)Document)
Embeddings)VectorStore)maximal_marginal_relevancec                    U US/S./S.$ )Ntext)namedataType)class
properties )
index_nametext_keys     a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/weaviate.py_default_schemar      s#     !#H
     c                J    SS K nU =(       d    [        R                  R	                  S5      n U=(       d    [        R                  R	                  S5      nU(       a  UR
                  R                  US9OS nUR                  " SXS.UD6$ ! [         a    [        S5      ef = f)Nr   _Could not import weaviate python  package. Please install it with `pip install weaviate-client`WEAVIATE_URLWEAVIATE_API_KEY)api_key)urlauth_client_secretr   )weaviateImportErrorosenvirongetauth
AuthApiKeyClient)r#   r"   kwargsr%   r*   s        r   _create_weaviate_clientr.   )   s    

 
//C;(:;G8?8==##G#4TD??FsFvFF  
C
 	

s   B B"c                @    SSS[         R                  " U 5      -   -  -
  $ )N   )npexp)vals    r   _default_score_normalizerr4   ;   s    qAsO$$$r   c                d    [        U [        R                  5      (       a  U R                  5       $ U $ N)
isinstancedatetime	isoformat)values    r   _json_serializabler;   ?   s'    %**++  Lr   z0.3.18z1.0z&langchain_weaviate.WeaviateVectorStore)sinceremovalalternative_importc                     \ rS rSrSrSS\S4             SS jjr\SS j5       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SSSSS\S.                         S!S jjj5       rSS"S jjrSrg)#WeaviateE   ab  `Weaviate` vector store.

To use, you should have the ``weaviate-client`` python package installed.

Example:
    .. code-block:: python

        import weaviate
        from langchain_community.vectorstores import Weaviate

        client = weaviate.Client(url=os.environ["WEAVIATE_URL"], ...)
        weaviate = Weaviate(client, index_name, text_key)

NTc                L    SSK n[        XR                  5      (       d  [	        S[        U5       35      eXl        X l        X@l        X0l	        U R                  /U l
        X`l        Xpl        Ub  U R                  R                  U5        gg! [         a    [        S5      ef = f)z Initialize with Weaviate client.r   Nz_Could not import weaviate python package. Please install it with `pip install weaviate-client`.z5client should be an instance of weaviate.Client, got )r%   r&   r7   r,   
ValueErrortype_client_index_name
_embedding	_text_key_query_attrsrelevance_score_fn_by_textextend)	selfclientr   r   	embedding
attributesrJ   by_textr%   s	            r   __init__Weaviate.__init__Z   s    	 &//22GV~V  %#!!^^,"4!$$Z0 "!  	H 	s   B B#c                    U R                   $ r6   )rG   rM   s    r   
embeddingsWeaviate.embeddings|   s    r   c                H    U R                   (       a  U R                   $ [        $ r6   )rJ   r4   rU   s    r   _select_relevance_score_fn#Weaviate._select_relevance_score_fn   s'     && ##	
 +	
r   c                   SSK Jn  / nSnU R                  (       a;  [        U[        5      (       d  [	        U5      nU R                  R                  U5      nU R                  R                   n[        U5       H  u  pU R                  U	0n
Ub(  X(   R                  5        H  u  p[        U5      X'   M     U" [        5       5      nSU;   a	  US   U   nOSU;   a  US   U   nUR                  U
U R                  UU(       a  Xh   OSUR                  S5      S9  UR!                  U5        M     SSS5        U$ ! , (       d  f       U$ = f)z4Upload texts with metadata (properties) to Weaviate.r   get_valid_uuidNuuidsidstenant)data_object
class_nameuuidvectorr`   )weaviate.utilr]   rG   r7   listembed_documentsrE   batch	enumeraterH   itemsr;   r   add_data_objectrF   r)   append)rM   texts	metadatasr-   r]   r_   rV   rh   ir   data_propertieskeyr3   _ids                 r   	add_textsWeaviate.add_texts   s3    	126
??eT**U88?J\\5$U+#'>>4"8($-L$6$6$8/A#/F, %9 %UW-f$ /!,Cf_ -*C%% /#//,6:=D!::h/ &  

3/ ,  2 
3  2 
s   -CD::
E	c                    U R                   (       a  U R                  " X40 UD6$ U R                  c  [        S5      eU R                  R	                  U5      nU R
                  " XB40 UD6$ )Return docs most similar to query.

Args:
    query: Text to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.

Returns:
    List of Documents most similar to the query.
zC_embedding cannot be None for similarity_search when _by_text=False)rK   similarity_search_by_textrG   rC   embed_querysimilarity_search_by_vector)rM   querykr-   rO   s        r   similarity_searchWeaviate.similarity_search   si     ==11%EfEE& %  33E:I33IKFKKr   c                H   SU/0nUR                  S5      (       a  UR                  S5      US'   U R                  R                  R                  U R                  U R                  5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  U5      R                  U5      R                  5       nSU;   a  [        SUS    35      e/ nUS	   S
   U R                      H6  nUR                  U R                  5      n	UR                  [        XS95        M8     U$ )rv   conceptssearch_distance	certaintywhere_filterr`   
additionalerrorsError during query: dataGetpage_contentmetadata)r)   rE   rz   rF   rI   
with_wherewith_tenantwith_additionalwith_near_text
with_limitdorC   poprH   rl   r   )
rM   rz   r{   r-   content	query_objresultdocsresr   s
             r   rw   "Weaviate.similarity_search_by_text   sf    $.w"7::'((#)::.?#@GK LL&&**4+;+;T=N=NO	::n%%!,,VZZ-GHI::h!--fjj.BCI::l##!11&**\2JKI))'2==a@CCEv3F84D3EFGG&>%()9)9:C774>>*DKKdAB ; r   c                   SU0nU R                   R                  R                  U R                  U R                  5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  U5      R                  U5      R                  5       nSU;   a  [        SUS    35      e/ nUS   S   U R                      H6  nUR                  U R                  5      n	UR                  [        XS	95        M8     U$ )
z:Look up similar documents by embedding vector in Weaviate.rd   r   r`   r   r   r   r   r   r   )rE   rz   r)   rF   rI   r   r   r   with_near_vectorr   r   rC   r   rH   rl   r   )
rM   rO   r{   r-   rd   r   r   r   r   r   s
             r   ry   $Weaviate.similarity_search_by_vector   s@    I&LL&&**4+;+;T=N=NO	::n%%!,,VZZ-GHI::h!--fjj.BCI::l##!11&**\2JKI++F3>>qADDFv3F84D3EFGG&>%()9)9:C774>>*DKKdAB ; r   c                    U R                   b  U R                   R                  U5      nO[        S5      eU R                  " U4X#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.

Returns:
    List of Documents selected by maximal marginal relevance.
zCmax_marginal_relevance_search requires a suitable Embeddings object)r{   fetch_klambda_mult)rG   rx   rC   'max_marginal_relevance_search_by_vector)rM   rz   r{   r   r   r-   rO   s          r   max_marginal_relevance_search&Weaviate.max_marginal_relevance_search   s\    2 ??&33E:IU  ;;

HN
 	
r   c                "   SU0nU R                   R                  R                  U R                  U R                  5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      R                  U5      R                  U5      R                  5       nUS   S   U R                     n	U	 V
s/ sH
  oS   S   PM     nn
[        [        R                  " U5      XUS9n/ nU HP  nX   R                  U R                  5      nX   R                  S5        X   nUR!                  [#        UUS95        MR     U$ 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.
    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.
rd   r   r`   r   r   _additional)r{   r   r   )rE   rz   r)   rF   rI   r   r   r   r   r   r   r   r1   arrayr   rH   rl   r   )rM   rO   r{   r   r   r-   rd   r   resultspayloadr   rV   mmr_selectedr   idxr   metas                    r   r   0Weaviate.max_marginal_relevance_search_by_vector!  sd   2 I&LL&&**4+;+;T=N=NO	::n%%!,,VZZ-GHI::h!--fjj.BCI%%h/f%Z RT	 	 &/%()9)9:DKLG&]+H5G
L1HHYk
 C<##DNN3DL]+<DKKdTBC	  
  Ms   Fc                D   U R                   c  [        S5      eSU/0nUR                  S5      (       a  UR                  S5      US'   U R                  R                  R                  U R
                  U R                  5      nUR                  S5      (       a   UR                  UR                  S5      5      nUR                  S5      (       a   UR                  UR                  S5      5      nU R                   R                  U5      nU R                  (       dB  SU0nUR                  U5      R                  U5      R                  S5      R                  5       nO=UR                  U5      R                  U5      R                  S5      R                  5       nSU;   a  [        S	US    35      e/ n	US
   S   U R
                      HU  n
U
R!                  U R"                  5      n[$        R&                  " U
S   S   U5      nU	R)                  [+        XS9U45        MW     U	$ )z
Return list of documents most similar to the query
text and cosine distance in float for each.
Lower score represents more similarity.
z:_embedding cannot be None for similarity_search_with_scorer   r   r   r   r`   rd   r   r   r   r   r   r   )rG   rC   r)   rE   rz   rF   rI   r   r   rx   rK   r   r   r   r   r   r   rH   r1   dotrl   r   )rM   rz   r{   r-   r   r   embedded_queryrd   r   docs_and_scoresr   r   scores                r   similarity_search_with_score%Weaviate.similarity_search_with_scoreU  s    ??"L  $.w"7::'((#)::.?#@GK LL&&**4+;+;T=N=NO	::n%%!,,VZZ-GHI::h!--fjj.BCI44U;}}/F**62A *	  ((1A *	  v3F84D3EFGG&>%()9)9:C774>>*DFF3}-h7HE""H$$Mu#UV ; r   r   F)rN   weaviate_urlweaviate_api_key
batch_sizer   r   rQ   rJ   c                   SSK Jn  U=(       d
    [        UUS9nU(       a  UR                  R                  US9  U=(       d    S[        5       R                   3n[        X5      nUR                  R                  U5      (       d  UR                  R                  U5        U(       a  UR                  U5      OSnU(       a  [        US   R                  5       5      OSnSU;   a  UR                  S5      nO1[!        [#        U5      5       Vs/ sH  nU" [        5       5      PM     nnUR                   n[%        U5       HZ  u  nnU	U0nUb%  UU   R                  5        H  nUU   U   UU'   M     UU   nUUUS	.nUb  UU   US
'   UR&                  " S0 UD6  M\     UR)                  5         SSS5        U " UUU	4UUUU
S.UD6$ ! [         a  n[        S5      UeSnAff = fs  snf ! , (       d  f       NA= f)aN  Construct Weaviate wrapper from raw documents.

This is a user-friendly interface that:
    1. Embeds documents.
    2. Creates a new index for the embeddings in the Weaviate instance.
    3. Adds the documents to the newly created Weaviate index.

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

Args:
    texts: Texts to add to vector store.
    embedding: Text embedding model to use.
    metadatas: Metadata associated with each text.
    client: weaviate.Client to use.
    weaviate_url: The Weaviate URL. If using Weaviate Cloud Services get it
        from the ``Details`` tab. Can be passed in as a named param or by
        setting the environment variable ``WEAVIATE_URL``. Should not be
        specified if client is provided.
    weaviate_api_key: The Weaviate API key. If enabled and using Weaviate Cloud
        Services, get it from ``Details`` tab. Can be passed in as a named param
        or by setting the environment variable ``WEAVIATE_API_KEY``. Should
        not be specified if client is provided.
    batch_size: Size of batch operations.
    index_name: Index name.
    text_key: Key to use for uploading/retrieving text to/from vectorstore.
    by_text: Whether to search by text or by embedding.
    relevance_score_fn: Function for converting whatever distance function the
        vector store uses to a relevance score, which is a normalized similarity
        score (0 means dissimilar, 1 means similar).
    kwargs: Additional named parameters to pass to ``Weaviate.__init__()``.

Example:
    .. code-block:: python

        from langchain_community.embeddings import OpenAIEmbeddings
        from langchain_community.vectorstores import Weaviate

        embeddings = OpenAIEmbeddings()
        weaviate = Weaviate.from_texts(
            texts,
            embeddings,
            weaviate_url="http://localhost:8080"
        )
r   r\   r   N)r#   r"   )r   
LangChain_r^   )rc   ra   rb   rd   )rO   rP   rJ   rQ   r   )re   r]   r&   r.   rh   	configurer   hexr   schemaexistscreate_classrg   rf   keysr   rangelenri   rk   flush)clsrm   rO   rn   rN   r   r   r   r   r   rQ   rJ   r-   r]   er   rV   rP   r^   _rh   ro   r   rp   rq   rr   paramss                              r   
from_textsWeaviate.from_texts  s   @	4  
2$
 LL""j"9=Z}#=
 6}}##J//MM&&v.9BY..u5
2;T)A,++-.
 fJJw'E6;CJ6GH6G^EG,6GEH\\U$U+4d# ((|002/8|C/@,  3 Ah  #2",
 )'1!}F8$%%//- ,0 KKM3 6 	
  !1	
 	
 		
q  	G 	6 I\s*   G G%5A:G*
G"GG"*
G8c                x    Uc  [        S5      eU H&  nU R                  R                  R                  US9  M(     g)z=Delete by vector IDs.

Args:
    ids: List of ids to delete.
NzNo ids provided to delete.)rc   )rC   rE   ra   delete)rM   r_   r-   ids       r   r   Weaviate.delete
  s<     ;9:: BLL$$+++4 r   )rK   rE   rG   rF   rI   rH   rJ   )rN   r   r   strr   r   rO   Optional[Embeddings]rP   Optional[List[str]]rJ   "Optional[Callable[[float], float]]rQ   bool)returnr   )r   zCallable[[float], float]r6   )rm   zIterable[str]rn   Optional[List[dict]]r-   r   r   	List[str])   )rz   r   r{   intr-   r   r   List[Document])rO   List[float]r{   r   r-   r   r   r   )r      g      ?)rz   r   r{   r   r   r   r   floatr-   r   r   r   )rO   r   r{   r   r   r   r   r   r-   r   r   r   )rz   r   r{   r   r-   r   r   zList[Tuple[Document, float]])rm   r   rO   r   rn   r   rN   zOptional[weaviate.Client]r   Optional[str]r   r   r   zOptional[int]r   r   r   r   rQ   r   rJ   r   r-   r   r   r@   )r_   r   r-   r   r   None)__name__
__module____qualname____firstlineno____doc__r4   rR   propertyrV   rY   rs   r|   rw   ry   r   r   r   classmethodr   r   __static_attributes__r   r   r   r@   r@   E   s   ( +/*. & 1 1  1 	 1
 ( 1 ( 1
 1  1D  
 +/)) () 	)
 
)X $%LL L03L	L0 $% 03	@ 01$),<?	0  "
"
 "
 	"

 "
 "
 
"
N  22 2 	2
 2 2 
2j $%.. .03.	%.` 
 +/	B
 -1&**.$($( &B
B
 B
 (	B
 *B
 $B
 (B
 "B
 "B
 B
 B

B
  !B
" 
#B
 B
H5 5r   r@   )r   r   r   r   r   r   )NN)r#   r   r"   r   r-   r   r   zweaviate.Client)r3   r   r   r   )r:   r   r   r   )!
__future__r   r8   r'   typingr   r   r   r   r   r	   r
   r   rc   r   numpyr1   langchain_core._apir   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   r%   r   r.   r4   r;   r@   r   r   r   <module>r      s    "  		 	 	   * - 0 3 M	 !G	GG G 	G$% 
?
L5{ L5
L5r   