
    3ficI                    L   d Z ddlmZ ddlmZmZmZmZmZ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mZmZmZ dd
lmZ erddlmZ ddlmZm Z   G d de      Z! eddd       G d dee!             Z" eddd       G d dee!             Z# G d de"      Z$y)z.Retriever wrapper for Google Vertex AI Search.    )annotations)TYPE_CHECKINGAnyDictListOptionalSequenceTuple)
deprecated)CallbackManagerForRetrieverRun)Document)BaseRetriever)get_from_dict_or_env)	BaseModel
ConfigDictFieldmodel_validator)get_client_infoClientOptions)SearchRequestSearchResultc                      e Zd ZU ded<   	 dZded<   	 dZded<   	 dZded<   	 d	Zded
<   	 dZded<   	  e	ddd      Z
ded<   	  ed      edd              Zedd       Z	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)"_BaseGoogleVertexAISearchRetrieverstr
project_idNOptional[str]data_store_idsearch_engine_idgloballocation_iddefault_configserving_config_idr   credentialsr      defaultgeleintengine_data_typebeforemodec                   	 ddl m} 	 ddlm} t        |dd      |d<   	 t        |d	d
      |d	<   t        |dd      |d<   |S # t        $ r}t        d      |d}~ww xY w# t        $ r}t        d      |d}~ww xY w# t        $ r Y |S w xY w)zValidates the environment.r   )discoveryengine_v1betazvgoogle.cloud.discoveryengine is not installed.Please install it with pip install google-cloud-discoveryengine>=0.11.10NInvalidArgumentz_google.api_core.exceptions is not installed. Please install it with pip install google-api-corer   
PROJECT_IDr   DATA_STORE_IDr   SEARCH_ENGINE_ID)google.cloudr0   ImportErrorgoogle.api_core.exceptionsr2   r   	Exception)clsvaluesr0   excr2   s        t/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/retrievers/google_vertex_ai_search.pyvalidate_environmentz7_BaseGoogleVertexAISearchRetriever.validate_environment,   s    	;	B  4FL,W|	&:'F?# *>*,>*F%& 5  	8 		  	E 	  		s?   A A  A< 	AAA	A9(A44A9<	B	B	c                f    ddl m}  || j                  dk7  r| j                   d      S d       S )Nr   r   r    z-discoveryengine.googleapis.com)api_endpoint)google.api_core.client_optionsr   r!   )selfr   s     r=   client_optionsz1_BaseGoogleVertexAISearchRetriever.client_optionsN   sE    @ ##x/ ##$$CD
 	
 	
 	
    c                    ddl }ddlm} g }|D ]d  } ||j                  j                  d      }|j                  t        |j                  |j                  di             |d   |d   d	
             f |S )GConverts a sequence of search results to a list of LangChain documents.r   NMessageToDictTpreserving_proto_field_namestruct_dataidname)rL   rM   page_contentmetadata)	jsongoogle.protobuf.json_formatrH   document_pbappendr   dumpsget)rB   resultsrQ   rH   	documentsresultdocument_dicts          r=   #_convert_structured_search_responsezF_BaseGoogleVertexAISearchRetriever._convert_structured_search_responseZ   s     	=$&	 
	F)##M !%M,=,=mR,P!Q$1$$7vAVW
	 rD   c           
        ddl m} g }|D ]  } ||j                  j                  d      }|j	                  d      }|s5|j	                  di       }|d   |d<   ||vrT||   D ]x  }	|j                         }
|j	                  dd	      |
d
<   |dk(  r |
d
xx   d|	j	                  dd	       z  cc<   |j                  t        |	j	                  dd	      |
             z  |S )rF   r   rG   TrI   derived_struct_datarK   rL   link sourceextractive_answers:
pageNumbercontentrN   )rR   rH   rS   rT   rW   copyrU   r   )rB   rX   
chunk_typerH   rY   rZ   r[   r^   doc_metadatachunkchunk_metadatas              r=   %_convert_unstructured_search_responsezH_BaseGoogleVertexAISearchRetriever._convert_unstructured_search_responser   s    	>$&	 	F)##M #0"3"34I"J&(,,]B?L!.t!4L!44,Z8 !-!2!2!4+>+B+B62+Nx(!55"8,!EIIlB4O3P0QQ,  %*YYy"%=	6 rD   c           
        ddl m} g }|D ]  } ||j                  j                  d      }|j	                  d      }|s5|j	                  di       }|d   |d<   |j	                  dd	      |d
