o
    ¡o iØ  ã                
   @   sö   d dl Zd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d
dl m Z  d
dlmZ eeƒ fde
e	e ee f dedefdd„ƒZe e¡eƒ fdede	e dedefdd„ƒZdS )é    N)ÚEnum)Úsingledispatch)ÚCallableÚListÚUnion)Úbuild_regex_from_schema)Úget_schema_from_enum)ÚSequenceGeneratorAdapter)ÚOpenAI)ÚSamplerÚmultinomialé   )Újson)ÚregexÚchoicesÚsamplerÚreturnc                 C   sx   t |ttƒƒrtt t|ƒ¡ƒ}ndd„ |D ƒ}dd |¡ d }t| ||ƒ}t |ttƒƒr5dd„ |_	|S dd„ |_	|S )	Nc                 S   s   g | ]}t  |¡‘qS © )ÚreÚescape)Ú.0Úchoicer   r   úd/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/outlines/generate/choice.pyÚ
<listcomp>   s    zchoice.<locals>.<listcomp>ú(ú|ú)c                 S   s
   t  | ¡S ©N)ÚpyjsonÚloads©Úxr   r   r   Ú<lambda>   s   
 zchoice.<locals>.<lambda>c                 S   s   | S r   r   r    r   r   r   r"       s    )
Ú
isinstanceÚtyper   r   r   Údumpsr   Újoinr   Zformat_sequence)Úmodelr   r   Z	regex_strÚ	generatorr   r   r   r      s   

þr   r'   c                    s<   t  ddd|dœiddgdœ¡}t| ||ƒ‰ ‡ fdd„}|S )	zZ
    Call OpenAI API with response_format of a dict:
    {"result": <one of choices>}
    ÚobjectÚresultÚstring)r$   ÚenumF)r$   Ú
propertiesZadditionalPropertiesÚrequiredc                     s   ˆ | i |¤Žd S )Nr*   r   )ÚargsÚkwargs©r(   r   r   Úgenerate_choice8   s   z&choice_openai.<locals>.generate_choice)r   r%   r   )r'   r   r   Zchoices_schemar2   r   r1   r   Úchoice_openai%   s   	üÿr3   )r   r   r   r,   r   Ú	functoolsr   Útypingr   r   r   Zoutlines_core.fsm.json_schemar   Zoutlines.fsm.json_schemar   Zoutlines.generate.apir	   Zoutlines.modelsr
   Zoutlines.samplersr   r   r   Ústrr$   r   Úregisterr3   r   r   r   r   Ú<module>   s@    ÿÿÿþÿÿÿÿþ