o
    Zh
                    @  s<  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	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l,m-Z- e. Z/e
rd dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8 d dl7m5Z9 d dl:m;Z;m<Z<m=Z=m>Z>m?Z? e&ddddZ@e&ddddZAe&ddddZBe&ddddZCdZD															dcddd=d>ZEG d?d@ d@e(ZFG dAdB dBe$ZGdedFdGZHdfdIdJZI	K	LdgdhdSdTZJ	K	LdgdhdUdVZKdidZd[ZLdjdkdadbZMdS )l    )annotationsN)TYPE_CHECKINGAnyCallableClassVar
CollectionDictIterableListLiteralOptionalTupleTypeUnioncast)#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VectorSearchZAZURESEARCH_FIELDS_IDid)keyZenv_keydefaultZAZURESEARCH_FIELDS_CONTENTcontentZ!AZURESEARCH_FIELDS_CONTENT_VECTORZcontent_vectorZAZURESEARCH_FIELDS_TAGmetadatai  "langchain-comm-python-azure-searchFendpointstr
index_namer(   Optional[str]azure_ad_access_tokensemantic_configuration_namefieldsOptional[List[SearchField]]vector_searchOptional[VectorSearch]semantic_configurationsCOptional[Union[SemanticConfiguration, List[SemanticConfiguration]]]scoring_profilesOptional[List[ScoringProfile]]default_scoring_profiledefault_fields
user_agentcors_optionsOptional[CorsOptions]async_bool additional_search_client_optionsOptional[Dict[str, Any]]azure_credentialOptional[TokenCredential]azure_async_credentialOptional[AsyncTokenCredential]return&Union[SearchClient, AsyncSearchClient]c           0        s   ddl m m}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  fd	d
d
|}'|p[i }|
p_g }
|d ur|| dkru| }(|(d |(})n||}(|(})n|d ur|'|}(|(})n
|p| }(|p| })|d0| |(|d|}*z|*j|d W n |yd   |d urdd |D dd |
D fddt ! t !  D }+t"|+dkrd1fddd#fdd|+D },t$d|+ d|, n|
}|d u r&|#|d|$j%|ddd |%j&d!d"|d#|$j'||%j&d$d"g|&d%dd&|&d'd#d&gd(}|r9t(|t)s2|g}|"||d)}-n|rP|||!| t*d*gd+d,}.|"|.gd-}-nd }-|||||-||	|d.}/|*+|/ Y nw |st|d0| ||(|d/|S |d0| ||)|d/|S )2Nr   )AccessTokenAzureKeyCredentialr   )ResourceNotFoundError)DefaultAzureCredentialInteractiveBrowserCredential)rM   r!   )SearchIndexClient)#ExhaustiveKnnAlgorithmConfigurationExhaustiveKnnParametersHnswAlgorithmConfigurationHnswParametersSearchIndexr%   SemanticFieldSemanticPrioritizedFieldsSemanticSearchr&   VectorSearchAlgorithmKindVectorSearchAlgorithmMetricVectorSearchProfilec                      s.   e Zd Zd fddZdddddddZdS )z6_get_search_client.<locals>.AzureBearerTokenCredentialtokenr.   c                   s    |t t d | _d S )Ni  )inttime_token)selfr[   rJ    c/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/vectorstores/azuresearch.py__init__   s   z?_get_search_client.<locals>.AzureBearerTokenCredential.__init__NF)claims	tenant_id
enable_caescopesrd   r0   re   rf   rA   kwargsr   rH   rJ   c                _  s   | j S N)r^   )r_   rd   re   rf   rg   rh   ra   ra   rb   	get_token   s   z@_get_search_client.<locals>.AzureBearerTokenCredential.get_token)r[   r.   )rg   r.   rd   r0   re   r0   rf   rA   rh   r   rH   rJ   )__name__
__module____qualname__rc   rj   ra   r`   ra   rb   AzureBearerTokenCredential   s    rn   ZINTERACTIVEz!https://search.azure.com/.default)r-   
credentialr=   namec                 S     i | ]}|j |jqS ra   rq   type).0fra   ra   rb   
<dictcomp>       z&_get_search_client.<locals>.<dictcomp>c                 S  rr   ra   rs   )ru   dfra   ra   rb   rw      rx   c                   s   i | ]	\}}| | qS ra   ra   ru   r(   value)mandatory_fieldsra   rb   rw      s    xr.   rH   c              
     s:   |  d  | d d |  d |  d|   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)r}   )fields_typesr|   ra   rb   fmt_err   s   z#_get_search_client.<locals>.fmt_err
c                   s   g | ]} |qS ra   ra   ru   r}   )r   ra   rb   
<listcomp>       z&_get_search_client.<locals>.<listcomp>z2You need to specify at least the following fields z; or provide alternative field names in the env variables.

r)      i  i  )mZef_constructionZ	ef_searchmetric)rq   kind
parametersZdefault_exhaustive_knn)r   myHnswProfile)rq   Zalgorithm_configuration_nameZmyExhaustiveKnnProfile)Z
algorithmsZprofiles)configurationsZdefault_configuration_name)
field_name)Zcontent_fields)rq   Zprioritized_fields)r   )rq   r3   r5   semantic_searchr9   r;   r>   )r-   r/   ro   r=   ra   )r}   r.   rH   r.   ),azure.core.credentialsrJ   rK   r   Zazure.core.exceptionsrL   Zazure.identityrM   rN   Zazure.identity.aioazure.search.documentsr   azure.search.documents.aioZazure.search.documents.indexesrO   %azure.search.documents.indexes.modelsrP   rQ   rR   rS   rT   r%   rU   rV   rW   r&   rX   rY   rZ   r   rj   Z	get_indexsetitemslenjoin
ValueErrorZHNSWZCOSINEZEXHAUSTIVE_KNN
isinstancelistFIELDS_CONTENTZcreate_index)0r-   r/   r(   r1   r2   r3   r5   r7   r9   r;   r<   r=   r>   r@   rB   rD   rF   rK   r   rL   rM   rN   ZAsyncDefaultAzureCredentialr   AsyncSearchClientrO   rP   rQ   rR   rS   rT   r%   rU   rV   rW   r&   rX   rY   rZ   rn   ro   Zasync_credentialZindex_clientZmissing_fieldserrorr   Zsemantic_configurationindexra   )rJ   r   r   r|   rb   _get_search_clientS   s   <







!
	ar   c                   @  sL  e Zd ZdZ								ddddddddd$d%Zdd(d)Zedd+d,Zdd/d0Z	ddd1dd9d:Z		ddd1dd;d<Z
	ddd1dd?d@Z	ddd1ddAdBZdddEdFZdddGdHZ	IdddJddOdPZdIdQddSdTZ	IdddJddUdVZdIdQddWdXZ	IdddYdd\d]Z	IdddYdd^d_Z	Iddd`ddbdcZ	Iddd`ddddeZ	I	dddfdgZ	I	dddhdiZ	I	j	kddd`ddodpZ	I	j	kddd`ddqdrZdddsdtZ	IdddudvZ	I	dddwdxZ	I	dddydzZ	IdddYdd{d|Z	IdddYdd}d~Z 	I	j	kddd`dddZ!	I	j	kddd`dddZ"dd`dddZ#dd`dddZ$	IddddZ%	IddddZ&	I	dddYdddZ'	I	dddYdddZ(	Iddd`dddZ)	Iddd`dddZ*e+						ddddZ,e+						ddddZ-e+	dddddddddZ.e+	dddddddddZ/dddZ0dS )AzureSearchz&`Azure Cognitive Search` vector store.hybridN)vector_search_dimensionsrB   r1   rD   rF   azure_search_endpointr.   azure_search_keyr0   r/   embedding_functionUnion[Callable, Embeddings]search_typer2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r>   r?   r   Optional[int]rB   rC   r1   rD   rE   rF   rG   rh   r   c                K  s  zddl m}m}m}m} W n ty } ztd|d }~ww 	 || _t| jtr0| jj	| _	n| j| _	|t
