
    3fiM                         d Z ddl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 erdd	lmZmZmZ dd
lmZ  G d de      Zy)z9Retriever wrapper for Google Cloud Document AI Warehouse.    )TYPE_CHECKINGAnyDictListOptional)CallbackManagerForRetrieverRun)Document)BaseRetriever)get_from_dict_or_env)model_validator)get_client_info)DocumentServiceClientRequestMetadataSearchDocumentsRequest)SearchDocumentsPagerc            	           e Zd ZU dZdZeed<   	 eed<   	 dZee   ed<   	 dZ	e
ed<   	 dZd	ed
<    ed      ededefd              ZdeddfdZdedededee   fdZdededdfdZdddee   fdZy)DocumentAIWarehouseRetrievera  Retriever for Google Cloud Document AI Warehouse.

    Inherits from [`BaseRetriever`][langchain_core.retrievers.BaseRetriever].

    Searches documents using Document AI Warehouse schema. Documents must be
    created and uploaded separately before retrieval.

    See [Document AI Warehouse documentation](https://cloud.google.com/document-ai-warehouse)
    for more information.

    !!! note "Installation"

        Requires additional dependencies:

        ```bash
        pip install langchain-google-community[docai]
        ```
    uslocationproject_numberN	schema_id   qa_size_limitr   clientbefore)modevaluesreturnc                     	 ddl m} t        |dd      |d<    |t	        d      	      |d
<   |S # t        $ r}t        d      |d}~ww xY w)zValidates the environment.r   )r   zCould not import google-cloud-documentai python package. Please, install docai dependency group: `pip install langchain-google-community[docai]`Nr   PROJECT_NUMBERzdocument-ai-warehouse)module)client_infor   ) google.cloud.contentwarehouse_v1r   ImportErrorr   r   )clsr   r   excs       m/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/documentai_warehouse.pyvalidate_environmentz1DocumentAIWarehouseRetriever.validate_environment=   sp    	N $8$&6$
  1'/FG
x   	B 		s   0 	A
AA
	user_ldapr   c                 <    ddl m}m}  |d|       } ||      S )Nr   )r   UserInfozuser:)id)	user_info)r#   r   r+   )selfr)   r   r+   r-   s        r'   _prepare_request_metadataz6DocumentAIWarehouseRetriever._prepare_request_metadataR   s!    N%	{ 34	33    queryrun_managerkwargsc                     | j                   |fi |}| j                  j                  |      }| j                  |      S )N)request)response)_prepare_search_requestr   search_documents_parse_search_response)r.   r1   r2   r3   r5   r6   s         r'   _get_relevant_documentsz4DocumentAIWarehouseRetriever._get_relevant_documentsX   sF     /$..u??;;///@**H*==r0   r   c           	         ddl m}m} 	 |d   }| j                  |      }g }| j                  rK|j                  | j                  j                  | j                  | j                  | j                                || j                  j                  | j                  | j                        | ||d|      | j                  	      S # t        $ r t	        d      w xY w)
Nr   )DocumentQueryr   r)   z&Argument user_ldap should be provided!)r)   )projectr   document_schemaT)r1   is_nl_querydocument_schema_names)parentrequest_metadatadocument_queryr   )r#   r<   r   KeyError
ValueErrorr/   r   appendr   document_schema_pathr   r   common_location_pathr   )r.   r1   r3   r<   r   r)   rB   schemass           r'   r7   z4DocumentAIWarehouseRetriever._prepare_search_request_   s    	

	G{+I  99I9N>>NN00 //!]]$(NN 1  &;;33D4G4GW-(W ,,
 	
  	GEFF	Gs   C Cr6   r   c                     g }|j                   D ]U  }|j                  j                  |j                  j                  d}|j	                  t        |j                  |             W |S )N)titlesource)page_contentmetadata)matching_documentsdocumentrK   raw_document_pathrF   r	   search_text_snippet)r.   r6   	documentsdocrN   s        r'   r9   z3DocumentAIWarehouseRetriever._parse_search_response   sg     	.. 	C++,,88H c&=&=Q	 r0   )__name__
__module____qualname____doc__r   str__annotations__r   r   r   intr   r   classmethodr   r   r(   r/   r   r   r	   r:   r7   r9    r0   r'   r   r      s    & HcFB#Ix}#
 M38&*F#*(#$ 3   $&43 4;L 4>>*H>TW>	h>

$'
	!
@.	hr0   r   N)rX   typingr   r   r   r   r   langchain_core.callbacksr   langchain_core.documentsr	   langchain_core.retrieversr
   langchain_core.utilsr   pydanticr   !langchain_google_community._utilsr   r#   r   r   r   Agoogle.cloud.contentwarehouse_v1.services.document_service.pagersr   r   r]   r0   r'   <module>rf      s@    ? ; ; C - 3 5 $ = 

s= sr0   