o
    ȳg"                     @   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   _/var/www/html/chatdoc2/venv/lib/python3.10/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                   @   sV   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
d	ed
ef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_name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name_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   r0   F   s   
 r0   c                   @   s&   e Zd ZdZededef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%   	source_idr1   r/   r2   )page_contentmetadata)r
   r3   r$   r%   r/   r1   r2   )r(   r6   r   r   r   adaptR   s   zArceeDocumentAdapter.adaptN)r   r   r   r   r-   r0   r
   r:   r   r   r   r   r5   O   s    r5   c                   @   s  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dZ
d"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dZd$dee d	efddZdeeef d	efddZdedeee	f d	eee	f fddZdede	d	efddZdede	d	ee f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|}|| _|| _|| _|| _ztjj	j
|d}| d|}|d| _|d| _W dS  tyL }	 ztd| d|	 |	d}	~	w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)
isinstancer*   r   r<   r?   r=   r>   r   r   r   format_make_requestr'   rB   	Exception
ValueError)
selfr<   r=   r>   r?   r@   arcee_api_key_routeresponseer   r   r   __init__i   s(   

zArceeWrapper.__init__r#   Nc                 C   s    | j dkrtd| j dd S )Ntraining_completezModel z4 is not ready. Please wait for training to complete.)r   rG   rB   )rI   r   r   r   validate_model_training_status   s
   
z+ArceeWrapper.validate_model_training_statusmethod)postr'   rK   bodyparamsheadersc           	      C   sT   | j |d}| j|d}tt|}|||||d}|jdvr&t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
        )rU   )rK   )jsonrT   rU   )      z"Failed to make request. Response: )_make_request_headers_make_request_urlgetattrrequestsstatus_coderG   textrV   )	rI   rQ   rK   rS   rT   rU   urlreq_typerL   r   r   r   rF      s   

zArceeWrapper._make_requestc                 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)rD   r<   r   	TypeErrortypeget_secret_valueupdate)rI   rU   api_keyinternal_headersr   r   r   rY      s   

z"ArceeWrapper._make_request_headersc                 C   s   | j  d| j d| S )N/)r=   r>   )rI   rK   r   r   r   rZ      s   zArceeWrapper._make_request_urlpromptkwargsc                 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 d i |qS r   )r   ).0fr   r   r   
<listcomp>   s    z>ArceeWrapper._make_request_body_for_models.<locals>.<listcomp>filterssize   )rB   queryro   rn   r/   )r?   r'   dictrB   )rI   rh   ri   _model_kwargs_paramsrn   r   r   r   _make_request_body_for_models   s   

z*ArceeWrapper._make_request_body_for_modelsc                 K   s,   | j dtjj| jd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.
        rR   rh   rQ   rK   rS   r^   Nr   )rF   r   r   r   ru   )rI   rh   ri   rL   r   r   r   r      s   zArceeWrapper.generaterq   c                 K   s6   | j dtjj| jd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.
        rR   rh   rv   c                 S   s    g | ]}t td i |qS rj   )r5   r:   r0   )rk   docr   r   r   rm      s    z)ArceeWrapper.retrieve.<locals>.<listcomp>resultsNr   )rF   r   r   r   ru   )rI   rq   ri   rL   r   r   r   r      s   zArceeWrapper.retrieve)r#   N)NNN)N)r   r   r   r   r	   r*   r   r   r   r   rN   rP   r   r   r   rr   rF   rY   rZ   ru   r   r   r
   r   r   r   r   r   r;   c   sj    


$





r;   )enumr   typingr   r   r   r   r   r   r	   r\   langchain_core.retrieversr
   pydanticr   r   r   r*   r   r   r   r.   r0   r5   r;   r   r   r   r   <module>   s   $"	