|jddd|t|jd|t||jd|pRt| 	ddd	|t|jdg}d
}d|v ro|d ro|d|d  7 }t||||||||	|
||||||d| _t||||||||	|
||||d||d| _|| _|| _|r|n|| _|| _|| _|| _|| _|| _|| _|	| _|
| _|| _ || _!|| _"|| _#d S )Nr   )SearchableFieldr$   SearchFieldDataTypeSimpleFieldzeUnable to import azure.search.documents. Please install with `pip install -U azure-search-documents`.T)rq   rt   r(   Z
filterablers   Textr   )rq   rt   Z
searchabler   Zvector_search_profile_nameZ	langchainr=    )r2   r3   r5   r7   r9   r;   r<   r=   r>   rB   rD   )r2   r3   r5   r7   r9   r;   r<   r=   r>   r@   rD   rF   )$r   r   r$   r   r   ImportErrorr   r   r   embed_query	FIELDS_IDStringr   FIELDS_CONTENT_VECTORr   Singler   FIELDS_METADATAr   clientasync_clientr   r2   r3   Z_azure_search_endpointZ_azure_search_keyZ_index_nameZ_semantic_configuration_name_fieldsZ_vector_searchZ_semantic_configurationsZ_scoring_profilesZ_default_scoring_profileZ_default_fieldsZ_user_agentZ_cors_options)r_   r   r   r/   r   r   r2   r3   r5   r7   r9   r;   r>   r   rB   r1   rD   rF   rh   r   r$   r   r   er<   r=   ra   ra   rb   rc      s   

