ó
    qyüiÕc  ã                   ó  • S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
J
r
  S SKJr  S SK Jr  S SKJrJrJrJrJrJrJr  S S	KJr  S
SKJr  S
SKJrJrJr  \R<                  " \5      r \" 5       (       a%  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'  S SK(J)r)  OSr!\" 5       (       a  S SK*J+r+  \,\-\.\4      r/\0\1\.\2\\3" S5      S4r4\Rj                  " S\Rl                  5      r7\Rj                  " S\Rl                  5      r8\Rj                  " S\Rl                  \Rr                  -  5      r:\Rj                  " S\Rl                  5      r; " S S\<5      r= " S S\<5      r>S\3S\-\.\.4   4S jr?S\.S\-4S jr@S\S\-4S jrAS \.S\B\.S-  \-S-  \.S-  4   4S! jrCS\S\-4S" jrD\\S#\.S-  S\E\.   4S$ j5       5       rFS% rG\S& 5       rH\S' 5       rI      S2S(\,\/   S)\,\-\-     S-  S*\/S-  S#\.S-  S+\2S,\2S-\2S\.4S. jjrJS/ rK " S0 S15      rLg)3é    N)ÚCallable)Úcontextmanager)Údeepcopy)Údatetime)Ú	lru_cache)Ú
isfunction)ÚAnyÚLiteralÚUnionÚget_argsÚ
get_originÚget_type_hintsÚno_type_check)Úversioné   )Úlogging)Úis_jinja_availableÚis_torch_availableÚis_vision_available)Ú	Extension)ÚImmutableSandboxedEnvironment)ÚImage.z(^(.*?)[\n\s]*(Args:|Returns:|Raises:|\Z)z0\n\s*Args:\n\s*(.*?)[\n\s]*(Returns:|Raises:|\Z)a1  
(?:^|\n)  # Match the start of the args block, or a newline
\s*(\w+):\s*  # Capture the argument name and strip spacing
(.*?)\s*  # Capture the argument description, which can span multiple lines, and strip trailing spacing
(?=\n\s*\w+:|\Z)  # Stop when you hit the next argument or the end of the block
z*\n\s*Returns:\n\s*(.*?)[\n\s]*(Raises:|\Z)c                   ó   • \ rS rSrSrSrg)ÚTypeHintParsingExceptionéH   zJException raised for errors in parsing type hints to generate JSON schemas© N©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__static_attributes__r   ó    Úc/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/utils/chat_template_utils.pyr   r   H   ó   † ÜTr$   r   c                   ó   • \ rS rSrSrSrg)ÚDocstringParsingExceptionéL   zJException raised for errors in parsing docstrings to generate JSON schemasr   Nr   r   r$   r%   r(   r(   L   r&   r$   r(   Ú
param_typeÚreturnc                 ó  • [         SS0[        SS0[        SS0[        SS0[	        S 5      SS0[
        0 0n[        5       (       a  SS0U[        '   [        5       (       a  SS K	nSS	0XR                  '   UR                  U SS
05      $ )NÚtypeÚintegerÚnumberÚstringÚbooleanÚnullÚimager   ÚaudioÚobject)ÚintÚfloatÚstrÚboolr-   r	   r   r   r   ÚtorchÚTensorÚget)r*   Útype_mappingr:   s      r%   Ú_get_json_schema_typer>   P   s“   € äˆf�iÐ Ü�˜Ð!Üˆf�hÐÜˆv�yÐ!ÜˆT‹
�V˜VÐ$ÜˆRð€Lô ×ÑØ% wÐ/ˆ”UÑÜ×ÑÛà&,¨gÐ%6ˆ—\‘\Ñ"Ø×Ñ˜J¨°Ð(:Ó;Ð;r$   Úhintc                 ó~  • [        U 5      n[        U 5      nUc   [        U 5      $ U[
        L d(  [        [        S5      (       a¤  U[        R                  L a‘  U Vs/ s H  o3[        S 5      Ld  M  [        U5      PM     nn[        U5      S:X  a  US   nO<[        S U 5       5      (       a!  S[        U Vs/ s H  ofS   PM	     sn5      0nOSU0n[        S 5      U;   a  SUS	'   U$ U[        L aÊ  [        U5      S:”  a»  [        [         ["        [$        [        S 5      4n/ nU H]  n	[        U	5      U;  a  [	        S
5      e[        [        U	5      5      R'                  S5      n
U
c  ME  X¨;  d  ML  UR)                  U
5        M_     [        U5      S:X  a  UR+                  5       O
[-        U5      [-        U5      S.$ U[,        L a  U(       d  SS0$ S[        US   5      S.$ U[.        L ar  U(       d  SS0$ [        U5      S:X  a(  [	        S[#        U 5      R1                  SS5       S35      eSU;   a  [	        S5      eSU Vs/ s H  n[        U5      PM     snS.$ U[2        L a&  SS0n[        U5      S:X  a  [        US   5      US'   U$ [	        SU 5      e! [         a    [	        SU 5      ef = fs  snf s  snf s  snf )NzGCouldn't parse this type hint, likely due to a custom class or object: Ú	UnionTyper   r   c              3   ó`   #   • U  H$  nS U;   =(       a    [        US    [        5      v •  M&     g7f)r-   N)Ú
isinstancer8   )Ú.0Úsubtypes     r%   Ú	<genexpr>Ú#_parse_type_hint.<locals>.<genexpr>t   s*   é € Ð`ÒW_ÈG�˜7Ñ"×G¤z°'¸&±/Ä3Ó'GÔGÒW_ùs   ‚,.r-   ÚanyOfTÚnullablez?Only the valid python literals can be listed in typing.Literal.)r-   ÚenumÚarray)r-   ÚitemszThe type hint ztyping.Ú a1   is a Tuple with a single element, which we do not automatically convert to JSON schema as it is rarely necessary. If this input can contain more than one element, we recommend using a list[] type instead, or if it really is a single element, remove the tuple[] wrapper and just pass the element directly..znConversion of '...' is not supported in Tuple type hints. Use list[] types for variable-length inputs instead.)r-   ÚprefixItemsr5   é   ÚadditionalProperties)r   r   r>   ÚKeyErrorr   r   ÚhasattrÚtypesrA   r-   Ú_parse_type_hintÚlenÚallÚsortedr
   r6   r7   r8   r9   r<   ÚappendÚpopÚlistÚtupleÚreplaceÚdict)r?   ÚoriginÚargsÚtÚsubtypesÚreturn_dictrE   ÚLITERAL_TYPESÚ
args_typesÚargÚarg_typeÚouts               r%   rT   rT   b   sÆ  € Ü˜Ó€FÜ�D‹>€Dà�~ð	Ü(¨Ó.Ð.ð 
”5ŠœW¤U¨K×8Ñ8¸VÄuÇÁÒ=Vá15ÓM²¨AÄ$ÀtÃ*Ð9LÓ'Ô$ QÖ'±ˆÐMÜˆx‹=˜AÓà" 1™+‰KÜÑ`ÑW_Ó`×`Ñ`à!¤6É(Ó*SÊ(¸w°6¬?É(Ñ*SÓ#TÐU‰Kð # HÐ-ˆKÜ�‹:˜ÓØ&*ˆK˜
Ñ#ØÐà	”7Ò	œs 4›y¨1›}Üœe¤S¬$´°T³
Ð;ˆØˆ
ÛˆCÜ�C‹y Ó-Ü.Ð/pÓqÐqÜ,¬T°#«YÓ7×;Ñ;¸FÓCˆHØÓ#¨Õ(BØ×!Ñ! (Ö+ñ ô ),¨J«¸1Ó(<�J—N‘NÔ$Ä$ÀzÓBRÜ˜“Jñ
ð 	
ð
 
”4ŠÞØ˜GÐ$Ð$ð $Ô.>¸tÀA¹wÓ.GÑHÐHà	”5ŠÞØ˜GÐ$Ð$Üˆt‹9˜‹>Ü*Ø ¤ T£×!2Ñ!2°9¸bÓ!AÐ Bð C-ð -óð ð �$‹;Ü*ð#óð ð
  ÉdÓ0SÊdÈÔ1AÀ!Ö1DÉdÑ0SÑTÐTà	”4Šð �xÐ ˆÜˆt‹9˜‹>Ü*:¸4À¹7Ó*CˆCÐ&Ñ'Øˆ
ä
"Ð#lÐnrÓ
sÐsøôK ó 	Ü*ØYÐ[_óð ð	üò Nùò +Tùò\ 1Ts#   ›
J ÁJ0Á0J0Â7J5
ÉJ:ÊJ-Úfuncc                 óò  • [        U 5      n[        R                  " U 5      n[        U SS5      n[	        [        UR                  5      S 5      nUS;   a8  UR                  U   R                  [        R                  R                  :X  a  UnOS n/ nUR                  R                  5        HŠ  u  pxXu:X  a  M  UR                  [        R                  R                  :X  a  [        SUR                   SU 35      eUR                  [        R                  R                  :X  d  My  UR                  U5        MŒ     0 n	UR                  5        H  u  pzXu:X  a  M  [        U
5      X—'   M     SU	S.nU(       a  XkS'   U$ )	Nr   Ú	operation>   ÚclsÚselfz	Argument z$ is missing a type hint in function r5   )r-   Ú
propertiesÚrequired)r   ÚinspectÚ	signatureÚgetattrÚnextÚiterÚ
parametersÚ
annotationÚ	ParameterÚemptyrL   r   ÚnameÚdefaultrX   rT   )rh   Ú
type_hintsrp   Ú	func_nameÚfirst_param_nameÚimplicit_arg_namern   Ú
param_nameÚparamrm   r*   Úschemas               r%   Ú"_convert_type_hints_to_json_schemar�   ±   sT  € Ü Ó%€JÜ×!Ò! $Ó'€IÜ˜˜j¨+Ó6€Iô œD ×!5Ñ!5Ó6¸Ó=Ðà˜OÓ+Ø× Ñ Ð!1Ñ2×=Ñ=Ä×ARÑAR×AXÑAXÓXà,Ñà ÐØ€HØ&×1Ñ1×7Ñ7Ö9Ñˆ
ØÓ*ÙØ×Ñœw×0Ñ0×6Ñ6Ó6Ü*¨Y°u·z±z°lÐBfÐgpÐfqÐ+rÓsÐsØ�=‰=œG×-Ñ-×3Ñ3Õ3Ø�O‰O˜JÖ'ñ :ð €JØ",×"2Ñ"2Ö"4Ñˆ
ØÓ*ÙÜ!1°*Ó!=ˆ
Óñ #5ð
 ¨jÑ9€FÞØ%ˆzÑà€Mr$   Ú	docstringc           
      óÈ  • [         R                  U 5      n[        R                  U 5      n[        R                  U 5      nU(       a  UR	                  S5      R                  5       OSnU(       a  UR	                  S5      R                  5       OSnU(       a  UR	                  S5      R                  5       OSnUb™  SR                  UR                  S5       Vs/ s H  owR                  5       (       d  M  UPM     sn5      n[        R                  U5      nU V	s0 s H/  o™S   [        R                  " SSU	S   R                  5       5      _M1     n
n	O0 n
XJU4$ s  snf s  sn	f )zô
Parses a Google-style docstring to extract the function description,
argument descriptions, and return description.

Args:
    docstring (str): The docstring to parse.

Returns:
    The function description, arguments, and return description.
r   NÚ
r   z	\s*\n+\s*Ú )Údescription_reÚsearchÚargs_reÚ
returns_reÚgroupÚstripÚjoinÚsplitÚargs_split_reÚfindallÚreÚsub)r‚   Údescription_matchÚ
args_matchÚreturns_matchÚdescriptionÚdocstring_argsÚreturnsÚlineÚmatchesÚmatchÚ	args_dicts              r%   Úparse_google_format_docstringrœ   Õ   s)  € ô '×-Ñ-¨iÓ8ÐÜ—‘ 	Ó*€JÜ×%Ñ% iÓ0€Mö 9JÐ#×)Ñ)¨!Ó,×2Ñ2Ô4Èt€KÞ4>�Z×%Ñ% aÓ(×.Ñ.Ô0ÀD€NÞ0=ˆm×!Ñ! !Ó$×*Ñ*Ô,À4€Gð Ñ!ØŸ™°^×5IÑ5IÈ$Ô5OÓ#`Ò5O¨T×S]ÑS]×S_§DÑ5OÑ#`ÓaˆÜ×'Ñ'¨Ó7ˆÙX_Ó`ÒX_Èu˜1‘XœrŸvšv l°C¸¸q¹¿¹Ó9IÓJÒJÑX_ˆ	Ð`ˆ	àˆ	à 7Ð*Ð*ùò $aùâ`s   ÃEÃ5EÄ6Ec                 óÞ  • [         R                  " U 5      n[        U SS5      nU(       d  [        SU S35      eUR	                  5       n[        U5      u  p4n[        U 5      nUS   R                  SS5      =nb  Ub  XWS'   US   R                  5        HÀ  u  p‰X„;  a  [        SU S	U S
35      eXH   n
[        R                  " SU
[        R                  S9nU(       aq  [        R                  " UR                  S5      5       Vs/ s H  oÌR	                  5       PM     snU	S'   UR                  SUR!                  5        R	                  5       n
X©S'   MÂ     X#US.nUb  X}S'   SUS.$ s  snf )aÇ  
This function generates a JSON schema for a given function, based on its docstring and type hints. This is
mostly used for passing lists of tools to a chat template. The JSON schema contains the name and description of
the function, as well as the names, types and descriptions for each of its arguments. `get_json_schema()` requires
that the function has a docstring, and that each argument has a description in the docstring, in the standard
Google docstring format shown below. It also requires that all user-facing arguments have valid Python type hints.
When passing methods, implicit receiver arguments (`self` or `cls`) are ignored.

Although it is not required, a `Returns` block can also be added, which will be included in the schema. This is
optional because most chat templates ignore the return value of the function.

Args:
    func: The function to generate a JSON schema for.

Returns:
    A dictionary containing the JSON schema for the function.

Examples:
```python
>>> def multiply(x: float, y: float):
>>>    '''
>>>    A function that multiplies two numbers
>>>
>>>    Args:
>>>        x: The first number to multiply
>>>        y: The second number to multiply
>>>    '''
>>>    return x * y
>>>
>>> print(get_json_schema(multiply))
{
    "name": "multiply",
    "description": "A function that multiplies two numbers",
    "parameters": {
        "type": "object",
        "properties": {
            "x": {"type": "number", "description": "The first number to multiply"},
            "y": {"type": "number", "description": "The second number to multiply"}
        },
        "required": ["x", "y"]
    }
}
```

The general use for these schemas is that they are used to generate tool descriptions for chat templates that
support them, like so:

```python
>>> from transformers import AutoTokenizer
>>> from transformers.utils import get_json_schema
>>>
>>> def multiply(x: float, y: float):
>>>    '''
>>>    A function that multiplies two numbers
>>>
>>>    Args:
>>>        x: The first number to multiply
>>>        y: The second number to multiply
>>>    return x * y
>>>    '''
>>>
>>> multiply_schema = get_json_schema(multiply)
>>> tokenizer = AutoTokenizer.from_pretrained("CohereForAI/c4ai-command-r-v01")
>>> messages = [{"role": "user", "content": "What is 179 x 4571?"}]
>>> formatted_chat = tokenizer.apply_chat_template(
>>>     messages,
>>>     tools=[multiply_schema],
>>>     chat_template="tool_use",
>>>     return_dict=True,
>>>     return_tensors="pt",
>>>     add_generation_prompt=True
>>> )
>>> # The formatted chat can now be passed to model.generate()
```

Each argument description can also have an optional `(choices: ...)` block at the end, such as
`(choices: ["tea", "coffee"])`, which will be parsed into an `enum` field in the schema. Note that this will
only be parsed correctly if it is at the end of the line:

```python
>>> def drink_beverage(beverage: str):
>>>    '''
>>>    A function that drinks a beverage
>>>
>>>    Args:
>>>        beverage: The beverage to drink (choices: ["tea", "coffee"])
>>>    '''
>>>    pass
>>>
>>> print(get_json_schema(drink_beverage))
```
{
    'name': 'drink_beverage',
    'description': 'A function that drinks a beverage',
    'parameters': {
        'type': 'object',
        'properties': {
            'beverage': {
                'type': 'string',
                'enum': ['tea', 'coffee'],
                'description': 'The beverage to drink'
                }
            },
        'required': ['beverage']
    }
}
r   rj   z Cannot generate JSON schema for z because it has no docstring!rm   r+   Nr•   z< because the docstring has no description for the argument 'Ú'z\(choices:\s*(.*?)\)\s*$)Úflagsr   rJ   )rx   r•   rt   Úfunction)r-   r    )ro   Úgetdocrq   r(   r‹   rœ   r�   rY   rL   r�   r‡   Ú
IGNORECASEÚjsonÚloadsrŠ   r0   Ústart)rh   Údocr{   Úmain_docÚparam_descriptionsÚ
return_docÚjson_schemarb   re   r€   ÚdescÚenum_choicesÚcÚoutputs                 r%   Úget_json_schemar¯   ö   s}  € ôX �.Š.˜Ó
€CÜ˜˜j¨+Ó6€IæÜ'Ð*JÈ9È+ÐUrÐ(sÓtÐtØ
�)‰)‹+€CÜ/LÈSÓ/QÑ,€H *ä4°TÓ:€KØ" <Ñ0×4Ñ4°X¸tÓDÐDˆÑQØÑ!Ø)3˜Ñ&Ø" <Ñ0×6Ñ6Ö8‰ˆØÓ(Ü+Ø2°9°+Ð=yÐz}Ðy~Ð~ð  Aóð ð "Ñ&ˆÜ—y’yÐ!<¸dÌ"Ï-É-ÑXˆÞÜ15·²¸L×<NÑ<NÈqÓ<QÔ1RÓSÒ1R¨AŸg™gžiÑ1RÑSˆF�6‰NØ×&Ñ&Ð'=¨×);Ñ);Ó)=Ð>×DÑDÓFˆDØ $ˆ}Óñ 9ð  ÈÑT€FØÑØ&ˆxÑØ¨FÑ3Ð3ùò Ts   ÄE*Úchat_templatec                 ó¸   • U c
  [        5       $ [        U 5      nUR                  R                  U 5      n[        [        R
                  R                  U5      5      $ )aO  Return the set of undeclared variables referenced by a chat template.

Uses ``jinja2.meta.find_undeclared_variables`` so that callers can
automatically distinguish template-level kwargs from processor kwargs
without maintaining a manual allowlist. Needed only to support BC as we
allowed all `kwargs` to be merged into one in the past
)Ú	frozensetÚ_compile_jinja_templateÚenvironmentÚparseÚjinja2ÚmetaÚfind_undeclared_variables)r°   ÚcompiledÚasts      r%   Ú_get_template_variablesr»   €  sL   € ð ÑÜ‹{ÐÜ& }Ó5€HØ
×
Ñ
×
$Ñ
$ ]Ó
3€CÜ”V—[‘[×:Ñ:¸3Ó?Ó@Ð@r$   c           	      ó   • / n/ nU R                   R                  Xg5         U R                  " SUUUUS.UD6 H  nUR                  U5        M     SR	                  U5      n	S S S 5        X—4$ ! , (       d  f       W	U4$ = f)N©ÚmessagesÚtoolsÚ	documentsÚadd_generation_promptrM   r   )r´   Úactivate_trackerÚgeneraterX   rŒ   )
Úcompiled_templater¾   r¿   rÀ   rÁ   Útemplate_kwargsÚrendered_blocksÚgeneration_indicesÚblockÚrendered_chats
             r%   Ú_render_with_assistant_indicesrÊ   ‘  sœ   € ð €OØÐØ	×	&Ñ	&×	7Ñ	7¸Õ	\Ø&×/Ò/ð 
ØØØØ"7ñ	
ð
 ô
ˆEð ×"Ñ" 5Ö)ñ
ð Ÿ™ Ó0ˆ÷ 
]ð Ð,Ð,÷ 
]Ô	\ð Ð,Ð,Ð,ús    AA,Á,
A=c                 ó   • [        U 5      $ ©N)Ú_cached_compile_jinja_template)r°   s    r%   r³   r³   £  s   € ä)¨-Ó8Ð8r$   c                 óÜ  • [        5       (       d  [        S5      e " S S[        5      n[        R                  " [
        R                  5      [        R                  " S5      :  a  [        S[
        R                   S35      eS nSS jnS	 n[        S
S
U[
        R                  R                  /S9nX5R                  S'   X%R                  S'   XER                  S'   UR                  U 5      $ )Nzbapply_chat_template requires jinja2 to be installed. Please install it using `pip install jinja2`.c                   óR  ^ • \ rS rSrS1rS\4U 4S jjrS\R                  R                  S\R                  R                  4S jr\R                  S\R                  R                  S	\R                  R                   S\4S
 j5       rS\4S jr\S\\   S\\   4S j5       rSrU =r$ )Ú8_cached_compile_jinja_template.<locals>.AssistantTrackeri¯  Ú
generationr´   c                 ór   >• [         TU ]  U5        UR                  U R                  S9  S U l        S U l        g )N)rÂ   )ÚsuperÚ__init__ÚextendrÂ   Ú_rendered_blocksÚ_generation_indices)rl   r´   Ú	__class__s     €r%   rÔ   ÚA_cached_compile_jinja_template.<locals>.AssistantTracker.__init__³  s7   ø€ ä‰GÑ˜[Ô)Ø×Ñ°×0EÑ0EÐÑFØ$(ˆDÔ!Ø'+ˆDÕ$r$   Úparserr+   c                 óâ   • [        UR                  5      R                  nUR                  S/SS9n[        R
                  R                  U R                  S5      / / U5      R                  U5      $ )Nzname:endgenerationT)Údrop_needleÚ_generation_support)	rr   ÚstreamÚlinenoÚparse_statementsr¶   ÚnodesÚ	CallBlockÚcall_methodÚ
set_lineno)rl   rÚ   rß   Úbodys       r%   rµ   Ú>_cached_compile_jinja_template.<locals>.AssistantTracker.parseº  se   € Ü˜&Ÿ-™-Ó(×/Ñ/ˆFØ×*Ñ*Ð,@Ð+AÈtÐ*ÐTˆDÜ—<‘<×)Ñ)¨$×*:Ñ*:Ð;PÓ*QÐSUÐWYÐ[_Ó`×kÑkÐlrÓsÐsr$   ÚcontextÚcallerc                 óÚ   • U" 5       nU R                  5       (       aN  [        SR                  U R                  5      5      nU[        U5      -   nU R                  R                  XE45        U$ )NrM   )Ú	is_activerU   rŒ   rÖ   r×   rX   )rl   rç   rè   ÚrvÚstart_indexÚ	end_indexs         r%   rÝ   ÚL_cached_compile_jinja_template.<locals>.AssistantTracker._generation_support¿  sY   € á“ˆBØ�~‰~×Ñä! "§'¡'¨$×*?Ñ*?Ó"@ÓA�Ø'¬#¨b«'Ñ1�	Ø×(Ñ(×/Ñ/°Ð0HÔIØˆIr$   c                 óH   • U R                   S L=(       d    U R                  S L$ rÌ   )rÖ   r×   )rl   s    r%   rê   ÚB_cached_compile_jinja_template.<locals>.AssistantTracker.is_activeÉ  s$   € Ø×(Ñ(°Ð4×\¸×8PÑ8PÐX\Ð8\Ð\r$   rÆ   rÇ   c              3   ó°   #   •  U R                  5       (       a  [        S5      eXl        X l        S v •  S U l        S U l        g ! S U l        S U l        f = f7f)Nz3AssistantTracker should not be reused before closed)rê   Ú
ValueErrorrÖ   r×   )rl   rÆ   rÇ   s      r%   rÂ   ÚI_cached_compile_jinja_template.<locals>.AssistantTracker.activate_trackerÌ  sU   é € ð	0Ø—>‘>×#Ñ#Ü$Ð%ZÓ[Ð[Ø(7Ô%Ø+=Ô(ãà(,�Ô%Ø+/�Õ(øð )-�Ô%Ø+/�Õ(üs   ‚A„0A ´AÁAÁA)r×   rÖ   )r   r   r    r!   Útagsr   rÔ   r¶   rÚ   ÚParserrá   râ   rµ   Úpass_eval_contextÚEvalContextÚruntimeÚMacror8   rÝ   r9   rê   r   rZ   r6   rÂ   r#   Ú__classcell__)rØ   s   @r%   ÚAssistantTrackerrÐ   ¯  sÃ   ø† àˆ~ˆð	,Ð(E÷ 	,ð	t §¡× 4Ñ 4ð 	t¸¿¹×9OÑ9Oô 	tð
 
×	!Ñ	!ð	¨v¯|©|×/GÑ/Gð 	ÐQW×Q_ÑQ_×QeÑQeð 	Ðjmó 	ó 
"ð	ð	]˜tô 	]ð 
ð
	0°D¸±Ið 
	0ÐSWÐX[ÑS\ó 
	0ó 
ö
	0r$   rû   z3.1.0zLapply_chat_template requires jinja2>=3.1.0 to be installed. Your version is Ú.c                 ó@   • [         R                  R                  U 5      erÌ   )r¶   Ú
exceptionsÚTemplateError©Úmessages    r%   Úraise_exceptionÚ7_cached_compile_jinja_template.<locals>.raise_exceptionÞ  s   € Ü×Ñ×-Ñ-¨gÓ6Ð6r$   c                 ó.   • [         R                  " XX#US9$ )N)Úensure_asciiÚindentÚ
separatorsÚ	sort_keys)r£   Údumps)Úxr  r  r  r  s        r%   ÚtojsonÚ._cached_compile_jinja_template.<locals>.tojsoná  s   € ô �zŠz˜!¸vÐhqÑrÐrr$   c                 óJ   • [         R                  " 5       R                  U 5      $ rÌ   )r   ÚnowÚstrftime)Úformats    r%   Ústrftime_nowÚ4_cached_compile_jinja_template.<locals>.strftime_nowæ  s   € Ü�|Š|‹~×&Ñ& vÓ.Ð.r$   T)Útrim_blocksÚlstrip_blocksÚ
extensionsr  r  r  )FNNF)r   ÚImportErrorr   r   rµ   r¶   Ú__version__r   ÚextÚloopcontrolsÚfiltersÚglobalsÚfrom_string)r°   rû   r  r  r  Ú	jinja_envs         r%   rÍ   rÍ   ¨  sÝ   € ä×ÑÜØpó
ð 	
ô(0œ9ô (0ôT ‡}‚}”V×'Ñ'Ó(¬7¯=ª=¸Ó+AÓAÜØZÔ[a×[mÑ[mÐZnÐnoÐpó
ð 	
ò7ôsò
/ô .Ø¨Ð:JÌFÏJÉJ×LcÑLcÐ9dñ€Ið #)×Ñ�hÑØ+:×ÑÐ'Ñ(Ø(4×Ñ�nÑ%Ø× Ñ  Ó/Ð/r$   Úconversationsr¿   rÀ   Úreturn_assistant_tokens_maskÚcontinue_final_messagerÁ   c           
      ó6  • U(       a1  [         R                  " SU5      (       d  [        R                  S5        [	        U5      nUb„  / n	U H{  n
[        U
[        5      (       a  U	R                  U
5        M+  [        U
5      (       d  [        R                  " U
5      (       a  U	R                  [        U
5      5        Mr  [        S5      e   OS n	Ub)  U H#  n[        U[        5      (       a  M  [        S5      e   / n/ nSnU  GH’  n[        US5      (       a  UR                  nU(       aŽ  [!        U5      nUS   R#                  S5      nUc  [        S	5      e[        U[$        [&        45      (       a5  [)        U5       H  nS
U;   d  M  US
   nUS
   U-   US
'     O   [        S5      eUS   S   U-   US   S'   U(       a&  [+        SUUU	UUS.UD6u  nnUR                  U5        OUR,                  " SUU	UUS.UD6nU(       a€  WR/                  5       U;  d  UR/                  5       U;  a  [        S5      eUR1                  UR/                  5       5      nUUU[3        U5      -    U:X  a  US U nOUS U R5                  5       nUR                  U5        GM•     XÍ4$ )Nz\{\%-?\s*generation\s*-?\%\}zareturn_assistant_tokens_mask==True but chat template does not contain `{% generation %}` keyword.z†Tools should either be a JSON schema, or a callable function with type hints and a docstring suitable for auto-conversion to a schema.zADocuments should be a list of dicts with 'title' and 'text' keys!zCONTINUE_FINAL_MESSAGE_TAG r¾   éÿÿÿÿÚcontentzOcontinue_final_message is set but the final message has no content to continue!Útextz^continue_final_message is set but we could not find any text to continue in the final message!)rÄ   r¾   r¿   rÀ   rÁ   r½   a  continue_final_message is set but the final message does not appear in the chat after applying the chat template! This can happen if the chat template deletes portions of the final message. Please verify the chat template and final message in your chat to ensure they are compatible.r   )r�   r‡   ÚloggerÚwarning_oncer³   rC   r]   rX   r   ro   Úismethodr¯   rò   Ú	TypeErrorrR   r¾   r   r<   rZ   r[   ÚreversedrÊ   Úrenderr‹   ÚrindexrU   Úrstrip)r  r¿   rÀ   r°   r  r   rÁ   ÚkwargsrÄ   Útool_schemasÚtoolÚdocumentÚrenderedÚall_generation_indicesÚcontinue_final_message_tagÚchatÚfinal_messageÚcontent_blockrÉ   rÇ   Útag_locs                        r%   Úrender_jinja_templater8  ò  sÑ  € ö $¬B¯IªIÐ6UÐWd×,eÑ,eÜ×ÑØoô	
ô
 0°Ó>Ðð ÑØˆÛˆDÜ˜$¤×%Ñ%Ø×#Ñ# DÖ)Ü˜D×!Ñ!¤W×%5Ò%5°d×%;Ñ%;Ø×#Ñ#¤O°DÓ$9Ö:ä ðPóð ò ð ˆàÑÛ!ˆHÜ˜h¬×-Ó-ÜÐ cÓdÐdñ "ð €HØÐØ!>ÐÜˆÜ�4˜×$Ñ$à—=‘=ˆDÞ!Ü˜D“>ˆDØ  ™HŸL™L¨Ó3ˆMØÑ$Ü Ð!rÓsÐsÜ˜M¬D´%¨=×9Ñ9Ü%-¨mÖ%<�MØ Õ.à(5°fÑ(=˜Ø0=¸fÑ0EÐHbÑ0b˜ fÑ-Ùñ &=ô %Øxóð ð '+¨2¡h¨yÑ&9Ð<VÑ&V��R‘˜Ñ#Þ'Ü0Nð 1Ø"3ØØ"Ø#Ø&;ñ1ð ñ1Ñ-ˆMÐ-ð #×)Ñ)Ð*<Õ=à-×4Ò4ð ØØ"Ø#Ø&;ñ	ð
 ñˆMö "Ø×#Ñ#Ó%¨]Ó:Ø*×0Ñ0Ó2¸-ÓGä ð2óð ð $×*Ñ*Ð+E×+KÑ+KÓ+MÓNˆGØ˜W w´Ð5OÓ1PÑ'PÐQÐUoÓoà -¨h¨wÐ 7‘ð !.¨h¨wÐ 7× >Ñ >Ó @�Ø�‰˜×&ñs ðv Ð+Ð+r$   c                 óJ   • [        U [        5      (       d  gSU ;   a  SU ;   d  gg)z^
Check that input is a valid message in a chat, namely a dict with "role" and "content" keys.
FÚroler#  T)rC   r]   r   s    r%   Úis_valid_messager;  Z  s)   € ô �gœt×$Ñ$ØØ�gÓ )¨wÓ"6ØØr$   c                   ó&   • \ rS rSrSrS\4S jrSrg)ÚChatie  a  This class is intended to just be used internally for pipelines and not exposed to users. We convert chats
to this format because the rest of the pipeline code tends to assume that lists of messages are
actually a batch of samples rather than messages in the same conversation.r¾   c                 óX   • U H  n[        U5      (       a  M  [        S5      e   Xl        g )NzQWhen passing chat dicts as input, each dict must have a 'role' and 'content' key.)r;  rò   r¾   )rl   r¾   r  s      r%   rÔ   ÚChat.__init__j  s*   € ÛˆGÜ# G×,Ó,Ü Ð!tÓuÐuñ  ð !�r$   )r¾   N)r   r   r    r!   r"   r]   rÔ   r#   r   r$   r%   r=  r=  e  s   † ñRð! ÷ !r$   r=  )NNNFFF)Mro   r£   r�   rS   Úcollections.abcr   Ú
contextlibr   Úcopyr   r   Ú	functoolsr   r   Útypingr	   r
   r   r   r   r   r   Ú	packagingr   rM   r   Úimport_utilsr   r   r   Ú
get_loggerr   r%  r¶   Újinja2.exceptionsÚ
jinja2.extÚjinja2.metaÚjinja2.nodesÚjinja2.runtimer   Újinja2.sandboxr   Ú	PIL.Imager   rZ   r]   r8   ÚChatTyper6   r7   r9   r-   ÚBASIC_TYPESÚcompileÚDOTALLr†   rˆ   ÚVERBOSErŽ   r‰   Ú	Exceptionr   r(   r>   rT   r�   r[   rœ   r¯   r²   r»   rÊ   r³   rÍ   r8  r;  r=  r   r$   r%   Ú<module>rU     s¹  ðó Û Û 	Û Ý $Ý %Ý Ý Ý Ý ß [× [Ñ [å å ß UÑ Uð 
×	Ò	˜HÓ	%€á×ÑÛÛÛÛÛÛÝ$Þ<à€Fá×ÑÝà��S˜#�X‘Ñ€ð �E˜3  c©4°«:°sÐ;€à—’ÐGÈÏÉÓS€à
�*Š*ÐHÈ"Ï)É)Ó
T€à—
’
ðð ‡I�I�—
‘
Ñó€ð �ZŠZÐEÀrÇyÁyÓQ€
ôU˜yô UôU 	ô Uð< dð <¨t°C¸°H©~ô <ð$Lt˜3ð Lt 4ô Ltð^!¨Xð !¸$ô !ðH+¨Sð +°U¸3À¹:ÀtÈdÁ{ÐTWÐZ^ÑT^Ð;^Ñ5_ô +ðBG4˜(ð G4 tô G4ðT ØðA¨3°©:ð A¸)ÀC¹.ó Aó ó ðAò-ð$ ñ9ó ð9ð ñF0ó ðF0ðV +/Ø!%Ø $Ø).Ø#(Ø"'ñe,Ø˜‘>ðe,à��x‘Ñ  4Ñ'ðe,ð ˜$‰ðe,ð ˜‘:ð	e,ð
 #'ðe,ð !ðe,ð  ðe,ð 	õe,òP÷	!ò 	!r$   