
    `ix`                    R   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlZddlmZmZ 	 ddlmZ dd	lmZ dd
lmZ ddlmZ n8# e$ r0  G d d          Z  e             Z e             Z e             Ze!Z"Y nw xY wd@dZ#dAdZ$ G d de%e          Z& G d de          Z' ed          Z( G d de          Z) G d de)          Z* G d  d!e          Z+ G d" d#e%e          Z, G d$ d%e          Z- G d& d'e          Z. G d( d)e)          Z/ G d* d+e)          Z0 G d, d-e          Z1 G d. d/e          Z2 G d0 d1e          Z3 G d2 d3e          Z4 G d4 d5          Z5 G d6 d7e          Z6 G d8 d9          Z7dBd<Z G d= d>e%e          Z8g d?Z9dS )Cu  Python wrapper around the Rust implementation of *harmony*.

The heavy lifting (tokenisation, rendering, parsing, …) is implemented in
Rust.  The thin bindings are available through the private ``openai_harmony``
extension module which is compiled via *maturin* / *PyO3*.

This package provides a small, typed convenience layer that mirrors the public
API of the Rust crate so that it can be used from Python code in an
idiomatic way (``dataclasses``, ``Enum``s, …).
    )annotationsN)Enum)AbstractSetAny
CollectionDictListLiteralOptionalPatternSequenceTypeVarUnion)	BaseModelField   HarmonyError)PyHarmonyEncoding)PyStreamableParser)load_harmony_encodingc                      e Zd ZddZdS )_StubnamestrreturnNonec                     t          d          )NzThe compiled harmony bindings are not available. Make sure to build the project with `maturin develop` before running this code.)RuntimeError)selfr   s     k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/openai_harmony/__init__.py__getattr__z_Stub.__getattr__6   s          N)r   r   r   r   )__name__
__module____qualname__r"    r#   r!   r   r   5   s(        	 	 	 	 	 	r#   r   tokensfrozenset[str]r   Pattern[str]c                p    d                     d | D                       }t          j        d| d          S )N|c              3  >   K   | ]}t          j        |          V  d S N)reescape).0tokens     r!   	<genexpr>z'_special_token_regex.<locals>.<genexpr>D   s,      ::%RYu%%::::::r#   ())joinr/   compile)r(   inners     r!   _special_token_regexr9   C   s:    HH::6:::::E:l%lll###r#   r2   r   r   c           	     4    t          d| d|  d|  d          )Nz;Encountered text corresponding to disallowed special token zp.
If you want this text to be encoded as a special token, pass it to `allowed_special`, e.g. `allowed_special={'z', ...}`.
If you want this text to be encoded as normal text, disable the check for this token by passing `disallowed_special=(enc.special_tokens_set - {'zR'})`.
To disable this check for all special tokens, pass `disallowed_special=()`.
r   )r2   s    r!   raise_disallowed_special_tokenr;   H   sU    
	X	X 	X CH	X 	X
 HM	X 	X 	X  r#   c                  >    e Zd ZdZdZdZdZdZdZe	dd            Z
