o
    Zhh                     @   s   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rFdd	lmZmZmZ dd
lmZ G dd deZdS )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                	   @   s   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dededefddZdeddfddZdedededee fddZdededdfddZdddee fdd ZdS )!DocumentAIWarehouseRetrievera4  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before)modevaluesreturnc              
   C   s\   zddl m} W n ty } ztd|d}~ww t|dd|d< |tddd	|d
< |S )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   ZPROJECT_NUMBERzdocument-ai-warehouse)module)Zclient_infor   ) google.cloud.contentwarehouse_v1r   ImportErrorr
   r   )clsr   r   exc r"   f/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_community/documentai_warehouse.pyvalidate_environment-   s"   
z1DocumentAIWarehouseRetriever.validate_environment	user_ldapr   c                 C   s*   ddl m}m} |d| d}||dS )Nr   )r   UserInfozuser:)id)	user_info)r   r   r&   )selfr%   r   r&   r(   r"   r"   r#   _prepare_request_metadataB   s   
z6DocumentAIWarehouseRetriever._prepare_request_metadataqueryrun_managerkwargsc                K   s,   | j |fi |}| jj|d}| j|dS )N)request)response)_prepare_search_requestr   Zsearch_documents_parse_search_response)r)   r+   r,   r-   r.   r/   r"   r"   r#   _get_relevant_documentsH   s   z4DocumentAIWarehouseRetriever._get_relevant_documentsr   c                 K   s   ddl m}m} z|d }W n ty   tdw | j|d}g }| jr4|| jj	| j
| j| jd || j| j
| j|||d|d| jd	S )
Nr   )DocumentQueryr   r%   z&Argument user_ldap should be provided!)r%   )projectr   Zdocument_schemaT)r+   Zis_nl_queryZdocument_schema_names)parentrequest_metadataZdocument_queryr   )r   r3   r   KeyError
ValueErrorr*   r   appendr   Zdocument_schema_pathr   r   Zcommon_location_pathr   )r)   r+   r-   r3   r   r%   r6   Zschemasr"   r"   r#   r0   O   s0   z4DocumentAIWarehouseRetriever._prepare_search_requestr/   r   c                 C   s:   g }|j D ]}|jj|jjd}|t|j|d q|S )N)titlesource)Zpage_contentmetadata)Zmatching_documentsZdocumentr:   Zraw_document_pathr9   r   Zsearch_text_snippet)r)   r/   Z	documentsdocr<   r"   r"   r#   r1   o   s   
z3DocumentAIWarehouseRetriever._parse_search_response)__name__
__module____qualname____doc__r   str__annotations__r   r   r   intr   r   classmethodr   r   r$   r*   r   r   r   r2   r0   r1   r"   r"   r"   r#   r      sH   
 	

 r   N)rA   typingr   r   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.retrieversr	   Zlangchain_core.utilsr
   Zpydanticr   Z!langchain_google_community._utilsr   r   r   r   r   ZAgoogle.cloud.contentwarehouse_v1.services.document_service.pagersr   r   r"   r"   r"   r#   <module>   s    