
    7|hv"                        d dl mZ d dlZd dlmZ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Z ej&                  e      Z G d	 d
e      Zy)    )annotationsN)AnyCallableIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)
ConfigDictz mlx-community/quantized-gemma-2bc                  .   e Zd ZU dZeZded<   	 dZded<   	 dZded<   	 dZ	ded	<   	 dZ
d
ed<   	 dZded<   	 dZded<   	  ed      Ze	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zedd       Zedd       Z	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 ddZy)MLXPipelinea  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                    	 ddl m} |xs i }|r |||||      \  }}	n ||||      \  }}	|xs i }
 | d|||	||||
d|S # t        $ r t        d      w xY w)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              d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/mlx_pipeline.pyfrom_model_idzMLXPipeline.from_model_idR   s    	# ,1r#*D E9  $H.>TJE9*0b 	
-%,	
 	
 		
  	? 	s   A Ac                v    | j                   | j                  | j                  | j                  | j                  dS )zGet the identifying parameters.r   r   r   r   r   r*   selfs    r'   _identifying_paramszMLXPipeline._identifying_paramsz   s7      $ 5 5 --II#33
 	
    c                     y)Nmlx_pipeliner!   r+   s    r'   	_llm_typezMLXPipeline._llm_type   s    r.   c           
     >   	 ddl m} ddlm}m} |j                  d| j                        }|j                  dd      }	|j                  dd	      }
|j                  d
d      }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd      }|j                  dd      }|j                  dd      } ||	|||      } |d ||      } || j                  | j                  ||
||||      S # t
        $ r t        d      w xY w)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   promptr9   r;   r<   samplerlogits_processors)
r"   r3   mlx_lm.sample_utilsr5   r6   r#   getr   r   r   )r,   rD   stoprun_managerr%   r3   r5   r6   r   r7   r9   r;   r<   r=   r>   r?   rA   rB   rE   rF   s                       r'   _callzMLXPipeline._call   sN   	'P !**%68L8LM%))&#6)--lC@
'++Iu=(7(;(;K(N	.=.A.A $/
 2A1D1D%t2
 '**7C8&**7C8"1"5"56JA"NtUE3EF2$&=
 **nn!/	
 		
7  	? 	s   D Dc              +    K   	 dd l m} ddlm}m} ddlm} |j                  d| j                        }	|	j                  dd      }
|	j                  dd	      }|	j                  d
d       }|	j                  dd       }|	j                  dd      }|	j                  dd      }|	j                  dd      }| j                  j                  |d      }|j                  |d         }| j                  j                  }| j                  j                  }|j                           ||
xs d|||      } |d ||      }t!         ||| j"                  ||      t%        |            D ]t  \  \  }}}d }|j'                  |       |j)                          |j*                  }|r-t-        |      }|r|j/                  |j0                         | ||k(  s|o||v st y  y # t        $ r t        d      w xY ww)Nr   r4   )generate_stepr   r   r7   r8   r9   r:   r=   r>   r?   r@   rA   rB   rC   np)return_tensors)rD   r   rE   rF   )text)mlx.corecorerG   r5   r6   mlx_lm.utilsrM   r#   rH   r   r   encodearrayeos_token_iddetokenizerresetzipr   range	add_tokenfinalizelast_segmentr   on_llm_new_tokenrP   )r,   rD   rI   rJ   r%   mxr5   r6   rM   r   r7   max_new_tokensr=   r>   r?   rA   rB   prompt_tokensrV   rW   rE   rF   tokenprobnrP   chunks                              r'   _streamz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{sE5:LM2$&=
 !$$jj"3	 .!!
 	MUD1 #'D!!%(  "++D 'T200< $)9ddl1	E  	? 	s(   G1G F7G1G1G1G..G1)NNFN)r   r   r   r   r   r   r   r   r   r   r%   r   returnr   )rg   zMapping[str, Any])rg   r   )NN)
rD   r   rI   Optional[List[str]]rJ   "Optional[CallbackManagerForLLMRun]r%   r   rg   r   )
rD   r   rI   rh   rJ   ri   r%   r   rg   zIterator[GenerationChunk])__name__
__module____qualname____doc__DEFAULT_MODEL_IDr   __annotations__r   r   r   r   r   r   r   model_configclassmethodr(   propertyr-   r1   rK   rf   r!   r.   r'   r   r      s   , %Hc$E3Is'+n+ #'L-& D$
 '+O^*  L  ,0&**.%
%
 )%
 $	%

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

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