zAzureSearch.__init__rH   Nonec                 C  s   t | dr| jr| j  t | drZ| jr\zt }| r)|| j  W d S || j  W d S  t	yY   t
 }t| z|| j  W |  Y d S |  w w d S d S )Nr   r   )hasattrr   closer   asyncioget_event_loop
is_runningcreate_taskrun_until_completeRuntimeErrornew_event_loopset_event_loop)r_   loopra   ra   rb   __del__  s    

zAzureSearch.__del__Optional[Embeddings]c                 C  s   t | jtr	| jS d S ri   )r   r   r   r_   ra   ra   rb   
embeddings  s
   
zAzureSearch.embeddingstextList[float]c                   s*   | j r| j |I d H S tt| j|S ri   )r   aembed_queryr   r   r   )r_   r   ra   ra   rb   _aembed_query  s   zAzureSearch._aembed_querykeystextsIterable[str]	metadatasOptional[List[dict]]r   Optional[List[str]]	List[str]c                  s   t  jtr#z
 jt|}W n ty"    fdd|D }Y n
w  fdd|D }t|dkr9td g S |sMd|v rMt|d t|krM|d } j	t
||||dS )z$Add texts data to an existing index.c                   s   g | ]} j |qS ra   )r   r   r   r   ra   rb   r     s    z)AzureSearch.add_texts.<locals>.<listcomp>c                      g | ]}  |qS ra   r   r   r   ra   rb   r     rx   r   Nothing to insert, skipping.idsr   )r   r   r   Zembed_documentsr   NotImplementedErrorr   loggerdebugadd_embeddingszipr_   r   r   r   rh   r   ra   r   rb   	add_texts  s   

 zAzureSearch.add_textsc                  s   t  jtr*z jt|I d H }W n ty)    fdd|D I d H }Y n
w  fdd|D }t|dkr@td g S |sTd|v rTt|d t|krT|d } j	t
||||dI d H S )Nc                   s    g | ]} j |I d H qS ri   )r   r   r   r   ra   rb   r     s    z*AzureSearch.aadd_texts.<locals>.<listcomp>c                   r   ra   r   r   r   ra   rb   r     rx   r   r   r   r   )r   r   r   Zaembed_documentsr   r   r   r   r   aadd_embeddingsr   r   ra   r   rb   
aadd_texts  s    

 zAzureSearch.aadd_textstext_embeddings!Iterable[Tuple[str, List[float]]]c                  s8  g }g }t |D ]u\}\}}|r|| }	ntt }	tt|	dd}	|r,|| ni }
ddt|	t	|t
tj|tjd tt|
i}|
rW fdd|
 D }|| || ||	 t|tkr} jj|d}td	d
 |D s{t|g }qt|dkr|S  jj|d}tdd
 |D r|S t|)$Add embeddings to an existing index.utf-8ascii@search.actionuploadZdtypec                   *   i | ]\}}|d d  j D v r||qS )c                 S     g | ]}|j qS ra   rp   r   ra   ra   rb   r   $      z9AzureSearch.add_embeddings.<locals>.<dictcomp>.<listcomp>r3   ru   kvr   ra   rb   rw   !  
    z.AzureSearch.add_embeddings.<locals>.<dictcomp>	documentsc                 s      | ]}|j V  qd S ri   Z	succeededru   rra   ra   rb   	<genexpr>-      z-AzureSearch.add_embeddings.<locals>.<genexpr>r   c                 s  r   ri   r   r   ra   ra   rb   r   9  r   )	enumerater.   uuiduuid4base64urlsafe_b64encodebytesdecoder   r   r   nparrayfloat32tolistr   jsondumpsr   updateappendr   MAX_UPLOAD_BATCH_SIZEr   upload_documentsallr   r_   r   r   r   r   datair   	embeddingr(   r+   docZadditional_fieldsresponsera   r   rb   r     sB   





zAzureSearch.add_embeddingsc                  sD  g }g }t |D ]w\}\}}|r|| ntt }	tt|	dd}	|r,|| ni }
ddt|	t	|t
tj|tjd tt|
i}|
rW fdd|
 D }|| || ||	 t|tkr jj|dI d	H }td