dS )Rolez6The role of a message author (mirrors ``chat::Role``).user	assistantsystem	developertoolvalueobjectr   'Role'c                &    t          d|          )NzUnknown role: )
ValueError)clsrC   s     r!   	_missing_zRole._missing_b   s    3%33444r#   N)rC   rD   r   rE   )r$   r%   r&   __doc__USER	ASSISTANTSYSTEM	DEVELOPERTOOLclassmethodrI   r'   r#   r!   r=   r=   Y   sQ        @@DIFID5 5 5 [5 5 5r#   r=   c                  @    e Zd ZU ded<   dZded<   ed
d	            ZdS )Authorr=   roleNOptional[str]r   r   r   'Author'c                     | ||          S )NrS   r   r'   )rH   rS   r   s      r!   newz
Author.newk   s    s4((((r#   )rS   r=   r   r   r   rU   )r$   r%   r&   __annotations__r   rP   rX   r'   r#   r!   rR   rR   g   sN         JJJD) ) ) [) ) )r#   rR   Tc                      e Zd ZddZdS )Contentr   Dict[str, Any]c                    t           r.   )NotImplementedErrorr    s    r!   to_dictzContent.to_dictw   s    !!r#   Nr   r]   )r$   r%   r&   ra   r'   r#   r!   r\   r\   v   s(        " " " " " "r#   r\   c                  "    e Zd ZU ded<   ddZdS )TextContentr   textr   r]   c                    d| j         dS )Nre   typere   re   r`   s    r!   ra   zTextContent.to_dict~   s    	222r#   Nrb   )r$   r%   r&   rY   ra   r'   r#   r!   rd   rd   {   s3         III3 3 3 3 3 3r#   rd   c                  N    e Zd ZU ded<   ded<   dZded<   e	 d
dd	            ZdS )ToolDescriptionr   r   descriptionNOptional[dict]
parametersr   'ToolDescription'c                     | |||          S )N)r   rl   rn   r'   )rH   r   rl   rn   s       r!   rX   zToolDescription.new   s     s+*MMMMr#   r.   )r   r   rl   r   rn   rm   r   ro   )r$   r%   r&   rY   rn   rP   rX   r'   r#   r!   rk   rk      si         III!%J%%%%GKN N N N [N N Nr#   rk   c                      e Zd ZdZdZdZdS )ReasoningEffortLowMediumHighN)r$   r%   r&   LOWMEDIUMHIGHr'   r#   r!   rr   rr      s        
CFDDDr#   rr   c                  <    e Zd ZU ded<   ded<   ed
d            Zd	S )ChannelConfig	List[str]valid_channelsboolchannel_requiredchannelsr   'ChannelConfig'c                     | |d          S )NT)r|   r~   r'   )rH   r   s     r!   require_channelszChannelConfig.require_channels   s    s(TBBBBr#   N)r   r{   r   r   )r$   r%   r&   rY   rP   r   r'   r#   r!   rz   rz      sR         C C C [C C Cr#   rz   c                  b    e Zd ZU ded<   dZded<   ded<   edd
            Zedd            ZdS )ToolNamespaceConfigr   r   NrT   rl   zList[ToolDescription]toolsr   'ToolNamespaceConfig'c                 <    ddl m}   | d          }t          di |S )Nr   get_tool_namespace_configbrowserr'   openai_harmonyr   r   _get_tool_namespace_configcfgs     r!   r   zToolNamespaceConfig.browser   sF    	
 	
 	
 	
 	
 	
 )(33"))S)))r#   c                 <    ddl m}   | d          }t          di |S )Nr   r   pythonr'   r   r   s     r!   r   zToolNamespaceConfig.python   sF    	
 	
 	
 	
 	
 	
 )(22"))S)))r#   )r   r   )r$   r%   r&   rY   rl   staticmethodr   r   r'   r#   r!   r   r      sz         III!%K%%%%    * * * \* * * * \* * *r#   r   c                     e Zd ZU dZded<   ej        Zded<   dZded<   dZ	ded	<    e
d
           Zded<   dZded<   ed'd            Zd(dZd)dZd*dZd+dZd,dZd-dZd.d Zd'd!Zd'd"Zd/d$Zed0d&            ZdS )1SystemContentz:You are ChatGPT, a large language model trained by OpenAI.rT   model_identityzOptional[ReasoningEffort]reasoning_effortNconversation_start_datez2024-06knowledge_cutoffc                 :    t                               g d          S )N)analysis
commentaryfinal)rz   r   r'   r#   r!   <lambda>zSystemContent.<lambda>   s     > >///!
 !
 r#   default_factoryzOptional[ChannelConfig]channel_config(Optional[dict[str, ToolNamespaceConfig]]r   r   'SystemContent'c                     |             S r.   r'   rH   s    r!   rX   zSystemContent.new       suur#   r   c                    || _         | S r.   )r   )r    r   s     r!   with_model_identityz!SystemContent.with_model_identity       ,r#   rr   c                    || _         | S r.   )r   )r    r   s     r!   with_reasoning_effortz#SystemContent.with_reasoning_effort   s     !1r#   c                    || _         | S r.   )r   )r    r   s     r!   with_conversation_start_datez*SystemContent.with_conversation_start_date   s     (?$r#   c                    || _         | S r.   )r   )r    r   s     r!   with_knowledge_cutoffz#SystemContent.with_knowledge_cutoff   s     0r#   rz   c                    || _         | S r.   )r   )r    r   s     r!   with_channel_configz!SystemContent.with_channel_config   r   r#   r   	list[str]c                D    t                               |          | _        | S r.   )rz   r   r   )r    r   s     r!   with_required_channelsz$SystemContent.with_required_channels   s    +<<XFFr#   	ns_configr   c                @    | j         i | _         || j         |j        <   | S r.   r   r   r    r   s     r!   
