
    hhe                         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dS )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dS )"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} n"# t          $ r}t          d          |d}~ww xY wt          |dd          |d<    |t	          d          	          |d
<   |S )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/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_community/retrievers/google_cloud_documentai_warehouse.pyvalidate_environmentz7GoogleDocumentAIWarehouseRetriever.validate_environment2   s    	NNNNNNN 	 	 	S  	 $8$&6$
 $
  10'/FGGG
 
 
x s   	 
(#(	user_ldapr   c                 H    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   sH    NNNNNNNNH 3	 3 3444	3333    queryrun_managerkwargsc                     | j         |fi |}| 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   sM     /$.u????;///@@**H*===r2   r   c           	         ddl m}m} 	 |d         }n# t          $ r t	          d          w xY w|                     |          }g }| j        r?|                    | j        	                    | j
        | j        | j                              || j                            | j
        | j                  | ||d|          | j        	          S )
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{+II 	G 	G 	GEFFF	G  99I9NN> 	NN00 /!]$(N 1     &%;33D4GWW-(=W   ,
 
 
 	
s    -r8   r   c                     g }|j         D ]D}|j        j        |j        j        d}|                    t          |j        |                     E|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   sq     	. 	 	C+,8 H c&=QQ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    X$43 4;L 4 4 4 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      sf   ? ? ; ; ; ; ; ; ; ; ; ; ; ; ; ; 6 6 6 6 6 6 C C C C C C - - - - - - 3 3 3 3 3 3 ? ? ? ? ? ? ? ? B B B B B B          
     
 
P  
a a a a a a a 
a a ar2   