d |D s~t|g }q	t|dkr|S  jj|dI d	H }tdd |D r|S t|)r   r   r   r   r   r   c                   r   )c                 S  r   ra   rp   r   ra   ra   rb   r   ]  r   z:AzureSearch.aadd_embeddings.<locals>.<dictcomp>.<listcomp>r   r   r   ra   rb   rw   Z  r   z/AzureSearch.aadd_embeddings.<locals>.<dictcomp>r   Nc                 s  r   ri   r   r   ra   ra   rb   r   f  r   z.AzureSearch.aadd_embeddings.<locals>.<genexpr>r   c                 s  r   ri   r   r   ra   ra   rb   r   r  r   )r   r.   r   r   r   r   r   r  r   r   r   r  r  r  r  r   r  r  r   r  r	  r   r
  r   r  r  r   r  ra   r   rb   r   >  s@   




zAzureSearch.aadd_embeddingsr   rA   c                 K  s*   |r| j dd |D }t|dkS dS )Delete by vector ID.

        Args:
            ids: List of ids to delete.

        Returns:
            bool: True if deletion is successful,
            False otherwise.
        c                 S  s   g | ]}t |iqS ra   )r   ru   r  ra   ra   rb   r     r   z&AzureSearch.delete.<locals>.<listcomp>r   F)r   delete_documentsr   r_   r   rh   resra   ra   rb   deletew  s   
zAzureSearch.deletec                   s2   |r| j dd |D I dH }t|dkS dS )r  c                 S  s   g | ]}d |iqS )r'   ra   r  ra   ra   rb   r     r   z'AzureSearch.adelete.<locals>.<listcomp>Nr   F)r   r  r   r  ra   ra   rb   adelete  s
   
zAzureSearch.adeleter   )r   queryr   r\   List[Document]c                K  s   |p| j }|dkr| j|fd|i|}|S |dkr'| j|fd|i|}|S |dkr8| j|fd|i|}|S td| dN
similarityr   r   semantic_hybridsearch_type of  not allowed.)r   r5   hybrid_searchsemantic_hybrid_searchr   r_   r  r   r   rh   docsra   ra   rb   similarity_search  s   
zAzureSearch.similarity_search)r   List[Tuple[Document, float]]c                K  sx   | d| j}|dkr| j|fd|i|S |dkr%| j|fd|i|S |dkr4| j|fd|i|S td| d)$Run similarity search with distance.r   r  r   r   r  r  r   )popr   vector_search_with_scorehybrid_search_with_score!semantic_hybrid_search_with_scorer   r_   r  r   rh   r   ra   ra   rb   similarity_search_with_score  s   z(AzureSearch.similarity_search_with_scorec                  s   |p| j }|dkr| j|fd|i|I d H }|S |dkr.| j|fd|i|I d H }|S |dkrB| j|fd|i|I d H }|S td| dr  )r   avector_searchahybrid_searchasemantic_hybrid_searchr   r#  ra   ra   rb   asimilarity_search  s   
zAzureSearch.asimilarity_searchc                  s   | d| j}|dkr| j|fd|i|I dH S |dkr,| j|fd|i|I dH S |dkr>| j|fd|i|I dH S td| d)	r'  r   r  r   Nr   r  r  r   )r   r   avector_search_with_scoreahybrid_search_with_score"asemantic_hybrid_search_with_scorer   r,  ra   ra   rb   asimilarity_search_with_score  s   z)AzureSearch.asimilarity_search_with_scorescore_thresholdr7  Optional[float]c                  4   | j |fd|i|} d u r|S  fdd|D S )Nr   c                      g | ]
}|d   kr|qS    ra   r   r6  ra   rb   r         zGAzureSearch.similarity_search_with_relevance_scores.<locals>.<listcomp>r)  r_   r  r   r7  rh   resultra   r6  rb   'similarity_search_with_relevance_scores     z3AzureSearch.similarity_search_with_relevance_scoresc                  <   | j |fd|i|I d H } d u r|S  fdd|D S )Nr   c                   r:  r;  ra   r   r6  ra   rb   r     r=  zHAzureSearch.asimilarity_search_with_relevance_scores.<locals>.<listcomp>r2  r?  ra   r6  rb   (asimilarity_search_with_relevance_scores     z4AzureSearch.asimilarity_search_with_relevance_scores)filtersrG  c                K  s   | j |||d}dd |D S )\  
        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.
        r   rG  c                 S     g | ]\}}|qS ra   ra   ru   r  _ra   ra   rb   r     r   z-AzureSearch.vector_search.<locals>.<listcomp>r>  r_   r  r   rG  rh   docs_and_scoresra   ra   rb   r5     s   zAzureSearch.vector_searchc                  s&   | j |||dI dH }dd |D S )rH  rI  Nc                 S  rJ  ra   ra   rK  ra   ra   rb   r     r   z.AzureSearch.avector_search.<locals>.<listcomp>rD  rM  ra   ra   rb   r.    s
   zAzureSearch.avector_searchc                 K  s,   |  |}| 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
         rG  r   _simple_search_results_to_documentsr_   r  r   rG  rh   r  resultsra   ra   rb   r)    s   
