U
    h                      @   sv   d Z ddlZddlmZmZmZmZ ddlZddlm	Z	 ddl
mZmZ ddlmZ eeZdZG dd	 d	eZdS )
zUtil that calls Outline.    N)AnyDictListOptional)Document)	BaseModelroot_validatorget_from_dict_or_envi,  c                   @   s   e Zd ZU dZdZeed< dZeed< dZ	eed< dZ
ee ed	< dZee ed
< dZeed< eddeedddZeedddZeedddZeee dddZdS )OutlineAPIWrapperzWrapper around OutlineAPI.

    This wrapper will use the Outline API to query the documents of your instance.
    By default it will return the document content of the top-k results.
    It limits the document content by doc_content_chars_max.
       top_k_resultsFload_all_available_metai  doc_content_chars_maxNoutline_instance_urloutline_api_keyz/api/documents.searchoutline_search_endpointT)pre)valuesreturnc                 C   s,   t |dd}||d< t |dd}||d< |S )z=Validate that instance url and api key exists in environment.r   ZOUTLINE_INSTANCE_URLr   ZOUTLINE_API_KEYr	   )clsr   r   r    r   I/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/outline.pyvalidate_environment   s        z&OutlineAPIWrapper.validate_environment)outline_resr   c                 C   s   |d d | j |d d  d}| jrh|d d |d |d d |d d |d d	 |d d
 d dni }t|d d d | j ||d}|S )Ndocumenttitleurl)r   sourceidrankingZcollectionIdZparentDocumentIdrevisionZ	createdByname)r   r    Zcollection_idZparent_document_idr!   Z
created_bytext)Zpage_contentmetadata)r   r   r   r   )selfr   Z	main_metaZadd_metadocr   r   r   _result_to_document.   s*    




	z%OutlineAPIWrapper._result_to_document)queryr   c                 C   sN   t j| j | j || jddd| j id}|jsBtd|j|	 d S )N)r(   limitAuthorizationzBearer )dataheaderszOutline API returned an error: r+   )
requestspostr   r   r   r   ok
ValueErrorr#   json)r%   r(   Z
raw_resultr   r   r   _outline_api_queryH   s    
z$OutlineAPIWrapper._outline_api_queryc                 C   sF   |  |dt }g }|d| j D ]}| | }r$|| q$|S )z
        Run Outline search and get the document content plus the meta information.

        Returns: a list of documents.

        N)r2   OUTLINE_MAX_QUERY_LENGTHr   r'   append)r%   r(   resultsZdocsresultr&   r   r   r   runT   s    zOutlineAPIWrapper.run)__name__
__module____qualname____doc__r   int__annotations__r   boolr   r   r   strr   r   r   r   r   r   r   r'   r   r2   r7   r   r   r   r   r      s   
r   )r;   loggingtypingr   r   r   r   r-   Zlangchain_core.documentsr   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr
   	getLoggerr8   loggerr3   r   r   r   r   r   <module>   s   
