o
    ȳg%                     @   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   OUTLINE_INSTANCE_URLr   OUTLINE_API_KEYr   )clsr   r   r    r   a/var/www/html/chatdoc2/venv/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 )Ndocumenttitleurl)r   sourceidrankingcollectionIdparentDocumentIdrevision	createdByname)r"   r#   collection_idparent_document_idr&   
created_bytext)page_contentmetadata)r   r   r   r   )selfr   	main_meta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)r4   limitAuthorizationzBearer )dataheaderszOutline API returned an error: r7   )
requestspostr   r   r   r   ok
ValueErrorr,   json)r/   r4   
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)r?   OUTLINE_MAX_QUERY_LENGTHr   r3   append)r/   r4   resultsdocsresultr2   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   r3   r   r?   rE   r   r   r   r   r      s   
 r   )rI   loggingtypingr   r   r   r   r9   langchain_core.documentsr   langchain_core.utilsr   pydanticr	   r
   	getLoggerrF   loggerr@   r   r   r   r   r   <module>   s    
