
    dh                         S SK Jr  S SKrS SKJrJrJrJrJrJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJrJr  S SKJr  S S	KJrJrJr  \(       a  S S
KJr  \R6                  " \5      r " S S\5      rg)    )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                     \ rS rSr% SrS\S'   \" \SS9rS\S'   S	r	S
\S'   S	r
S
\S'   \" S	SS9rS\S'    SrS\S'    \" \S9rS\S'    \" SSS9r\S&S j5       r\S'S j5       r\S(S j5       r\" SS9\S)S j5       5       r\S*S j5       r\S+S  j5       r\S,S! j5       r  S-         S.S" jjr  S-         S/S# jjrS0S$ jrS%rg	)1	Replicate   a  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]stopforbid)populate_by_nameextrac                
    SS0$ )Nr   REPLICATE_API_TOKEN selfs    Z/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/replicate.py
lc_secretsReplicate.lc_secrets@   s    %'<==    c                    g)NTr,   clss    r/   is_lc_serializableReplicate.is_lc_serializableD   s    r2   c                
    / SQ$ )z*Get the namespace of the langchain object.)	langchainllms	replicater,   r4   s    r/   get_lc_namespaceReplicate.get_lc_namespaceH   s
     21r2   before)modec                   [        U 5      R                  5        Vs1 sH  o"iM     nnUR                  S0 5      nU(       a  [        R	                  S5        0 UR                  S0 5      EUEn[        U5       HL  nXc;  d  M
  Xe;   a  [        SU S35      e[        R	                  U SU S35        UR                  U5      XV'   MN     XQS'   U$ s  snf )z>Build extra kwargs from additional params that were passed in.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)r5   valuesfieldall_required_field_namesr   r)   
field_names          r/   build_extraReplicate.build_extraM   s     8B#7K7K7M#N7MeE7M #N

7B'NNV <6::nb1;U;v,J9&$vj\9I%JKK# %))34JN %+JJz$:! ' "'~% $Os   
Cc                (    [        USS5      nX!S'   U$ )z?Validate that api key and python package exists in environment.r   r+   )r   )r5   rG   r   s      r/   validate_environmentReplicate.validate_environmente   s(     3)+@
 )<$%r2   c                4    U R                   U R                  S.$ )zGet the identifying parameters.r   r   rQ   r-   s    r/   _identifying_paramsReplicate._identifying_paramsn   s     ZZ --
 	
r2   c                    g)zReturn type of model.r;   r,   r-   s    r/   	_llm_typeReplicate._llm_typev   s     r2   c                >   U R                   (       a>  SnU R                  " U4X#S.UD6 H"  nUc  UR                  nM  XVR                  -  nM$     OU R                  " U40 UD6nUR	                  5         UR
                  S:X  a  [        UR                  5      e[        UR                  [        5      (       a  UR                  nOSR                  UR                  5      nUc   eU=(       d    U R                  nU H  n	X;   d  M
  USUR                  U	5       nM      U$ )zCall to replicate endpoint.N)r&   run_managerfailed )r$   _streamtext_create_predictionwaitstatusRuntimeErrorerror
isinstanceoutputr   joinr&   find)
r.   promptr&   rX   kwargs
completionchunk
predictionstop_conditionsss
             r/   _callReplicate._call{   s    >>(,J!>D %!&J**,J 00B6BJOO  H,":#3#344*++S11'..
WWZ%6%67
%%%+$)) A'(<*//!*<=
 ! r2   c              +    #    U R                   " U40 UD6nU=(       d    U R                  nSnSnUR                  5        H  n	X-  nU HE  n
X;   d  M
  UR                  5         Sn[	        U	R                  U
5      S5      nU	S U n	U	(       a  ME    O   U	(       a,  U(       a  UR                  U	U R                  S9  [        U	S9v   U(       d  M    g    g 7f)NFrZ   Tr   )verbose)r\   )	r]   r&   output_iteratorcancelmaxre   on_llm_new_tokenrp   r   )r.   rf   r&   rX   rg   rj   rk   stop_condition_reachedcurrent_completionrc   rl   
stop_indexs               r/   r[   Replicate._stream   s      ,,V>v>
+$))!&"$ 002F($*%%'-1* "%V[[^Q!7J#KZ0F!6 % 00 $ 1  &622%%+ 3s   AC7C<CCc                8    SS K nU R                  c  SU R                  ;   aY  U R                  R	                  S5      u  pEUR
                  R                  U5      nUR                  R                  U5      U l        O6UR
                  R                  U R                  5      nUR                  U l        U R                  cF  [        U R                  R                  S   S   S   S   R                  5       S S	9nUS   S   U l	        U R                  U0U R                  EUEnSU R                  ;  a.  UR
                  R                  R                  U R                  US
9$ UR                  R                  U R                  US9$ ! [         a    [        S5      ef = f)Nr   zZCould not import replicate python package. Please install it with `pip install replicate`.:
componentsschemasInput
propertiesc                ,    U S   R                  SS5      $ )N   zx-orderr   )get)items    r/   <lambda>.Replicate._create_prediction.<locals>.<lambda>   s    aY!:r2   )key)r   )versionr   )r;   ImportErrorr"   r   splitmodelsr   versionslatest_versionr   sortedopenapi_schemaitemsr   predictionscreate)	r.   rf   rg   replicate_python	model_strversion_strr   input_propertiesinput_s	            r/   r]   Replicate._create_prediction   s   	0 #djj )-)9)9#)>&	(//33I>#(>>#5#5k#B (//33DJJ?#(#7#7 ??"%  //=iHQ %':	  /q1!4DO OOV

 
 djj #**66==djjPV=WW#//66(( 7  K  	B 	s   F F)r   r"   )returnzDict[str, str])r   r#   )r   r%   )rG   r   r   r   )rG   r   r   r   )r   r   )r   r   )NN)
rf   r   r&   Optional[List[str]]rX   "Optional[CallbackManagerForLLMRun]rg   r   r   r   )
rf   r   r&   r   rX   r   rg   r   r   zIterator[GenerationChunk])rf   r   rg   r   r   r   ) __name__
__module____qualname____firstlineno____doc____annotations__r   dictr   r   r   r"   r$   rE   r&   r   model_configpropertyr0   classmethodr6   r<   r   rK   r   rN   rR   rU   rm   r[   r]   __static_attributes__r,   r2   r/   r   r      s   . J#(W#ML.M)-- $J$T48K8
 It(D1D)17L
 > >   2 2 (#  $,   
 
   %):>	 " 8	
  
H %):>	   "  8	 
   
# D*r2   r   )
__future__r   loggingtypingr   r   r   r   r   r	   langchain_core.callbacksr
   #langchain_core.language_models.llmsr   langchain_core.outputsr   langchain_core.utilsr   r   langchain_core.utils.pydanticr   pydanticr   r   r   replicate.predictionr   	getLoggerr   rC   r   r,   r2   r/   <module>r      sJ    "  E E = 3 2 ? 4 7 7/			8	$U Ur2   