with_toolszSystemContent.with_tools   $    :DJ%.
9>"r#   c                Z    |                      t                                                    S r.   )r   r   r   r`   s    r!   with_browser_toolzSystemContent.with_browser_tool   s!    2::<<===r#   c                Z    |                      t                                                    S r.   )r   r   r   r`   s    r!   with_python_toolzSystemContent.with_python_tool   s!    299;;<<<r#   dictc                <    |                      d          }d|d<   |S )NTexclude_nonesystem_contentrh   
model_dumpr    outs     r!   ra   zSystemContent.to_dict   s#    oo4o00&F
r#   rawc                     | di |S Nr'   r'   rH   r   s     r!   	from_dictzSystemContent.from_dict       szzSzzr#   )r   r   )r   r   r   r   )r   rr   r   r   )r   r   r   r   )r   r   r   r   )r   rz   r   r   )r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   )r$   r%   r&   r   rY   rr   rw   r   r   r   r   r   r   rP   rX   r   r   r   r   r   r   r   r   r   ra   r   r'   r#   r!   r   r      s        D      3B2HHHHH-11111&/////.3e
 
/ / /N    
 7;E::::   [
                     > > > >= = = =   
    [  r#   r   c                  |    e Zd ZU dZded<   dZded<   edd            Zdd
ZddZ	ddZ
ddZedd            ZdS )DeveloperContentNrT   instructionsr   r   r   'DeveloperContent'c                     |             S r.   r'   r   s    r!   rX   zDeveloperContent.new   r   r#   r   c                    || _         | S r.   )r   )r    r   s     r!   with_instructionsz"DeveloperContent.with_instructions      (r#   r   r   c                @    | j         i | _         || j         |j        <   | S r.   r   r   s     r!   r   zDeveloperContent.with_tools  r   r#   Sequence[ToolDescription]c           	     f    |                      t          dd t          |                              S )N	functions)r   rl   r   )r   r   list)r    r   s     r!   with_function_toolsz$DeveloperContent.with_function_tools  s2     [d$u++VVV
 
 	
r#   r   c                <    |                      d          }d|d<   |S )NTr   developer_contentrh   r   r   s     r!   ra   zDeveloperContent.to_dict  s#    oo4o00)F
r#   r   c                     | di |S r   r'   r   s     r!   r   zDeveloperContent.from_dict  r   r#   )r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   r   )r   r   r   r   )r$   r%   r&   r   rY   r   rP   rX   r   r   r   ra   r   r'   r#   r!   r   r      s         "&L&&&&6:E::::   [      
 
 
 
   
    [  r#   r   c                      e Zd ZU ded<    ee          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            Ze
d!d            Zd"dZd#dZd$dZd%dZd&dZd'dZe
d(d            ZdS ))MessagerR   authorr   zList[Content]contentNrT   channel	recipientcontent_typeUnion[str, Content]r   	'Message'c                h    t          |t                    rt          |          } | ||g          S )Nri   r   r   )
isinstancer   rd   )rH   r   r   s      r!   from_author_and_contentzMessage.from_author_and_content,  s=     gs## 	0!w///Gs&7)4444r#   rS   r=   c                J    |                      t          |          |          S )NrS   )r   rR   )rH   rS   r   s      r!   from_role_and_contentzMessage.from_role_and_content4  s%     **6t+<+<+<gFFFr#   contentsSequence[Content]c                R     | t          |          t          |                    S )Nr   r   )rR   r   )rH   rS   r   s      r!   from_role_and_contentszMessage.from_role_and_contents:  s*     s&d+++T(^^DDDDr#   c                    t          |t                    rt          |          }| j                            |           | S )Nri   )r   r   rd   r   append)r    r   s     r!   adding_contentzMessage.adding_contentD  s?    gs## 	0!w///GG$$$r#   r   c                    || _         | S r.   )r   )r    r   s     r!   with_channelzMessage.with_channelJ  s    r#   c                    || _         | S r.   )r   )r    r   s     r!   with_recipientzMessage.with_recipientN  s    "r#   c                    || _         | S r.   )r   )r    r   s     r!   with_content_typezMessage.with_content_typeR  r   r#   r]   c                    i | j                                         dd | j        D             i}| j        
| j        |d<   | j        
| j        |d<   | j        
| j        |d<   |S )Nr   c                6    g | ]}|                                 S r'   ra   )r1   cs     r!   
<listcomp>z#Message.to_dict.<locals>.<listcomp>]  s     :::		:::r#   r   r   r   )r   r   r   r   r   r   r   s     r!   ra   zMessage.to_dictZ  s    
k$$&&
::T\:::
 
 <#!\C	N>%#~C("&"3C