z$AzureSearch.vector_search_with_scorec                   s@   |  |I dH }| j|d|fd|i|I dH }t|I dH S )rO  NrP  rG  r   _asimple_search_aresults_to_documentsrT  ra   ra   rb   r2  1  s   z%AzureSearch.avector_search_with_score         ?fetch_klambda_multfloatc          	      K  s:   |  |}| j|d|fd|i|}t|t|||d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
        rP  rG  query_embeddingr\  r   r   rR  0_reorder_results_with_maximal_marginal_relevancer  r  	r_   r  r   r[  r\  rG  rh   r  rU  ra   ra   rb   (max_marginal_relevance_search_with_scoreJ  s
   
z4AzureSearch.max_marginal_relevance_search_with_scorec          	        sN   |  |I dH }| j|d|fd|i|I dH }t|t|||dI dH S )r^  NrP  rG  r_  r   rW  1_areorder_results_with_maximal_marginal_relevancer  r  rc  ra   ra   rb   )amax_marginal_relevance_search_with_scorel  s   z5AzureSearch.amax_marginal_relevance_search_with_scorec                 K  $   | j |fd|i|}dd |D S )rH  r   c                 S  rJ  ra   ra   rK  ra   ra   rb   r     r   z-AzureSearch.hybrid_search.<locals>.<listcomp>r*  r_   r  r   rh   rN  ra   ra   rb   r!    s   zAzureSearch.hybrid_searchc                   ,   | j |fd|i|I dH }dd |D S )rH  r   Nc                 S  rJ  ra   ra   rK  ra   ra   rb   r     r   z.AzureSearch.ahybrid_search.<locals>.<listcomp>r3  rj  ra   ra   rb   r/    s   zAzureSearch.ahybrid_searchc                 K  s,   |  |}| 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
        rG  rQ  rT  ra   ra   rb   r*    s   
z$AzureSearch.hybrid_search_with_scorec                   s@   |  |I dH }| j|||fd|i|I dH }t|I dH S )rm  NrG  rV  rT  ra   ra   rb   r3    s   z%AzureSearch.ahybrid_search_with_scorec                  r9  )Nr   c                   r:  r;  ra   r   r6  ra   rb   r     r=  zCAzureSearch.hybrid_search_with_relevance_scores.<locals>.<listcomp>ri  r?  ra   r6  rb   #hybrid_search_with_relevance_scores  rB  z/AzureSearch.hybrid_search_with_relevance_scoresc                  rC  )Nr   c                   r:  r;  ra   r   r6  ra   rb   r     r=  zDAzureSearch.ahybrid_search_with_relevance_scores.<locals>.<listcomp>rl  r?  ra   r6  rb   $ahybrid_search_with_relevance_scores  rF  z0AzureSearch.ahybrid_search_with_relevance_scoresc          	      K  s:   |  |}| j|||fd|i|}t|t|||d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
        rG  r_  ra  rc  ra   ra   rb   /hybrid_max_marginal_relevance_search_with_score  s   
z;AzureSearch.hybrid_max_marginal_relevance_search_with_scorec          	        sN   |  |I dH }| j|||fd|i|I dH }t|t|||dI dH S )rp  NrG  r_  re  rc  ra   ra   rb   0ahybrid_max_marginal_relevance_search_with_score"  s   z<AzureSearch.ahybrid_max_marginal_relevance_search_with_scorer  
text_querySearchItemPaged[dict]c                K  sD   ddl m} | jjd||tj|tjd |tdg||d|S a  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   VectorizedQueryr   vectorZk_nearest_neighborsr3   )search_textvector_queriesfiltertopNra   )	azure.search.documents.modelsrw  r   searchr  r  r  r  r   r_   r  rs  r   rG  rh   rw  ra   ra   rb   rR  J  s   zAzureSearch._simple_searchAsyncSearchItemPaged[dict]c                  sL   ddl m} | jjd||tj|tjd |tdg||d|I dH S ru  )	r~  rw  r   r  r  r  r  r  r   r  ra   ra   rb   rW  n  s   zAzureSearch._asimple_searchc                 K  rh  )  
        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.
        r   c                 S     g | ]\}}}|qS ra   ra   rK  ra   ra   rb   r     rx   z6AzureSearch.semantic_hybrid_search.<locals>.<listcomp>,semantic_hybrid_search_with_score_and_rerankrj  ra   ra   rb   r"    s   z"AzureSearch.semantic_hybrid_searchc                   rk  )r  r   Nc                 S  r  ra   ra   rK  ra   ra   rb   r     rx   z7AzureSearch.asemantic_hybrid_search.<locals>.<listcomp>-asemantic_hybrid_search_with_score_and_rerankrj  ra   ra   rb   r0    s   z#AzureSearch.asemantic_hybrid_searchscore
