
    hF                      d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZmZmZmZmZmZ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m Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d d	l'm(Z( d d
l)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1  ejd                         Z3er.d dl4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z: d dl;m<Z< d dl;m9Z= d dl>m?Z?m@Z@mAZAmBZBmCZC  e*ddd      ZD e*ddd      ZE e*ddd      ZF e*ddd      ZGdZHe	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d,d       ZIe	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.d       ZI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d/d ZI G d! d"e,      ZJ G d# d$e(      ZK	 	 	 	 d0d%ZL	 	 	 	 d1d&ZM	 	 d2	 	 	 	 	 	 	 	 	 d3d'ZN	 	 d2	 	 	 	 	 	 	 	 	 d4d(ZOd5d)ZPd6d7d*ZQy)8    )annotationsN)TracebackType)TYPE_CHECKINGAnyCallableClassVar
CollectionDictIterableListLiteralOptionalTupleTypeUnioncastoverload)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRun)Document)
Embeddings)LangChainException)BaseRetriever)get_from_env)VectorStore)
ConfigDictmodel_validator)maximal_marginal_relevance)TokenCredentialAsyncTokenCredential)SearchClientSearchItemPaged)AsyncSearchItemPagedr"   )CorsOptionsScoringProfileSearchFieldSemanticConfigurationVectorSearchAZURESEARCH_FIELDS_IDid)keyenv_keydefaultAZURESEARCH_FIELDS_CONTENTcontent!AZURESEARCH_FIELDS_CONTENT_VECTORcontent_vectorAZURESEARCH_FIELDS_TAGmetadatai  c                     y N endpoint
index_namer-   azure_ad_access_tokensemantic_configuration_namefieldsvector_searchsemantic_configurationsscoring_profilesdefault_scoring_profiledefault_fields
user_agentcors_optionsasync_ additional_search_client_optionsazure_credentialazure_async_credentials                    j/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/azuresearch.py_get_search_clientrK   V   s    *     c                     y r7   r8   r9   s                    rJ   rK   rK   n   s    *  #rL   c                  789 ddl m7m}m} ddlm} ddlm} ddlm	}m
} ddlm	} ddlm} ddlm} ddlm} dd	lm}m}m}m}m}m} m}!m}"m}#m}$m}%m}&m}'  G 7fd
d|      }( G d d|      })|xs i }|
xs g }
|I|j?                         dk(  r+tA        d |             }*|*jC                  d        |)|*      }+n4 ||      }*|*}+n)| |(|      }* |)|*      }+n|xs  |       }*|xs  |       }+ |d*| |*|d|},	 |,jE                  |       |s |d*| ||*|d)|S  |d*| ||+|d)|S # |$ r ||D -ci c]  }-|-jF                  |-jH                   nc c}-w c}-8|
D .ci c]  }.|.jF                  |.jH                   nc c}.w c}.9tK        9jM                               tK        8jM                               z
  D /ci c]  \  }}/|9|    nc c}/}w }0}}/tO        |0      dkD  rEd+89fd}1djQ                  |0D 2cg c]
  }2 |1|2       nc c}2w c}2      }3tS        d|0 d|3       |
}|i |$ |d|%jT                   |ddd|&jV                               |d|%jX                   ||&jV                              g |'dd        |'d!d       g"      }|rt[        |t\              s|g} |#||#      }4n,|r( | | |" |!t^        $      g%      &      }5 |#|5g'      }4nd }4 |||||4||	|(      }6|,ja                  |6       Y w xY w),Nr   )AccessTokenAzureKeyCredentialr   r    )ResourceNotFoundError)DefaultAzureCredentialInteractiveBrowserCredential)rR   r%   )SearchIndexClient)#ExhaustiveKnnAlgorithmConfigurationExhaustiveKnnParametersHnswAlgorithmConfigurationHnswParametersSearchIndexr)   SemanticFieldSemanticPrioritizedFieldsSemanticSearchr*   VectorSearchAlgorithmKindVectorSearchAlgorithmMetricVectorSearchProfilec                  B    e Zd Zd fdZdddd	 	 	 	 	 	 	 	 	 	 	 ddZy)6_get_search_client.<locals>.AzureBearerTokenCredentialc                ^     |t        t        j                               dz         | _        y )Ni  )inttime_token)selftokenrO   s     rJ   __init__z?_get_search_client.<locals>.AzureBearerTokenCredential.__init__   s!    %eS-=-DEDKrL   NFclaims	tenant_id
enable_caec                   | j                   S r7   )re   rf   rj   rk   rl   scopeskwargss         rJ   	get_tokenz@_get_search_client.<locals>.AzureBearerTokenCredential.get_token   s     ;;rL   )rg   strro   rr   rj   Optional[str]rk   rt   rl   boolrp   r   returnrO   )__name__
__module____qualname__rh   rq   )rO   s   rJ   AzureBearerTokenCredentialra      sS    	F %)'+$		 "	 %		
 	 	 	rL   rz   c                  b    e Zd ZddZdddd	 	 	 	 	 	 	 	 	 	 	 d	dZd
dZ	 	 	 d	 	 	 	 	 	 	 ddZy)7_get_search_client.<locals>.AsyncTokenCredentialWrapperc                    || _         y r7   )_credential)rf   
credentials     rJ   rh   z@_get_search_client.<locals>.AsyncTokenCredentialWrapper.__init__   s
    )DrL   NFri   c               J   K    | j                   j                  ||||d|S w)Nri   )r~   rq   rn   s         rJ   rq   zA_get_search_client.<locals>.AsyncTokenCredentialWrapper.get_token   s9      .4##--#%	
  s   !#c                   K   y wr7   r8   rf   s    rJ   closez=_get_search_client.<locals>.AsyncTokenCredentialWrapper.close   s	        c                   K   y wr7   r8   )rf   exc_type	exc_value	tracebacks       rJ   	__aexit__zA_get_search_client.<locals>.AsyncTokenCredentialWrapper.__aexit__   s      r   )r   r   rs   rv   None)NNN)r   zOptional[Type[BaseException]]r   zOptional[BaseException]r   zOptional[TracebackType]rv   r   )rw   rx   ry   rh   rq   r   r   r8   rL   rJ   AsyncTokenCredentialWrapperr|      s    	* %)'+$		 "	 %		
 	 	 	 	
 7;1515		3	 /	 /		
 	rL   r   INTERACTIVEr   z!https://search.azure.com/.default)r:   r   rD   )namec           
         |  dj                  | d       dj                  |        dj                  |        d| j                          d
S )Nz current type: 'MISSINGz'. It has to be 'z#' or you can point to a different 'z;' field name by using the env variable 'AZURESEARCH_FIELDS_')getupper)xfields_typesmandatory_fieldss    rJ   fmt_errz#_get_search_client.<locals>.fmt_err  sk    #-l.>.>q).L-M N))9)=)=a)@(A B++;+?+?+B*C DFFGggi[PQSrL   
z2You need to specify at least the following fields z; or provide alternative field names in the env variables.

