o
    Zh)                     @   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dedefddZedee	ef fddZede	fddZdedee	ef fddZ		d$d e	deee	  d!ee  dede	f
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   sD   t |dd}zddlm} ||d|d< W |S  ty!   tdw )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#   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/predictionguard.pyvalidate_environmentB   s   
z$PredictionGuard.validate_environmentc                 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kwargsc              	   K   sf   | d| j}| d| j}i | j| j| j| jt|tr |	 n|t|tr*|	 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promptrun_managerc                 K   s   | j di |}d}| jdur|durtd| jdur | j}n|}| jjjd| j|d|}|d D ]}|dddrM|d 	d}	td|	 q4|d d	 d
 }
|r]t
|
|}
|
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   textr#   )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      sR   
  

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    
