
    iF~                     v   S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	J
r
  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKrS SKJrJr  S SKJrJrJr  S S	KJr  S S
KJrJ r   S SK!J"r"J#r#   S SK$r$\RL                  " 5       S:w  a  S SK'r'\"" 5       (       a  S SK(J)r)  S SK*J+r+  S SK,J-r-  \#" 5       (       a  S SK.r.S SKJ/r/J0r0  \1" \Rd                  \Rf                  -   5      r4\1" \Rd                  \Rj                  -   \Rf                  -   S-   5      r6SS0SS0SS0SS0SS0SS0SS0S.r7Sr8SSRs                  \7Ru                  5       5       S3r; " S S 5      r<\ " S! S"5      5       r=S#\
4S$ jr> " S% S&\5      r?\@S':X  a.  \=" 5       rAS(\AlB        S)\AlB        \?" \A5      rC\CR                  5         gg! \% a     GNCf = f)*    N)ArgumentParser	Namespace)AsyncIterator)	dataclassfield)Thread)Optional)AsyncInferenceClientChatCompletionStreamOutput)AutoTokenizerGenerationConfigPreTrainedTokenizer)BaseTransformersCLICommand)ServeArgumentsServeCommand)is_rich_availableis_torch_availableWindows)Console)Live)Markdown)AutoModelForCausalLMBitsAndBytesConfigz .!\"#$%&'()*+,\-/:<=>?@[]^_`{|}~textz5There is a Llama in my lawn, how can I get rid of it?zyWrite a Python function that integrates any Python function f(x) numerically over an arbitrary interval [x_start, x_end].z4How many helicopters can a human eat in one sitting?z4Count to 10 but skip every number ending with an 'e'zWhy aren't birds real?z2Why is it important to eat socks after meditating?z$Which number is larger, 9.9 or 9.11?)llamacode
helicopternumbersbirdssocksnumbers2a  

**TRANSFORMERS CHAT INTERFACE**

Chat interface to try out a model. Besides chatting with the model, here are some basic commands:
- **!help**: shows all available commands (set generation settings, save chat, etc.)
- **!status**: shows the current status of the model and generation settings
- **!clear**: clears the current conversation and starts a new one
- **!exit**: closes the interface
am  

**TRANSFORMERS CHAT INTERFACE HELP**

Full command list:
- **!help**: shows this help message
- **!clear**: clears the current conversation and starts a new one
- **!status**: shows the current status of the model and generation settings
- **!example {NAME}**: loads example named `{NAME}` from the config and uses it as the user input.
Available example names: `z`, `a%  `
- **!set {ARG_1}={VALUE_1} {ARG_2}={VALUE_2}** ...: changes the system prompt or generation settings (multiple
settings are separated by a space). Accepts the same flags and format as the `generate_flags` CLI argument.
If you're a new user, check this basic flag guide: https://huggingface.co/docs/transformers/llm_tutorial#common-options
- **!save {SAVE_NAME} (optional)**: saves the current chat and settings to file by default to
`./chat_history/{MODEL_NAME}/chat_{DATETIME}.yaml` or `{SAVE_NAME}` if provided
- **!exit**: closes the interface
c                       \ rS rSrSS\\   S\\   4S jjrS\\   S\	\\
4   4S jrS\4S	 jrS
 rS\4S jrS\S\4S jrSS\4S jjrS\S\S\4S jrSrg)RichInterfacev   N