r/      i  i  )mef_construction	ef_searchmetric)r   kind
parametersdefault_exhaustive_knn)r   myHnswProfile)r   algorithm_configuration_namemyExhaustiveKnnProfile)
algorithmsprofiles)configurationsdefault_configuration_name)
field_name)content_fields)r   prioritized_fields)r   )r   r>   r?   semantic_searchrA   rB   rE   )r:   r;   r   rD   r8   )r   rr   rv   rr   )1azure.core.credentialsrO   rP   r   azure.core.credentials_asyncr!   azure.core.exceptionsrQ   azure.identityrR   rS   azure.identity.aioazure.search.documentsr"   azure.search.documents.aioazure.search.documents.indexesrT   %azure.search.documents.indexes.modelsrU   rV   rW   rX   rY   r)   rZ   r[   r\   r*   r]   r^   r_   r   r   rq   	get_indexr   typesetitemslenjoin
ValueErrorHNSWCOSINEEXHAUSTIVE_KNN
isinstancelistFIELDS_CONTENTcreate_index):r:   r;   r-   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rP   r   r!   rQ   rR   rS   AsyncDefaultAzureCredentialr"   AsyncSearchClientrT   rU   rV   rW   rX   rY   r)   rZ   r[   r\   r*   r]   r^   r_   rz   r   r   async_credentialindex_clientfdfvaluemissing_fieldsr   r   errorr   semantic_configurationindexrO   r   r   s:                                                          @@@rJ   rK   rK      s   * XWA;SX3L@    _ &: > (H'M2$#)rN
 99;-'/1M1OPJ  !DE::F+C0J)		*/0EF
6zB%A)?)A
1R5P5R&7 '' +	'La)J/F  
!!!	

 /
 	
 ! 
!'!	

 /
 	
W ! _)4:;qAFFAFFN;;L;IJR 0JJ #&&6&<&<&>"?l((*+#,C %c** N 
 >"Q& 		~"F!71:"F"FG H%& '%%*G-  $F (.&6;;#1,/&)#>#E#E	$	 856EE#:#>#E#E$( (,5> (55M	)MB #5t<+B*C',6+FO )%:0#<$1^$L#M$&" -=S<TUO #O '+-$;%
 	!!%(_)sC   =D+ +L7EL"F ?L?G.L=H
C7LLc                  <   e Zd ZdZ	 	 	 	 	 	 	 	 d8dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d9dZd:dZed;d       Zd<dZ	 d=dd	 	 	 	 	 	 	 	 	 d>d	Z		 d=dd	 	 	 	 	 	 	 	 	 d>d
Z
	 d=dd	 	 	 	 	 	 	 d?dZ	 d=dd	 	 	 	 	 	 	 d?dZd=d@dZd=d@dZ	 dAdd	 	 	 	 	 	 	 	 	 dBdZdd	 	 	 	 	 	 	 dCdZ	 dAdd	 	 	 	 	 	 	 	 	 dBdZdd	 	 	 	 	 	 	 dCdZ	 dAdd	 	 	 	 	 	 	 	 	 dDdZ	 dAdd	 	 	 	 	 	 	 	 	 dDdZ	 dAdd	 	 	 	 	 	 	 	 	 dEdZ	 dAdd	 	 	 	 	 	 	 	 	 dEdZ	 	 dF	 	 	 	 	 	 	 	 	 dGdZ	 	 dF	 	 	 	 	 	 	 	 	 dGdZ	 	 	 dHdd	 	 	 	 	 	 	 	 	 	 	 	 	 dIdZ	 	 	 dHdd	 	 	 	 	 	 	 	 	 	 	 	 	 dIdZdAdJd Z	 dA	 	 	 	 	 	 	 dJd!Z	 	 dF	 	 	 	 	 	 	 	 	 dGd"Z	 	 dF	 	 	 	 	 	 	 	 	 dGd#Z	 dAdd	 	 	 	 	 	 	 	 	 dDd$Z	 dAdd	 	 	 	 	 	 	 	 	 dDd%Z 	 	 	 dHdd	 	 	 	 	 	 	 	 	 	 	 	 	 dId&Z!	 	 	 dHdd	 	 	 	 	 	 	 	 	 	 	 	 	 dId'Z"dd	 	 	 	 	 	 	 	 	 	 	 dKd(Z#dd	 	 	 	 	 	 	 	 	 	 	 dLd)Z$	 dA	 	 	 	 	 	 	 dJd*Z%	 dA	 	 	 	 	 	 	 dJd+Z&	 	 dMdd	 	 	 	 	 	 	 	 	 	 	 dNd,Z'	 	 dMdd	 	 	 	 	 	 	 	 	 	 	 dNd-Z(	 dAdd	 	 	 	 	 	 	 	 	 dOd.Z)	 dAdd	 	 	 	 	 	 	 	 	 dOd/Z*e+	 	 	 	 	 	 dP	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dQd2       Z,e+	 	 	 	 	 	 dP	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dQd3       Z-e+	 d=d0d0d1dd4	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRd5       Z.e+	 d=d0d0d1dd4	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dRd6       Z/dSd7Z0y)TAzureSearchz&`Azure Cognitive Search` vector store.N)vector_search_dimensionsrG   r<   rH   rI   c                  	 ddl m}m}m}m} 	 || _        t        | j                  t              r| j                  j                  | _	        n| j                  | _	         |t        |j                  dd       |t        |j                         |t        |j                  |j                        d|xs t!        | j                  d            d	       |t"        |j                        g}d
}d|v r|d   r|d|d   z   z  }t%        ||||||||	|
||||t'        j(                  |      |      | _        t%        ||||||||	|
||||d|||      | _        || _        || _        |r|n|| _        || _        || _        || _        || _        || _        || _        |	| _         |
| _!        || _"        || _#        || _$        || _%        y # t
        $ r}t        d      |d }~ww xY w)Nr   )SearchableFieldr(   SearchFieldDataTypeSimpleFieldzeUnable to import azure.search.documents. Please install with `pip install -U azure-search-documents`.T)r   r   r-   