score_type"Literal['score', 'reranker_score']c                  sN   | j |fd|i|}|dkr fdd|D S |dkr% fdd|D S dS )  
        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.
        r   r  c                   *   g | ]\}}} d u s| kr||fqS ri   ra   ru   r  r  rL  r6  ra   rb   r     
    zAAzureSearch.semantic_hybrid_search_with_score.<locals>.<listcomp>reranker_scorec                   *   g | ]\}}} d u s| kr||fqS ri   ra   ru   r  rL  r  r6  ra   rb   r     r  Nr  r_   r  r   r  r7  rh   rN  ra   r6  rb   r+    s    

z-AzureSearch.semantic_hybrid_search_with_scorec                  sV   | j |fd|i|I dH }|dkr fdd|D S |dkr) fdd|D S dS )r  r   Nr  c                   r  ri   ra   r  r6  ra   rb   r     r  zBAzureSearch.asemantic_hybrid_search_with_score.<locals>.<listcomp>r  c                   r  ri   ra   r  r6  ra   rb   r      r  r  r  ra   r6  rb   r4    s"   

z.AzureSearch.asemantic_hybrid_search_with_score#List[Tuple[Document, float, float]]c          
        s   ddl m} | jjd||tj| |tjd |t	dg|d| j
dd|d|}| p/g }i  |D ]}|j|jd |j< q4 fd	d
|D }	|	S )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   rv  r   rx  semantic
extractiverz  r{  r|  Z
query_typer2   Zquery_captionZquery_answerr}  r   
highlightsc                   s   g | ]^}t |ti t|v rt|tini t|v r"t|t nd d | D |drE|di gd j	|di gd j
dni  |tddddt|d t|d	 fqS )
c                 S     i | ]\}}|t kr||qS ra   r   r   ra   ra   rb   rw   <  
    zWAzureSearch.semantic_hybrid_search_with_score_and_rerank.<locals>.<listcomp>.<dictcomp>@search.captionsr   r  rP  ZcaptionsZanswersZpage_contentr+   @search.score@search.reranker_scorer   r(  r   r   r   r  loadsr   r   r   r  r]  ru   r@  Zsemantic_answers_dictra   rb   r   /  sL    *	

%
zLAzureSearch.semantic_hybrid_search_with_score_and_rerank.<locals>.<listcomp>Nra   )r~  rw  r   r  r  r  r   r  r  r   r2   get_answersr   r  r(   )
r_   r  r   rG  rh   rw  rU  semantic_answerssemantic_answerr$  ra   r  rb   r    s8   
*,z8AzureSearch.semantic_hybrid_search_with_score_and_rerankc                  s   ddl m} | |I dH }| jjd||tj|tjd |t	dg|d| j
dd|d|I dH }| I dH p;g }i  |D ]}	|	j|	jd	 |	j< q@ fd
d|2 I dH }
|
S )r  r   rv  Nr   rx  r  r  r  r  c                   s   g | zb3 d H W }t |ti t|v rt|tini t|v r't|t ndd | D |drJ|di gd j	|di gd j
dni  |tddddt|d	 t|d
 fq6 S )Nc                 S  r  ra   r  r   ra   ra   rb   rw     r  zXAzureSearch.asemantic_hybrid_search_with_score_and_rerank.<locals>.<listcomp>.<dictcomp>r  r   r  rP  r  r  r  r  r  r  r  ra   rb   r     sN    *	

%
zMAzureSearch.asemantic_hybrid_search_with_score_and_rerank.<locals>.<listcomp>ra   )r~  rw  r   r   r  r  r  r  r  r   r2   r  r   r  r(   )r_   r  r   rG  rh   rw  ry  rU  r  r  r$  ra   r  rb   r  ]  s<   
*,z9AzureSearch.asemantic_hybrid_search_with_score_and_rerankrP  langchain-indexclsType[AzureSearch]r   c	                 K  s4   | ||||f||d|	}
|
j ||fi |	 |
S N)r3   r1   )r   r  r   r  r   r   r   r1   r/   r3   rh   azure_searchra   ra   rb   
from_texts  s   	zAzureSearch.from_textsc	                   s<   | ||||f||d|	}
|
j ||fi |	I d H  |
S r  )r   r  ra   ra   rb   afrom_texts  s   	zAzureSearch.afrom_texts)r   r   r/   r3   c             	     sd   t |\}}	|	d u rtdt|	d }
| d||||||
d|}|j||fi |I d H  |S Nz0Cannot create AzureSearch from empty embeddings.r<  )r   r   r/   r   r3   r   ra   )_peekr   r   r   r  r   r  r   r   r   r/   r3   rh   Zfirst_text_embeddingr   r  ra   ra   rb   afrom_embeddings  s"   	zAzureSearch.afrom_embeddingsc             	   K  s\   t |\}}	|	d u rtdt|	d }
| d||||||
d|}|j||fi | |S r  )r  r   r   r   r  ra   ra   rb   from_embeddings  s    	zAzureSearch.from_embeddingsAzureSearchVectorStoreRetrieverc                 K  sN   | d| j}||d< |ddpg }||   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vectorstorera   )r   r   r(  extendZ_get_retriever_tagsr  )r_   rh   r   r  ra   ra   rb   as_retriever&  s
   zAzureSearch.as_retriever)r   NNNNNNN)$r   r.   r   r0   r/   r.   r   r   r   r.   r2   r0   r3   r4   r5   r6   r7   r8   r9   r:   r;   r0   r>   r?   r   r   rB   rC   r1   r0   rD   rE   rF   rG   rh   r   )rH   r   )rH   r   )r   r.   rH   r   ri   )