r#   c                N    t          j        |                                           S r.   jsondumpsra   r`   s    r!   to_jsonzMessage.to_jsong      z$,,..)))r#   datac                   t          |d                   }t          ||                    d                    }g }|d         }t          |t                    rd|dg}|D ]}|                    d          dk    r |                    t          di |           ;|                    d          dk    r |                    t          di |           t|                    d          d	k    r |                    t          di |           t          d
|            | ||          }|                    d          |_
        |                    d          |_        |                    d          |_        |S )NrS   r   rW   r   re   rg   rh   r   r   zUnknown content variant: r   r   r   r   r'   )r=   rR   getr   r   r   rd   r   r   rG   r   r   r   )rH   r  rS   r   r   raw_contentr   msgs           r!   r   zMessage.from_dictj  s    DL!!T(8(8999"$9o
 k3'' 	B$*K@@AK 	D 	DCwwv&(( 2 2c 2 23333$444 4 4 4 45555$777 0 7 73 7 78888 !BS!B!BCCCc222hhy))--88N33
r#   )r   rR   r   r   r   r   )rS   r=   r   r   r   r   )rS   r=   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   rb   r   r   )r  r]   r   r   )r$   r%   r&   rY   r   r   r   r   r   r   rP   r   r   r   r   r   r   r   ra   r  r   r'   r#   r!   r   r   !  sp        NNN"U4888G8888!G!!!!#I####"&L&&&& 5 5 5 [5 G G G [G
 E E E [E               * * * *    [  r#   r   c                  x    e Zd ZU  ee          Zded<   edd            Zd Z	dd
Z
ddZedd            ZdS )Conversationr   List[Message]messagesSequence[Message]r   'Conversation'c                4     | t          |                    S )Nr  )r   )rH   r  s     r!   from_messageszConversation.from_messages  s    sDNN++++r#   c                *    t          | j                  S r.   )iterr  r`   s    r!   __iter__zConversation.__iter__  s    DM"""r#   r]   c                (    dd | j         D             iS )Nr  c                6    g | ]}|                                 S r'   r   r1   ms     r!   r   z(Conversation.to_dict.<locals>.<listcomp>  s     @@@QQYY[[@@@r#   r  r`   s    r!   ra   zConversation.to_dict  s    @@$-@@@AAr#   r   c                N    t          j        |                                           S r.   r  r`   s    r!   r  zConversation.to_json  r  r#   payloadc                b    t          j        |          } | d |d         D                       S )Nc                B    g | ]}t                               |          S r'   r   r   r  s     r!   r   z*Conversation.from_json.<locals>.<listcomp>  s&    LLLaW..q11LLLr#   r  r  )r  loads)rH   r  r  s      r!   	from_jsonzConversation.from_json  s8    z'""sLL4
;KLLLMMMMr#   N)r  r  r   r  rb   r  )r  r   r   r  )r$   r%   r&   r   r   r  rY   rP   r  r  ra   r  r#  r'   r#   r!   r  r    s         #eD999H9999, , , [,# # #
B B B B* * * * N N N [N N Nr#   r  c                      e Zd ZU dZded<   dS )RenderConversationConfigTr}   auto_drop_analysisN)r$   r%   r&   r&  rY   r'   r#   r!   r%  r%    s$         #######r#   r%  c                      e Zd ZU dZded<   dS )RenderOptionsFr}   conversation_has_function_toolsN)r$   r%   r&   r)  rY   r'   r#   r!   r(  r(    s$         ,1#111111r#   r(  c                      e Zd ZdZd6dZed7d            Zej        d8d	            Z		 d9d:dZ
	 d9d;dZ	 d9d;dZ	 d9d<dZ	 d9ddd=d#Zd>d$Z e            d%d&d?d-Zd@dAd0ZdBd3ZdCd4ZdCd5Zd
S )DHarmonyEncodingz?High-level wrapper around the Rust ``PyHarmonyEncoding`` class.r8   _PyHarmonyEncodingc                    || _         d S r.   )_inner)r    r8   s     r!   __init__zHarmonyEncoding.__init__  s    r#   r   r   c                    | j         j        S r.   )r.  r   r`   s    r!   r   zHarmonyEncoding.name  s    {r#   set[str]c                N    t          | j                                                  S r.   )setr.  special_tokensr`   s    r!   special_tokens_setz"HarmonyEncoding.special_tokens_set  s    4;--//000r#   Nconversationr  next_turn_roler=   config"Optional[RenderConversationConfig]	List[int]c                    |ddi}n	d|j         i}| j                            |                                t	          |j                  |          S )z
        Render a conversation for completion.
        Args:
            conversation: Conversation object
            next_turn_role: Role for the next turn
            config: Optional RenderConversationConfig (default auto_drop_analysis=True)
        Nr&  T)conversation_jsonr7  r8  )r&  r.  "render_conversation_for_completionr  r   rC   )r    r6  r7  r8  config_dicts        r!   r=  z2HarmonyEncoding.render_conversation_for_completion  sa     >/6KK/1JKK{==*2244~344 > 
 
 	
