
    dh                         S SK JrJrJrJrJr  S SKrS SKJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJr  SrS	rS
rSR+                  \\S\S9r " S S\
5      rg)    )AnyDictListMappingOptionalN)CallbackManagerForLLMRun)LLM)get_from_dict_or_envpre_init)
ConfigDict)enforce_stop_tokensz### Instruction:z### Response:ziBelow is an instruction that describes a task. Write a response that appropriately completes the request.z7{intro}
{instruction_key}
{instruction}
{response_key}
z{instruction})introinstruction_keyinstructionresponse_keyc                   &   \ rS rSr% SrSr\\S'    Sr\	\S'    Sr
\\   \S'    S	r\\S
'    Sr\\   \S'   \" SS9r\S\S\4S j5       r\S\\\4   4S j5       r\S\4S j5       rS\S\4S jr   SS\S\\\      S\\   S\	S\S\4S jjrSrg)MosaicML   a  MosaicML LLM service.

To use, you should have the
environment variable ``MOSAICML_API_TOKEN`` set with your API token, or pass
it as a named parameter to the constructor.

Example:
    .. code-block:: python

        from langchain_community.llms import MosaicML
        endpoint_url = (
            "https://models.hosted-on.mosaicml.hosting/mpt-7b-instruct/v1/predict"
        )
        mosaic_llm = MosaicML(
            endpoint_url=endpoint_url,
            mosaicml_api_token="my-api-key"
        )
zDhttps://models.hosted-on.mosaicml.hosting/mpt-7b-instruct/v1/predictendpoint_urlFinject_instruction_formatNmodel_kwargsg      ?retry_sleepmosaicml_api_tokenforbid)extravaluesreturnc                 (    [        USS5      nX!S'   U$ )z?Validate that api key and python package exists in environment.r   MOSAICML_API_TOKEN)r
   )clsr   r   s      Y/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/mosaicml.pyvalidate_environmentMosaicML.validate_environmentB   s(     2(*>
 (:#$    c                 T    U R                   =(       d    0 n0 SU R                  0ESU0E$ )zGet the identifying parameters.r   r   )r   r   )self_model_kwargss     r!   _identifying_paramsMosaicML._identifying_paramsK   s>     ))/R
t001
}-
 	
r$   c                     g)zReturn type of llm.mosaic )r&   s    r!   	_llm_typeMosaicML._llm_typeT   s     r$   promptc                 N    U R                   (       a  [        R                  US9nU$ )zTransform prompt.)r   )r   PROMPT_FOR_GENERATION_FORMATformat)r&   r/   s     r!   _transform_promptMosaicML._transform_promptY   s*    ))188" 9 F r$   stoprun_manageris_retrykwargsc                 "   U R                   =(       d    0 nU R                  U5      nSU/0nUR                  U5        UR                  U5        U R                   SS.n [        R
                  " U R                  XS9n	 U	R                  S:X  aO  U(       d0  SSKnUR                  U R                  5        U R                  XUS	S
9$ [        SU	R                   35      eU	R!                  5       n[#        U[$        5      (       aV  / SQnU H  nX;   d  M
  X   n  O"   [        SSR'                  U5       SU 35      e[#        U[(        5      (       a  US   nOUnO[        SU 35      eUR+                  U5      (       a  U[-        U5      S nUb  [1        UU5      nU$ ! [        R                  R                   a  n
[        SU
 35      eSn
A
ff = f! [        R                  R.                   a   n
[        SU
 SU	R                   35      eSn
A
ff = f)a2  Call out to a MosaicML LLM inference 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 = mosaic_llm.invoke("Tell me a joke.")
inputszapplication/json)AuthorizationzContent-Type)headersjsonz$Error raised by inference endpoint: Ni  r   T)r7   z>Error raised by inference API: rate limit exceeded.
Response: )dataoutputoutputszNo valid key (z, z) in response: zUnexpected response type: zError raised by inference API: z.
Response: )r   r3   updater   requestspostr   
exceptionsRequestException
ValueErrorstatus_codetimesleepr   _calltextr=   
isinstancedictjoinlist
startswithlenJSONDecodeErrorr   )r&   r/   r5   r6   r7   r8   r'   payloadr<   responseerH   parsed_responseoutput_keyskeyoutput_itemrK   s                    r!   rJ   MosaicML._calla   s%   , ))/R''/fX&}%v !% 7 78.
	I}}T%6%6VH+	##s*JJt//0::fK$:OO U}}o' 
 'mmoO /400;&C-&5&: '
 %(;)?(@ A+,.  k400&q>D&D #=o=N!OPP v&&CKM* &tT2Dg ""33 	ICA3GHH	IV ""22 	1!M(--Q 	s?   F ?AG A	G A8G G:GGH.H		Hr,   )NNF)__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   boolr   r   rM   r   floatr   r   model_configr   r   r"   propertyr   r   r(   r-   r3   r   r   rJ   __static_attributes__r,   r$   r!   r   r      s@   ( 	O #  &+t+C#'L(4.'1KE(,,L $ 4   
WS#X%6 
 
 3     %):>ZZ tCy!Z 67	Z
 Z Z 
Z Zr$   r   )typingr   r   r   r   r   rB   langchain_core.callbacksr   #langchain_core.language_models.llmsr	   langchain_core.utilsr
   r   pydanticr   langchain_community.llms.utilsr   INSTRUCTION_KEYRESPONSE_KEYINTRO_BLURBr2   r1   r   r,   r$   r!   <module>rp      sg    5 5  = 3 ?  >$A   F
#	  	 ^s ^r$   