
    3fie                         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mZ ddlmZ erdd	lmZmZmZ dd
lmZ  eddd       G d de             Zy)z9Retriever wrapper for Google Cloud Document AI Warehouse.    )TYPE_CHECKINGAnyDictListOptional)
deprecated)CallbackManagerForRetrieverRun)Document)BaseRetriever)get_from_dict_or_envpre_init)get_client_info)DocumentServiceClientRequestMetadataSearchDocumentsRequest)SearchDocumentsPagerz0.0.32z1.0z7langchain_google_community.DocumentAIWarehouseRetriever)sinceremovalalternative_importc            	           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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)"GoogleDocumentAIWarehouseRetrievera4  A retriever based on Document AI Warehouse.

    Documents should be created and documents should be uploaded
        in a separate flow, and this retriever uses only Document AI
        schema_id provided to search for relevant documents.

    More info: https://cloud.google.com/document-ai-warehouse.
    uslocationproject_numberN	schema_id   qa_size_limitr   client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   zogoogle.cloud.contentwarehouse is not installed.Please install it with pip install google-cloud-contentwarehouseNr   PROJECT_NUMBERzdocument-ai-warehouse)module)client_infor   ) google.cloud.contentwarehouse_v1r   ImportErrorr   r   )clsr   r   excs       ~/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/retrievers/google_cloud_documentai_warehouse.pyvalidate_environmentz7GoogleDocumentAIWarehouseRetriever.validate_environment2   sp    	N $8$&6$
  1'/FG
x   	S 	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_metadataz<GoogleDocumentAIWarehouseRetriever._prepare_request_metadataE   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)r0   r3   r4   r5   r7   r8   s         r)   _get_relevant_documentsz:GoogleDocumentAIWarehouseRetriever._get_relevant_documentsK   sF     /$..u??;;///@**H*==r2   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)r3   is_nl_querydocument_schema_names)parentrequest_metadatadocument_queryr   )r%   r>   r   KeyError
ValueErrorr1   r   appendr   document_schema_pathr   r   common_location_pathr   )r0   r3   r5   r>   r   r+   rD   schemass           r)   r9   z:GoogleDocumentAIWarehouseRetriever._prepare_search_requestR   s    	

	G{+I  99I9N>>NN00 //!]]$(NN 1  &;;33D4G4GW-(W ,,
 	
  	GEFF	Gs   C Cr8   r   c                     g }|j                   D ]U  }|j                  j                  |j                  j                  d}|j	                  t        |j                  |             W |S )N)titlesource)page_contentmetadata)matching_documentsdocumentrM   raw_document_pathrH   r
   search_text_snippet)r0   r8   	documentsdocrP   s        r)   r;   z9GoogleDocumentAIWarehouseRetriever._parse_search_responser   sg     	.. 	C++,,88H c&=&=Q	 r2   )__name__
__module____qualname____doc__r   str__annotations__r   r   r   intr   r   r   r*   r1   r	   r   r   r
   r<   r9   r;    r2   r)   r   r      s     HcFB#Ix}#NM38&*F#*$ 4  $43 4;L 4>>*H>TW>	h>

$'
	!
@.	hr2   r   N)rZ   typingr   r   r   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr	   langchain_core.documentsr
   langchain_core.retrieversr   langchain_core.utilsr   r   &langchain_community.utilities.vertexair   r%   r   r   r   Agoogle.cloud.contentwarehouse_v1.services.document_service.pagersr   r   r^   r2   r)   <module>rg      s^    ? ; ; 6 C - 3 ? B 

 
P
a a
ar2   