
    7|h                         d dl Z d dlmZmZmZ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 d dlmZ  e j(                  e      Zd	Z G d
 de	      Zy)    N)AnyDictListOptional)CallbackManagerForLLMRun)LLM)
Generation	LLMResult)pre_init)
ConfigDictField)enforce_stop_tokensz8https://clarifai.com/openai/chat-completion/models/GPT-4c                      e Zd ZU dZdZee   ed<   	 dZee   ed<   	 dZ	ee   ed<   	 dZ
ee   ed<   	 dZee   ed<   	  edd	      Zee   ed
<   	  edd	      Zee   ed<   	  edd	      Zeed<   dZeed<    ed      Zededefd       Zedeeef   fd       Zedeeef   fd       Zedefd       Z	 	 	 ddedeee      dee   deeeef      dedefdZ	 	 	 ddee   deee      dee   deeeef      dedefdZy) Clarifaia2  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           
         	 ddl m} |j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d	      }|j                  d
      }	|j                  d      }
 ||||t	        |      |	|
||      |d<   |S # t        $ r t        d      w xY w)zuValidate 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              `/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/clarifai.pyvalidate_environmentzClarifai.validate_environment8   s    	3 **Y'H%::j)!::&89JJ{+	::j)jj

7#"23	
w 3  	A 	s   B/ /Cc                     i S )z4Get the default parameters for calling Clarifai API. selfs    r,   _default_paramszClarifai._default_paramsY   s	     	    c                 d    i | j                   | j                  | j                  | j                  dS )zGet the identifying parameters.r   r   r   r   r5   r0   s    r,   _identifying_paramszClarifai._identifying_params^   s2    
!^^<<++ MM	
 	
r3   c                      y)zReturn type of llm.clarifair/   r0   s    r,   	_llm_typezClarifai._llm_typej   s     r3   promptstoprun_managerinference_paramskwargsc                 <   	 |i x}n| | j                   j                  t        |d      d|      }|j                  d   j                  j
                  j                  }|t        ||      }|S # t        $ r#}t        j                  d|        Y d}~S d}~ww xY w)a~  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_typer=   r   Predict failed, exception: )r   predict_by_bytesbytesoutputsdatar@   rawr   	Exceptionloggererror)	r1   r:   r;   r<   r=   r>   predict_responser@   es	            r,   _callzClarifai._callo   s    .	<(8(@#FV#zz::fg&!!1  ;  
 $++A.3388<<D*46
   	<LL6qc:;;	<s   A+A/ /	B8BBpromptsc           
         	 ddl m} g }d}|j                  | j                  j
                        }		 t        dt        |      |      D ]f  }
||
|
|z    }t        |      D cg c]!  \  }}|	j                  t        |      |      # }}}|i x}n| | j                  j                  ||      }h j                  D ]k  }|+t        |j                  j                  j                   |      }n |j                  j                  j                   }|j#                  t%        |      g       m 	 t-        |
      S # t        $ r t        d      w xY wc c}}w # t&        $ r"}t(        j+                  d	|        Y d}~Pd}~ww xY w)z*Run the LLM on the given prompt and input.r   )Inputsr"       )input_idraw_textN)inputsr=   )r@   rB   )generations)clarifai.client.inputrP   r(   from_auth_helperr   auth_helperrangelen	enumerateget_text_inputstrpredictrE   r   rF   r@   rG   appendr	   rH   rI   rJ   r
   )r1   rN   r;   r<   r=   r>   rP   rU   
batch_size	input_objibatchr#   inpinput_batchrK   outputr@   rL   s                      r,   	_generatezClarifai._generate   s   	4 
++DJJ,B,BC		<1c'lJ7 A
N3 $-U#3C ,,c"g,L  &- )+*$3C#'::#5#5&9I $6 $  +22 <#.v{{/?/?/C/CTJD!;;++//D""JD$9#:;< [11E  	A 	&  	<LL6qc:;;	<s5   D8 /E !&EB$E 8EE 	FE<<F)NNN) __name__
__module____qualname____doc__r   r   r]   __annotations__r   r   r   r   r   r   r   r   r   r   r   model_configr   r   r-   propertyr2   r6   r9   r   r   rM   r
   rg   r/   r3   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 
/2r3   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   	getLoggerrh   rI   EXAMPLE_URLr   r/   r3   r,   <module>ry      sH     , , = 3 8 ) & >			8	$ Is2s s2r3   