filterable)r   r   Textr   )r   r   
searchabler   vector_search_profile_name	langchainrD    )r=   r>   r?   r@   rA   rB   rC   rD   rE   rG   rH   )r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   )&r   r   r(   r   r   ImportErrorembedding_functionr   r   embed_query	FIELDS_IDStringr   FIELDS_CONTENT_VECTORr	   Singler   FIELDS_METADATArK   copydeepcopyclientasync_clientsearch_typer=   r>   _azure_search_endpoint_azure_search_key_index_name_semantic_configuration_name_fields_vector_search_semantic_configurations_scoring_profiles_default_scoring_profile_default_fields_user_agent_cors_options)rf   azure_search_endpointazure_search_keyr;   r   r   r=   r>   r?   r@   rA   rB   rE   r   rG   r<   rH   rI   rp   r   r(   r   r   erC   rD   s                             rJ   rh   zAzureSearch.__init__u  s=   0	  	4"4d--z:#66BBD#66D (//	 #(// *(334G4N4NO)A *1t''/0+: $(//'
0 !
6!f\&:#| 444J )!!(C'$;-$;)!%-1]]0. .#
* /!!(C'$;-$;)!%-M-#9#
& '+F( &fN&;#!1%,G)+(?%!1(?%-%)G  	; 	s   G 	GGGc                   t        | d      r&| j                  r| j                  j                          t        | d      r| j                  ry	 t	        j
                         }|j                         r*|j                  | j                  j                                y |j                  | j                  j                                y y y # t        $ r{ t	        j                         }t	        j                  |       	 |j                  | j                  j                                |j                          Y y # |j                          w xY ww xY w)Nr   r   )hasattrr   r   r   asyncioget_event_loop
is_runningcreate_taskrun_until_completeRuntimeErrornew_event_loopset_event_loop)rf   loops     rJ   __del__zAzureSearch.__del__  s    4"t{{KK 4(T->->!--/??$$$T%6%6%<%<%>? ++D,=,=,C,C,EF .?(   !--/&&t,!++D,=,=,C,C,EFJJLDJJL!s+   AC )C 2E
9)D4"E
4EE
c                R    t        | j                  t              r| j                  S d S r7   )r   r   r   r   s    rJ   
embeddingszAzureSearch.embeddings  s.    
 $11:> ##	
 	
rL   c                   K   | j                   r#| j                   j                  |       d {   S  t        t        | j                        |      S 7 $wr7   )r  aembed_queryr   r   r   )rf   texts     rJ   _aembed_queryzAzureSearch._aembed_query  sD     ??55d;;;:4$"9"9:4@@ <s   +AA%Akeysc                  t        | j                  t              r&	 | j                  j                  t	        |            }n|D cg c]  }| j                  |       }}t        |      dk(  rt        j                  d       g S |s#d|v rt        |d         t        |      k(  r|d   }| j                  t        ||      ||      S # t
        $ r1 |D cg c]  }| j                  j                  |       nc c}w }}Y w xY wc c}w )z$Add texts data to an existing index.r   Nothing to insert, skipping.idsr	  )r   r   r   embed_documentsr   NotImplementedErrorr   r   loggerdebugadd_embeddingsziprf   texts	metadatasr
  rp   r  r   s          rJ   	add_textszAzureSearch.add_texts!  s     d--z:U!44DDT%[Q
 ?DD$11!4DJDz?aLL78I
 5F?VE]1Cs:1V%=D""3uj#994"PP ' UNSTd55AA!DTT
TU Es#   $C DD"C76D Dc               T  K   t        | j                  t              r.	 | j                  j                  t	        |             d {   }n|D cg c]  }| j                  |       }}t        |      dk(  rt        j                  d       g S |s#d|v rt        |d         t        |      k(  r|d   }| j                  t        ||      ||       d {   S 7 # t
        $ r: |D cg c]&  }| j                  j                  |       d {  7  ( nc c}w }}Y w xY wc c}w 7 Qw)Nr   r  r  r	  )r   r   r   aembed_documentsr   r  r  r   r  r  aadd_embeddingsr  r  s          rJ   
aadd_textszAzureSearch.aadd_texts?  s     d--z:#'#:#:#K#KDQVK#XX
 ?DD$11!4DJDz?aLL78I
 5F?VE]1Cs:1V%=D))#eZ*@)RV)WWW% Y& KPFG$11>>qAAA 
 
 E Xsk   D('C CC 
D(D!'A-D(D&D(C D(#DDDDD(D	D(c                  g }g }t        |      D ]  \  }\  }}|r||   }	nKt        t        j                               }	t	        j
                  t        |	d            j                  d      }	|r||   ni }
ddt        |	t        |t        t        j                  |t        j                        j                         t        t!        j"                  |
      i}|
r\|
j%                         D ci c].  \  }}|| j&                  D cg c]  }|j(                   c}v r||0 }}}}|j+                  |       |j-                  |       |j-                  |	       t/        |      t0        k(  sZ| j2                  j5                  |      }t7        d |D              st9        |      g } t/        |      dk(  r|S | j2                  j5                  |      }t7        d	 |D              r|S t9        |      c c}w c c}}}w )
$Add embeddings to an existing index.utf-8ascii@search.actionuploaddtype	documentsc              3  4   K   | ]  }|j                     y wr7   	succeeded.0rs     rJ   	<genexpr>z-AzureSearch.add_embeddings.<locals>.<genexpr>       911;;9   r   c              3  4   K   | ]  }|j                     y wr7   r'  r)  s     rJ   r,  z-AzureSearch.add_embeddings.<locals>.<genexpr>       -qq{{-r.  )	enumeraterr   uuiduuid4base64urlsafe_b64encodebytesdecoder   r   r   nparrayfloat32tolistr   jsondumpsr   r>   r   updateappendr   MAX_UPLOAD_BATCH_SIZEr   upload_documentsallr   rf   text_embeddingsr  r
  r  datair  	embeddingr-   r5   dockvr   additional_fieldsresponses                    rJ   r  zAzureSearch.add_embeddings]  s     $-o$> #	 A i1g$**,'..uS'/BCJJ7S'0y|bH !(3%rxx	'L'S'S'UH!5C  !) 0% %1T[[9QVV99 qD%! %
 

,-KKJJsO4y11;;77$7G999,X66G#	L t9>J ;;//$/?-H--J$X..1 :%s   ,G8
G3	G8
3G8
c               ,  K   g }g }t        |      D ]  \  }\  }}|r||   nt        t        j                               }	t	        j
                  t        |	d            j                  d      }	|r||   ni }
ddt        |	t        |t        t        j                  |t        j                        j                         t        t!        j"                  |
      i}|
r\|
j%                         D ci c].  \  }}|| j&                  D cg c]  }|j(                   c}v r||0 }}}}|j+                  |       |j-                  |       |j-                  |	       t/        |      t0        k(  sY| j2                  j5                  |       d{   }t7        d |D              st9        |      g } t/        |      d	k(  r|S | j2                  j5                  |       d{   }t7        d
 |D              r|S t9        |      c c}w c c}}}w 7 7 1w)r  r  r  r   r!  r"  r$  Nc              3  4   K   | ]  }|j                     y wr7   r'  r)  s     rJ   r,  z.AzureSearch.aadd_embeddings.<locals>.<genexpr>  r-  r.  r   c              3  4   K   | ]  }|j                     y wr7   r'  r)  s     rJ   r,  z.AzureSearch.aadd_embeddings.<locals>.<genexpr>  r0  r.  )r1  rr   r2  r3  r4  r5  r6  r7  r   r   r   r8  r9  r:  r;  r   r<  r=  r   r>   r   r>  r?  r   r@  r   rA  rB  r   rC  s                    rJ   r  zAzureSearch.aadd_embeddings  s      $-o$> 	 A i!$q's4::<'8C**5g+>?FFwOC'0y|bH !(3%rxx	'L'S'S'UH!5C  !) 0% %1T[[9QVV99 qD%! %
 

