
    3fir                       d Z ddlm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 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 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rTddl'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+Z0 ee+e0f   Z1ee)e.f   Z2ee*jf                  e/jf                  f   Z4ee(e-f   Z5ddZ6 G d de      Z7 G d dee7      Z8 G d dee7      Z9 G d dee8      Z:y)zRetriever wrapper for Google Vertex AI Search.

Set the following environment variables before the tests:

```
export PROJECT_ID=... # Set to your Google Cloud project ID
export DATA_STORE_ID=... # The ID of the search engine to use for the test
```
    )annotationsN)TYPE_CHECKINGAnyDictListOptionalSequenceUnion)ClientOptions)InvalidArgument)MessageToDict)CallbackManagerForRetrieverRun)Document)Serializableload)BaseRetriever)BaseTool)get_from_dict_or_env)
ConfigDictFieldPrivateAttrmodel_validator)get_client_info)!ConversationalSearchServiceClientSearchRequestSearchResponseSearchServiceClientr   r   )r   r   c                "    t        | dg|g      S )Nlangchain_google_community)valid_namespacesallowed_objects)r   )dumpretriever_clss     i/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/vertex_ai_search.py_loadr(   <   s    67&     c                  `    e Zd ZU ded<   	 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      Z
ded<   	  eddd      Zd" fdZed#d       Zed#d       Zd$dZ ed      ed%d              Zed&d       Z	 	 	 	 d'dZ	 	 	 	 	 	 d(d Z	 	 	 	 	 	 d(d!Z xZS ))_BaseVertexAISearchRetrieverstr
project_iddata_store_idgloballocation_iddefault_configserving_config_idNr   credentialsr      defaultgeleintengine_data_typeF)r6   bool_betaforbidT)extraarbitrary_types_allowedvalidate_assignmentc                V    |j                  dd      }t        |   di | || _        y )NbetaF )popsuper__init__r<   )selfkwargsrB   	__class__s      r'   rF   z%_BaseVertexAISearchRetriever.__init__i   s*    zz&%("6"
r)   c                    | j                   S )z$Get whether beta version is enabled.)r<   rG   s    r'   rB   z!_BaseVertexAISearchRetriever.betan   s     zzr)   c                     y)NTrC   rK   s    r'   is_lc_serializablez/_BaseVertexAISearchRetriever.is_lc_serializables   s    r)   c                D    t         | j                         t        |       ffS N)r(   to_jsontyperK   s    r'   
__reduce__z'_BaseVertexAISearchRetriever.__reduce__w   s    t||~tDz222r)   beforemodec                ,   	 |j                  dd      rddlm} nddlm} t        |dd	      |d<   	 t        |d
d      }|rt        j                  dt               ||d<   t        |dd      |d<   |S # t        $ r}t	        d      |d}~ww xY w#  Y 3xY w)zValidates the environment.rB   Fr   )discoveryengine_v1beta)discoveryengine_v1zCould not import google-cloud-discoveryengine python package. Please, install vertexaisearch dependency group: uv sync --extra vertexaisearchNr-   
PROJECT_IDsearch_engine_idSEARCH_ENGINE_IDzLThe `search_engine_id` parameter is deprecated. Use `data_store_id` instead.r.   DATA_STORE_ID)	getgoogle.cloudrW   rX   ImportErrorr   warningswarnDeprecationWarning)clsvaluesrW   rX   excrZ   s         r'   validate_environmentz1_BaseVertexAISearchRetriever.validate_environmentz   s    
	zz&%(?;  4FL,W|	3*,>   b& +;' #7O_#
 9  	1 		*	s"   A2 .B 2	B;BBBc                `    t        | j                  dk7  r| j                   d      S d       S )Nr/   z-discoveryengine.googleapis.com)api_endpoint)r   r0   rK   s    r'   client_optionsz+_BaseVertexAISearchRetriever.client_options   sB     ##x/ ##$$CD
 	
 	
 	
r)   c                    g }|D ]k  }t        |j                  j                  d      }|j                  t	        t        j                  |j                  di             |d   |d   d             m |S )GConverts a sequence of search results to a list of LangChain documents.Tpreserving_proto_field_namestruct_dataidname)ro   rp   page_contentmetadata)r   document_pbappendr   jsondumpsr]   )rG   results	documentsresultdocument_dicts        r'   #_convert_structured_search_responsez@_BaseVertexAISearchRetriever._convert_structured_search_response   s|     %'	 
	F)##M !%M,=,=mR,P!Q$1$$7vAVW
	 r)   c           
     V   g }|D ]   }t        |j                  j                  d      }|j                  di       }|r||vr>|d   |j                  dd      d|j                  di       }||   D ]  }|j	                         }	|d	v ro|	d
