a
    bg                     @  s   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eZG d	d
 d
eZdS )    )annotationsN)AnyCallableIteratorListMappingOptional)CallbackManagerForLLMRun)LLM)GenerationChunk)
ConfigDictz mlx-community/quantized-gemma-2bc                
   @  s   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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_idNr   model	tokenizerzOptional[dict]tokenizer_configzOptional[str]adapter_fileFboollazypipeline_kwargsZforbid)extra)r   r   r   r   r   kwargsreturnc              
   K  s   zddl m} W n ty*   tdY n0 |p2i }|rL|||||\}}	n||||d\}}	|pdi }
| 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_kwargs r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/mlx_pipeline.pyfrom_model_idR   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_paramsx   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y*   tdY n0 |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+   r-   r/   r0   r1   r2   r3   )r   r*   r   getr   r   r   )r$   r'   r(   r)   r   r*   r   r+   r-   r/   r0   r1   r2   r3   r   r   r    _call   s<    
zMLXPipeline._callzIterator[GenerationChunk]c              	   k  sN  zdd l m} ddlm} W n ty6   tdY n0 |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}|r"||j |V  ||ksB|d ur||v r qJqd S )Nr   )generate_stepr   r   r+   r,   r-   r.   r1   r2   r3   r4   np)Zreturn_tensors)r'   r   r+   r1   r2   r3   )text)Zmlx.corecoreZmlx_lm.utilsr7   r   r5   r   r   encodearrayeos_token_iddetokenizerresetzipr   rangeZ	add_tokenfinalizelast_segmentr   Zon_llm_new_tokenr9   )r$   r'   r(   r)   r   Zmxr7   r   r+   Zmax_new_tokensr1   r2   r3   Zprompt_tokensr=   r>   tokenZprobnr9   chunkr   r   r    _stream   sX    


zMLXPipeline._stream)NNFN)NN)NN)__name__
__module____qualname____doc__DEFAULT_MODEL_IDr   __annotations__r   r   r   r   r   r   r   Zmodel_configclassmethodr!   propertyr%   r&   r6   rG   r   r   r   r    r      s6   
    %
  .  r   )
__future__r   loggingtypingr   r   r   r   r   r   Zlangchain_core.callbacksr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.outputsr   Zpydanticr   rL   	getLoggerrH   loggerr   r   r   r   r    <module>   s    