r#   c                ~    |ddi}n	d|j         i}| j                            |                                |          S )z3Render a conversation without appending a new role.Nr&  Tr<  r8  )r&  r.  render_conversationr  r    r6  r8  r>  s       r!   rA  z#HarmonyEncoding.render_conversation  sS     >/6KK/1JKK{..*2244 / 
 
 	
r#   c                ~    |ddi}n	d|j         i}| j                            |                                |          S )z#Render a conversation for training.Nr&  Tr@  )r&  r.   render_conversation_for_trainingr  rB  s       r!   rD  z0HarmonyEncoding.render_conversation_for_training  sS     >/6KK/1JKK{;;*2244 < 
 
 	
r#   messager   render_optionsOptional[RenderOptions]c                ~    |ddi}n	d|j         i}| j                            |                                |          S )z$Render a single message into tokens.Nr)  F)message_jsonrF  )r)  r.  renderr  )r    rE  rF  render_options_dicts       r!   rJ  zHarmonyEncoding.render  s[     !#De"L 2>3a# {!! **;N " 
 
 	
r#   Tstrictr(   Sequence[int]rS   Optional[Role] | NonerM  r}   r  c                   | j                             t          |          |d nt          |j                  |          }d t          j        |          D             S )Nc                B    g | ]}t                               |          S r'   r!  r  s     r!   r   zIHarmonyEncoding.parse_messages_from_completion_tokens.<locals>.<listcomp>  s&    CCC!!!$$CCCr#   )r.  %parse_messages_from_completion_tokensr   r   rC   r  r"  )r    r(   rS   rM  raw_jsons        r!   rR  z5HarmonyEncoding.parse_messages_from_completion_tokens
  sY     IILL$,$$C