xx   |j                  dd       z  cc<   |dk(  rL|	j                  |j                  dg       |j                  dg       d       d|v r|j                  d      |	d<   |j                  t        |j                  dd      |	              # |S )rk   Trl   derived_struct_dataro   link )ro   sourcern   )extractive_answersextractive_segmentsr   
pageNumberr   previous_segmentsnext_segments)r   r   relevanceScorerelevance_scorecontentrq   )r   rt   ru   r]   copyupdaterv   r   )
rG   ry   
chunk_typerz   r{   r|   r   doc_metadatachunkchunk_metadatas
             r'   %_convert_unstructured_search_responsezB_BaseVertexAISearchRetriever._convert_unstructured_search_response   se    %'	 %	F)##M #0"3"34I2"N&*<O*O $D)-11&"=  ##M26L -Z8 !-!2!2!4!NN"8,599\23N2OP,!%::&--5:YY?RTV5W16?B1O ,u4@E		 0AN+<=   %*YYy"%=%%	N r)   c           
        g }|D ]  }t        |j                  j                  d      }|j                  d      }|s8|j                  di       }|d   |d<   |j                  dd      |d<   ||vrl|d	k(  rd
nd}||   D ].  }	|j	                  t        |	j                  |d      |             0  |st        d| d       |dk(  rt        d       |S )rk   Trl   r   rn   ro   r   r   r   snippetssnippetr   rq   zNo z could be found.r   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)r   rt   ru   r]   rv   r   print)
rG   ry   r   rz   r{   r|   r   r   
text_fieldr   s
             r'    _convert_website_search_responsez=_BaseVertexAISearchRetriever._convert_website_search_response   s    %'	 	F)##M #0"3"34I"J&(,,]B?L!.t!4L%8%<%<VR%HL"!44&0J&>IJ,Z8   %*YYz2%>#	0 C
|#34511@ r)   rH   r   returnNone)r   r;   )r   r   )rd   r   r   r   )r   z'ClientOptions')ry   %Sequence[DiscoveryEngineSearchResult]r   List[Document])ry   r   r   r,   r   r   )__name__
__module____qualname____annotations__r0   r2   r3   r   r:   r   r<   r   model_configrF   propertyrB   classmethodrM   rR   r   rf   ri   r}   r   r   __classcell__rI   s   @r'   r+   r+   D   s8   O")K/-s--K "!a8c8 e,E4,: $ L
    3 (##  $#J 
 
<	(-<-JM-	-^'<'JM'	'r)   r+   c                  "    e Zd ZU dZ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dd
      Zded<   	  e	dd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<   	 dZded<   	 dZded<   	 dZded<   	  e	d d d!      Zd"ed#<   	  e       Zd$ed%<    e       Zd&ed'<   d/ fd(Zd0d)Zd0d*Zd1d+Zd2d,Zd3d-Z	 	 	 	 	 	 d4d.Z xZ S )5VertexAISearchRetrievera  `Google Vertex AI Search` retriever.

    This retriever supports both stable (v1) and beta versions of the Discovery Engine.

    Beta features are only available when `beta=True`.

    For a detailed explanation of Vertex AI Search concepts and configuration parameters
    see the [Vertex AI Search documentation](https://cloud.google.com/generative-ai-app-builder/docs/enterprise-search-introduction).
    NOptional[str]filterorder_bycanonical_filterFr;   get_extractive_answers      d   r5   r9   max_documentsmax_extractive_answer_count
   max_extractive_segment_countreturn_extractive_segment_score   num_previous_segmentsnum_next_segmentsr   r4   query_expansion_conditionspell_correction_modezOptional[Dict[Any, Any]]
boost_speczOptional[Any]custom_embeddingcustom_embedding_field_pathg        g      ?zOptional[float]custom_embedding_ratioDiscoveryEngineClient_clientr,   _serving_configc                d    t        |   di | | j                          | j                          y)zDInitialize the retriever with the appropriate version of the client.NrC   )rE   rF   _validate_version_compatibility_initialize_client)rG   rH   rI   s     r'   rF   z VertexAISearchRetriever.__init__  s+    "6",,.!r)   c           	     ,   | j                   | j                  | j                  d}| j                  s^t	        d |j                         D              r=t        j                  d|j                         D cg c]
  \  }}|	| c}}        yyyc c}}w )zValidate version compatibility of all components.

        Raises:
            Warning: If beta features are configured but `beta=False`
        )r   r   r   c              3  $   K   | ]  }|d u 
 y wrO   rC   ).0values     r'   	<genexpr>zJVertexAISearchRetriever._validate_version_compatibility.<locals>.<genexpr>  s      Wud!2 Ws   zYBeta features are configured but beta=False. The following beta features will be ignored:N)	r   r   r   rB   anyrd   r`   ra   items)rG   beta_featureskvs       r'   r   z7VertexAISearchRetriever._validate_version_compatibility  s     !% 5 5+/+K+K&*&A&A
 yyS W@T@T@V WWMMk"/"5"5"7I$!Q1=AIJL Xy Js   4
