a
    bg)                     @   s   d dl Z d dlmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZ d dlmZ e eZed	d
ddG dd deZdS )    N)AnyDictListOptionalUnion)
deprecated)CallbackManagerForLLMRun)LLM)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)enforce_stop_tokensz0.3.28z1.0z)langchain_predictionguard.PredictionGuard)ZsinceZremovalZalternative_importc                   @   sV  e Zd ZU dZdZ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< dZee ed< dZeee	  ed< dZeee	ee	ef f  ed< dZeee	ef  ed< dZee	 ed< eddZeddeedddZeee	ef dddZee	dddZeee	ef dddZd#e	eee	  ee  ee	d d!d"Z!dS )$PredictionGuarda  Prediction Guard large language models.

    To use, you should have the ``predictionguard`` python package installed, and the
    environment variable ``PREDICTIONGUARD_API_KEY`` set with your API key, or pass
    it as a named parameter to the constructor.

    Example:
        .. code-block:: python

            llm = PredictionGuard(
                model="Hermes-3-Llama-3.1-8B",
                predictionguard_api_key="your Prediction Guard API key",
            )
    NclientzHermes-3-Llama-3.1-8Bmodel   
max_tokensg      ?temperatureg?top_ptop_kstoppredictionguard_inputpredictionguard_outputpredictionguard_api_keyZforbid)extrabefore)mode)valuesreturnc                 C   sJ   t |dd}zddlm} ||d|d< W n tyD   tdY n0 |S )zCValidate that the api_key and python package exists in environment.r   ZPREDICTIONGUARD_API_KEYr   )r   )Zapi_keyr   zfCould not import predictionguard python package. Please install it with `pip install predictionguard`.)r
   predictionguardr   ImportError)clsr   Z
pg_api_keyr    r#   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/predictionguard.pyvalidate_environmentB   s    
z$PredictionGuard.validate_environment)r   c                 C   s
   d| j iS )zGet the identifying parameters.r   )r   selfr#   r#   r$   _identifying_paramsX   s    z#PredictionGuard._identifying_paramsc                 C   s   dS )zReturn type of llm.r    r#   r&   r#   r#   r$   	_llm_type]   s    zPredictionGuard._llm_type)kwargsr   c              	   K   sf   | d| j}| d| j}i | j| j| j| jt|tr@|	 n|t|trT|	 n|d|}|S )Nr   r   )r   r   r   r   inputoutput)
popr   r   r   r   r   r   
isinstancer   Z
model_dump)r'   r*   r+   r,   paramsr#   r#   r$   _get_parametersb   s    zPredictionGuard._get_parameters)promptr   run_managerr*   r   c                 K   s   | j f i |}d}| jdur0|dur0tdn| jdurB| j}n|}| jjjf | j|d|}|d D ]2}|dddrj|d 	d}	td|	 qj|d d	 d
 }
|rt
|
|}
|
S )a$  Call out to Prediction Guard's model API.
        Args:
            prompt: The prompt to pass into the model.
        Returns:
            The string generated by the model.
        Example:
            .. code-block:: python
                response = llm.invoke("Tell me a joke.")
        Nz2`stop` found in both the input and default params.)r   r1   choicesstatus zerror: z Error from PredictionGuard API: r   text)r0   r   
ValueErrorr   Zcompletionscreater   get
startswithremoveprefixr   )r'   r1   r   r2   r*   r/   stopsresponsereserr_msgr6   r#   r#   r$   _cally   s*    



zPredictionGuard._call)NN)"__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   intr   floatr   r   r   r   r   r   r   boolr   r   r   Zmodel_configr   r%   propertyr(   r)   r0   r   r@   r#   r#   r#   r$   r      s8   
 
  
r   )loggingtypingr   r   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.utilsr
   Zpydanticr   r   r   Zlangchain_community.llms.utilsr   	getLoggerrA   loggerr   r#   r#   r#   r$   <module>   s   
