
    7|h"                         d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZmZmZ  G d dee      Z G d d	ee      Z G d
 de      Z G d de      Z G d de      Z G d d      Z G d d      Zy)    )Enum)AnyDictListLiteralMappingOptionalUnionN)Document)	BaseModel	SecretStrmodel_validatorc                       e Zd ZdZdZdZdZy)
ArceeRoutez1Routes available for the Arcee API as enumerator.zmodels/generatezmodels/retrievezmodels/status/{id_or_name}N)__name__
__module____qualname____doc__generateretrievemodel_training_status     b/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/arcee.pyr   r      s    ; H H8r   r   c                       e Zd ZdZdZdZy)DALMFilterTypez:Filter types available for a DALM retrieval as enumerator.fuzzy_searchstrict_searchN)r   r   r   r   r   r   r   r   r   r   r      s    D!L#Mr   r   c                   p    e Zd ZU dZeed<   eed<   eed<   dZeed<    e	d      e
d	ed
efd              Zy)
DALMFilteraX  Filters available for a DALM retrieval and generation.

    Arguments:
        field_name: The field to filter on. Can be 'document' or 'name' to filter
            on your document's raw text or title. Any other field will be presumed
            to be a metadata field you included when uploading your context data
        filter_type: Currently 'fuzzy_search' and 'strict_search' are supported.
            'fuzzy_search' means a fuzzy search on the provided field is performed.
            The exact strict doesn't need to exist in the document
            for this to find a match.
            Very useful for scanning a document for some keyword terms.
            'strict_search' means that the exact string must appear
            in the provided field.
            This is NOT an exact eq filter. ie a document with content
            "the happy dog crossed the street" will match on a strict_search of
            "dog" but won't match on "the dog".
            Python equivalent of `return search_string in full_string`.
        value: The actual value to search for in the context data/metadata
    
