
    7|h)                         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 j*                  e      Z ed	d
d       G d de             Zy)    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)sinceremovalalternative_importc                      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      Z ed      dedefd       Zedee	ef   fd       Zede	fd       Zdedee	ef   fdZ	 	 dde	deee	      dee    dede	f
dZ!y) 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_keyforbid)extrabefore)modevaluesreturnc                 v    t        |dd      }	 ddlm}  ||      |d<   |S # t        $ r t        d      w xY w)zCValidate that the api_key and python package exists in environment.r   PREDICTIONGUARD_API_KEYr   )r   )api_keyr   zfCould not import predictionguard python package. Please install it with `pip install predictionguard`.)r   predictionguardr   ImportError)clsr$   
pg_api_keyr   s       g/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/predictionguard.pyvalidate_environmentz$PredictionGuard.validate_environmentB   s\     *-/H

	7." F8   	H 	s   # 8c                     d| j                   iS )zGet the identifying parameters.r   )r   selfs    r-   _identifying_paramsz#PredictionGuard._identifying_paramsX   s     $$    c                      y)zReturn type of llm.r)    r0   s    r-   	_llm_typezPredictionGuard._llm_type]   s     !r3   kwargsc           
      `   |j                  d| j                        }|j                  d| j                        }i | j                  | j                  | j
                  | j                  t        |t              r|j                         n|t        |t              r|j                         n|d|}|S )Nr   r   )r   r   r   r   inputoutput)
popr   r   r   r   r   r   
isinstancer   
model_dump)r1   r7   r9   r:   paramss        r-   _get_parameterszPredictionGuard._get_parametersb   s    

2D4N4NO4d6Q6QR
"oo#//*4UI*FE$$&E ,6fi+HF%%'f
 
  r3   promptrun_managerc                     | j                   di |}d}| j                  |t        d      | j                  | j                  }n|} | j                  j                  j
                  d| j                  |d|}|d   D ]E  }|j                  dd      j                  d      s%|d   j                  d      }	t        d|	        |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   r@   choicesstatus zerror: z Error from PredictionGuard API: r   textr5   )r?   r   
ValueErrorr   completionscreater   get
startswithremoveprefixr   )r1   r@   r   rA   r7   r>   stopsresponsereserr_msgrF   s              r-   _callzPredictionGuard._cally   s
   " &%%//99 T%5QRRYY"IIEE14;;**11 
**
 
 I& 	OCwwx$//	:h-44Y? #CG9!MNN	O
 	"1%f- &tU3Dr3   )NN)"__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   intr   floatr   r   r   r   r   r   r   boolr   r   r   model_configr   r.   propertyr2   r6   r?   r	   rQ   r5   r3   r-   r   r      s    FC2E8C=2 #J#A#'K%'Q E8E? SE8C=F $D(49
$CG8DeCI.>)>$?@GK8<HT#t)_5<9-1Xc]1#H-L(#$ 4  $* %T#s(^ % % !3 ! ! S#X 4 %):>	-- tCy!- 67	-
 - 
-r3   r   )loggingtypingr   r   r   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   langchain_core.utilsr   pydanticr   r   r   langchain_community.llms.utilsr   	getLoggerrR   loggerr   r5   r3   r-   <module>rg      sa     3 3 6 = 3 5 ; ; >			8	$ 
B
Rc R
Rr3   