
    hhcI                    ~   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$dS )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dS )%"_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modevaluesr   returnc                J   	 ddl m} n"# t          $ r}t          d          |d}~ww xY w	 ddlm} n"# t          $ r}t          d          |d}~ww xY wt          |dd          |d<   	 t          |d	d
          |d	<   t          |dd          |d<   n# t          $ r Y nw xY w|S )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.cloudr2   ImportErrorgoogle.api_core.exceptionsr4   r   	Exception)clsr/   r2   excr4   s        s/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/retrievers/google_vertex_ai_search.pyvalidate_environmentz7_BaseGoogleVertexAISearchRetriever.validate_environment,   s1   	;;;;;;; 	 	 	8  			BBBBBBB 	 	 	E  	  4FL,WW|	&:' 'F?# *>*,>* *F%&&  	 	 	D	 s6   	 
(#(3 
AAA*(B 
B B 'ClientOptions'c                P    ddl m}  || j        dk    r
| j         dn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   sT    @@@@@@} #x// #DDDD	
 
 
 	
    resultsSequence[SearchResult]List[Document]c                
   ddl }ddlm} g }|D ]s} ||j        j        d          }|                    t          |                    |                    di                     |d         |d         d	
                     t|S )GConverts a sequence of search results to a list of LangChain documents.r   NMessageToDictTpreserving_proto_field_namestruct_dataidname)rQ   rR   page_contentmetadata)	jsongoogle.protobuf.json_formatrM   document_pbappendr   dumpsget)rD   rG   rV   rM   	documentsresultdocument_dicts          r>   #_convert_structured_search_responsezF_BaseGoogleVertexAISearchRetriever._convert_structured_search_responseZ   s     	======$&	 
	 
	F)M#  M !%M,=,=mR,P,P!Q!Q$1$$7vAVWW      rF   
chunk_typec           
        ddl m} g }|D ]} ||j        j        d          }|                    d          }|s1|                    di           }|d         |d<   ||vrW||         D ]}	|                                }
|                    dd	          |
d
<   |dk    r'|
d
xx         d|	                    dd	           z  cc<   |                    t          |	                    dd	          |
                     |S )rK   r   rL   TrN   derived_struct_datarP   rQ   link sourceextractive_answers:
pageNumbercontentrS   )rW   rM   rX   rY   r\   copyrZ   r   )rD   rG   ra   rM   r]   r^   r_   rc   doc_metadatachunkchunk_metadatas              r>   %_convert_unstructured_search_responsezH_BaseGoogleVertexAISearchRetriever._convert_unstructured_search_responser   sl    	>=====$&	 	 	F)M#  M #0"3"34I"J"J& (,,]B??L!.t!4L!444,Z8  !-!2!2!4!4+>+B+B62+N+Nx(!555"8,,,0QEIIlB4O4O0Q0QQ,,,  %*YYy"%=%=      rF   c           
     D   ddl m} g }|D ]} ||j        j        d          }|                    d          }|s1|                    di           }|d         |d<   |                    dd	          |d
<   |                    d          |                    d          |d<   ||vr|dk    rdnd}	||         D ]:}
|                    t          |
                    |	d	          |                     ;|s(t          d| d           |dk    rt          d           |S )rK   r   rL   TrN   rc   rP   rQ   rd   re   rf   titleNsnippetssnippetrj   rS   zNo z could be found.rg   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)rW   rM   rX   rY   r\   rZ   r   print)rD   rG   ra   rM   r]   r^   r_   rc   rl   
text_fieldrm   s              r>    _convert_website_search_responsezC_BaseGoogleVertexAISearchRetriever._convert_website_search_response   s    	>=====$&	 	 	F)M#  M #0"3"34I"J"J& (,,]B??L!.t!4L%8%<%<VR%H%HL""&&w//;(;(?(?(H(HW%!444&0J&>&>IJ,Z8    %*YYz2%>%>       	4
444555111@   rF   )r/   r   r0   r   )r0   r@   )rG   rH   r0   rI   )rG   rH   ra   r   r0   rI   )__name__
__module____qualname____annotations__r   r   r!   r#   r$   r   r+   r   classmethodr?   propertyrE   r`   ro   rv    rF   r>   r   r      sJ        OOO"#'M'''')&*****"K/------K "E!a8888888 _(###   [ $#@ 	
 	
 	
 X	
   0# # # #J+ + + + + +rF   r   z0.0.33z1.0z2langchain_google_community.VertexAISearchRetriever)sinceremovalalternative_importc                  X    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extrakwargsr0   Nonec                   	 ddl m} n"# t          $ r}t          d          |d}~ww xY w t                      j        di |  || j        | j        t          d                    | _        | j	        dk    r| j
        st          d	          | j
        r"d
| j         d| j         d| j
         d| _        dS | j        r9| j                            | j        | j        | j        | j                  | _        dS t          d          )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$   rE   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   r9   super__init__r$   rE   r   r   r+   r   
