
    Ng                         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dS )    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dS ) 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} n# t          $ r t          d          w xY w|                    d          }|                    d          }|                    d          }|                    d          }|                    d          }|                    d	          }|                    d
          }	|                    d          }
 ||||t	          |          |	|
||          |d<   |S )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/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/clarifai.pyvalidate_environmentzClarifai.validate_environment8   s    	3333333 	 	 	A  	
 **Y''H%%::j))!::&899JJ{++	::j))jj

7##%"2333	
 	
 	
w s   	 #c                     i S )z4Get the default parameters for calling Clarifai API. selfs    r,   _default_paramszClarifai._default_paramsY   s	     	    c                 <    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^   s/    
!^<+ M	 
 	
r3   c                     dS )zReturn type of llm.clarifair/   r0   s    r,   	_llm_typezClarifai._llm_typej   s	     zr3   promptstoprun_managerinference_paramskwargsc                 4   	 |i x}n| | j                             t          |d          d|          }|j        d         j        j        j        }|t          ||          }n4# t          $ r'}t          
                    d|            Y d}~nd}~ww xY w|S )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#z::fg&&!!1  ;    
 $+A.38<D*466 	< 	< 	<LL:q::;;;;;;;;	< s   A!A$ $
B.BBpromptsc                    	 ddl m} n# t          $ r t          d          w xY wg }d}|                    | j        j                  	 t          dt          |          |          D ]N}	||	|	|z            }
fdt          |
          D             }|i x}n| | j        	                    ||          }O|j
        D ]Y}| t          |j        j        j        |          }n|j        j        j        }|                    t!          |          g           Zn4# t"          $ r'}t$                              d	|            Y d}~nd}~ww xY wt)          |
          S )z*Run the LLM on the given prompt and input.r   )Inputsr"       c                 ^    g | ])\  }}                     t          |          |           *S ))input_idraw_text)get_text_inputstr).0r#   inp	input_objs      r,   
<listcomp>z&Clarifai._generate.<locals>.<listcomp>   sF       C ,,c"gg,LL  r3   N)inputsr=   )r@   rB   )generations)clarifai.client.inputrP   r(   from_auth_helperr   auth_helperrangelen	enumeratepredictrE   r   rF   r@   rG   appendr	   rH   rI   rJ   r
   )r1   rN   r;   r<   r=   r>   rP   r\   
batch_sizeibatchinput_batchrK   outputr@   rL   rY   s                   @r,   	_generatezClarifai._generate   s   	4444444 	 	 	A  	 
++DJ,BCC		<1c'llJ77  A
N 23   #,U#3#3   &- )+*$$3C#':#5#5&9I $6 $ $   +2 < <#.v{/?/CTJJDD!;+/D""JD$9$9$9#:;;;;<  	< 	< 	<LL:q::;;;;;;;;	< [1111s!   
 $CD 
E$EE)NNN) __name__
__module____qualname____doc__r   r   rV   __annotations__r   r   r   r   r   r   r   r   r   r   r   model_configr   r   r-   propertyr2   r6   r9   r   r   rM   r
   rj   r/   r3   r,   r   r      s            $Ix}###"Hhsm"""&*hsm***" FHSM   )!GXc]!!!"tT:::C#:::0 5t<<<E8C=<<<(tT222E3222.Hc...:  L $ 4    X@ c3h    X 	
T#s(^ 	
 	
 	
 X	
 3    X %):>59% %% tCy!% 67	%
 #4S>2% % 
% % % %T %):>59/2 /2c/2 tCy!/2 67	/2
 #4S>2/2 /2 
/2 /2 /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   	getLoggerrk   rI   EXAMPLE_URLr   r/   r3   r,   <module>r|      s    , , , , , , , , , , , , = = = = = = 3 3 3 3 3 3 8 8 8 8 8 8 8 8 ) ) ) ) ) ) & & & & & & & & > > > > > >		8	$	$ Is2 s2 s2 s2 s2s s2 s2 s2 s2 s2r3   