r   r   r   r   r   r   rh   r   rH   r   )r   r   r   r   r   r   rH   r   )r   r   rh   r   rH   rA   )r   )
r  r.   r   r\   r   r0   rh   r   rH   r  )r  r.   r   r\   rh   r   rH   r&  )
r  r.   r   r\   r7  r8  rh   r   rH   r&  )
r  r.   r   r\   rG  r0   rh   r   rH   r  )r   N)
r  r.   r   r\   rG  r0   rh   r   rH   r&  )r   rY  rZ  )r  r.   r   r\   r[  r\   r\  r]  rG  r0   rh   r   rH   r&  )r  r.   r   r\   rh   r   rH   r  )r  r   rs  r.   r   r\   rG  r0   rh   r   rH   rt  )r  r   rs  r.   r   r\   rG  r0   rh   r   rH   r  )r   r  )r  r.   r   r\   r  r  r7  r8  rh   r   rH   r&  )
r  r.   r   r\   rG  r0   rh   r   rH   r  )NrP  rP  Nr  N)r  r  r   r   r  r   r   r   r   r.   r   r.   r1   r0   r/   r.   r3   r4   rh   r   rH   r   )r  r  r   r   r  r   r   r   r   r.   r   r.   r/   r.   r3   r4   rh   r   rH   r   )rh   r   rH   r  )1rk   rl   rm   __doc__rc   r   propertyr   r   r   r   r   r   r  r  r%  r-  r1  r5  rA  rE  r5   r.  r)  r2  rd  rg  r!  r/  r*  r3  rn  ro  rq  rr  rR  rW  r"  r0  r+  r4  r  r  classmethodr  r  r  r  r  ra   ra   ra   rb   r     sZ   
}
	!!@9%'(.*%*(XXr   c                   @  s   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dZ
edded$ddZd%ddZd&d!d"Zd#S )'r  z-Retriever that uses `Azure Cognitive Search`.r   r  r   r.   r   r   r\   r   dictsearch_kwargs)r  similarity_score_thresholdr   hybrid_score_thresholdr  semantic_hybrid_score_thresholdzClassVar[Collection[str]]allowed_search_typesT)Zarbitrary_types_allowedbefore)modevaluesr   rH   r   c                 C  s4   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  r  r   ra   ra   rb   validate_search_typec  s   
z4AzureSearchVectorStoreRetriever.validate_search_typer  run_managerr   rh   r  c                 K  s.  i | j |}| jdkr| jj|fd| ji|}|S | jdkr4dd | jj|fd| ji|D }|S | jdkrH| jj|fd| ji|}|S | jdkradd | jj|fd| ji|D }|S | jd	kru| jj|fd| ji|}|S | jd
krdd | jj	|fd| ji|D }|S t
d| j d)Nr  r   r  c                 S  rJ  ra   ra   rK  ra   ra   rb   r   {      zKAzureSearchVectorStoreRetriever._get_relevant_documents.<locals>.<listcomp>r   r  c                 S  rJ  ra   ra   rK  ra   ra   rb   r     r  r  r  c                 S  rJ  ra   ra   rK  ra   ra   rb   r     r  r  r   )r  r   r  r5   r   rA  r!  rn  r"  r+  r   )r_   r  r  rh   paramsr$  ra   ra   rb   _get_relevant_documentsp  sX   