OOV
 
 DCdj.B.BCCCCr#   c                P    | j                             t          |                    S )zDecode a list of tokens into a UTF-8 string. Will raise an error if the tokens result in invalid UTF-8. Use decode if you want to replace invalid UTF-8 with the unicode replacement character.)r.  decode_utf8r   )r    r(   s     r!   rU  zHarmonyEncoding.decode_utf8  s    {&&tF||444r#   all)allowed_specialdisallowed_specialre   rW  !Literal['all'] | AbstractSet[str]rX   Literal['all'] | Collection[str]	list[int]c               B   |dk    r| j         }|dk    r| j         t          |          z
  }|rit          |t                    st          |          }t	          |                              |          x}r!t          |                                           	 | j        	                    |t          |                    S # t          $ rU |	                    dd                              dd          }| j        	                    |t          |                    cY S w xY w)aH  Encodes a string into tokens.

        Special tokens are artificial tokens used to unlock capabilities from a model,
        such as fill-in-the-middle. So we want to be careful about accidentally encoding special
        tokens, since they can be used to trick a model into doing something we don't want it to do.

        Hence, by default, encode will raise an error if it encounters text that corresponds
        to a special token. This can be controlled on a per-token level using the `allowed_special`
        and `disallowed_special` parameters. In particular:
        - Setting `disallowed_special` to () will prevent this function from raising errors and
          cause all text corresponding to special tokens to be encoded as natural text.
        - Setting `allowed_special` to "all" will cause this function to treat all text
          corresponding to special tokens to be encoded as special tokens.

        ```
        >>> enc.encode("hello world")
        [31373, 995]
        >>> enc.encode("<|endoftext|>", allowed_special={"<|endoftext|>"})
        [50256]
        >>> enc.encode("<|endoftext|>", allowed_special="all")
        [50256]
        >>> enc.encode("<|endoftext|>")
        # Raises ValueError
        >>> enc.encode("<|endoftext|>", disallowed_special=())
        [27, 91, 437, 1659, 5239, 91, 29]
        ```
        rV  zutf-16surrogatepassreplace)r5  r3  r   	frozensetr9   searchr;   groupr.  encoder   UnicodeEncodeErrordecode)r    re   rW  rX  matchs        r!   rb  zHarmonyEncoding.encode  s,   D e##"5O&&!%!83;O;O!O 	>0)<< C%./A%B%B",-?@@GGMMMu >.u{{}}===	C;%%dD,A,ABBB! 	C 	C 	C;;x99@@9UUD;%%dD,A,ABBBBB	Cs   'B? ?ADDr^  errorsc                    t          | j                            t          |                              }|                    d|          S )aS  Decodes a list of tokens into a string.

        WARNING: the default behaviour of this function is lossy, since decoded bytes are not
        guaranteed to be valid UTF-8. You can use `decode_utf8` if you want to raise an error on invalid UTF-8.

        ```
        >>> enc.decode([31373, 995])
        'hello world'
        ```
        zutf-8)rf  )bytesr.  decode_bytesr   rd  )r    r(   rf  r  s       r!   rd  zHarmonyEncoding.decodeN  s=     T[--d6ll;;<<{{76{222r#   r2   intc                6    | j                             |          S )z1Returns if an individual token is a special token)r.  is_special_tokenr    r2   s     r!   rl  z HarmonyEncoding.is_special_token\  s    {++E222r#   c                4    | j                                         S r.   )r.  stop_tokensr`   s    r!   ro  zHarmonyEncoding.stop_tokensb  s    {&&(((r#   c                4    | j                                         S r.   )r.  !stop_tokens_for_assistant_actionsr`   s    r!   rq  z1HarmonyEncoding.stop_tokens_for_assistant_actionse  s    {<<>>>r#   )r8   r,  r  )r   r1  r.   )r6  r  r7  r=   r8  r9  r   r:  )r6  r  r8  r9  r   r:  )rE  r   rF  rG  r   r:  )r(   rN  rS   rO  rM  r}   r   r  )r(   rN  r   r   )re   r   rW  rY  rX  rZ  r   r[  )r^  )r(   rN  rf  r   r   r   )r2   rj  r   r}   r   r:  )r$   r%   r&   rJ   r/  propertyr   	functoolscached_propertyr5  r=  rA  rD  rJ  rR  rU  r3  rb  rd  rl  ro  rq  r'   r#   r!   r+  r+    s       II          X  1 1 1 1 6:	
 
 
 
 
4 6:
 
 
 
 
$ 6:
 
 
 
 
  KO
 
 
 
 
( '+
D
 
D 
D 
D 
D 
D 
D5 5 5 5 >ASUU?D0C 0C 0C 0C 0C 0Cd3 3 3 3 33 3 3 3) ) ) )? ? ? ? ? ?r#   r+  c                      e Zd ZdZdZdZdS )StreamStateExpectStartHeaderr\   N)r$   r%   r&   EXPECT_STARTHEADERCONTENTr'   r#   r!   rw  rw  i  s         LFGGGr#   rw  c                      e Zd ZdZddd$dZd%dZd&dZed'd            Zed(d            Z	ed)d            Z
ed)d            Zed*d            Zed+d            Zed,d            Zed-d             Zed)d!            Zed)d"            Zd#S ).StreamableParserz*Incremental parser over completion tokens.TrL  encodingr+  rS   Role | NonerM  r}   r   r   c               l    |t          |j                  nd }t          |j        ||          | _        d S r.   )r   rC   _PyStreamableParserr.  )r    r  rS   rM  role_strs        r!   r/  zStreamableParser.__init__r  s3     '+&63tz???D)(/8VLLr#   r2   rj  'StreamableParser'c                :    | j                             |           | S r.   )r.  processrm  s     r!   r  zStreamableParser.process|  s    E"""r#   c                8    | j                                          | S r.   )r.  process_eosr`   s    r!   r  zStreamableParser.process_eos  s    !!!r#   r   c                    | j         j        S r.   )r.  current_contentr`   s    r!   r  z StreamableParser.current_content      {**r#   Optional[Role]c                @    | j         j        }|t          |          nd S r.   )r.  current_roler=   r    r   s     r!   r  zStreamableParser.current_role  s     k&OtCyyy5r#   rT   c                    | j         j        S r.   )r.  current_content_typer`   s    r!   r  z%StreamableParser.current_content_type  s    {//r#   c                    | j         j        S r.   )r.  last_content_deltar`   s    r!   r  z#StreamableParser.last_content_delta  s    {--r#   r  c                V    | j         j        }d t          j        |          D             S )Nc                B    g | ]}t                               |          S r'   r!  r  s     r!   r   z-StreamableParser.messages.<locals>.<listcomp>  s&    >>>!!!$$>>>r#   )r.  r  r  r"  r  s     r!   r  zStreamableParser.messages  s(    k">>djoo>>>>r#   r:  c                    | j         j        S r.   )r.  r(   r`   s    r!   r(   zStreamableParser.tokens  s    {!!r#   r]   c                >    t          j        | j        j                  S )z>Return a JSON string representing the parser's internal state.)r  r"  r.  stater`   s    r!   
state_datazStreamableParser.state_data  s     z$++,,,r#   rw  c                :    | j         }t          |d                   S )Nr  )r  rw  )r    r  s     r!   r  zStreamableParser.state  s    4=)))r#   c                    | j         j        S r.   )r.  current_recipientr`   s    r!   r  z"StreamableParser.current_recipient  s    {,,r#   c                    | j         j        S r.   )r.  current_channelr`   s    r!   r  z StreamableParser.current_channel  r  r#   N)r  r+  rS   r  rM  r}   r   r   )r2   rj  r   r  )r   r  r  )r   r  )r   rT   )r   r  rr  rb   )r   rw  )r$   r%   r&   rJ   r/  r  r  rs  r  r  r  r  r  r(   r  r  r  r  r'   r#   r!   r~  r~  o  s       44 M M M M M M       + + + X+ 6 6 6 X6 0 0 0 X0 . . . X. ? ? ? X? " " " X" - - - X- * * * X* - - - X- + + + X+ + +r#   r~  r   str | 'HarmonyEncodingName'c                    t          | t                    st          |           } t          |           }t          |          S )zBLoad an encoding by *name* (delegates to the Rust implementation).)r   r   _load_harmony_encodingr+  )r   r8   s     r!   r   r     s<     dC   4yy 6t < <E5!!!r#   c                      e Zd ZdZddZdS )HarmonyEncodingNameHarmonyGptOssr   r   c                *    t          | j                  S r.   )r   rC   r`   s    r!   __str__zHarmonyEncodingName.__str__  s    4:r#   Nr  )r$   r%   r&   HARMONY_GPT_OSSr  r'   r#   r!   r  r    s-        %O     r#   r  )r=   rR   r\   rd   r   rk   r   r   r  r+  r  r   r~  rw  r   )r(   r)   r   r*   )r2   r   r   r   )r   r  r   r+  ):rJ   
__future__r   rt  r  enumr   typingr   r   r   r   r	   r
   r   r   r   r   r   r/   pydanticr   r   r   r   r   r,  r   r  r   r  ModuleNotFoundErrorr   r   _HarmonyErrorr9   r;   r   r=   rR   rZ   r\   rd   rk   rr   rz   r   r   r   r   r  r%  r(  r+  rw  r~  r  __all__r'   r#   r!   <module>r     sg  	 	 # " " " " "                                     
			 % % % % % % % %!      HGGGGG             ! ! !
        #UWW%'' MMM!!&$ $ $ $
	 	 	 	"5 5 5 5 53 5 5 5) ) ) ) )Y ) ) ) GCLL" " " " "i " " "
3 3 3 3 3' 3 3 3	N 	N 	N 	N 	Ni 	N 	N 	N    c4   C C C C CI C C C* * * * *) * * *0C C C C CG C C CL         w      Lg g g g gi g g gTN N N N N9 N N N:$ $ $ $ $y $ $ $2 2 2 2 2I 2 2 2v? v? v? v? v? v? v? v?r    $   ?+ ?+ ?+ ?+ ?+ ?+ ?+ ?+J" " " "     #t     s   A 2BB