o
    Zh                      @  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rLd 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                   @  s   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d9ddZed:ddZed;ddZed d!ed<d#d$Zed=d&d'Zed>d(d)Zed?d*d+Z		d@dAd1d2Z		d@dBd4d5ZdCd7d8ZdS )D	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aliasDict[str, Any]model_kwargsNzOptional[str]replicate_api_token
prompt_keyT)defaultexcluder   version_objFbool	streaming)r   	List[str]stopZforbid)Zpopulate_by_nameextrareturnDict[str, str]c                 C  s   ddiS )Nr   REPLICATE_API_TOKEN selfr(   r(   Y/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/replicate.py
lc_secrets@   s   zReplicate.lc_secretsc                 C     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valuesc                 C  s   dd t |  D }|di }|rtd i |di |}t|D ]$}||vrJ||v r8td| dt| d| d	 ||||< q&||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/   r5   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/   r5   r   r(   r(   r+   validate_environmente   s
   zReplicate.validate_environmentc                 C  s   | j | jdS )zGet the identifying parameters.r   r   rC   r)   r(   r(   r+   _identifying_paramsn   s   zReplicate._identifying_paramsc                 C  r-   )zReturn type of model.r1   r(   r)   r(   r(   r+   	_llm_typev   s   zReplicate._llm_typepromptOptional[List[str]]run_manager"Optional[CallbackManagerForLLMRun]kwargsc           
      K  s   | j r"d}| j|f||d|D ]}|du r|j}q||j7 }qn'| j|fi |}|  |jdkr9t|jt|j	t
rC|j	}nd|j	}|dusOJ |pS| j}|D ]}	|	|v re|d||	 }qV|S )zCall to replicate endpoint.N)r#   rH   failed )r!   _streamtext_create_predictionwaitstatusRuntimeErrorerror
isinstanceoutputr   joinr#   find)
r*   rF   r#   rH   rJ   
completionchunk
predictionstop_conditionssr(   r(   r+   _call{   s6   



zReplicate._callIterator[GenerationChunk]c                 k  s    | j |fi |}|p| j}d}d}| D ]>}	||	7 }|D ]}
|
|v r=|  d}t|	|
d}|	d | }	|	s= nq|	rP|rJ|j|	| jd t|	dV  |rU d S qd S )NFrL   Tr   )verbose)rN   )	rO   r#   Zoutput_iteratorcancelmaxrW   Zon_llm_new_tokenr_   r   )r*   rF   r#   rH   rJ   rZ   r[   Zstop_condition_reachedZcurrent_completionrU   r\   
stop_indexr(   r(   r+   rM      s6   
zReplicate._streamr   c           	      K  s   zdd l }W n ty   tdw | jd u r=d| jv r2| jd\}}|j|}|j|| _n|j| j}|j| _| j	d u r]t
| jjd d d d  dd	 d
}|d d | _	| j	|i| j|}d| jvrv|jjj| j|dS |jj| j|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>   r9   z.Replicate._create_prediction.<locals>.<lambda>)key)r   )versionr   )r1   ImportErrorr   r   splitmodelsrg   versionsZlatest_versionr   sortedZopenapi_schemaitemsr   Zpredictionscreate)	r*   rF   rJ   Zreplicate_pythonZ	model_strversion_strr   Zinput_propertiesZinput_r(   r(   r+   rO      sD   



zReplicate._create_prediction)r%   r&   )r%   r    )r%   r"   )r5   r   r%   r   )r5   r   r%   r   )r%   r   )r%   r   )NN)
rF   r   r#   rG   rH   rI   rJ   r   r%   r   )
rF   r   r#   rG   rH   rI   rJ   r   r%   r^   )rF   r   rJ   r   r%   r   )__name__
__module____qualname____doc____annotations__r   dictr   r   r   r   r!   r>   r#   r   Zmodel_configpropertyr,   classmethodr0   r2   r   rA   r   rB   rD   rE   r]   rM   rO   r(   r(   r(   r+   r      sL   
 $"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   	getLoggerrt   r<   r   r(   r(   r(   r+   <module>   s     