field_namefilter_typevalueF_is_metadatabefore)modevaluesreturnc                 2    |j                  d      dv|d<   |S )zDdocument and name are reserved arcee keys. Anything else is metadatar!   )documentname_is_meta)get)clsr'   s     r   set_metazDALMFilter.set_meta6   s"     $ZZ5=QQzr   N)r   r   r   r   str__annotations__r   r$   boolr   classmethodr   r   r/   r   r   r   r    r       sP    ( OJL$(#d s   $r   r    c                   0    e Zd ZU dZeed<   eed<   eed<   y)ArceeDocumentSourcezSource of an Arcee document.r*   r+   idN)r   r   r   r   r0   r1   r   r   r   r5   r5   >   s    &M
IGr   r5   c                   :    e Zd ZU dZeed<   eed<   eed<   eed<   y)ArceeDocumentzArcee document.indexr6   scoresourceN)r   r   r   r   r0   r1   floatr5   r   r   r   r8   r8   F   s    JGLr   r8   c                   *    e Zd ZdZededefd       Zy)ArceeDocumentAdapterzAdapter for Arcee documentsarcee_documentr(   c           	          t        |j                  j                  |j                  j                  |j                  j                  |j
                  |j                  |j                  d      S )z=Adapts an `ArceeDocument` to a langchain's `Document` object.)r+   	source_idr9   r6   r:   )page_contentmetadata)r   r;   r*   r+   r6   r9   r:   )r.   r?   s     r   adaptzArceeDocumentAdapter.adaptR   s`     '..77 '--22+2255'--$'''--
 	
r   N)r   r   r   r   r3   r8   r   rD   r   r   r   r>   r>   O   s%    %
= 
X 
 
r   r>   c                   *   e Zd ZdZdeeef   dededeeee	f      def
dZ
dd
Z	 	 	 dded   deeef   deeee	f      dee   dee   defdZddee   defdZdeeef   defdZdedeee	f   deee	f   fdZdede	defdZdede	dee   fdZy	)ArceeWrapperzMWrapper for Arcee API.

    For more details, see: https://www.arcee.ai/
    arcee_api_keyarcee_api_urlarcee_api_versionmodel_kwargs
model_namec                    t        |t              rt        |      }n|}|| _        || _        || _        || _        	 t        j                  j                  j                  |      }| j                  d|      }|j                  d      | _        |j                  d      | _        y# t        $ r}	t        d| d|	       |	d}	~	ww xY w)a5  Initialize ArceeWrapper.

        Arguments:
            arcee_api_key: API key for Arcee API.
            arcee_api_url: URL for Arcee API.
            arcee_api_version: Version of Arcee API.
            model_kwargs: Keyword arguments for Arcee API.
            model_name: Name of an Arcee model.
        )
id_or_namer-   model_idstatusz2Error while validating model training status for 'z': N)
isinstancer0   r   rG   rJ   rH   rI   r   r   r#   format_make_requestr-   rN   	Exception
ValueError)
selfrG   rH   rI   rJ   rK   arcee_api_key_routeresponsees
             r   __init__zArceeWrapper.__init__i   s    " mS)&}5N*N(6(*!2	44::AAZAXE))%7H$LL4DM)1h)?D& 	DZLPSTUSVW	s   A(B% %	C.C  Cr(   Nc                 T    | j                   dk7  rt        d| j                   d      y )Ntraining_completezModel z4 is not ready. Please wait for training to complete.)r   rS   rN   )rU   s    r   validate_model_training_statusz+ArceeWrapper.validate_model_training_status   s8    %%)<< (8 8  =r   method)postr-   rW   bodyparamsheadersc                     | j                  |      }| j                  |      }t        t        |      } |||||      }|j                  dvrt        d|j                         |j                         S )a  Make a request to the Arcee API
        Args:
            method: The HTTP method to use
            route: The route to call
            body: The body of the request
            params: The query params of the request
            headers: The headers of the request
        )rb   )rW   )jsonra   rb   )      z"Failed to make request. Response: )_make_request_headers_make_request_urlgetattrrequestsstatus_coderS   textrd   )	rU   r^   rW   r`   ra   rb   urlreq_typerX   s	            r   rR   zArceeWrapper._make_request   sw      ,,W,=$$5$18V,Cd67Kz1@PQQ}}r   c                     |xs i }t        | j                  t              s!t        dt	        | j                               | j                  j                         }|dd}|j                  |       |S )Nz'arcee_api_key must be a SecretStr. Got zapplication/json)zX-TokenzContent-Type)rP   rG   r   	TypeErrortypeget_secret_valueupdate)rU   rb   api_keyinternal_headerss       r   rg   z"ArceeWrapper._make_request_headers   sv    -R$,,i89$t?Q?Q:R9ST  $$557.
 	'(r   c                 >    | j                    d| j                   d| S )N/)rH   rI   )rU   rW   s     r   rh   zArceeWrapper._make_request_url   s'    $$%Qt'='=&>awGGr   promptkwargsc                     | j                   xs i }i ||}|j                  dg       D cg c]  }t        di | }}t        | j                  ||j                  dd      || j                        S c c}w )z;Make the request body for generate/retrieve models endpointfilterssize   )rN   queryr|   r{   r6   r   )rJ   r-   r    dictrN   )rU   rx   ry   _model_kwargs_paramsfr{   s          r   _make_request_body_for_modelsz*ArceeWrapper._make_request_body_for_models   s|     ))/R-]-f-,3KK	2,FGq:??GG]]VQ'}}
 	
 Hs   A3c           	          | j                  dt        j                  j                   | j                  dd|i|      }|d   S )a+  Generate text from Arcee DALM.

        Args:
            prompt: Prompt to generate text from.
            size: The max number of context results to retrieve. Defaults to 3.
              (Can be less if filters are provided).
            filters: Filters to apply to the context dataset.
        r_   rx   r^   rW   r`   rl   r   )rR   r   r   r#   r   )rU   rx   ry   rX   s       r   r   zArceeWrapper.generate   sX     %%%%++333  & 
 r   r~   c           
          | j                  dt        j                  j                   | j                  dd|i|      }|d   D cg c]   }t
        j                  t        di |      " c}S c c}w )aI  Retrieve {size} contexts with your retriever for a given query

        Args:
            query: Query to submit to the model
            size: The max number of context results to retrieve. Defaults to 3.
              (Can be less if filters are provided).
            filters: Filters to apply to the context dataset.
        r_   rx   r   resultsr   )rR   r   r   r#   r   r>   rD   r8   )rU   r~   ry   rX   docs        r   r   zArceeWrapper.retrieve   s     %%%%++333  & 
  	*
 !&&}';s';<
 	
 
s   %A.)r(   N)NNN)N)r   r   r   r   r
   r0   r   r	   r   r   rZ   r]   r   r   r   r   rR   rg   rh   r   r   r   r   r   r   r   r   rF   rF   c   sq   
"S)^," " 	"
 tCH~." "H -1!%"&& Z_% wsCx()	
  $ 
4Xd^ t HuZ_'= H# H

%,S#X%6
	c	
      
	 0

 
 
h	
r   rF   )enumr   typingr   r   r   r   r   r	   r
   rj   langchain_core.retrieversr   pydanticr   r   r   r0   r   r   r    r5   r8   r>   rF   r   r   r   <module>r      sv   
  E E E  . : :9d 9$S$ $ D)  I  
 
(]
 ]
r   