a
    bg                      @  s   d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZmZ erd d
lmZ eeZG dd deZdS )    )annotationsN)TYPE_CHECKINGAnyDictIteratorListOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)get_from_dict_or_envpre_init)
get_fields)
ConfigDictFieldmodel_validator)
Predictionc                   @  sR  e Zd ZU dZded< eeddZded< dZd	ed
< dZ	d	ed< edddZ
ded< dZded< eedZded< edddZeddddZeddddZeddddZed d!eddd"d#d$Zed%d%d"d&d'Zeddd(d)Zeddd*d+Zd8dd,d-ddd.d/d0Zd9dd,d-dd1d.d2d3Zddd4d5d6d7ZdS ):	Replicatea  Replicate models.

    To use, you should have the ``replicate`` python package installed,
    and the environment variable ``REPLICATE_API_TOKEN`` set with your API token.
    You can find your token here: https://replicate.com/account

    The model param is required, but any other model parameters can also
    be passed in with the format model_kwargs={model_param: value, ...}

    Example:
        .. code-block:: python

            from langchain_community.llms import Replicate

            replicate = Replicate(
                model=(
                    "stability-ai/stable-diffusion: "
                    "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
                ),
                model_kwargs={"image_dimensions": "512x512"}
            )
    strmodelinput)default_factoryaliaszDict[str, Any]model_kwargsNzOptional[str]replicate_api_token
prompt_keyT)defaultexcluder   version_objFbool	streaming)r   z	List[str]stopZforbid)Zpopulate_by_nameextrazDict[str, str])returnc                 C  s   ddiS )Nr   REPLICATE_API_TOKEN selfr%   r%   p/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/replicate.py
lc_secrets@   s    zReplicate.lc_secretsc                 C  s   dS )NTr%   clsr%   r%   r(   is_lc_serializableD   s    zReplicate.is_lc_serializablec                 C  s   g dS )z*Get the namespace of the langchain object.)Z	langchainZllms	replicater%   r*   r%   r%   r(   get_lc_namespaceH   s    zReplicate.get_lc_namespacebefore)mode)valuesr#   c                 C  s   dd t |  D }|di }|r0td i |di |}t|D ]H}||vrL||v rptd| dt| d| d	 ||||< qL||d< |S )
z>Build extra kwargs from additional params that were passed in.c                 S  s   h | ]}|qS r%   r%   ).0fieldr%   r%   r(   	<setcomp>Q       z(Replicate.build_extra.<locals>.<setcomp>r   zDInit param `input` is deprecated, please use `model_kwargs` instead.r   zFound z supplied twice.zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.)r   keyspoploggerwarninglist
ValueError)r+   r1   Zall_required_field_namesr   r"   
field_namer%   r%   r(   build_extraM   s&    zReplicate.build_extrar   c                 C  s   t |dd}||d< |S )z?Validate that api key and python package exists in environment.r   r$   )r   )r+   r1   r   r%   r%   r(   validate_environmente   s
    zReplicate.validate_environmentc                 C  s   | j | jdS )zGet the identifying parameters.r   r   r?   r&   r%   r%   r(   _identifying_paramsn   s    zReplicate._identifying_paramsc                 C  s   dS )zReturn type of model.r-   r%   r&   r%   r%   r(   	_llm_typev   s    zReplicate._llm_typezOptional[List[str]]z"Optional[CallbackManagerForLLMRun])promptr!   run_managerkwargsr#   c           
      K  s   | j rDd}| j|f||d|D ]}|du r6|j}q"||j7 }q"nN| j|fi |}|  |jdkrrt|jt|j	t
r|j	}nd|j	}|dusJ |p| j}|D ]}	|	|v r|d||	 }q|S )zCall to replicate endpoint.N)r!   rC   failed )r    _streamtext_create_predictionwaitstatusRuntimeErrorerror
isinstanceoutputr   joinr!   find)
r'   rB   r!   rC   rD   
completionchunk
predictionstop_conditionssr%   r%   r(   _call{   s2    



zReplicate._callzIterator[GenerationChunk]c                 k  s   | j |fi |}|p| j}d}d}| D ]z}	||	7 }|D ]<}
|
|v r<|  d}t|	|
d}|	d | }	|	s< qzq<|	r|r|j|	| jd t|	dV  |r, qq,d S )NFrF   Tr   )verbose)rH   )	rI   r!   Zoutput_iteratorcancelmaxrQ   Zon_llm_new_tokenrX   r   )r'   rB   r!   rC   rD   rT   rU   Zstop_condition_reachedZcurrent_completionrO   rV   
stop_indexr%   r%   r(   rG      s.    
zReplicate._streamr   )rB   rD   r#   c           	      K  s  zdd l }W n ty&   tdY n0 | jd u r~d| jv rh| jd\}}|j|}|j|| _n|j| j}|j| _| j	d u rt
| jjd d d d  dd	 d
}|d d | _	| j	|i| j|}d| jvr|jjj| j|dS |jj| j|dS d S )Nr   zZCould not import replicate python package. Please install it with `pip install replicate`.:
componentsZschemasZInput
propertiesc                 S  s   | d  ddS )N   zx-orderr   )get)itemr%   r%   r(   <lambda>   r5   z.Replicate._create_prediction.<locals>.<lambda>)key)r   )versionr   )r-   ImportErrorr   r   splitmodelsr`   versionsZlatest_versionr   sortedZopenapi_schemaitemsr   Zpredictionscreate)	r'   rB   rD   Zreplicate_pythonZ	model_strversion_strr   Zinput_propertiesinput_r%   r%   r(   rI      s@    




zReplicate._create_prediction)NN)NN)__name__
__module____qualname____doc____annotations__r   dictr   r   r   r   r    r:   r!   r   Zmodel_configpropertyr)   classmethodr,   r.   r   r=   r   r>   r@   rA   rW   rG   rI   r%   r%   r%   r(   r      sD   
  $  "r   )
__future__r   loggingtypingr   r   r   r   r   r   Zlangchain_core.callbacksr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.outputsr   Zlangchain_core.utilsr   r   Zlangchain_core.utils.pydanticr   Zpydanticr   r   r   Zreplicate.predictionr   	getLoggerrn   r8   r   r%   r%   r%   r(   <module>   s    