ValueErrorr   r!   r   r   serving_config_pathr#   )rD   r   r   r=   	__class__s       r>   r   z&GoogleVertexAISearchRetriever.__init__   s   	OOOOOOO 	 	 	R  	 	""6""" +*(.'/ABBB
 
 
  A%%d.C%M     	 ${t  ${  ${4K[  ${  ${  FJ  F[  ${  ${  ${D    
	#'<#C#C)-#5	 $D $ $D    M  s   
 
)$)queryr   c           	     h   ddl m}  |j        | j                  } |j        | j                  }| j        dk    rZ| j        r!|j        	                    | j
                  }n |j        	                    | j                  } |j        |          }n{| j        dk    rd	}nm| j        d
v rJ |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 )r   r   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   )rD   r   r   r   r   r   r   s          r>   _create_search_requestz4GoogleVertexAISearchRetriever._create_search_request#  s   EEEEEE?}?4 
  
  
 !B A+!
 !
 !
  A%%* !3II484T J   (' "3II595V J   (
 #B-"A(?# # # "a''"&"f,,"A-"A(5(G(](]040P )^ ) ) +<HH#' I  	# # # &G1$/112   };/( 3!5"7
 
 
 	
rF   run_managerr   rI   c               8    |                      |          d         S )#Get documents relevant for a query.r   )$get_relevant_documents_with_response)rD   r   r   s      r>   _get_relevant_documentsz5GoogleVertexAISearchRetriever._get_relevant_documents[  s     88??BBrF   Tuple[List[Document], Any]c                   ddl m} |                     |          }	 | j                            |          }n-# |$ r%} t          |          |j        dz             d }~ww xY w| j        dk    r'| j        rdnd}| 	                    |j
        |          }np| j        dk    r|                     |j
                  }nJ| j        dv r'| j        rdnd}|                     |j
        |          }nt          d	d
| j         z             ||fS )Nr   r3   z9 This might be due to engine_data_type not set correctly.rg   extractive_segmentsr   r   rr   r   r   )r:   r4   r   r   searchtypemessager+   r   ro   rG   r`   rv   r   )rD   r   r4   search_requestresponser=   ra   r]   s           r>   r   zBGoogleVertexAISearchRetriever.get_relevant_documents_with_responsea  s    	?>>>>>44U;;	|**>::HH 	 	 	$s))MN  	  A%% .+$$* 
 BB * II "a''@@AQRRII"f,,(,(CS$$  == * II &G1$/112   (""s   8 A" AA")r   r   r0   r   )r   r   r0   r   r   r   r   r   r0   rI   )r   r   r0   r   )rw   rx   ry   __doc__r   rz   r   r   r   r   r   r   r   r   r   model_configr   r   r   r   __classcell__r   s   @r>   r   r      s          !F    #(((((^qQ3777M77774',uQ1'C'C'CCCCC ).aA!(D(D(D DDDD &+U1q%A%A%AAAAA "'qQ1!=!=!===== G: $  L
% % % % % %N6
 6
 6
 6
pC C C C(# (# (# (# (# (# (# (#rF   r   z;langchain_google_community.VertexAIMultiTurnSearchRetrieverc                  p     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   r   c                    t                      j        di | ddlm}  || j        | j        t          d                    | _        | j        st          d          | j        
                    | j        | j        | j        | j                  | _        | j        dk    s| j        d	k    rt!          d
d| j         z             d S )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$   rE   r   r   r   r   r   r   r!   r#   r   r+   r   )rD   r   r   r   s      r>   r   z/GoogleVertexAIMultiTurnSearchRetriever.__init__  s   ""6"""	
 	
 	
 	
 	
 	
 98(.'/ABBB
 
 
 ! 	XVWWW#|??O%)1	  @  
  
  A%%)>!)C)C%D1$/112   *D)CrF   r   r   r   r0   rI   c               `   ddl m}m}  || j                            | j        | j        | j        | j                  | j	         ||                    }| j        
                    |          }| j        dk    r|                     |j        d          S |                     |j        d          S )r   r   )ConverseConversationRequest	TextInput)input)rR   r   r   r   rg   )r   r   r   r   conversation_pathr   r!   r   r   r   converse_conversationr+   rv   search_resultsro   )rD   r   r   r   r   requestr   s          r>   r   z>GoogleVertexAIMultiTurnSearchRetriever._get_relevant_documents  s    	
 	
 	
 	
 	
 	
 	
 	

 .-// "$	   /)%(((	
 	
 	
 <55g>> A%%88')=   99#%9
 
 	
rF   )r   r   r   )rw   rx   ry   r   r   rz   r   r   r   r   r   r   r   s   @r>   r   r     s          LKO+ G: $  L
     :
 
 
 
 
 
 
 
rF   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.
    datar   c                x    dd l }|                    dt                      t                      j        di | d S )Nr   zUGoogleCloudEnterpriseSearchRetriever is deprecated, use GoogleVertexAISearchRetrieverr}   )warningswarnDeprecationWarningr   r   )rD   r   r   r   s      r>   r   z-GoogleCloudEnterpriseSearchRetriever.__init__  sM    c	
 	
 	

 	  4     rF   )r   r   )rw   rx   ry   r   r   r   r   s   @r>   r   r     sG         ! ! ! ! ! ! ! ! ! !rF   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   rC   r   r   r   r   r   r   r   r   r}   rF   r>   <module>r      s=   4 4 " " " " " " L L L L L L L L L L L L L L L L L L 6 6 6 6 6 6 C C C C C C - - - - - - 3 3 3 3 3 3 5 5 5 5 5 5 B B B B B B B B B B B B B B B B B B P<<<<<<OOOOOOOOm m m m m m m m` 
K  
# # # # #M3U # # 
#D 
T  
J
 J
 J
 J
 J
5J
 J
 
J
Z! ! ! ! !+H ! ! ! ! !rF   