U
    h"                     @   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mZmZ d dl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)	BaseModel	SecretStrroot_validator)Documentc                   @   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   G/tmp/pip-unpacked-wheel-9gdii04g/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                   @   sP   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
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_metadataT)pre)valuesreturnc                 C   s   | ddk|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   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,   E   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+   r.   )Zpage_contentmetadata)r   r/   r"   r#   r+   r-   r.   )r%   r2   r   r   r   adaptQ   s    zArceeDocumentAdapter.adaptN)r   r   r   r   classmethodr,   r   r4   r   r   r   r   r1   N   s   r1   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 n8 tk
r }	 ztd| d|	 |	W 5 d}	~	X Y nX 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   r7   r:   r8   r9   r   r   r   format_make_requestr$   r<   	Exception
ValueError)
selfr7   r8   r9   r:   r;   Zarcee_api_key_routeresponseer   r   r   __init__h   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   rA   r<   )rC   r   r   r   validate_model_training_status   s    
z+ArceeWrapper.validate_model_training_status)postr$   )methodrD   bodyparamsheadersr!   c           	      C   sT   | j |d}| j|d}tt|}|||||d}|jdk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
        )rM   )rD   )jsonrL   rM   )      z"Failed to make request. Response: )_make_request_headers_make_request_urlgetattrrequestsstatus_coderA   textrN   )	rC   rJ   rD   rK   rL   rM   urlZreq_typerE   r   r   r   r@      s    

zArceeWrapper._make_request)rM   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>   r7   r   	TypeErrortypeZget_secret_valueupdate)rC   rM   Zapi_keyZinternal_headersr   r   r   rQ      s    

z"ArceeWrapper._make_request_headers)rD   r!   c                 C   s   | j  d| j d| S )N/)r8   r9   )rC   rD   r   r   r   rR      s    zArceeWrapper._make_request_url)promptkwargsr!   c                 K   sF   | j p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 |qS r   )r   ).0fr   r   r   
<listcomp>   s     z>ArceeWrapper._make_request_body_for_models.<locals>.<listcomp>filterssize   )r<   queryrb   ra   r+   )r:   r$   dictr<   )rC   r\   r]   Z_model_kwargs_paramsra   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.
        rI   r\   rJ   rD   rK   rV   )r@   r   r   r   rg   )rC   r\   r]   rE   r   r   r   r      s    zArceeWrapper.generate)rd   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.
        rI   r\   rh   c                 S   s   g | ]}t tf |qS r   )r1   r4   r,   )r^   docr   r   r   r`      s   z)ArceeWrapper.retrieve.<locals>.<listcomp>results)r@   r   r   r   rg   )rC   rd   r]   rE   r   r   r   r      s    zArceeWrapper.retrieve)NNN)N)r   r   r   r   r	   r'   r   r   r   r   rG   rH   r   r   r   re   r@   rQ   rR   rg   r   r   r   r   r   r   r   r   r6   b   sB   
$   
 

r6   )enumr   typingr   r   r   r   r   r   r	   rT   Zlangchain_core.pydantic_v1r
   r   r   Zlangchain_core.retrieversr   r'   r   r   r   r*   r,   r1   r6   r   r   r   r   <module>   s   $!	