
    (HJjf                         d dl Z d dlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZ dZdZdZdZd Zd	Zd
Zd Zd Zedk(  r ed        e        yy)    N   )stream_generate)make_prompt_cache)make_sampler)loadsharded_load        g      ?   z(mlx-community/Llama-3.2-3B-Instruct-4bitc                     t        j                  d      } | j                  dt        dt               | j                  ddd	       | j                  d
t        d       | j                  dt
        t        d       | j                  dt
        t        d       | j                  dt
        t        d       | j                  dt
        dd       | j                  dt        t        d       | j                  dt        dd       | j                  ddt        t        d       | j                  ddd !       | j                  d"dd#	       | S )$z&Set up and return the argument parser.zChat with an LLM)descriptionz--modelz;The path to the local model directory or Hugging Face repo.)typehelpdefaultz--trust-remote-code
store_truez)Enable trusting remote code for tokenizer)actionr   z--adapter-pathz9Optional path for the trained adapter weights and config.)r   r   z--tempzSampling temperature)r   r   r   z--top-pzSampling top-pz--xtc-probabilityz5Probability of XTC sampling to happen each next tokenz--xtc-thresholdr	   zDThresold the probs of each next token candidate to be sampled by XTCz--seedz	PRNG seedz--max-kv-sizez$Set the maximum key-value cache sizeNz--max-tokensz-mz$Maximum number of tokens to generatez--system-promptz.System prompt to be used for the chat template)r   r   z
--pipelinez,Use pipelining instead of tensor parallelism)argparseArgumentParseradd_argumentstrDEFAULT_MODELfloatDEFAULT_TEMPDEFAULT_TOP_PDEFAULT_XTC_PROBABILITYintDEFAULT_SEEDDEFAULT_MAX_TOKENS)parsers    U/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx_lm/chat.pysetup_arg_parserr       s   $$1CDF
J	   8  
 H  
 ul9O   };K   'D	   S	   	   3	   "3   =  
 ;  
 M    c                     t               } | j                         }t        j                  j	                         }|j                         |j                  r|nd }|j                  s|nd }fdt        j                  j                  |j                         |j                         dkD  r8|j                  r| j                  d       t        |j                  ||      \  }}n5t        |j                  |j                  d|j                  rdnd i      \  }}fd} d|j                   d	        |        t!        ||j"                        }	 t%        d
k(  rdnd      }	|	dk(  ry |	dk(  rt!        ||j"                        }5|	dk(  r |        Bg }
|j&                  |
j)                  d|j&                  d       |
j)                  d|	d       |j+                  |
d      }t-        ||||j.                  t1        |j2                  |j4                  |j6                  |j8                  |j;                  d      t=        |j>                        z         |      D ]  } |j@                  dd                 -)Nc                  (    dk(  rt        | i | y y )Nr   )print)argskwargsranks     r   rprintzmain.<locals>.rprintc   s    194"6" r!   r   z*Adapters not supported in distributed modetrust_remote_codeT)adapter_pathtokenizer_configc                  F      d         d         d         d       y )NzThe command list:z- 'q' to exitz- 'r' to reset the chatz- 'h' to display these commands )r(   s   r   
print_helpzmain.<locals>.print_helpv   s&    "#()01r!   z"[INFO] Starting chat session with .r   z>>  qrhsystem)rolecontentuser)add_generation_prompt
)xtc_thresholdxtc_probabilityxtc_special_tokens)
max_tokenssamplerprompt_cache)flushend)!r    
parse_argsmxdistributedinitr'   pipelinerandomseedsizer*   errorr   modelr   r)   r   max_kv_sizeinputsystem_promptappendapply_chat_templater   r=   r   temptop_pr:   r;   encodelisteos_token_idstext)r   r%   grouppipeline_grouptensor_grouprK   	tokenizerr.   r?   querymessagespromptresponser'   r(   s                @@r   mainr_   Z   s6   FDNN!E::<D"mmUN $54L# IINN499zz|aLLEF'

NLQyJJ**#T-C-CT
y2 /

|1=>L$UD,<,<=L
tqyeb1C<C<,UD4D4DELC<L)OOX$:L:LMNE:;.."& / 
 ( 		

"00 $ 4 4$$T*T)2I2I-JJ &
H  8=="5!
" 	G r!   __main__zwCalling `python -m mlx_lm.chat...` directly is deprecated. Use `mlx_lm.chat...` or `python -m mlx_lm chat ...` instead.)r   mlx.corecorerC   generater   models.cacher   sample_utilsr   utilsr   r   r   r   r   DEFAULT_XTC_THRESHOLDr   r   r   r    r_   __name__r$   r-   r!   r   <module>ri      sq      % + & %   :BJHV z		H 	F r!   