,-KKJJsO4y11!%!2!2!C!Cd!C!SS999,X66?	D t9>J **;;d;KK-H--J$X..1 :% T LsP   C+H-H	
H	H	
 A	H+H
HAH H!#HH	
	HHc                    |r=| j                   j                  |D cg c]
  }t        |i c}      }t        |      dkD  S yc c}w )Delete by vector ID.

        Args:
            ids: List of ids to delete.

        Returns:
            bool: True if deletion is successful,
            False otherwise.
        r   F)r   delete_documentsr   r   rf   r  rp   rF  ress        rJ   deletezAzureSearch.delete  sA     ++../L1A/LMCs8a< 0Ms   Ac                   K   |rA| j                   j                  |D cg c]  }d|i c}       d{   }t        |      dkD  S yc c}w 7 w)rQ  r,   Nr   F)r   rR  r   rS  s        rJ   adeletezAzureSearch.adelete  sM      ))::s;S!T1I;STTCs8a< <TTs   AA
AAAr   )r   c                   |xs | j                   }|dk(  r | j                  |fd|i|}|S |dk(  r | j                  |fd|i|}|S |dk(  r | j                  |fd|i|}|S t	        d| d      N
similarityrI  hybridsemantic_hybridsearch_type of  not allowed.)r   r?   hybrid_searchsemantic_hybrid_searchr   rf   queryrI  r   rp   docss         rJ   similarity_searchzAzureSearch.similarity_search  s     "5T%5%5,&%4%%e;q;F;D  H$%4%%e;q;F;D
 	 --.4..uDDVDD  {m=IJJrL   )rI  c                   |j                  d| j                        }|dk(  r | j                  |fd|i|S |dk(  r | j                  |fd|i|S |dk(  r | j                  |fd|i|S t        d| d      )$Run similarity search with distance.r   rZ  rI  r[  r\  r]  r^  )popr   vector_search_with_scorehybrid_search_with_score!semantic_hybrid_search_with_scorer   rf   rb  rI  rp   r   s        rJ   similarity_search_with_scorez(AzureSearch.similarity_search_with_score  s    
 jj0@0@A,&0400F!FvFFH$0400F!FvFF--9499%O1OOO{m=IJJrL   c               ,  K   |xs | j                   }|dk(  r | j                  |fd|i| d {   }|S |dk(  r | j                  |fd|i| d {   }|S |dk(  r | j                  |fd|i| d {   }|S t	        d| d      7 ]7 ;7 wrY  )r   avector_searchahybrid_searchasemantic_hybrid_searchr   ra  s         rJ   asimilarity_searchzAzureSearch.asimilarity_search  s      "5T%5%5,&,,,UBaB6BBD  H$,,,UBaB6BBD
 	 --555eKqKFKKD  {m=IJJ CBKs3   .BB#BB#B8B9BBBc               8  K   |j                  d| j                        }|dk(  r | j                  |fd|i| d{   S |dk(  r | j                  |fd|i| d{   S |dk(  r | j                  |fd|i| d{   S t        d| d      7 W7 77 w)	rf  r   rZ  rI  Nr[  r\  r]  r^  )r   r   avector_search_with_scoreahybrid_search_with_score"asemantic_hybrid_search_with_scorer   rk  s        rJ   asimilarity_search_with_scorez)AzureSearch.asimilarity_search_with_score(  s      jj0@0@A,&777MMfMMMH$777MMfMMM--@@@V!VvVVV{m=IJJ NMVs3   :BB!BB!B BBBB)score_thresholdc               p     | j                   |fd|i|}||S |D cg c]  }|d   |k\  s| c}S c c}w NrI     rh  rf   rb  rI  rw  rp   resultr+  s          rJ   'similarity_search_with_relevance_scoresz3AzureSearch.similarity_search_with_relevance_scores6  X     /..uDDVD & 	
 $?qt'>!?	
 @   33c                  K    | j                   |fd|i| d {   }||S |D cg c]  }|d   |k\  s| c}S 7 !c c}w wry  rs  r|  s          rJ   (asimilarity_search_with_relevance_scoresz4AzureSearch.asimilarity_search_with_relevance_scoresE  e      6t55eKqKFKK & 	
 $?qt'>!?	
 L @    A=A??AA)filtersc               `    | j                  |||      }|D cg c]  \  }}|	 c}}S c c}}w )\  
        Returns the most similar indexed documents to the query text.

        Args:
            query (str): The query text for which to find similar documents.
            k (int): The number of documents to return. Default is 4.

        Returns:
            List[Document]: A list of documents that are most similar to the query text.
        rI  r  r{  rf   rb  rI  r  rp   docs_and_scoresrH  _s           rJ   r?   zAzureSearch.vector_searchT  s3     77G7T"12Q222s   *c               |   K   | j                  |||       d{   }|D cg c]  \  }}|	 c}}S 7 c c}}w w)r  r  Nr  r  s           rJ   rn  zAzureSearch.avector_searchd  sJ      !% > >Q !? !
 
 #22Q22
 3s   <4	<6<<c                h    | j                  |      } | j                  |d|fd|i|}t        |      S )  Return docs most similar to query.

        Args:
            query (str): Text to look up documents similar to.
            k (int, optional): Number of Documents to return. Defaults to 4.
            filters (str, optional): Filtering expression. Defaults to None.

        Returns:
            List[Tuple[Document, float]]: List of Documents most similar
                to the query and score for each
         r  r   _simple_search_results_to_documentsrf   rb  rI  r  rp   rG  resultss          rJ   rh  z$AzureSearch.vector_search_with_scorev  s?    $ $$U+	%$%%iQRR6R$W--rL   c                   K   | j                  |       d{   } | j                  |d|fd|i| d{   }t        |       d{   S 7 67 7 w)r  Nr  r  r  _asimple_search_aresults_to_documentsr  s          rJ   rs  z%AzureSearch.avector_search_with_score  sj     $ ,,U33	,,,r1
&-
17
 
 ,G444 4
 51   AAAAA	A
AAAc                   | j                  |      } | j                  |d|fd|i|}t        |t        j                  |      ||      S )"  Perform a search and return results that are reordered by MMR.

        Args:
            query (str): Text to look up documents similar to.
            k (int, optional): How many results to give. Defaults to 4.
            fetch_k (int, optional): Total results to select k from.
                Defaults to 20.
            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
            filters (str, optional): Filtering expression. Defaults to None.

        Returns:
            List[Tuple[Document, float]]: List of Documents most similar
                to the query and score for each
        r  r  query_embeddinglambda_multrI  r   r  0_reorder_results_with_maximal_marginal_relevancer8  r9  	rf   rb  rI  fetch_kr  r  rp   rG  r  s	            rJ   (max_marginal_relevance_search_with_scorez4AzureSearch.max_marginal_relevance_search_with_score  sU    6 $$U+	%$%%iWXgXQWX?RXXi%8kUV
 	
rL   c                  K   | j                  |       d{   } | j                  |d|fd|i| d{   }t        |t        j                  |      ||       d{   S 7 M7 07 w)r  Nr  r  r  r  r  1_areorder_results_with_maximal_marginal_relevancer8  r9  r  s	            rJ   )amax_marginal_relevance_search_with_scorez5AzureSearch.amax_marginal_relevance_search_with_score  s     6 ,,U33	,,,r7
,3
7=
 
 GHHY/#	
 
 	
 4

1   A+A%A+A')A+ A)!A+'A+)A+c                b     | j                   |fd|i|}|D cg c]  \  }}|	 c}}S c c}}w )r  rI  ri  rf   rb  rI  rp   r  rH  r  s          rJ   r_  zAzureSearch.hybrid_search  s8     8$77MMfM"12Q222s   +c                ~   K    | j                   |fd|i| d{   }|D cg c]  \  }}|	 c}}S 7 c c}}w w)r  rI  Nrt  r  s          rJ   ro  zAzureSearch.ahybrid_search  sE      !? > >u T TV TT"12Q22 U2s   =5	=7==c                h    | j                  |      } | j                  |||fd|i|}t        |      S )!  Return docs most similar to query with a hybrid 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 and score for each
        r  r  r  s          rJ   ri  z$AzureSearch.hybrid_search_with_score  s?    " $$U+	%$%%iU7UfU$W--rL   c                   K   | j                  |       d{   } | j                  |||fd|i| d{   }t        |       d{   S 7 67 7 w)r  Nr  r  r  s          rJ   rt  z%AzureSearch.ahybrid_search_with_score#  sj     " ,,U33	,,,ua
)0
4:
 
 ,G444 4
 5r  c               p     | j                   |fd|i|}||S |D cg c]  }|d   |k\  s| c}S c c}w ry  r  r|  s          rJ   #hybrid_search_with_relevance_scoresz/AzureSearch.hybrid_search_with_relevance_scores;  r  r  c                  K    | j                   |fd|i| d {   }||S |D cg c]  }|d   |k\  s| c}S 7 !c c}w wry  r  r|  s          rJ   $ahybrid_search_with_relevance_scoresz0AzureSearch.ahybrid_search_with_relevance_scoresJ  r  r  c                   | j                  |      } | j                  |||fd|i|}t        |t        j                  |      ||      S )  Return docs most similar to query with a hybrid query
            and reorder results by MMR.

        Args:
            query (str): Text to look up documents similar to.
            k (int, optional): Number of Documents to return. Defaults to 4.
            fetch_k (int, optional): Total results to select k from.
                Defaults to 20.
            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
            filters (str, optional): Filtering expression. Defaults to None.

        Returns:
            List of Documents most similar to the query and score for each
        r  r  r  r  s	            rJ   /hybrid_max_marginal_relevance_search_with_scorez;AzureSearch.hybrid_max_marginal_relevance_search_with_scoreY  s`    8 $$U+	%$%%ug