B?Bc                f   	 | j                   rddlm} nddlm}  || j
                  | j                  t        d            | _        | j                  j                  | j                  | j                  | j                  | j                        | _        y# t        $ r}t	        d      |d}~ww xY w)	z@Initialize the appropriate version of the `SearchServiceClient`.r   r    zCould not import google-cloud-discoveryengine python package. Please, install vertexaisearch dependency group: `pip install langchain-google-community[vertexaisearch]`Nvertex-ai-searchmoduler3   ri   client_infoprojectlocation
data_storeserving_config)rB   #google.cloud.discoveryengine_v1betar   google.cloud.discoveryengine_v1r_   r3   ri   r   r   serving_config_pathr-   r0   r.   r2   r   )rG   r   re   s      r'   r   z*VertexAISearchRetriever._initialize_client  s    	yy +((..'/AB
  $||??OO%%))11	  @  
  	K 		s   B 	B0B++B0c           	        i }| j                   s|S | j                  | j                  | j                  t        d      | j                  t        d      | j                  t        d      ddlm} |j                  j                  | j                  | j                  j                  |            }|j                   |j                  |g      | j                   d| j                   d	d
| j                  z
   dd       |S )z7Get parameters that are only available in beta version.zUPlease provide a custom embedding model if you provide a custom_embedding_field_path.zUPlease provide a custom_embedding_field_path if you provide a custom embedding model.zqPlease provide a custom_embedding_ratio if you provide a custom embedding model or a custom_embedding_field_path.r   r   )
