o
    پi                      @   s"   d dl Z d dlZG dd dZdS )    Nc                   @   sp   e Zd ZdZ			d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dS )TestEBNFConstrainedMixinzroot ::= "test"Fr      c              	   C   s   t j| jd ||dkrdndd||dd||ddd	}| }ttj|d
d td t|ts<| dt	|  |D ]-}	|	
dd }
|
sO| d d}|D ]}| |
|r_d} nqS|sk| d|
 d q>d S )Nz	/generater   r   g      ?   )temperaturemax_new_tokensnebnfF)textsampling_paramsstreamreturn_logprobtop_logprobs_numlogprob_start_len)json   )indentzd====================================================================================================z(Expected response to be a list, but got r	    zGenerated text is empty.TzText 'z-' does not match any of the allowed patterns.)requestspostbase_urlr   printdumps
isinstancelistfailtypegetstripregex_match)selfr   expected_patternspromptr   r   r   responseretitemr	   matchpattern r'   Y/home/ubuntu/.local/lib/python3.10/site-packages/sglang/test/kits/ebnf_constrained_kit.py_run_decode_ebnf	   sB   	

z)TestEBNFConstrainedMixin._run_decode_ebnfc                 C   s   dd l }|||d uS )Nr   )rer%   )r   r	   r&   r*   r'   r'   r(   r   7   s   z$TestEBNFConstrainedMixin.regex_matchc                 C   ,   d| j _dg}d}| j| j j||dd d S )Nzroot ::= "user@example.com"z^user@example\.com$zGenerate an email address:   r   r    r!   r   	__class__ebnf_grammarr)   r   allowed_patternsr!   r'   r'   r(   test_ebnf_generate_email<      
z1TestEBNFConstrainedMixin.test_ebnf_generate_emailc                 C   r+   )Nzroot ::= "Hello" | "Hi" | "Hey"z^(Hello|Hi|Hey)$zGenerate a greeting:r,   r-   r.   r1   r'   r'   r(   test_ebnf_generate_greetingH   r4   z4TestEBNFConstrainedMixin.test_ebnf_generate_greetingc                 C   r+   )NzD
        root ::= digit digit digit
        digit ::= [0-9]
        z^\d{3}$zGenerate a three-digit number:r,   r-   r.   r1   r'   r'   r(   test_ebnf_generate_numberT      
z2TestEBNFConstrainedMixin.test_ebnf_generate_numberc                 C   r+   )Nz
        root ::= "(" area ")" " " prefix "-" line
        area ::= [0-9] [0-9] [0-9]
        prefix ::= [0-9] [0-9] [0-9]
        line ::= [0-9] [0-9] [0-9] [0-9]
        z^\(\d{3}\) \d{3}-\d{4}$zGenerate a phone number:r,   r-   r.   r1   r'   r'   r(   test_ebnf_generate_phonec   s   
z1TestEBNFConstrainedMixin.test_ebnf_generate_phonec                 C   r+   )Na  
        root ::= year "-" month "-" day
        year ::= "2024"
        month ::= "01" | "02" | "03" | "04" | "05" | "06" | "07" | "08" | "09" | "10" | "11" | "12"
        day ::= "01" | "02" | "03" | "04" | "05" | "06" | "07" | "08" | "09" | "10" |
               "11" | "12" | "13" | "14" | "15" | "16" | "17" | "18" | "19" | "20" |
               "21" | "22" | "23" | "24" | "25" | "26" | "27" | "28" | "29" | "30" | "31"
        z,^2024-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$z%Generate a date in YYYY-MM-DD format:r,   r-   r.   r1   r'   r'   r(   test_ebnf_generate_datet   s   
z0TestEBNFConstrainedMixin.test_ebnf_generate_datec                 C   r+   )NzT
        root ::= "#" hex hex hex hex hex hex
        hex ::= [0-9] | [A-F]
        z^#[0-9A-F]{6}$zGenerate a hex color code:r,   r-   r.   r1   r'   r'   r(   test_ebnf_generate_hex_color   r7   z5TestEBNFConstrainedMixin.test_ebnf_generate_hex_colorc                 C   r+   )Nac  
        root ::= object
        object ::= "{" ws pair (ws "," ws pair)* ws "}"
        pair ::= "\"name\"" ws ":" ws value |
                 "\"age\"" ws ":" ws number |
                 "\"city\"" ws ":" ws string
        value ::= string | number
        string ::= "\"" [a-zA-Z0-9 ]+ "\""
        number ::= [1-9] [0-9]*
        ws ::= [ ]*
        zg^{\s*"name"\s*:\s*"[a-zA-Z0-9 ]+"\s*,\s*"age"\s*:\s*[1-9][0-9]*\s*,\s*"city"\s*:\s*"[a-zA-Z0-9 ]+"\s*}$z0Generate a simple JSON with name, age, and city:r,   r-   r.   r1   r'   r'   r(   test_ebnf_generate_complex_json   s   
z8TestEBNFConstrainedMixin.test_ebnf_generate_complex_jsonc                 C   r+   )Nz
        root ::= logentry
        logentry ::= "[" datetime "] " level ": System.process - " message
        datetime ::= "2024-01-01T12:00:00Z"
        level ::= "INFO"
        message ::= "Operation " [a-z]+ " successfully"
        zP^\[2024-01-01T12:00:00Z\] INFO: System\.process - Operation [a-z]+ successfully$zGenerate a log entry:r,   r-   r.   r1   r'   r'   r(   $test_ebnf_generate_custom_log_format   s   
z=TestEBNFConstrainedMixin.test_ebnf_generate_custom_log_formatc                 C   s.   d| j _g d}d}| j| j j||dd dS )zMTest function call with all optional parameters - verifies flexible ordering.a9  
        root ::= function_call
        function_call ::= call_config_service
        call_config_service ::= "{" "\"name\"" ":" "\"config_service\"" ", " "\"arguments\"" ":" arguments_config_service "}"
        arguments_config_service ::= "{" ( "\"theme\"" ":" ("\"light\"" | "\"dark\"") ( "," "\"language\"" ":" ("\"en\"" | "\"es\"" | "\"fr\"") )? ( "," "\"notifications\"" ":" ("true" | "false") )? | "\"language\"" ":" ("\"en\"" | "\"es\"" | "\"fr\"") ( "," "\"notifications\"" ":" ("true" | "false") )? | "\"notifications\"" ":" ("true" | "false") )? "}"
        )z1^\{"name":"config_service",\s*"arguments":\{\}\}$zG^\{"name":"config_service",\s*"arguments":\{"theme":"(light|dark)"\}\}$zH^\{"name":"config_service",\s*"arguments":\{"language":"(en|es|fr)"\}\}$zM^\{"name":"config_service",\s*"arguments":\{"notifications":(true|false)\}\}$zb^\{"name":"config_service",\s*"arguments":\{"theme":"(light|dark)",\s*"language":"(en|es|fr)"\}\}$zg^\{"name":"config_service",\s*"arguments":\{"theme":"(light|dark)",\s*"notifications":(true|false)\}\}$zh^\{"name":"config_service",\s*"arguments":\{"language":"(en|es|fr)",\s*"notifications":(true|false)\}\}$z^\{"name":"config_service",\s*"arguments":\{"theme":"(light|dark)",\s*"language":"(en|es|fr)",\s*"notifications":(true|false)\}\}$z-Configure the service with optional settings:   r-   Nr.   r1   r'   r'   r(   /test_ebnf_generate_all_optional_function_params   s   
zHTestEBNFConstrainedMixin.test_ebnf_generate_all_optional_function_paramsN)Fr   r   )__name__
__module____qualname__r0   r)   r   r3   r5   r6   r8   r9   r:   r;   r<   r>   r'   r'   r'   r(   r      s     
.r   )r   r   r   r'   r'   r'   r(   <module>   s    