o
    پi	                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZ deeeee
 f defddZd	edefd
dZdee defddZdS )    N)Enum)CallableTypeUnion)	BaseModelcreate_modeljson_schemareturnc                 C   s^   t | trt| }|S t | tr| }|S t| tr#t|  }|S td|  dd d )a.  Convert a JSON schema to a string.

    Parameters
    ----------
    json_schema
        The JSON schema.

    Returns
    -------
    str
        The JSON schema converted to a string.

    Raises
    ------
    ValueError
        If the schema is not a dictionary, a string or a Pydantic class.
    zCannot parse schema z. The schema must be either zBa Pydantic class, a dictionary or a string that contains the JSON zschema specification)	
isinstancedictjsondumpsstr
issubclassr   model_json_schema
ValueError)r   
schema_str r   L/home/ubuntu/.local/lib/python3.10/site-packages/outlines/fsm/json_schema.pyconvert_json_schema_to_str
   s    


	

r   fnc              
   C   s   t | }i }|j D ]\}}|jt jkrtd|jdf||< qz| j}W n tyE } zd}t	j
d| td W Y d}~nd}~ww t|fi |}| S )zTurn a function signature into a JSON schema.

    Every JSON object valid to the output JSON Schema can be passed
    to `fn` using the ** unpacking syntax.

    z)Each argument must have a type annotation.	ArgumentszwThe function name could not be determined. Using default name 'Arguments' instead. For debugging, here is exact error:
)categoryN)inspect	signature
parametersitems
annotation_emptyr   __name__	ExceptionwarningswarnUserWarningr   r   )r   r   	argumentsnameargfn_nameemodelr   r   r   get_schema_from_signature+   s$   

r*   myenumc                 C   s<   t | dkrtd| j ddd | D }| j|d}|S )Nr   zYour enum class z has 0 members. If you are working with an enum of functions, do not forget to register them as callable (using `partial` for instance)c                 S   s,   g | ]}t |jrt|jjnd |jiqS )const)callablevaluer*   func).0eltr   r   r   
<listcomp>L   s    z(get_schema_from_enum.<locals>.<listcomp>)titleoneOf)lenr   r   )r+   choicesschemar   r   r   get_schema_from_enumG   s   r8   )r   r   r!   enumr   typingr   r   r   pydanticr   r   r   r   r   r*   typer8   r   r   r   r   <module>   s     !