field_pathvector)embedding_vectorsz * dotProduct(z) + r   z * relevance_score)embedding_specranking_expression)rB   r   r   
ValueErrorr   r   r   EmbeddingSpecEmbeddingVectorembed_queryr   )rG   queryparamsr   embedding_vectors        r'   _get_beta_specific_paramsz1VertexAISearchRetriever._get_beta_specific_params  s9    "$yyM !!-//;$$, 3  //7 .  **2 O 
 J,::JJ;;,,88?  K   MM&Am&A&A+;*<'  667 8&&*&F&F%Gtt:::;;MO	 r)   c                v   | j                   rddlm} nddlm} | j                  dk(  r| j
                  r'|j                  j                  | j                        }nG|j                  j                  | j                  | j                  | j                  | j                        }t        |      }|S | j                  dk(  rd}|S | j                  dk(  rMt        |j                  j                  | j                        |j                  j                  d	
            }|S t        dd| j                   z         )zPrepares a `ContentSpec` object.

        !!! note

            The `ContentSearchSpec` is identical between beta and stable versions,
            but we import from the appropriate version for consistency.
        r   r   )r   )r   r   r   r   )extractive_content_specr   Nr4   T)return_snippet)r   snippet_spec]Only data store type 0 (Unstructured), 1 (Structured),or 2 (Website) are supported currently. Got )rB   r   r   r   r:   r   ContentSearchSpecExtractiveContentSpecr   r   r   r   r   dictSnippetSpecNotImplementedError)rG   r   r   content_search_specs       r'   _get_content_spec_kwargsz0VertexAISearchRetriever._get_content_spec_kwargs  sX    99IE  A%**!33II484T4T J  ( "33II595V5V.2.H.H*.*@*@ @@ J  ( #'?V"W( #"' ""a'"&$ #"# ""a'"&!33II484T4T J  +<<HH#' I 	#  #" &:$//012 r)   c                   | j                   rddlm} nddlm}  |j                  | j
                        } |j                  | j                        }| j                         }| |j                  di |nd}|| j                  | j                  | j                  | j                  | j                  |||| j                  r |j                   di | j                  ndd
}| j#                  |      }|j%                  |        |di |S )z"Prepares a `SearchRequest` object.r   r   )	conditionrT   N)
r   r   r   r   r   	page_sizer   query_expansion_specspell_correction_specr   rC   )rB   r   r   r   QueryExpansionSpecr   SpellCorrectionSpecr   r   r   r   r   r   r   r   r   	BoostSpecr   r   )	rG   r   r   r   r   content_search_spec_kwargsr   request_paramsbeta_paramss	            r'   _create_search_requestz.VertexAISearchRetriever._create_search_request,  s   99IE?}??44 
 !B A A++!
 &*%B%B%D" *5 ,M++I.HI 	 kk $ 5 5"22++#6$8%: 2-11DDOOD
  44U;k*.~..r)   c               <   | j                  |      }	 | j                  j                  |      }| j                  dk(  r.| j                  rdnd}| j                  |j                  |      }|S | j                  dk(  r| j                  |j                        }|S | j                  dk(  r.| j                  rdnd}| j                  |j                  |      }|S t        d	d
| j                   z         # t        $ r#} t	        |      |j
                  dz         d}~ww xY w)#Get documents relevant for a query.z9 This might be due to engine_data_type not set correctly.Nr   r   r   r   r4   r   r   r   )r  r   searchr   rQ   messager:   r   r   ry   r}   r   r   )rG   r   run_managersearch_requestresponsere   r   rz   s           r'   _get_relevant_documentsz/VertexAISearchRetriever._get_relevant_documentsW  sO    44U;	||**>:H   A% .. %* 
 BB  *I& ! ""a'@@AQAQRI  ""a'(,(C(C$  ==  *I  &:$//012 3  	$s)MN 	s   C/ /	D8DDr   )r   r   )r   r,   r   Dict[str, Any]r   zOptional[Dict[str, Any]])r   r,   r   z'DiscoveryEngineSearchRequest'r   r,   r  r   r   r   )!r   r   r   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   r   r   r   r   r  r
  r   r   s   @r'   r   r     s    !FM "Hm"5&*m*&#(D(`qQ37M374',Q1'CCO(-aA"(E #EP,1#T1 "'qQ1!=3= #1q9s9 &+1q%AsA "'qQ1!=3=	 ,0J(/ '+m*
 265 /4CCC.POP &1]G"2&=OS("$
@/b3#j)/V''*H'	'r)   r   c                       e Zd ZU dZdZded<   	  e       Zded<    e       Zded<    e	dd	
      Z
d fdZ	 	 	 	 	 	 ddZ xZS ) VertexAIMultiTurnSearchRetrieverzGoogle Vertex AI Search retriever for multi-turn conversations.

    Supports both stable (v1) and beta versions of the Discovery Engine API.
    -r,   conversation_id#DiscoveryEngineConversationalClientr   r   ignoreTr>   r?   c                   t        |   d
i | | j                  rddlm} nddlm}  || j                  | j                  t        d            | _	        | j                  j                  | j                  | j                  | j                  | j                        | _        | j                   dk(  rt#        dd	| j                    z         y )Nr   r   r   r   r   r   r   zOData store type 1 (Structured)is not currently supported for multi-turn search.r   rC   )rE   rF   rB   r   r   r   r3   ri   r   r   r   r-   r0   r.   r2   r   r:   r   )rG   rH   r   rI   s      r'   rF   z)VertexAIMultiTurnSearchRetriever.__init__  s    "6"99 9((..'/AB
  $||??OO%%))11	  @  
   A%%D$//012  &r)   c                  | j                   r	ddlm}m} n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)rp   r   r   r4   r   )rB   r   r  r  r   r   conversation_pathr-   r0   r.   r  r   converse_conversationr:   r   search_resultsr   )rG   r   r  r  r  requestr	  s          r'   r
  z8VertexAIMultiTurnSearchRetriever._get_relevant_documents  s    
 99 

 .//  ""$$	  //%(	
 <<55g>  A%88'')=  99##%9
 	