<   |j	                  d      |j	                  d      |d<   ||vr|dk(  rdnd}	||   D ].  }
|j                  t        |
j	                  |	d	      |             0  |st        d| d       |dk(  rt        d       |S )rF   r   rG   TrI   r^   rK   rL   r_   r`   ra   titlesnippetssnippetre   rN   zNo z could be found.rb   zMake sure that your data store is using Advanced Website Indexing.
https://cloud.google.com/generative-ai-app-builder/docs/about-advanced-features#advanced-website-indexing)rR   rH   rS   rT   rW   rU   r   print)rB   rX   rg   rH   rY   rZ   r[   r^   rh   
text_fieldri   s              r=    _convert_website_search_responsezC_BaseGoogleVertexAISearchRetriever._convert_website_search_response   s6    	>$&	 	F)##M #0"3"34I"J&(,,]B?L!.t!4L%8%<%<VR%HL""&&w/;(;(?(?(HW%!44&0J&>IJ,Z8   %*YYz2%>'	4 C
|#34511@ rD   )r;   r   returnr   )rs   z'ClientOptions')rX   Sequence[SearchResult]rs   List[Document])rX   rt   rg   r   rs   ru   )__name__
__module____qualname____annotations__r   r   r!   r#   r$   r   r+   r   classmethodr>   propertyrC   r\   rk   rr    rD   r=   r   r      s    O"#'M=')&*m*"K/-s--K "!a8c8 (#  $@ 	
 	
-	0#-#;>#	#J+-+;>+	+rD   r   z0.0.33z1.0z2langchain_google_community.VertexAISearchRetriever)sinceremovalalternative_importc                  H    e Zd ZU dZdZded<   	 dZded<   	  edd	d
      Zded<   	  ed	d	d      Z	ded<   	  ed	d	d	      Z
ded<   	  ed	dd      Zded<   	  eddd      Zded<   	 dZded<   ded<    edd      Zd fdZd dZ	 	 	 	 	 	 d!dZ	 	 	 	 d"dZ xZS )#GoogleVertexAISearchRetrievera  `Google Vertex AI Search` retriever.

    For a detailed explanation of the Vertex AI Search concepts
    and configuration parameters, refer to the product documentation.
    https://cloud.google.com/generative-ai-app-builder/docs/enterprise-search-introduction
    Nr   filterFboolget_extractive_answers      d   r&   r*   max_documentsmax_extractive_answer_countmax_extractive_segment_countr      query_expansion_conditionspell_correction_moder   _clientr   _serving_configTignorearbitrary_types_allowedextrac                R   	 ddl m} t        |   di |  || j
                  | j                  t        d            | _        | j                  dk(  r| j                  st        d	      | j                  r0d
| j                   d| j                   d| j                   d| _        y| j                  rM| j                  j!                  | j                  | j                  | j                  | j"                        | _        yt        d      # t        $ r}t        d      |d}~ww xY w)zInitializes private fields.r   )SearchServiceClientzmgoogle.cloud.discoveryengine is not installed.Please install it with pip install google-cloud-discoveryengineNvertex-ai-searchmoduler$   rC   client_infor%   z;search_engine_id must be specified for blended search apps.z	projects/z/locations/z(/collections/default_collection/engines/z/servingConfigs/default_configprojectlocation
data_storeserving_configz;Either data_store_id or search_engine_id must be specified.r|   )#google.cloud.discoveryengine_v1betar   r7   super__init__r$   rC   r   r   r+   r   
ValueErrorr   r!   r   r   serving_config_pathr#   )rB   kwargsr   r<   	__class__s       r=   r   z&GoogleVertexAISearchRetriever.__init__   sK   	O 	"6" +((..'/AB
   A%d.C.CM    %.t.?{4K[K[J\  ]E  FJ  F[  F[  E\  \z  ${D #'<<#C#C))--#55	 $D $D  M ?  	R 	s   D 	D&D!!D&c           	        ddl m}  |j                  | j                        } |j                  | j
                        }| j                  dk(  rm| j                  r'|j                  j                  | j                        }n&|j                  j                  | j                        } |j                  |      }n| j                  dk(  rd	}n|| j                  d
v rS |j                  |j                  j                  | j                        |j                  j                  d            }nt        dd| j                   z          ||| j                  | j                  | j                   |||      S )z Prepares a SearchRequest object.r   )r   )	conditionr-   )r   )r   )extractive_content_specr   Nr   r%   T)return_snippet)r   snippet_specjOnly data store type 0 (Unstructured), 1 (Structured),2 (Website), or 3 (Blended) are supported currently. Got )queryr   r   	page_sizecontent_search_specquery_expansion_specspell_correction_spec)r   r   QueryExpansionSpecr   SpellCorrectionSpecr   r+   r   ContentSearchSpecExtractiveContentSpecr   r   SnippetSpecNotImplementedErrorr   r   r   )rB   r   r   r   r   r   r   s          r=   _create_search_requestz4GoogleVertexAISearchRetriever._create_search_request#  s   E?}??44 
 !B A A++!
   A%**!33II484T4T J  ( "33II595V5V J  (
 #B-"A"A(?# ""a'"&""f,"A-"A"A(5(G(G(](]040P0P )^ ) +<<HH#' I 	# &G$//012  ;;//(( 3!5"7
 	
rD   c               *    | j                  |      d   S )#Get documents relevant for a query.r   )$get_relevant_documents_with_response)rB   r   run_managers      r=   _get_relevant_documentsz5GoogleVertexAISearchRetriever._get_relevant_documents[  s     88?BBrD   c                J   ddl m} | j                  |      }	 | j                  j	                  |      }| j                  dk(  r0| j                  rdnd}| j                  |j                  |      }||fS | j                  dk(  r| j                  |j                        }||fS | j                  dv r0| j                  rdnd}| j                  |j                  |      }||fS t        d	d
| j                   z         # |$ r#} t        |      |j                  dz         d }~ww xY w)Nr   r1   z9 This might be due to engine_data_type not set correctly.rb   extractive_segmentsr   r   rn   r   r   )r8   r2   r   r   searchtypemessager+   r   rk   rX   r\   rr   r   )rB   r   r2   search_requestresponser<   rg   rY   s           r=   r   zBGoogleVertexAISearchRetriever.get_relevant_documents_with_responsea  sb    	?44U;	||**>:H   A% .. %* 
 BB  *I& (""! ""a'@@AQAQRI ("" ""f,(,(C(C$  ==  *I ("" &G$//012 3  	$s)MN 	s   C: :D"?DD")r   r   rs   None)r   r   rs   r   r   r   r   r   rs   ru   )r   r   rs   zTuple[List[Document], Any])rv   rw   rx   __doc__r   ry   r   r   r   r   r   r   r   r   r   model_configr   r   r   r   __classcell__r   s   @r=   r   r      s	    !FM #(D(^qQ37M374',Q1'CC ).aA!(D #D &+1q%AsA "'qQ1!=3= GS $L
%N6
pCC*HC	C(#(#	#(#rD   r   z;langchain_google_community.VertexAIMultiTurnSearchRetrieverc                  v     e Zd ZU dZdZded<   	 dZded<   ded<    ed	d
      Zd fdZ		 	 	 	 	 	 ddZ
 xZS )&GoogleVertexAIMultiTurnSearchRetrieverzA`Google Vertex AI Search` retriever for multi-turn conversations.-r   conversation_idNr   r   r   Tr   r   c                   t        |   di | ddlm}  || j                  | j
                  t        d            | _        | j                  st        d      | j                  j                  | j                  | j                  | j                  | j                        | _        | j                  dk(  s| j                  d	k(  rt!        d
d| j                   z         y )Nr   )!ConversationalSearchServiceClientr   r   r   z7data_store_id is required for MultiTurnSearchRetriever.r   r   r%   z_Data store type 1 (Structured) and 3 (Blended)is not currently supported for multi-turn search.r   r|   )r   r   r   r   r$   rC   r   r   r   r   r   r   r!   r#   r   r+   r   )rB   r   r   r   s      r=   r   z/GoogleVertexAIMultiTurnSearchRetriever.__init__  s    "6"	
 9((..'/AB
 !!VWW#||??OO%%))11	  @  
   A%)>)>!)C%D$//012  *DrD   c                  ddl m}m}  || j                  j	                  | j
                  | j                  | j                  | j                        | j                   ||            }| j                  j                  |      }| j                  dk(  r| j                  |j                  d      S | j                  |j                  d      S )r   r   )ConverseConversationRequest	TextInput)input)rM   r   r   r   rb   )r   r   r   r   conversation_pathr   r!   r   r   r   converse_conversationr+   rr   search_resultsrk   )rB   r   r   r   r   requestr   s          r=   r   z>GoogleVertexAIMultiTurnSearchRetriever._get_relevant_documents  s    	

 .//  ""$$	  //%(	
 <<55g>  A%88'')=  99##%9
 	
rD   )r   r   r   )rv   rw   rx   r   r   ry   r   r   r   r   r   r   r   s   @r=   r   r     sZ     LOS+ GS $L
:

*H
	
rD   r   c                  $     e Zd ZdZd fdZ xZS )$GoogleCloudEnterpriseSearchRetrieverz`Google Vertex Search API` retriever alias for backwards compatibility.
    DEPRECATED: Use `GoogleVertexAISearchRetriever` instead.
    c                X    dd l }|j                  dt               t        |   di | y )Nr   zUGoogleCloudEnterpriseSearchRetriever is deprecated, use GoogleVertexAISearchRetrieverr|   )warningswarnDeprecationWarningr   r   )rB   datar   r   s      r=   r   z-GoogleCloudEnterpriseSearchRetriever.__init__  s*    c	

 	 4 rD   )r   r   )rv   rw   rx   r   r   r   r   s   @r=   r   r     s    ! !rD   r   N)%r   
__future__r   typingr   r   r   r   r   r	   r
   langchain_core._api.deprecationr   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr   langchain_core.utilsr   pydanticr   r   r   r   &langchain_community.utilities.vertexair   rA   r   r   r   r   r   r   r   r   r|   rD   r=   <module>r      s    4 " L L L 6 C - 3 5 B B B<Om m` 
K
#M3U #
#D 
T
J
5J

J
Z!+H !rD   