o
    Zh%                     @   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 ddlmZ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get_from_dict_or_env)	BaseModelmodel_validatori,  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dedefddZdedefddZdedefddZdedee f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_endpointbefore)mode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   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/outline.pyvalidate_environment   s   z&OutlineAPIWrapper.validate_environmentoutline_resc                 C   s   |d d | j |d d  d}| jr4|d d |d |d d |d d |d d	 |d d
 d dni }t|d d d | j i ||d}|S )NZ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queryc                 C   sN   t j| j | j || jddd| j id}|js!t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_queryI   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(   resultsdocsresultr&   r   r   r   runU   s   
zOutlineAPIWrapper.run)__name__
__module____qualname____doc__r   int__annotations__r   boolr   r   r   strr   r   r
   classmethodr   r   r   r   r'   r   r2   r8   r   r   r   r   r      s   
 r   )r<   loggingtypingr   r   r   r   r-   Zlangchain_core.documentsr   Zlangchain_core.utilsr   Zpydanticr	   r
   	getLoggerr9   loggerr3   r   r   r   r   r   <module>   s    
