o
    پi                     @   s6   d dl Z d dlmZ d dlZd dlZG dd dZdS )    N)ThreadPoolExecutorc                   @   s`   e Zd Zeddddddiddd	gd
dZ	dddZdd Zdd Zdd Z	dd Z
dS )TestJSONConstrainedMixinobjectstringz^[\w]+$)typepatternr   integer)name
populationr	   r
   F)r   
propertiesrequiredadditionalPropertiesr      c              	   C   s   t j| jd dt| |dkrdndd|dg|dd	||dd
d}| }tt| td |r8|dkr:d S z	t|d }W n ttjj	fyO    w | 
|d t | 
|d t d S )Nz	/generateNIntroduce the capital of France. Return in a JSON format. The JSON Schema is: r   r   g      ?   i )temperaturemax_new_tokensnstop_token_idsjson_schemaF)textsampling_paramsstreamreturn_logprobtop_logprobs_numlogprob_start_len)jsonzd====================================================================================================INVALIDr   r	   r
   )requestspostbase_urlr   dumpsprintloads	TypeErrordecoderJSONDecodeErrorassertIsInstancestrint)selfr   r   r   r   responseretjs_obj r.   Y/home/ubuntu/.local/lib/python3.10/site-packages/sglang/test/kits/json_constrained_kit.py_run_decode_json   s:   z)TestJSONConstrainedMixin._run_decode_jsonc                 C   s   | j | jd d S )Nr   )r0   r   r*   r.   r.   r/   test_json_generate<   s   z+TestJSONConstrainedMixin.test_json_generatec                 C   s   | j dd d S )Nr   r1   )r0   r2   r.   r.   r/   test_json_invalid?   s   z*TestJSONConstrainedMixin.test_json_invalidc              
   C   s   t jd| j dd}|jjj| jdddddt| j	 dgd	d
ddt
| j	ddd}|jd	 jj}zt
|}W n ttjjfyO   td|  w | |d t | |d t d S )NEMPTYz/v1)api_keyr    systemzYou are a helpful AI assistant)rolecontentuserr   r   r   r   foo)r	   schema)r   r   )modelmessagesr   
max_tokensresponse_formatr&   r	   r
   )openaiClientr    chatcompletionscreater=   r   r!   r   r#   choicesmessager9   r$   r%   r&   r"   r'   r(   r)   )r*   clientr+   r   r-   r.   r.   r/   test_json_openaiB   s2   

z)TestJSONConstrainedMixin.test_json_openaic                 C   sX   d d | j | j gd }tt|}t|| j| W d    d S 1 s%w   Y  d S )N
   )r   r   lenlistmapr0   )r*   json_schemasexecutorr.   r.   r/   test_mix_json_and_othera   s   "z0TestJSONConstrainedMixin.test_mix_json_and_otherN)Fr   r   )__name__
__module____qualname__r   r!   r   r0   r3   r4   rI   rP   r.   r.   r.   r/   r      s     
'r   )r   concurrent.futuresr   rA   r   r   r.   r.   r.   r/   <module>   s
    