U
    hp                     @  sz   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ZeeZG dd	 d	eZdS )
    )annotationsN)AnyCallableIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunkz mlx-community/quantized-gemma-2bc                
   @  s   e Zd ZU dZeZded< ded< ded< dZded	< dZd
ed< dZ	ded< dZ
ded< G dd dZed#ddd
dddd dddZeddddZeddddZd$ddddddddZd%ddddd dd!d"ZdS )&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_idr   model	tokenizerNzOptional[dict]tokenizer_configzOptional[str]adapter_fileFboollazypipeline_kwargsc                   @  s   e Zd ZdZdS )zMLXPipeline.ConfigZforbidN)__name__
__module____qualname__extra r   r   I/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/mlx_pipeline.pyConfigM   s   r   )r   r   r   r   r   kwargsreturnc              
   K  s   zddl m} W n tk
r,   tdY nX |p4i }|rN|||||\}}	n||||d\}}	|pfi }
| f |||	||||
d|S )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`.)r   )r   r   r   r   r   r   r   )mlx_lmr   ImportError)clsr   r   r   r   r   r   r   r   r   Z_pipeline_kwargsr   r   r   from_model_idP   s,    
zMLXPipeline.from_model_idzMapping[str, Any])r   c                 C  s   | j | j| j| j| jdS )zGet the identifying parameters.r   r   r   r   r   r%   selfr   r   r   _identifying_paramsv   s    zMLXPipeline._identifying_paramsc                 C  s   dS )NZmlx_pipeliner   r&   r   r   r   	_llm_type   s    zMLXPipeline._llm_typezOptional[List[str]]z"Optional[CallbackManagerForLLMRun])promptstoprun_managerr   r   c                 K  s   zddl m} W n tk
r,   tdY nX |d| j}|dd}|dd}|d	d
}	|dd }
|dd }|dd }|dd}|| j| j||||	|
|||d
S )Nr   )generater    r   temp        
max_tokensd   verboseF	formatterrepetition_penaltyrepetition_context_sizetop_p      ?)
r   r   r*   r.   r0   r2   r3   r4   r5   r6   )r!   r-   r"   getr   r   r   )r'   r*   r+   r,   r   r-   r   r.   r0   r2   r3   r4   r5   r6   r   r   r   _call   s@    
  zMLXPipeline._callzIterator[GenerationChunk]c              	   k  sP  zdd l m} ddlm} W n tk
r8   tdY nX |d| j}|dd}|dd}	|d	d }
|d
d }|dd}| jj|dd}|	|d }| jj
}| jj}|  t||| j||
||dt|	D ]p\\}}}d }|| |  |j}|r*t|d}|V  |r*||j ||ksD|d k	r||kr qLqd S )Nr   )generate_stepr    r   r.   r/   r0   r1   r4   r5   r6   r7   np)Zreturn_tensors)r*   r   r.   r4   r5   r6   )text)Zmlx.corecoreZmlx_lm.utilsr:   r"   r8   r   r   encodearrayeos_token_iddetokenizerresetzipr   rangeZ	add_tokenfinalizeZlast_segmentr   Zon_llm_new_tokenr<   )r'   r*   r+   r,   r   Zmxr:   r   r.   Zmax_new_tokensr4   r5   r6   Zprompt_tokensr@   rA   tokenZprobnr<   chunkr   r   r   _stream   s\    
  

zMLXPipeline._stream)NNFN)NN)NN)r   r   r   __doc__DEFAULT_MODEL_IDr   __annotations__r   r   r   r   r   classmethodr$   propertyr(   r)   r9   rI   r   r   r   r   r      s2   
    %
  .  r   )
__future__r   loggingtypingr   r   r   r   r   r   Zlangchain_core.callbacksr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.outputsr   rK   	getLoggerr   loggerr   r   r   r   r   <module>   s    