model_name	user_namec                 f    [        5       U l        Uc  SU l        OXl        Uc  SU l        g X l        g )N	assistantuser)r   _consoler%   r&   )selfr%   r&   s      j/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/transformers/commands/chat.py__init__RichInterface.__init__w   s/    	)DO(O#DN&N    streamreturnc                   #    U R                   R                  SU R                   S35        [        U R                   SS9 nSnUI S h  vN   S h  vN nUR                  S   R
                  R                  nU(       d  M5  [        R                  " SSU5      nX5-  n/ nUR                  5        HN  nUR                  U5        UR                  S	5      (       a  UR                  S
5        M=  UR                  S5        MP     [        SR                  U5      R                  5       SS9nUR                  USS9  M   N N
 S S S 5        O! , (       d  f       O= fU R                   R                  5         W$ 7f)Nz[bold blue]<z>:   )consolerefresh_per_second r   z<(/*)(\w*)>z\<\1\2\>z```
z  
zgithub-dark)
code_themeT)refresh)r*   printr%   r   choicesdeltacontentresub
splitlinesappend
startswithr   joinstripupdate)	r+   r0   liver   tokenoutputslineslinemarkdowns	            r,   stream_outputRichInterface.stream_output   s2    l4??*;2>?$--A>$D%+|| $4e--*0088 &&gF  OO-DLL&u-- T*V, . $BGGEN$8$8$:}U Hd3I  , $4| ?>>P 	sN   >E6 E	D;	E	D?D=D?C'E	=D??E	 	E6	
E#E6c                     U R                   R                  SU R                   S35      nU R                   R                  5         U$ )z!Gets user input from the console.[bold red]<z>:
)r*   inputr&   r:   )r+   rP   s     r,   rP   RichInterface.input   s9    ##k$..1A$FGr/   c                 8    U R                   R                  5         g)zClears the console.N)r*   clearr+   s    r,   rS   RichInterface.clear   s    r/   r   c                     U R                   R                  SU R                   SU 35        U R                   R                  5         g)z%Prints a user message to the console.rO   z>:[/ bold red]
N)r*   r:   r&   )r+   r   s     r,   print_user_message RichInterface.print_user_message   s7    k$..)99I$PQr/   colorc                 z    U R                   R                  SU SU 35        U R                   R                  5         g)z,Prints text in a given color to the console.z[bold ]Nr*   r:   )r+   r   rY   s      r,   print_colorRichInterface.print_color   s1    fUG1TF34r/   minimalc                     U R                   R                  [        U(       a  [        O[        5      5        U R                   R                  5         g)z'Prints the help message to the console.N)r*   r:   r   HELP_STRING_MINIMALHELP_STRING)r+   r_   s     r,   
print_helpRichInterface.print_help   s.    HG%8UVr/   generation_configmodel_kwargsc                     U R                   R                  SU S35        U(       a  U R                   R                  SU 35        U R                   R                  SU 35        U R                   R                  5         g)zFPrints the status of the model and generation settings to the console.z[bold blue]Model: r7   z[bold blue]Model kwargs: z[bold blue]Nr\   )r+   r%   re   rf   s       r,   print_statusRichInterface.print_status   sf    0B?@MM";L> JKk*;)<=>r/   )r*   r%   r&   )NN)F)__name__
__module____qualname____firstlineno__r	   strr-   r   r   tupleintrL   rP   rS   rW   r]   boolrc   r   dictrh   __static_attributes__ r/   r,   r#   r#   v   s    	'8C= 	'HSM 	',-8R*S ,X]^acf^fXg ,\s s 
 C 
$ 
s ?O _c r/   r#   c                   v   \ rS rSr% Sr\" SSS0S9r\\   \	S'   \" SSS0S9r
\\   \	S	'   \" SSS
0S9r\\   \	S'   \" SSS0S9r\\	S'   \" SSS0S9r\\   \	S'   \" SSS0S9r\\	S'   \" SSS0S9r\\   \	S'   \" SSS0S9r\\	S'   \" SSS0S9r\\	S'   \" SS/ SQS.S9r\\   \	S'   \" SS / SQS.S9r\\   \	S!'   \" SSS"0S9r\\	S#'   \" SSS$0S9r\\   \	S%'   \" SSS&0S9r\\	S''   \" SSS(0S9r\\	S)'   \" S*S+S,S*/S.S9r\\	S-'   \" SSS.0S9r\\	S/'   \" S0SS10S9r\\	S2'   \" S3SS40S9r\\	S5'   S6 rS7rg)8ChatArguments   z
Arguments for the chat CLI.

See the metadata arg for each argument's description -- the medatata will be printed with
`transformers chat --help`
Nhelpz_Name of the pre-trained model. The positional argument will take precedence if both are passed.)defaultmetadatamodel_name_or_pathzKUsername to display in chat interface. Defaults to the current user's name.r)   zSystem prompt.system_promptz./chat_history/zFolder to save chat history.save_folderz"Path to a yaml file with examples.examples_pathFz7Whether to show runtime warnings in the chat interface.verbosezPath to a local generation config file or to a HuggingFace repo containing a `generation_config.json` file. Other generation settings passed as CLI arguments will be applied on top of this generation config.re   mainzLSpecific model version to use (can be a branch name, tag name or commit id).model_revisionautozDevice to use for inference.devicezA`torch_dtype` is deprecated! Please use `dtype` argument instead.)r   bfloat16float16float32)rx   r;   torch_dtypezOverride the default `torch.dtype` and load the model under this dtype. If `'auto'` is passed, the dtype will be automatically derived from the model's weights.dtypez2Whether to trust remote code when loading a model.trust_remote_codezWhich attention implementation to use; you can run --attn_implementation=flash_attention_2, in which case you must install this manually by running `pip install flash-attn --no-build-isolation`.attn_implementationzIWhether to use 8 bit precision for the base model - works only with LoRA.load_in_8bitzIWhether to use 4 bit precision for the base model - works only with LoRA.load_in_4bitnf4zQuantization type.fp4bnb_4bit_quant_typez#Whether to use nested quantization.use_bnb_nested_quant	localhostz%Interface the server will listen to..host@  zPort the server will listen to.portc                     U R                   b`  U R                  c  U R                   U l        gU R                   U R                  :w  a&  [        SU R                    SU R                   S35      egg)z(Only used for BC `torch_dtype` argument.Nz`torch_dtype` z and `dtype` zn have different values. `torch_dtype` is deprecated and will be removed in 4.59.0, please set `dtype` instead.)r   r   
ValueErrorrT   s    r,   __post_init__ChatArguments.__post_init__%  su     'zz!!--
!!TZZ/ $T%5%5$6mDJJ< PM M  0 (r/   )r   ) rj   rk   rl   rm   __doc__r   r{   r	   rn   __annotations__r)   r|   r}   r~   r   rq   re   r   r   r   r   r   r   r   r   r   r   r   r   rp   r   rs   rt   r/   r,   rv   rv      sV    ).u
)   ghD(3-  $)IY@Z#[M8C=[%6&JhAijKj#(Im@n#oM8C=o%6;t2uvGTv (-1
	(x} 	  hiNC  &:X1YZFCZ!&WA