/6
:@
 @RXXi%8kUV
 	
rL   c                  K   | j                  |       d{   } | j                  |||fd|i| d{   }t        |t        j                  |      ||       d{   S 7 M7 07 w)r  Nr  r  r  r  s	            rJ   0ahybrid_max_marginal_relevance_search_with_scorez<AzureSearch.ahybrid_max_marginal_relevance_search_with_score~  s     8 ,,U33	,,,ug
/6
:@
 
 GHHY/#	
 
 	
 4

r  c          	     l    ddl m}  | j                  j                  d| |||t              g||d|S )  Perform vector or hybrid search in the Azure search index.

        Args:
            embedding: A vector embedding to search in the vector space.
            text_query: A full-text search query expression;
                Use "*" or omit this parameter to perform only vector search.
            k: Number of documents to return.
            filters: Filtering expression.
        Returns:
            Search items
        r   VectorizedQueryvectork_nearest_neighborsr>   search_textvector_queriesfiltertopr8   )azure.search.documents.modelsr  r   searchr   rf   rG  
text_queryrI  r  rp   r  s          rJ   r  zAzureSearch._simple_search  sQ    ( 	B!t{{!! 
"$()0 
 
 	
rL   c          	        K   ddl m}  | j                  j                  d| |||t              g||d| d{   S 7 w)r  r   r  r  r  Nr8   )r  r  r   r  r   r  s          rJ   r  zAzureSearch._asimple_search  sa     ( 	B-T&&-- 
"$()0 
 
 
 	
 
s   9AA Ac                d     | j                   |fd|i|}|D cg c]  \  }}}|
 c}}S c c}}w )  
        Returns the most similar indexed documents to the query text.

        Args:
            query (str): The query text for which to find similar documents.
            k (int): The number of documents to return. Default is 4.
            filters: Filtering expression.

        Returns:
            List[Document]: A list of documents that are most similar to the query text.
        rI  ,semantic_hybrid_search_with_score_and_rerankr  s          rJ   r`  z"AzureSearch.semantic_hybrid_search  sF     L$KK

 
 &55	Q555s   ,c                   K    | j                   |fd|i| d{   }|D cg c]  \  }}}|
 c}}S 7 c c}}w w)r  rI  N-asemantic_hybrid_search_with_score_and_rerankr  s          rJ   rp  z#AzureSearch.asemantic_hybrid_search  sV      !S R R!
!
 !
 
 &55	Q55
 6s   >6	>8>>c                    | j                   |fd|i|}|dk(  r!|D 	cg c]  \  }}}	|||k\  r||f c}	}}S |dk(  r!|D 	
cg c]  \  }}	}
||
|k\  r||
f c}
}	}S yc c}	}}w c c}
}	}w )  
        Returns the most similar indexed documents to the query text.

        Args:
            query (str): The query text for which to find similar documents.
            k (int): The number of documents to return. Default is 4.
            score_type: Must either be "score" or "reranker_score".
                Defaulted to "score".
            filters: Filtering expression.

        Returns:
            List[Tuple[Document, float]]: A list of documents and their
                corresponding scores.
        rI  scoreNreranker_scorer  rf   rb  rI  
score_typerw  rp   r  rH  r  r  r  s              rJ   rj  z-AzureSearch.semantic_hybrid_search_with_score  s    . L$KK

 
   &5 !C"*e.F e 
 ++ /> *CN"*n.O n%  ,s   A#A*c                  K    | j                   |fd|i| d{   }|dk(  r!|D 	cg c]  \  }}}	|||k\  r||f c}	}}S |dk(  r!|D 	
cg c]  \  }}	}
||
|k\  r||
f c}
}	}S y7 Qc c}	}}w c c}
}	}w w)r  rI  Nr  r  r  r  s              rJ   ru  z.AzureSearch.asemantic_hybrid_search_with_score;  s     . !S R R!
!
 !
 
   &5 !C"*e.F e 
 ++ /> *CN"*n.O n%  ,
s,   A=A-A=A/A=A6'A=/A=c                  ddl m}  | j                  j                  d| || j	                  |      |t
              g|d| j                  dd|d|}|j                         xs g }i }|D ](  }	|	j                  |	j                  d||	j                  <   * |D 
cg c]/  }
t        |
j                  t              i t        |
v rt        |
j                  t              ini t        |
v rt!        j"                  |
t                 n,|
j%                         D ci c]  \  }}|t
        k7  r|| c}}|
j'                  d      rA|
j'                  di g      d   j                  |
j'                  di g      d   j                  dni |j'                  |
j'                  t        d	      d	      d
      t)        |
d         t)        |
d         f2 }}}
}|S c c}}w c c}}}
w )L  Return docs most similar to query with a hybrid query.

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

        Returns:
            List of Documents most similar to the query and score for each
        r   r  r  semantic
extractiver  r  r  
query_typer=   query_captionquery_answerr  r  
highlights@search.captionsr  captionsanswerspage_contentr5   @search.score@search.reranker_scorer8   )r  r  r   r  r   r   r=   get_answersr  r  r-   r   rg  r   r   r   r<  loadsr   r   float)rf   rb  rI  r  rp   r  r  semantic_answerssemantic_answers_dictsemantic_answerr}  rJ  rc  s                rJ   r  z8AzureSearch.semantic_hybrid_search_with_score_and_rerankb  s)    	B$$++$$ 
++E2()0 !(,(H(H&%
 
$ #..06B&(/ 	O',,-88:!/"5"56	` "U+
 +
