
    dhv"                        S SK Jr  S SKrS SKJrJrJrJrJrJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr  Sr\R&                  " \5      r " S	 S
\5      rg)    )annotationsN)AnyCallableIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)
ConfigDictz mlx-community/quantized-gemma-2bc                  @   \ rS rSr% Sr\rS\S'    SrS\S'    Sr	S\S'    Sr
S	\S
'    SrS\S'    SrS\S'    SrS	\S'    \" SS9r\    S             SS jj5       r\SS j5       r\SS j5       r  S         SS jjr  S         SS jjrSrg) MLXPipeline   a  MLX Pipeline API.

To use, you should have the ``mlx-lm`` python package installed.

Example using from_model_id:
    .. code-block:: python

        from langchain_community.llms import MLXPipeline
        pipe = MLXPipeline.from_model_id(
            model_id="mlx-community/quantized-gemma-2b",
            pipeline_kwargs={"max_tokens": 10, "temp": 0.7},
        )
Example passing model and tokenizer in directly:
    .. code-block:: python

        from langchain_community.llms import MLXPipeline
        from mlx_lm import load
        model_id="mlx-community/quantized-gemma-2b"
        model, tokenizer = load(model_id)
        pipe = MLXPipeline(model=model, tokenizer=tokenizer)
strmodel_idNr   model	tokenizerOptional[dict]tokenizer_configOptional[str]adapter_fileFboollazypipeline_kwargsforbid)extrac                     SSK Jn  U=(       d    0 nU(       a
  U" XX4S9u  pO	U" XUS9u  pU=(       d    0 n
U " SUUU	UUUU
S.UD6$ ! [         a    [        S5      ef = f)z5Construct the pipeline object from model_id and task.r   )loadTCould not import mlx_lm python package. Please install it with `pip install mlx_lm`.)adapter_pathr   )r   )r   r   r   r   r   r   r    )mlx_lmr   ImportError)clsr   r   r   r   r   kwargsr   r   r   _pipeline_kwargss              ]/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/mlx_pipeline.pyfrom_model_idMLXPipeline.from_model_idR   s    	# ,1r# E9  $HTJE*0b 	
-%,	
 	
 		
  	? 	s   A Ac                v    U R                   U R                  U R                  U R                  U R                  S.$ )zGet the identifying parameters.r   r   r   r   r   r,   selfs    r(   _identifying_paramsMLXPipeline._identifying_paramsz   s7      $ 5 5 --II#33
 	
    c                    g)Nmlx_pipeliner"   r-   s    r(   	_llm_typeMLXPipeline._llm_type   s    r1   c                6    SSK Jn  SSKJnJn  UR                  SU R                  5      nUR                  SS5      n	UR                  SS	5      n
UR                  S
S5      nUR                  SS 5      nUR                  SS 5      nUR                  SS 5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nU" XUU5      nU" S X5      nU" U R                  U R                  UU
UUUUS9$ ! [
         a    [        S5      ef = f)Nr   )generatemake_logits_processorsmake_samplerr    r   temp        
max_tokensd   verboseF	formatterrepetition_penaltyrepetition_context_sizetop_p      ?min_pmin_tokens_to_keep   )r   r   promptr=   r?   r@   samplerlogits_processors)
r#   r7   mlx_lm.sample_utilsr9   r:   r$   getr   r   r   )r.   rH   stoprun_managerr&   r7   r9   r:   r   r;   r=   r?   r@   rA   rB   rC   rE   rF   rI   rJ   s                       r(   _callMLXPipeline._call   sI   	'P !**%68L8LM%))&#6)--lC@
'++Iu=(7(;(;K(N	.=.A.A $/
 2A1D1D%t2
 '**7C8&**7C8"1"5"56JA"NtE3EF2$
 **nn!/	
 		
7  	? 	s   D Dc           	   +    #     SS K Jn  SSKJnJn  SSKJn  UR                  SU R                  5      n	U	R                  SS5      n
U	R                  SS	5      nU	R                  S
S 5      nU	R                  SS 5      nU	R                  SS5      nU	R                  SS5      nU	R                  SS5      nU R                  R                  USS9nUR                  US   5      nU R                  R                  nU R                  R                  nUR                  5         U" U
=(       d    SXU5      nU" S X5      n[!        U" UU R"                  UUS9[%        U5      5       H  u  u  nnnS nUR'                  U5        UR)                  5         UR*                  nU(       a/  [-        US9nU(       a  UR/                  UR0                  5        Uv   UU:X  d  Uc  My  UU;   d  M    g    g ! [         a    [        S5      ef = f7f)Nr   r8   )generate_stepr    r   r;   r<   r=   r>   rA   rB   rC   rD   rE   rF   rG   np)return_tensors)rH   r   rI   rJ   )text)mlx.corecorerK   r9   r:   mlx_lm.utilsrR   r$   rL   r   r   encodearrayeos_token_iddetokenizerresetzipr   range	add_tokenfinalizelast_segmentr   on_llm_new_tokenrU   )r.   rH   rM   rN   r&   mxr9   r:   rR   r   r;   max_new_tokensrA   rB   rC   rE   rF   prompt_tokensr[   r\   rI   rJ   tokenprobnrU   chunks                              r(   _streamMLXPipeline._stream   s    		!P2 !**%68L8LM%))&#6-11,D.=.A.A $/
 2A1D1D%t2
 '**7C8&**7C8"1"5"56JA"N&&vd&C+~~22nn00t{sE:LM2$
 !$$jj"3	 .!!
MUD1 #'D!!%(  "++D 'T200< $)9ddl1!
E  	? 	s(   G=G$ F;G=G=G=$G::G=r"   )NNFN)r   r   r   r   r   r   r   r   r   r   r&   r   returnr   )rm   zMapping[str, Any])rm   r   )NN)
rH   r   rM   Optional[List[str]]rN   "Optional[CallbackManagerForLLMRun]r&   r   rm   r   )
rH   r   rM   rn   rN   ro   r&   r   rm   zIterator[GenerationChunk])__name__
__module____qualname____firstlineno____doc__DEFAULT_MODEL_IDr   __annotations__r   r   r   r   r   r   r   model_configclassmethodr)   propertyr/   r4   rO   rk   __static_attributes__r"   r1   r(   r   r      s   , %Hc$E3Is'+n+ #'L-& D$
 '+O^*  L  ,0&**.%
%
 )%
 $	%

 %
 (%
 %
 
%
 %
N 
 
   %):>	/
/
 "/
 8	/

 /
 
/
h %):>	FF "F 8	F
 F 
#F Fr1   r   )
__future__r   loggingtypingr   r   r   r   r   r	   langchain_core.callbacksr
   #langchain_core.language_models.llmsr   langchain_core.outputsr   pydanticr   ru   	getLoggerrp   loggerr   r"   r1   r(   <module>r      sA    "  C C = 3 2 5 			8	$p# pr1   