"K#  !PA
E8C=  $)] ^t  */ r
*#  efL$  efL$   %UFZhmotgu=vww!&uHm?n!o$o kV=d4efD#fdf6W-XYD#Y
r/   rv   argsc                     [        U 5      $ )z3
Factory function used to chat with a local model.
)ChatCommand)r   s    r,   chat_command_factoryr   2  s     tr/   c                      \ rS rSr\S\4S j5       rS r\S\4S j5       r	\S#S\
S	\\   S\4S
 jj5       r\S#S\\   S\\   4S jj5       rS\\   S\4S jrS\
S\S\\\4   4S jr\S\S\S\\   S\\   S\\\\   4   4
S j5       r\S\
S\\   4S j5       rS\
S\S\4   4S jrS\S\
S\S\\\\\4   4   S\S\S\\   S\\\   \\4   4S jrS  rS! rS"rg)$r   i9  parserc                     [         4nU R                  SUS9nUR                  S5      nUR                  S[        SSS9  UR                  S[        SS	S
S9  UR                  [        S9  g)z
Register this command to argparse so it's available for the transformer-cli

Args:
    parser: Root parser to register command-specific arguments
chat)dataclass_typeszPositional argumentsmodel_name_or_path_or_addressNz7Name of the pre-trained model or address to connect to.)typery   rx   generate_flagsa  Flags to pass to `generate`, using a space as a separator between flags. Accepts booleans, numbers, and lists of integers, more advanced parameterization should be set through --generation-config. Example: `transformers chat <model_repo> max_new_tokens=100 do_sample=False eos_token_id=[1,2]`. If you're a new user, check this basic flag guide: https://huggingface.co/docs/transformers/llm_tutorial#common-options*)r   ry   rx   nargs)func)rv   
add_parseradd_argument_groupadd_argumentrn   set_defaultsr   )r   r   chat_parsergroups       r,   register_subcommandChatCommand.register_subcommand:  s     )*'''P../EF+J	 	 	
 	W  	 	
 	  &: ;r/   c                    UR                   b  UR                   nUR                  S5      (       d,  UR                  S5      (       d  UR                  S5      (       at  SU l        UR                  S:w  d  UR                  S:w  a  [        S5      eUR                   R                  SS5      u  Ul        Ul        UR                  c  [        S	5      eOS
U l        UR                   Ul        [        5       (       d+  [        5       (       d  U R                  (       a  [        S5      e[        5       (       d  [        S5      e[        5       (       d  U R                  (       a  [        S5      eXl        g )Nhttphttpsr   Fr   uu   Looks like you’ve set both a server address and a custom host/port. Please pick just one way to specify the server.:   z\When connecting to a server, please specify a model name with the --model_name_or_path flag.TzYou need to install rich to use the chat interface. Additionally, you have not specified a remote endpoint and are therefore spawning a backend. Torch is required for this: (`pip install rich torch`)zHYou need to install rich to use the chat interface. (`pip install rich`)zYou have not specified a remote endpoint and are therefore spawning a backend. Torch is required for this: (`pip install rich torch`))r   rB   spawn_backendr   r   r   rsplitr{   r   r   ImportErrorr   )r+   r   names      r,   r-   ChatCommand.__init__[  sC   --955Dv&&$//'*B*BdooVaFbFb%*"99+tyyD/@$J 
 (,'I'I'P'PQTVW'X$	49**2$v  3
 &*"*.*L*L' "",>,@,@TEWEWx  #$$hii#%%$*<*<7 
 	r/   r1   c                      [         R                  " 5       S:X  a  [        R                  " 5       $ [        R
                  " [        R                  " 5       5      R                  $ )z)Returns the username of the current user.r   )platformsystemosgetloginpwdgetpwuidgetuidpw_namert   r/   r,   get_usernameChatCommand.get_username  s:     ??	);;= <<		,444r/   Nr   filenamec                    0 n[        U5      US'   XS'   UR                  nUcG  [        R                  " S5      nUR                   SU S3n[
        R                  R                  XB5      n[
        R                  " [
        R                  R                  U5      SS9  [        US	5       n[        R                  " X6S
S9  SSS5        [
        R                  R                  U5      $ ! , (       d  f       N-= f)z!Saves the chat history to a file.settingschat_historyNz%Y-%m-%d_%H-%M-%Sz/chat_.jsonT)exist_okwr3   )indent)varsr}   timestrftimer   r   pathrC   makedirsdirnameopenjsondumpabspath)r   r   r   output_dictfoldertime_strfs          r,   	save_chatChatCommand.save_chat  s     "&t*J&*N#!!}}%89H<<=VH:USHww||F5H
BGGOOH-=(C AIIkQ/ !wwx(( ! s   )C&&
C4r|   c                      U c  / nU$ SU S./nU$ )zClears the chat history.r   roler=   rt   )r|   r   s     r,   clear_chat_historyChatCommand.clear_chat_history  s*      D  &-@ADr/   r   c           	      "   [        U5      S:X  a  0 $ U Vs0 s H0  nSUR                  S5      S   -   S-   UR                  S5      S   _M2     nnUR                  5        VVs0 s H+  u  pEXER                  5       S;   a  UR                  5       OU_M-     nnnUR                  5        VVs0 s H  u  pEXES:X  a  SOU_M     nnnS[        S	[
        4S
 jnUR                  5        VVs0 s H  u  pEXF" U5      (       d  SU S3OU_M     nnnSR                  UR                  5        VVs/ s H  u  pEU SU 3PM     snn5      nSU-   S-   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 [        R                  " U5      nU$ s  snf s  snnf s  snnf s  snnf s  snnf ! [        R                   a    [        S5      ef = f)zUParses the generate flags from the user input into a dictionary of `generate` kwargs.r   "=r   )truefalseNonenullsr1   c                 f    U R                  S5      n U R                  SSS5      R                  5       $ )N-.r6   r   )removeprefixreplaceisdigit)r   s    r,   	is_number3ChatCommand.parse_generate_flags.<locals>.is_number  s,    s#A99S"a(0022r/   z, z: {}z"null"z"true"r   z"false"r   z"[[z]"r[   r   zFailed to convert `generate_flags` into a valid JSON object.
`generate_flags` = {generate_flags}
Converted JSON string = {generate_flags_string})lensplititemslowerrn   rq   rC   r   r   loadsJSONDecodeErrorr   )	r+   r   flaggenerate_flags_as_dictkvr   generate_flags_stringprocessed_generate_flagss	            r,   parse_generate_flags ChatCommand.parse_generate_flags  sF   ~!#I
 bp!paoY]#

3(:":S"@$**S/RSBT"Tao!p
 OeNjNjNl"
NldaAGGI)::qwwyANl 	 "
 OeNjNjNl!mNlda!F{V"ANl!m	3 	3 	3
 VlUqUqUs!tUsTQ!Yq\\q1Xq"HUs!t
 !%		CYC_C_Ca*bCa41aS1#;Ca*b c !$&; ;c A !6 = =h O 5 = =h O 5 = =i Q 5 = =dC H 5 = =dC H !6 = =c3 G	'+zz2G'H$ ('W "q"
 "n "u
 +c" ## 	D 	s)   7G"2G*G'!G")G(
9G. . Hmodel_generation_configc                    UR                   b  SUR                   ;   ai  [        R                  R                  UR                   5      n[        R                  R	                  UR                   5      n[
        R                  " X45      nOL[
        R                  " UR                   5      nO+[        R                  " U5      nUR                  " S0 SSS.D6  U R                  UR                  5      nUR                  " S0 UD6nXW4$ )zZ
Returns a GenerationConfig object holding the generation parameters for the CLI command.
r   T   )	do_samplemax_new_tokensrt   )re   r   r   r   basenamer   from_pretrainedcopydeepcopyrE   r  r   )r+   r   r  r   r   re   parsed_generate_flagsrf   s           r,   get_generation_parameterization+ChatCommand.get_generation_parameterization  s     !!-$000''//$*@*@A77++D,B,BC$4$D$DW$W!$4$D$DTE[E[$\!
 !%.E F$$RTS'QR !% 9 9$:M:M N(//H2GH !..r/   	tokenizerre   
eos_tokenseos_token_idsc                    UR                   c  UR                  nOUR                   n/ nUb/  UR                  U R                  UR	                  S5      5      5        Ub:  UR                  UR	                  S5       Vs/ s H  n[        U5      PM     sn5        [        U5      S:X  a  UR                  UR                  5        XE4$ s  snf )z:Retrieves the pad token ID and all possible EOS token IDs.,r   )pad_token_ideos_token_idextendconvert_tokens_to_idsr   rp   r   rA   )r  re   r  r  r  all_eos_token_idstoken_ids          r,   parse_eos_tokensChatCommand.parse_eos_tokens  s     ))1,99L,99L!$$Y%D%DZEUEUVYEZ%[\$$$MDWDWX[D\%]D\c(mD\%]^ !Q&$$%6%C%CD.. &^s   <C
model_argsc                     U R                   (       a7  [        SU R                  U R                  U R                  U R                  S9nU$ U R
                  (       a  [        SS9nU$ S nU$ )NT)r   bnb_4bit_compute_dtyper   bnb_4bit_use_double_quantbnb_4bit_quant_storage)r   )r   r   r   r   r   r   )r  quantization_configs     r,   get_quantization_config#ChatCommand.get_quantization_config  st    """4!'1'7'7$.$B$B*4*I*I'1'7'7# #" $$"4!# #" #'""r/   r   c                    [         R                  " UR                  UR                  UR                  S9nUR
                  S;   a  UR
                  O[        [        UR
                  5      nU R                  U5      nUR                  UR                  USUS.n[        R                  " UR                  4SUR                  0UD6n[        USS 5      c  UR                  UR                  5      nXb4$ )N)revisionr   )r   Nr   )r'  r   r   
device_mapr#  r   hf_device_map)r   r  model_name_or_path_positionalr   r   r   getattrtorchr$  r   r   tor   )r+   r   r  r   r#  rf   models          r,   load_model_and_tokenizer$ChatCommand.load_model_and_tokenizer)  s    !11..(("44
	 #jjN:

tzz@Z"::4@++#'#;#; #6
 %44..
BFBXBX
\h
 5/408HHT[[)Er/   
user_input	interfaceexamplesrf   r   c                    SnUS:X  a-  U R                  UR                  5      nUR                  5         GO6US:X  a  UR                  5         GOUR	                  S5      (       al  [        UR                  5       5      S:  aO  UR                  5       n	[        U	5      S:X  a  U	S   n
OSn
U R                  XrU
5      n
UR                  SU
 S	3S
S9  GOUR	                  S5      (       a  USS R                  5       nUR                  5       nU H   nSU;  d  M  UR                  SU S3SS9    GO?   U R                  U5      nUR                  " S0 UD6nUR                  " S0 UD6  GOUR	                  S5      (       a  [        UR                  5       5      S:X  a  UR                  5       S   nX;   aB  UR                  5         / nUR                  XO   S   5        UR                  SXO   S   S.5        OySU S[        UR                  5       5       S3nUR                  USS9  OHUS:X  a  UR!                  UR"                  UUS9  O&SnUR                  SU S3SS9  UR                  5         XxXV4$ )z
Handles all user commands except for `!exit`. May update the chat history (e.g. reset it) or the
generation config (e.g. set a new flag).
Tz!clearz!helpz!save   r   NzChat saved in !green)r   rY   z!setr3   r   z(Invalid flag format, missing `=` after `z;`. Please use the format `arg_1=value_1 arg_2=value_2 ...`.red!exampler   r)   r   zExample z* not found in list of available examples: r   z!status)r%   re   rf   F'z/' is not a valid command. Showing help message.rt   )r   r|   rS   rc   rB   r   r   r   r]   rD   r  rE   rW   rA   listkeysrh   r{   )r+   r1  r   r2  r3  re   rf   r   valid_commandsplit_inputr   new_generate_flagsr   parsed_new_generate_flagsnew_model_kwargsexample_nameexample_errors                    r,   handle_non_exit_user_commands)ChatCommand.handle_non_exit_user_commandsD  s    !**4+=+=>DOO7"  """7++J4D4D4F0G!0K$**,K;1$&q>~~d(;H!!z'C7!S""6** ",AB!5!5!7!3!9!9!;*d?))Ftf MA A $ *   + -1,E,EFX,Y)#4#;#;#X>W#X ##7&67"":..3z7G7G7I3Ja3O%++-a0L'!,,X-CF-KLV8Nv8VWX |n,VW[\d\i\i\kWlVmmno  %%=%F9$""22"3) #  "M!!:,6e'fns!t  "$5CCr/   c                 L    [         R                  " U R                  5       5        g N)asynciorun
_inner_runrT   s    r,   rI  ChatCommand.run  s    DOO%&r/   c                   #    U R                   (       Ga  [        U R                  R                  U R                  R                  U R                  R
                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  SS9n[        U5      n[        UR                  S9nSUl        UR#                  5         U R                  R$                  S-   U R                  R&                  -   nU R                  R                  S:X  a  SOU R                  R                  nU R                  nUR(                  c  [*        nO4[-        UR(                  5       n[.        R0                  " U5      nS S S 5        UR2                  c  U R5                  5       n	OUR2                  n	[6        R8                  " UR$                  5      n
U R;                  Xj5      u  p[=        UR$                  U	S9nUR?                  5         U RA                  URB                  5      nURE                  SS	9  [G        U S
U R                  R                   35       IS h  vN n  URI                  5       nURK                  S5      (       a@  US:X  a  OU RM                  UUUWUUUS9u  nnpU(       a  URK                  S5      (       d  Mg  OURO                  SUS.5        URQ                  USURS                  5       US.S9nURU                  U5      I S h  vN nURO                  SUS.5        M  S S S 5      IS h  vN   g ! , (       d  f       GN= f N N?! [V         a     M4  f = f N,! , IS h  vN  (       d  f       g = f7f)Nerror)r   r   r   r   r   r   r   r   r   r   	log_level)targetT@r   zhttp://localhost)r%   r&   )r_   r   r6  z!exit)r1  r   r2  r3  re   rf   r   r9  r)   r   )re   r.  )r0   
extra_bodyr(   ),r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rI  daemonstartr{   r   r~   DEFAULT_EXAMPLESr   yaml	safe_loadr)   r   r   r  r  r#   rS   r   r|   rc   r
   rP   rB   rD  rA   chat_completionto_json_stringrL   KeyboardInterrupt)r+   
serve_argsserve_commandthreadr.  r   r   r3  r   r)   r  re   rf   r2  r   clientr1  r=  r0   model_outputs                       r,   rJ  ChatCommand._inner_run  s3    'yy''iioo"&))"="=$(II$A$A!YY33!YY33$(II$A$A%)YY%C%CYY^^YY^^!J )4M=#4#45F FMLLN		,,s2TYY5M5MM%)YY^^{%B!		yy%'Hd(()Q>>!, * 99$$&D99D"2"B"B4CZCZ"[*.*N*Nt*m'!T-D-DPTU	&&t'9'9: 	T*'4&$))..1A(BCCv%!*!2J ",,S11%0!SWSuSu+5%)*3)12C-9%) Tv TPD-1B  -J4I4I*4U4U$ 5V V
$KL#33#1B1Q1Q1S%*$ 4 F *3)@)@)H#HLKK NOI  DCC% *)$ DF $I ) M DCCCs   F:O<NCO.N#/O2N:5,N'!N:"6N'N:AN'%N%&N'>N: ON8O
N 	O%N''
N51N:4N55N:8O:O OOO)r   r   rG  ) rj   rk   rl   rm   staticmethodr   r   r-   rn   r   rv   r	   r   r;  rr   r   r  r   ro   r  r   rp   r  r   r$  r   r/  r#   rD  rI  rJ  rs   rt   r/   r,   r   r   9  s   <N < <@#N 5# 5 5 )m )x} )PS ) )$ (3- 4:  3(49 3( 3(j/!/<L/	%	&/8 /&/+/ SM/  }	/
 
sDI~	/ /6 #M #hGY>Z # #& ]  uE[]jEj?k  6PDPD PD !	PD
 sDcN*+PD ,PD PD 4jPD 
tDz+T1	2PDh'Ur/   r   __main__z meta-llama/Llama-3.2-3b-Instructzhttp://localhost:8000)ErH  r  r   r   r   r>   stringr   argparser   r   collections.abcr   dataclassesr   r   	threadingr   typingr	   rU  huggingface_hubr
   r   transformersr   r   r   transformers.commandsr   transformers.commands.servingr   r   transformers.utilsr   r   readliner   r   r   rich.consoler   	rich.liver   rich.markdownr   r,  r   r   setascii_letters
whitespaceALLOWED_KEY_CHARSdigitsALLOWED_VALUE_CHARSrT  ra   rC   r<  rb   r#   rv   r   r   rj   r   r   r   rI  rt   r/   r,   <module>rw     s       	  	   . ) (    L 
 = F D	
 ??	!$&
 ,,v/@/@@A 
6==(6+<+<<?bb 
 MN) QRNO./JK?@  	 	 ";;'7'<'<'>?@ A&Y Yx \ \ \~y w, wt z?D)KD&)@D&tDHHJ M  		s   0F. .F87F8