
    dh                         S SK r S SKJrJrJr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  S SKJr  \ R(                  " \5      rS	r " S
 S\	5      rg)    N)AnyDictListOptional)CallbackManagerForLLMRun)LLM)
Generation	LLMResult)pre_init)
ConfigDictField)enforce_stop_tokensz8https://clarifai.com/openai/chat-completion/models/GPT-4c                      \ rS rSr% SrSr\\   \S'    Sr	\\   \S'    Sr
\\   \S'    Sr\\   \S'    Sr\\   \S'    \" SS	S
9r\\   \S'    \" SS	S
9r\\   \S'    \" SS	S
9r\\S'   Sr\\S'   \" SS9r\S\S\4S j5       r\S\\\4   4S j5       r\S\\\4   4S j5       r\S\4S j5       r   S!S\S\\\      S\\   S\\\\4      S\S\4S jjr   S!S\\   S\\\      S\\   S\\\\4      S\S\4S jjr S r!g)"Clarifai   a  Clarifai large language models.

To use, you should have an account on the Clarifai platform,
the ``clarifai`` python package installed, and the
environment variable ``CLARIFAI_PAT`` set with your PAT key,
or pass it as a named parameter to the constructor.

Example:
    .. code-block:: python

        from langchain_community.llms import Clarifai
        clarifai_llm = Clarifai(user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
                         (or)
        clarifai_llm = Clarifai(model_url=EXAMPLE_URL)
N	model_urlmodel_idmodel_version_idapp_iduser_idT)defaultexcludepattokenmodelzhttps://api.clarifai.comapi_baseforbid)extravaluesreturnc                     SSK Jn  UR                  S5      nUR                  S5      nUR                  S5      nUR                  S5      nUR                  S5      nUR                  S	5      nUR                  S
5      n	UR                  S5      n
U" UUU[	        US9U	U
UUS9US'   U$ ! [         a    [        S5      ef = f)zmValidate that we have all required info to access Clarifai
platform and python package exists in environment.r   )ModelXCould not import clarifai python package. Please install it with `pip install clarifai`.r   r   r   r   r   r   r   r   )id)urlr   r   model_versionr   r   r   base_urlr   )clarifai.client.modelr"   ImportErrorgetdict)clsr   r"   r   r   r   r   r   r   r   r   s              Y/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/clarifai.pyvalidate_environmentClarifai.validate_environment8   s    	3 **Y'H%::j)!::&89JJ{+	::j)jj

7#"23	
w 3  	A 	s   B) )B?c                     0 $ )z4Get the default parameters for calling Clarifai API. selfs    r-   _default_paramsClarifai._default_paramsY   s	     	    c                 d    0 U R                   U R                  U R                  U R                  S.E$ )zGet the identifying parameters.r   r   r   r   r8   r2   s    r-   _identifying_paramsClarifai._identifying_params^   s2    
!^^<<++ MM	
 	
r6   c                     g)zReturn type of llm.clarifair1   r2   s    r-   	_llm_typeClarifai._llm_typej   s     r6   promptstoprun_managerinference_paramskwargsc                 :    Uc  0 =nOU  U R                   R                  [        US5      SUS9nUR                  S   R                  R
                  R                  nUb  [        Xr5      nU$ ! [         a#  n[        R                  SU 35         SnAW$ SnAff = f)a6  Call out to Clarfai's PostModelOutputs endpoint.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        response = clarifai_llm.invoke("Tell me a joke.")
Nzutf-8text)
input_typerB   r   Predict failed, exception: )r   predict_by_bytesbytesoutputsdatarE   rawr   	Exceptionloggererror)	r3   r?   r@   rA   rB   rC   predict_responserE   es	            r-   _callClarifai._callo   s    .	<(8(@#FV#zz::fg&!!1  ;  
 $++A.3388<<D*46
   	<LL6qc:;;	<s   A)A- -
B7BBpromptsc           
          SSK Jn  / nSnUR                  U R                  R
                  5      n	 [        S[        U5      U5       H`  n
XX-    n[        U5       VVs/ sH  u  pU	R                  [        U5      US9PM      nnnUc  0 =nOU  U R                  R                  XS9nMb     WR                   Hj  nUb+  [        UR                  R                  R                   U5      nO UR                  R                  R                   nUR#                  [%        US9/5        Ml     [-        US
9$ ! [         a    [        S5      ef = fs  snnf ! [&         a"  n[(        R+                  S	U 35         SnANOSnAff = f)z*Run the LLM on the given prompt and input.r   )Inputsr#       )input_idraw_textN)inputsrB   )rE   rG   )generations)clarifai.client.inputrV   r)   from_auth_helperr   auth_helperrangelen	enumerateget_text_inputstrpredictrJ   r   rK   rE   rL   appendr	   rM   rN   rO   r
   )r3   rT   r@   rA   rB   rC   rV   r[   
batch_size	input_objibatchr$   inpinput_batchrP   outputrE   rQ   s                      r-   	_generateClarifai._generate   s   	4 
++DJJ,B,BC		<1c'lJ7AN3 $-U#3#3 ,,c"g,L#3   &- )+*$3C#'::#5#5& $6 $  8 +22#.v{{/?/?/C/CTJD!;;++//D""JD$9#:; 3 [11E  	A 	&  	<LL6qc:;;	<s5   D/ -E $EB#E /EE 
E:E55E:r1   )NNN)"__name__
__module____qualname____firstlineno____doc__r   r   rc   __annotations__r   r   r   r   r   r   r   r   r   r   r   model_configr   r   r.   propertyr4   r9   r=   r   r   rR   r
   rm   __static_attributes__r1   r6   r-   r   r      s      $Ix}#"Hhsm"&*hsm*" FHSM )!GXc]!"tT:C#:0 t<E8C=<(tT2E32.Hc.L $ 4  @ c3h   	
T#s(^ 	
 	
 3   %):>59%% tCy!% 67	%
 #4S>2% % 
%T %):>59/2c/2 tCy!/2 67	/2
 #4S>2/2 /2 
/2 /2r6   r   )loggingtypingr   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   langchain_core.outputsr	   r
   langchain_core.utilsr   pydanticr   r   langchain_community.llms.utilsr   	getLoggerro   rN   EXAMPLE_URLr   r1   r6   r-   <module>r      sF     , , = 3 8 ) & >			8	$ Is2s s2r6   