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   úL/home/ubuntu/.local/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   Úformat_sequence)Úmodelr   r   Ú	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$   Ú
propertiesÚ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   Úchoices_schemar5   r   r4   r   Úchoice_openai%   s   	üÿr7   )r   r   r   r.   r   Ú	functoolsr   Útypingr   r   r   Úoutlines_core.fsm.json_schemar   Úoutlines.fsm.json_schemar   Úoutlines.generate.apir	   Úoutlines.modelsr
   Úoutlines.samplersr   r   r   Ústrr$   r   Úregisterr7   r   r   r   r   Ú<module>   s@    ÿÿÿþÿÿÿÿþ