T Q !'N!;!  )F2 '

9(=>!#	!  /&8 !JJvo'>? -3LLN"$(Aq#$(=#= !"1"!2 $*::.@#A -3JJ7IB4,P()-&&*d28**=ORTQU2V()3&&0j!" &('<'@'@ &

9b 9 "(!$J f_-.f567O(+
 +
X ?"+
s   A<GG.B#GGc               (  K   ddl m} | j                  |       d{   } | j                  j                  d| |||t
              g|d| j                  dd|d| d{   }|j                          d{   xs g }i }	|D ](  }
|
j                  |
j                  d|	|
j                  <   * |2 cg c3 d{   }t        |j                  t              i t        |v rt        |j                  t              ini t        |v rt!        j"                  |t                 n,|j%                         D ci c]  \  }}|t
        k7  r|| c}}|j'                  d	      rA|j'                  d	i g      d   j                  |j'                  d	i g      d   j                  dni |	j'                  |j'                  t        d
      d
      d      t)        |d         t)        |d         f77 7 7 }7 <c c}}w 6 nc c}}}w }}}}|S w)r  r   r  Nr  r  r  r  r  r  r  r  r  r  r  r8   )r  r  r  r   r  r   r=   r  r  r  r-   r   rg  r   r   r   r<  r  r   r   r  )rf   rb  rI  r  rp   r  r  r  r  r  r  r}  rJ  rc  s                 rJ   r  z9AzureSearch.asemantic_hybrid_search_with_score_and_rerank  sV     	B))%000))00 
!()0 !(,(H(H&%
 
 
$ #*"5"5"77>B&(/ 	O',,-88:!/"5"56	` !(U+
 +
T Q !'N!;!  )F2 '

9(=>!#	!  /&8 !JJvo'>? -3LLN"$(Aq#$(=#= !"1"!2 $*::.@#A -3JJ7IB4,P()-&&*d28**=ORTQU2V()3&&0j!" &('<'@'@ &

9b 9 "(!$J f_-.f567O(9 1
$ 8+
"+
 +
 +
X sx   HG1AH"G4#H:G7;=H8H:H
>G:?H
A7H9G=B"H1H4H7H:H
=HHr  langchain-indexc	                L     | ||||f||d|	}
 |
j                   ||fi |	 |
S N)r>   r<   )r  clsr  rG  r  r   r   r<   r;   r>   rp   azure_searchs              rJ   
from_textszAzureSearch.from_texts  sP     !	

 "7
 
 	ui:6:rL   c	                h   K    | ||||f||d|	}
 |
