
    7|h%                         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j                  e      ZdZ G d d	e      Zy)
zUtil that calls Outline.    N)AnyDictListOptional)Documentget_from_dict_or_env)	BaseModelmodel_validatori,  c                       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      ededefd              ZdedefdZdedefdZdedee   fdZy)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                 N    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   s       d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/outline.pyvalidate_environmentz&OutlineAPIWrapper.validate_environment   sJ      4*,B 
 *>%&.%'8
 %4 !    outline_resc                     |d   d   | j                   |d   d   z   d}| j                  r-|d   d   |d   |d   d   |d   d   |d   d	   |d   d
   d   dni }t        |d   d   d | j                   i ||      }|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docs        r   _result_to_documentz%OutlineAPIWrapper._result_to_document/   s     ,W5//+j2I%2PP
	 ++ "*-d3&y1!,Z!8!H&1*&=>P&Q'
3J?)*5kB6J  	 $Z089U4;U;UV
 
r   queryc                    t        j                  | j                   | j                   || j                  ddd| j
                   i      }|j                  st        d|j                        |j                         d   S )N)r8   limitAuthorizationzBearer )dataheaderszOutline API returned an error: r<   )
requestspostr   r   r   r   ok
ValueErrorr0   json)r3   r8   
raw_results      r   _outline_api_queryz$OutlineAPIWrapper._outline_api_queryI   s{    ]](()$*F*F)GH 4+=+=>$0D0D/E&FG

 }}>
PP ((r   c                     | j                  |dt               }g }|d| j                   D ]'  }| j                  |      x}s|j	                  |       ) |S )z
        Run Outline search and get the document content plus the meta information.

        Returns: a list of documents.

        N)rD   OUTLINE_MAX_QUERY_LENGTHr   r7   append)r3   r8   resultsdocsresultr6   s         r   runzOutlineAPIWrapper.runU   sh     ))%0I1I*JK2 2 23 	!F..v66s6C 	! r   )__name__
__module____qualname____doc__r   int__annotations__r   boolr   r   r   strr   r   r   classmethodr   r   r   r   r7   r   rD   rK    r   r   r   r      s     M3$)T)!%3%*.(3-.%)OXc])#:S:(#$ 3   $s x 4
) 
) 
) h r   r   )rO   loggingtypingr   r   r   r   r>   langchain_core.documentsr   langchain_core.utilsr	   pydanticr
   r   	getLoggerrL   loggerrF   r   rU   r   r   <module>r]      sC      , ,  - 5 /			8	$ Q	 Qr   