r)   )rH   r   r  )r   r   r   r  r  r   r   r   r   r   r   rF   r
  r   r   s   @r'   r  r    sc    
 OS+3>=G0@&=OS( $L
@#
#
*H#
	#
r)   r  c                       e Zd ZU dZdZded<   	 dZded<   	 dZd	ed
<   	  ee	      Z
ded<   	  edd      Zd fdZddZ xZS )VertexAISearchSummaryToolzClass that exposes a tool to interface with an App in Vertex Search and

    Conversation and get the summary of the documents retrieved.

    Supports both stable (v1) and beta versions of the Discovery Engine API.
    Nr   summary_promptr   r9   summary_result_countTr;   summary_include_citations)default_factoryr  summary_spec_kwargsr=   r  c                <   | j                   rddlm} nddlm} t        |          xs i } |j                  j                  d| j                  | j                  |j                  j                  j                  | j                        d| j                  |d<   |S )zAdds additional summary_spec parameters to the configuration of the search.
        Returns:
            kwargs for the specification of the content.
        r   r   )preamble)r"  include_citationsmodel_prompt_specsummary_specrC   )rB   r   r   r   rE   r   r   SummarySpecr"  r#  ModelPromptSpecr!  r%  )rG   r   rH   rI   s      r'   r   z2VertexAISearchSummaryTool._get_content_spec_kwargs  s     99IE139r "M!@!@!L!L "
!%!:!:"<<+==IIYY,, Z "
 &&"
~ r)   c                    | j                  |      }| j                  j                  |      }|j                  j                  S )zRuns the tool.

        Args:
            search_query: The query to run by the agent.

        Returns:
            The response from the agent.
        )r  r   r  summarysummary_text)rG   
user_queryr  r	  s       r'   _runzVertexAISearchSummaryTool._run  s:     --j9<<&&w/,,,r)   r  )r0  r,   r   r,   )r   r   r   r  r!  r   r"  r#  r   r   r%  r   r   r   r1  r   r   s   @r'   r   r     s`     %)NM(, !#!8&*t*7*/*EEN $L
4-r)   r   )r%   r  r&   rQ   r   r   );r  
__future__r   rw   r`   typingr   r   r   r   r   r	   r
   google.api_core.client_optionsr   google.api_core.exceptionsr   google.protobuf.json_formatr   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.loadr   r   langchain_core.retrieversr   langchain_core.toolsr   langchain_core.utilsr   pydanticr   r   r   r   !langchain_google_community._utilsr   r   r   r   r   r   r   %BetaConversationalSearchServiceClientBetaSearchRequestBetaSearchResponseBetaSearchServiceClientr   DiscoveryEngineSearchRequestSearchResultDiscoveryEngineSearchResultr  r(   r+   r   r  r   rC   r)   r'   <module>rF     s   #   L L L 8 6 5 C - 2 3 ) 5 D D =  ""57N"NO#(8I)I#J "'##%7%D%DD# +0)+PP+'
Q< Qhfm-I fRU
}6R U
p>-*A >-r)   