j                   ||fi |	 d {    |
S 7 wr  )r  r  s              rJ   afrom_textszAzureSearch.afrom_texts+  s`      !	

 "7
 
 &l%%eYA&AAA 	Bs   '202)r   r   r;   r>   c          
        K   t        |      \  }}	|	t        d      t        |	d         }
 | d||||||
d|} |j                  ||fi | d {    |S 7 wNz0Cannot create AzureSearch from empty embeddings.rz  )r   r   r;   r   r>   r   r8   )_peekr   r   r  r  rD  rG  r  r   r   r;   r>   rp   first_text_embeddingr   r  s               rJ   afrom_embeddingszAzureSearch.afrom_embeddingsE  s      16o0F--'OPP#&';A'>#?  
"7-!(%=
 
 +l**?IPPPP 	Qs   AAAAc          
         t        |      \  }}	|	t        d      t        |	d         }
 | d||||||
d|} |j                  ||fi | |S r  )r  r   r   r  r  s               rJ   from_embeddingszAzureSearch.from_embeddingsc  s     16o0F--'OPP#&';A'>#?  
"7-!(%=
 
 	$##OYI&IrL   c                    |j                  d| j                        }||d<   |j                  dd      xs g }|j                  | j	                                t        dd| i|d|iS )a  Return AzureSearchVectorStoreRetriever initialized from this VectorStore.

        Args:
            search_type (Optional[str]): Overrides the type of search that
                the Retriever should perform. Defaults to `self.search_type`.
                Can be "similarity", "hybrid", or "semantic_hybrid".
            search_kwargs (Optional[Dict]): Keyword arguments to pass to the
                search function. Can include things like:
                    score_threshold: Minimum relevance threshold
                        for similarity_score_threshold
                    fetch_k: Amount of documents to pass to MMR algorithm (Default: 20)
                    lambda_mult: Diversity of results returned by MMR;
                        1 for minimum diversity and 0 for maximum. (Default: 0.5)
                    filter: Filter by document metadata

        Returns:
            AzureSearchVectorStoreRetriever: Retriever class for VectorStore.
        r   tagsNvectorstorer8   )r   r   rg  extend_get_retriever_tagsAzureSearchVectorStoreRetriever)rf   rp   r   r  s       rJ   as_retrieverzAzureSearch.as_retriever  sg    & jj0@0@A +}zz&$'-2D,,./.U4U6UPTUUrL   )r[  NNNNNNN)$r   rr   r   rt   r;   rr   r   zUnion[Callable, Embeddings]r   rr   r=   rt   r>   Optional[List[SearchField]]r?   Optional[VectorSearch]r@   COptional[Union[SemanticConfiguration, List[SemanticConfiguration]]]rA   Optional[List[ScoringProfile]]rB   rt   rE   Optional[CorsOptions]r   zOptional[int]rG   Optional[Dict[str, Any]]r<   rt   rH   Optional[TokenCredential]rI   Optional[AsyncTokenCredential]rp   r   r   )rv   zOptional[Embeddings])r  rr   rv   List[float]r7   )
r  zIterable[str]r  Optional[List[dict]]r
  Optional[List[str]]rp   r   rv   	List[str])rD  !Iterable[Tuple[str, List[float]]]r  r  r
  r  rv   r  )r  r  rp   r   rv   ru   )r   )
rb  rr   rI  rc   r   rt   rp   r   rv   List[Document])rb  rr   rI  rc   rp   r   rv   List[Tuple[Document, float]])
rb  rr   rI  rc   rw  Optional[float]rp   r   rv   r  )
rb  rr   rI  rc   r  rt   rp   r   rv   r  )r   N)
rb  rr   rI  rc   r  rt   rp   r   rv   r  )r            ?)rb  rr   rI  rc   r  rc   r  r  r  rt   rp   r   rv   r  )rb  rr   rI  rc   rp   r   rv   r  )rG  r  r  rr   rI  rc   r  rt   rp   r   rv   zSearchItemPaged[dict])rG  r  r  rr   rI  rc   r  rt   rp   r   rv   zAsyncSearchItemPaged[dict])r   r  )rb  rr   rI  rc   r  z"Literal['score', 'reranker_score']rw  r  rp   r   rv   r  )
rb  rr   rI  rc   r  rt   rp   r   rv   z#List[Tuple[Document, float, float]])Nr  r  Nr  N)r  Type[AzureSearch]r  r  rG  r   r  r  r   rr   r   rr   r<   rt   r;   rr   r>   r  rp   r   rv   r   )r  r  rD  r  rG  r   r  r  r   rr   r   rr   r;   rr   r>   r  rp   r   rv   r   )rp   r   rv   r	  )1rw   rx   ry   __doc__rh   r  propertyr  r  r  r  r  r  rU  rW  rd  rl  rq  rv  r~  r  r?   rn  rh  rs  r  r  r_  ro  ri  rt  r  r  r  r  r  r  r`  rp  rj  ru  r  r  classmethodr  r  r  r  r
  r8   rL   rJ   r   r   r  s   0 $59.204 ;?15.2B*" 37EI/36:AE+B*"B* (B* 	B*
 8B* B* &3B* ,B* .B*"
B* 9B* "/B* ,B*" #0#B*$ +C%B*&  -'B*( 4)B** !?+B*, -B*H!2 
 
A +/Q
 %)QQ (Q
 "Q Q 
QB +/X
 %)XX (X
 "X X 
XB +/;/
 %);/:;/ (;/
 ";/ 
;/@ +/7/
 %)7/:7/ (7/
 "7/ 
7/r & 
 &* 
 #  
( '(KK #K36K	%K$ 
 &* 
 #  
( '(KK #K36K	%K" 

 ,0

 

 )
 
 
&
$ 

 ,0

 

 )
 
 
&
  $%3BF33 32?3RU3	3" $%3BF33 32?3RU3	3* !%	.. . 	.
 . 
&.4 !%	55 5 	5
 5 
&58   
 "& 
 
  
 	 

  
  
  
 
& 
J  %
 "&%
%
 %
 	%

 %
 %
 %
 
&%
N3 $%33 3033	3& !%	.. . 	.
 . 
&.2 !%	55 5 	5
 5 
&56 

 ,0

 

 )
 
 
&
$ 

 ,0

 

 )
 
 
&
$  #
 "&#
#
 #
 	#

 #
 #
 #
 
&#
P  &
 "&&
&
 &
 	&

 &
 &
 &
 
&&
\ "&"
"
 "
 	"
 "
 "
 
"
T "&"
"
 "
 	"
 "
 "
 
$"
J $%66 6036	6( $%66 6036	6, 9@	% ,0%% % 7	% )% % 
&%T 9@	% ,0%% % 7	% )% % 
&%P $%UBFUU U2?URUU	,Up $%VBFVV V2?VRUV	,Vp 
 +/%' "/3+.2  (	
  #   -  ,  
 2 
 +/%' "/3+.2  (	
  #   -  ,  
 2 
 +/	 &( "+.2:  (	  #   ,  
 : 
 +/	 &( "+.2:  (	  #   ,  
 <VrL   r   c                      e Zd ZU dZded<   	 dZded<   	 dZded	<   	 i Zd
ed<   	 dZded<    e	d      Z
 ed      edd              Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZy)r	  z-Retriever that uses `Azure Cognitive Search`.r   r  r[  rr   r   r   rc   rI  dictsearch_kwargs)rZ  similarity_score_thresholdr[  hybrid_score_thresholdr\  semantic_hybrid_score_thresholdzClassVar[Collection[str]]allowed_search_typesT)arbitrary_types_allowedbefore)modec                j    d|v r.|d   }|| j                   vrt        d| d| j                          |S )zValidate search type.r   r]  z  not allowed. Valid values are: )r'  r   )r  valuesr   s      rJ   validate_search_typez4AzureSearchVectorStoreRetriever.validate_search_type  sS     F" /K#":":: %k]2R//02  rL   c                   i | j                   |}| j                  dk(  r+ | j                  j                  |fd| j                  i|}|S | j                  dk(  r= | j                  j
                  |fd| j                  i|D cg c]  \  }}|	 }}}|S | j                  dk(  r+ | j                  j                  |fd| j                  i|}|S | j                  dk(  r= | j                  j                  |fd| j                  i|D cg c]  \  }}|	 }}}|S | j                  dk(  r+ | j                  j                  |fd| j                  i|}|S | j                  dk(  r= | j                  j                  |fd| j                  i|D cg c]  \  }}|	 }}}|S t        d| j                   d	      c c}}w c c}}w c c}}w 
