a
    bg"                     @   s   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 deeZG dd	 d	eeZG d
d deZG dd deZG dd deZG dd dZG dd dZdS )    )Enum)AnyDictListLiteralMappingOptionalUnionN)Document)	BaseModel	SecretStrmodel_validatorc                   @   s   e Zd ZdZdZdZdZdS )
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 r   r   q/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/arcee.pyr      s   r   c                   @   s   e Zd ZdZdZdZdS )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   r   c                   @   sT   e Zd ZU dZeed< eed< eed< dZeed< e	dde
eed	d
dZdS )
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_nameZfilter_typevalueF_is_metadatabefore)mode)valuesreturnc                 C   s   | ddv|d< |S )zDdocument and name are reserved arcee keys. Anything else is metadatar   )documentnameZ_is_meta)get)clsr!   r   r   r   set_meta6   s    zDALMFilter.set_metaN)r   r   r   r   str__annotations__r   r   boolr   classmethodr   r   r'   r   r   r   r   r      s   
r   c                   @   s*   e Zd ZU dZeed< eed< eed< dS )ArceeDocumentSourcezSource of an Arcee document.r#   r$   idN)r   r   r   r   r(   r)   r   r   r   r   r,   >   s   
r,   c                   @   s2   e Zd ZU dZeed< eed< eed< eed< dS )ArceeDocumentzArcee document.indexr-   scoresourceN)r   r   r   r   r(   r)   floatr,   r   r   r   r   r.   F   s
   
r.   c                   @   s$   e Zd ZdZeeedddZdS )ArceeDocumentAdapterzAdapter for Arcee documents)arcee_documentr"   c                 C   s*   t |jj|jj|jj|j|j|jddS )z=Adapts an `ArceeDocument` to a langchain's `Document` object.)r$   Z	source_idr/   r-   r0   )Zpage_contentmetadata)r
   r1   r#   r$   r-   r/   r0   )r&   r4   r   r   r   adaptR   s    zArceeDocumentAdapter.adaptN)r   r   r   r   r+   r.   r
   r6   r   r   r   r   r3   O   s   r3   c                   @   s   e Zd ZdZeeef eeeeee	f  edddZ
ddddZded	 eeef eeee	f  ee ee ed
ddZdee edddZeeef edddZeeee	f eee	f dddZee	edddZee	ee dddZdS )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           
   
   C   s   t |trt|}n|}|| _|| _|| _|| _z8tjj	j
|d}| d|}|d| _|d| _W n: ty }	 z"td| d|	 |	W Y d}	~	n
d}	~	0 0 dS )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.
        )Z
id_or_namer%   model_idstatusz2Error while validating model training status for 'z': N)
isinstancer(   r   r8   r;   r9   r:   r   r   r   format_make_requestr%   r=   	Exception
ValueError)
selfr8   r9   r:   r;   r<   Zarcee_api_key_routeresponseer   r   r   __init__i   s$    

zArceeWrapper.__init__N)r"   c                 C   s    | j dkrtd| j dd S )NZtraining_completezModel z4 is not ready. Please wait for training to complete.)r   rB   r=   )rD   r   r   r   validate_model_training_status   s    
z+ArceeWrapper.validate_model_training_status)postr%   )methodrE   bodyparamsheadersr"   c           	      C   sT   | j |d}| j|d}tt|}|||||d}|jdvrLtd|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
        )rN   )rE   )jsonrM   rN   )      z"Failed to make request. Response: )_make_request_headers_make_request_urlgetattrrequestsstatus_coderB   textrO   )	rD   rK   rE   rL   rM   rN   urlZreq_typerF   r   r   r   rA      s    

zArceeWrapper._make_request)rN   r"   c                 C   sJ   |pi }t | jts(tdt| j | j }|dd}|| |S )Nz'arcee_api_key must be a SecretStr. Got zapplication/json)zX-TokenzContent-Type)r?   r8   r   	TypeErrortypeZget_secret_valueupdate)rD   rN   Zapi_keyZinternal_headersr   r   r   rR      s    

z"ArceeWrapper._make_request_headers)rE   r"   c                 C   s   | j  d| j d| S )N/)r9   r:   )rD   rE   r   r   r   rS      s    zArceeWrapper._make_request_url)promptkwargsr"   c                 K   sJ   | j pi }i ||}dd |dg D }t| j||dd|| jdS )z;Make the request body for generate/retrieve models endpointc                 S   s   g | ]}t f i |qS r   )r   ).0fr   r   r   
<listcomp>       z>ArceeWrapper._make_request_body_for_models.<locals>.<listcomp>filterssize   )r=   queryrd   rc   r-   )r;   r%   dictr=   )rD   r]   r^   Z_model_kwargs_paramsrc   r   r   r   _make_request_body_for_models   s    

z*ArceeWrapper._make_request_body_for_modelsc                 K   s,   | j dtjj| jf d|i|d}|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.
        rJ   r]   rK   rE   rL   rW   )rA   r   r   r   ri   )rD   r]   r^   rF   r   r   r   r      s    zArceeWrapper.generate)rf   r^   r"   c                 K   s6   | j dtjj| jf d|i|d}dd |d D S )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.
        rJ   r]   rj   c                 S   s    g | ]}t tf i |qS r   )r3   r6   r.   )r_   docr   r   r   ra      s   z)ArceeWrapper.retrieve.<locals>.<listcomp>results)rA   r   r   r   ri   )rD   rf   r^   rF   r   r   r   r      s    zArceeWrapper.retrieve)NNN)N)r   r   r   r   r	   r(   r   r   r   r   rH   rI   r   r   r   rg   rA   rR   rS   ri   r   r   r
   r   r   r   r   r   r7   c   s@   
$   

r7   )enumr   typingr   r   r   r   r   r   r	   rU   Zlangchain_core.retrieversr
   Zpydanticr   r   r   r(   r   r   r   r,   r.   r3   r7   r   r   r   r   <module>   s   $"	