
    +#h                     :    d Z ddlmZmZ ddlmZ  G d de      Zy)zClarifai LM integration    )AnyOptional)LMc                   d     e Zd ZdZ	 	 ddedee   f fdZd ZdefdZ	 	 ddede	d	e	fd
Z
 xZS )ClarifaiLLMa1  Integration to call models hosted in clarifai platform.

    Args:
        model (str, optional): Clarifai URL of the model. Defaults to "Mistral-7B-Instruct".
        api_key (Optional[str], optional): CLARIFAI_PAT token. Defaults to None.
        **kwargs: Additional arguments to pass to the API provider.
    Example:
        import dspy
        dspy.configure(lm=dspy.Clarifai(model=MODEL_URL,
                                        api_key=CLARIFAI_PAT,
                                        inference_params={"max_tokens":100,'temperature':0.6}))
    modelapi_keyc                    t         |   |       	 ddlm} d| _        || _         |||      | _        ddi|| _        g | _	        d| j                  v r#d	| j                  d   v r| j                  d   d	   nd
| j                  d	<   d| j                  v r#d| j                  d   v r| j                  d   d   nd| j                  d<   y # t        $ r}t	        d      |d }~ww xY w)Nr   )Modelz,ClarifaiLLM requires `pip install clarifai`.clarifai)urlpatn   inference_paramstemperatureg        
max_tokens   )
super__init__clarifai.client.modelr   ImportErrorproviderr   _modelkwargshistory)selfr   r	   r   r   err	__class__s         Q/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dsp/modules/clarifai.pyr   zClarifaiLLM.__init__   s    		W3
 #73A((-/ "T[[0-?!@@ KK*+M: 	 	M" "T[[0,> ?? KK*+L9 	 	L!  	WLMSVV	Ws   C 	C"CC"c                 X   d| j                   v r| j                   d   ni }| j                  j                  |j                  d      d|      j                  d   j
                  j                  j                  }i | j                   |}|||d}| j                  j                  |       |S )Nr   zutf-8)encodingtext)input_bytes
input_typer   r   )promptresponser   )
r   r   predict_by_bytesencodeoutputsdatar#   rawr   append)r   r&   r   paramsr'   r   s         r    basic_requestzClarifaiLLM.basic_request5   s    /AT[[/PDKK*+VX 	 KK(("MM7M;!!' ) 
 WQ T$$ss 	 +DKK*6* 

 	G$    r&   c                 (     | j                   |fi |S )N)r/   )r   r&   r   s      r    requestzClarifaiLLM.requestK   s    !t!!&3F33r0   only_completedreturn_sortedc                     |sJ d       |du sJ d       |j                  dd      }g }t        |      D ]&  } | j                  |fi |}|j                  |       ( |S )Nzfor nowFr   r   )popranger2   r-   )	r   r&   r3   r4   r   r   completionsir'   s	            r    __call__zClarifaiLLM.__call__N   sw     (y(~%0y0%JJsAq 	)A#t||F5f5Hx(	) r0   )zDhttps://clarifai.com/mistralai/completion/models/mistral-7B-InstructN)TF)__name__
__module____qualname____doc__strr   r   r/   r2   boolr:   __classcell__)r   s   @r    r   r      sg     \!%

 #
@,4c 4  $#	  	r0   r   N)r>   typingr   r   dsp.modules.lmr   r    r0   r    <module>rE      s       X" Xr0   