z7AzureSearchVectorStoreRetriever._get_relevant_documentsr   c                  s\  i | j |}| jdkr| jj|fd| ji|I d H }|S | jdkr=| jj|fd| ji|I d H }dd |D }|S | jdkrT| jj|fd| ji|I d H }|S | jdkrr| jj|fd| ji|I d H }dd |D }|S | jd	kr| jj|fd| ji|I d H }|S | jd
krdd | jj	|fd| ji|I d H D }|S t
d| j d)Nr  r   r  c                 S  rJ  ra   ra   rK  ra   ra   rb   r     r   zLAzureSearchVectorStoreRetriever._aget_relevant_documents.<locals>.<listcomp>r   r  c                 S  rJ  ra   ra   rK  ra   ra   rb   r     r   r  r  c                 S  rJ  ra   ra   rK  ra   ra   rb   r     r  r  r   )r  r   r  r.  r   rE  r/  ro  r0  r4  r   )r_   r  r  rh   r  r$  rN  ra   ra   rb   _aget_relevant_documents  sf   
 


 




z8AzureSearchVectorStoreRetriever._aget_relevant_documentsN)r  r   rH   r   )r  r.   r  r   rh   r   rH   r  )r  r.   r  r   rh   r   rH   r  )rk   rl   rm   r  __annotations__r   r   r  r  r   Zmodel_configr   r  r  r  r  ra   ra   ra   rb   r  A  s&   
 		
'r  rU  SearchItemPaged[Dict]r&  c                 C  s   dd | D }|S )Nc                 S  s    g | ]}t |t|d  fqS r  _result_to_documentr]  r  ra   ra   rb   r     s    
z)_results_to_documents.<locals>.<listcomp>ra   rU  r$  ra   ra   rb   rS    s   rS  AsyncSearchItemPaged[Dict]c                   s   dd | 2 I d H }|S )Nc                   s,   g | z3 d H W }t |t|d fq6 S Nr  r  r  ra   ra   rb   r     s    
z*_aresults_to_documents.<locals>.<listcomp>ra   r  ra   ra   rb   rX    s
   rX  rZ  r   r`  
np.ndarrayr\  r]  r   r\   c                   sp   dd | 2 I d H }t tt| \}}}t||||d}g }	|D ]}
|
dkr* |	S |	||
 ||
 f q!|	S )Nc                   s2   g | z3 d H W }t |t|d |t fq6 S r  r  r]  r   r  ra   ra   rb   r     s    
zE_areorder_results_with_maximal_marginal_relevance.<locals>.<listcomp>r   r\  mapr   r   r   r	  rU  r`  r\  r   r$  r   ZscoresZvectorsZnew_orderingretr}   ra   ra   rb   rf    s   rf  c                 C  sp   dd | D }|sg S t tt| \}}}t||||d}g }	|D ]}
|
dkr* |	S |	||
 ||
 f q!|	S )Nc                 S  s&   g | ]}t |t|d  |t fqS r  r  r  ra   ra   rb   r     s    
zD_reorder_results_with_maximal_marginal_relevance.<locals>.<listcomp>r  r  r  r  ra   ra   rb   rb    s    rb  r@  r   r   c                 C  sv   t | v rt| t  tr| t  }nt| t  }n	dd |  D }t| v r-t| ti}ni }t| t	 i ||dS )Nc                 S  s"   i | ]\}}|t tfvr||qS ra   )r   r   rz   ra   ra   rb   rw   (  s
    z'_result_to_document.<locals>.<dictcomp>r  )
r   r   r  r  r  r   r   r(  r   r   )r@  Zfields_metadataZ	fields_idra   ra   rb   r     s$   
r  iterabler	   r)   Optional[Any]Tuple[Iterable, Any]c                 C  sD   zt | }t|}t|g|} | |fW S  ty!   | |f Y S w ri   )iternext	itertoolschainStopIteration)r  r)   iteratorr{   ra   ra   rb   r  ;  s   
r  )NNNNNNNNNr,   NFNNN)$r-   r.   r/   r.   r(   r0   r1   r0   r2   r0   r3   r4   r5   r6   r7   r8   r9   r:   r;   r0   r<   r4   r=   r0   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   )rU  r  rH   r&  )rU  r  rH   r&  )rZ  r   )
rU  r  r`  r  r\  r]  r   r\   rH   r&  )r@  r   rH   r   ri   )r  r	   r)   r  rH   r  )N
__future__r   r   r   r  r  loggingr]   r   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   numpyr  Zlangchain_core.callbacksr   r   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.exceptionsr   Zlangchain_core.retrieversr   Zlangchain_core.utilsr   Zlangchain_core.vectorstoresr   Zpydanticr   r   Z&langchain_community.vectorstores.utilsr   	getLoggerr   r   r   Zazure.core.credentials_asyncr   r   r   r   r   r    r   r   r"   r#   r$   r%   r&   r   r   r   r   r
  r   r   r  rS  rX  rf  rb  r  r  ra   ra   ra   rb   <module>   s    @	 K          . 

$
#