NrZ  rI  r$  r[  r%  r\  r&  r]  r^  )r#  r   r  r?   rI  r~  r_  r  r`  rj  r   )rf   rb  run_managerrp   paramsrc  rH  r  s           rJ   _get_relevant_documentsz7AzureSearchVectorStoreRetriever._get_relevant_documents  s-    2D&&1&1|+14##11%L466LVLD8 7 !== Wd..VV!VV'-C D 4 ) )14##11%L466LVLD& % !99 Sd..RR!VV'-C D "  !22:4##::5UDFFUfUD  !BB Qd..PP!VV'-C D   t/?/?.@NOO3s   F<GGc               *  K   i | j                   |}| j                  dk(  r3 | j                  j                  |fd| j                  i| d {   }|S | j                  dk(  rG | j                  j
                  |fd| j                  i| d {   }|D cg c]  \  }}|	 }}}|S | j                  dk(  r3 | j                  j                  |fd| j                  i| d {   }|S | j                  dk(  rG | j                  j                  |fd| j                  i| d {   }|D cg c]  \  }}|	 }}}|S | j                  dk(  r3 | j                  j                  |fd| j                  i| d {   }|S | j                  dk(  rE | j                  j                  |fd| j                  i| d {   D cg c]  \  }}|	 }}}|S t        d| j                   d	      7 7 ec c}}w 7 7 c c}}w 7 7 Jc c}}w wr/  )r#  r   r  rn  rI  r  ro  r  rp  ru  r   )	rf   rb  r0  rp   r1  rc  r  rH  r  s	            rJ   _aget_relevant_documentsz8AzureSearchVectorStoreRetriever._aget_relevant_documents  sn     2D&&1&1|+8))88S$&&SFSSD< ; !==Od&&OO!VV'-  
 '66FCC6D6. - )8))88S$&&SFSSD* ) !99Kd&&KK!VV'-  
 '66FCC6D6  !22A))AA#) D  !BB %XD$4$4$W$W%!VV%'-% C D   t/?/?.@NOO; T 7S 7s   AHG2AHG5	HG8&A H&G>'AH(H)	H2H>A H>H	?AH HHHH5H8HHHHHN)r,  r
   rv   r   )rb  rr   r0  r   rp   r   rv   r  )rb  rr   r0  r   rp   r   rv   r  )rw   rx   ry   r  __annotations__r   rI  r#  r'  r   model_configr   r   r-  r2  r4  r8   rL   rJ   r	  r	    s    7?K- AsJ(M473   $L (#	  $	%% 4% 	%
 
%N(( 9	(
 ( 
(rL   r	  c                \    | D cg c]  }t        |      t        |d         f }}|S c c}w Nr  _result_to_documentr  r  r}  rc  s      rJ   r  r    sF     
   '&)*	
D  Ks    )c                t   K   | 2 cg c3 d {   }t        |      t        |d         f#7 6 }}|S c c}w wr8  r9  r;  s      rJ   r  r  +  sU      $ 
   '&)*	
 D  Ks$   83.,
.3.3	8c                ,  K   | 2 cg c3 d {   }t        |      t        |d         |t           f+7 &6 nc c}w }}t        t        t        |       \  }}}t        ||||      }	g }
|	D ]#  }|dk(  r |
S |
j                  ||   ||   f       % |
S wNr  )rI  r  r:  r  r   mapr   r  r   r?  r  r  r  rI  r}  rc  r%  scoresvectorsnew_orderingretr   s               rJ   r  r  8  s      $  	  '&)*()	
  D  "%T3:!6Ivw .A;L
 )+C .7 J 	

IaL&),-	. Js'   B864
6#868ABc                   | D cg c]#  }t        |      t        |d         |t           f% }}|sg S t        t        t        |       \  }}}t        ||||      }	g }
|	D ]#  }|dk(  r |
S |
j                  ||   ||   f       % |
S c c}w r>  r@  rB  s               rJ   r  r  Y  s      	  '&)*()	
D  	!$T3:!6Ivw .A;L
 )+C .7 J 	

IaL&),-	. J3s   (Bc                x   t         | v r>t        | t            t              r
| t            }nNt        j                  | t                  }n1| j                         D ci c]  \  }}|t        t        fvr|| }}}t        | v rt        | j                  t              i}ni }t        | t           i ||      S c c}}w )Nr  )r   r   r"  r<  r  r   r   r   r   rg  r   )r}  fields_metadatar-   r   	fields_ids        rJ   r:  r:  |  s    & f_-t4$_5O"jj)@AO %lln
U0.AA J
 
 F

9 56		N+


 
s   B6c                    	 t        |       }t        |      }t        j                  |g|      } | |fS # t        $ r | |fcY S w xY wr7   )iternext	itertoolschainStopIteration)iterabler/   iteratorr   s       rJ   r  r    sO    !>X??E7H5 !  !s   03 AA)NNNNNNNNN"langchain-comm-python-azure-searchNFNNN)$r:   rr   r;   rr   r-   rt   r<   rt   r=   rt   r>   r  r?   r  r@   r  rA   r  rB   rt   rC   r  rD   rt   rE   r  rF   zLiteral[False]rG   r  rH   r  rI   r  rv   zUnion[SearchClient])NNNNNNNNNrS  NTNNN)$r:   rr   r;   rr   r-   rt   r<   rt   r=   rt   r>   r  r?   r  r@   r  rA   r  rB   rt   rC   r  rD   rt   rE   r  rF   zLiteral[True]rG   r  rH   r  rI   r  rv   zUnion[AsyncSearchClient])$r:   rr   r;   rr   r-   rt   r<   rt   r=   rt   r>   r  r?   r  r@   r  rA   r  rB   rt   rC   r  rD   rt   rE   r  rF   ru   rG   r  rH   r  rI   r  rv   z&Union[SearchClient, AsyncSearchClient])r  SearchItemPaged[Dict]rv   r  )r  AsyncSearchItemPaged[Dict]rv   r  )r  r   )
r  rU  r  
np.ndarrayr  r  rI  rc   rv   r  )
r  rT  r  rV  r  r  rI  rc   rv   r  )r}  r
   rv   r   r7   )rQ  r   r/   zOptional[Any]rv   zTuple[Iterable, Any])R
__future__r   r   r4  r   rN  r<  loggingrd   r2  typesr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   numpyr8  langchain_core.callbacksr   r   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.exceptionsr   langchain_core.retrieversr   langchain_core.utilsr   langchain_core.vectorstoresr   pydanticr   r   &langchain_community.vectorstores.utilsr   	getLoggerr  r   r   r   r!   r   r"   r#   r   r$   r   r   r&   r'   r(   r)   r*   r   r   r   r   r@  rK   r   r	  r  r  r  r  r:  r  r8   rL   rJ   <module>rf     s   "             $  . 0 8 3 - 3 0 M				6AD  )@$	 $(
 %+/ 
  *BJ   
 +/15*.,0 	7;-126 D*."AE26=A' 
 )	
 "/ ( * 5 + 0  (  !" '?#$ 0%& ;'( ) 
. 
 +/15*.,0 	7;-126 D*. AE26=A'### 
# )	#
 "/# (# *## 5# +# 0# # (#  !#" '?##$ 0%#& ;'#( )# 
#4 +/15*.,0 	7;-126 D*.AE26=A'i
i
i
 
i
 )	i

 "/i
 (i
 *i
i
 5i
 +i
 0i
 i
 (i
  !i
" '?#i
$ 0%i
& ;'i
( ,)i
XhV+ hVV)~m ~B
"
!

'
!
  	'  	
 "H 	 "     	 
 " F6!rL   