o
    پi                  
   @   s   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mZ d dlmZ d d	lmZ d d
lmZmZ ddlmZ ee dfde	eeef dedee defddZ e ee fde	eef defddZdS )    N)Enum)singledispatch)CallableOptionalUnion)build_regex_from_schema)	BaseModel)get_schema_from_enumget_schema_from_signature)SequenceGeneratorAdapter)OpenAI)Samplermultinomial   )regexschema_objectsamplerwhitespace_patternreturnc                    s  t  ttr"t  }t||}t| ||} fdd|_|S t  tt	rBtt
 }t||}t| ||}dd |_|S t r_tt }t||}t| ||}dd |_|S t  trx }t||}t| ||}dd |_|S td  dd d	 )
aj  
    Generate structured JSON data with a `Transformer` model based on a specified JSON Schema.

    Parameters
    ----------
    model:
        An instance of `Transformer` that represents a model from the
        `transformers` library.
    schema_object:
        The JSON Schema to generate data for. Can be a JSON string, a Pydantic model, or a callable
        that returns a JSON schema.
    sampler:
        The sampling algorithm to use to generate token ids from the logits
        distribution.
    whitespace_pattern
        Pattern to use for JSON syntactic whitespace (doesn't impact string literals)
        Example: allow only a single space or newline with `whitespace_pattern=r"[
 ]?"`

    Returns
    -------
    A `SequenceGenerator` instance that generates text constrained by the schema_object and
    transforms the result if BaseModel is used.

    c                    
     | S N	parse_rawxr    J/home/ubuntu/.local/lib/python3.10/site-packages/outlines/generate/json.py<lambda>4      
 zjson.<locals>.<lambda>c                 S   
   t | S r   pyjsonloadsr   r   r   r   r   9   r   c                 S   r    r   r!   r   r   r   r   r   >   r   c                 S   r    r   r!   r   r   r   r   r   C   r   Cannot parse schema . The schema must be either Aa Pydantic object, a function or a string that contains the JSON Schema specification)
isinstancetyper   r"   dumpsmodel_json_schemar   r   format_sequencer   r	   callabler
   str
ValueError)modelr   r   r   schema	regex_str	generatorr   r   r   json   s>   








r4   c                    s   t |ts	tdt  ttrt  } fdd}nt  tr* }dd }nt	d  dd d | j
d	d
dt|ddd}||_|S )NzZThe OpenAI API does not support any other sampling algorithm than the multinomial sampler.c                    r   r   r   r   r   r   r   r   Z   r   zjson_openai.<locals>.<lambda>c                 S   r    r   r!   r   r   r   r   r   ]   r   r$   r%   r&   r'   json_schemadefaultT)namestrictr1   )r)   r5   )response_format)r(   r   NotImplementedErrorr)   r   r"   r*   r+   r.   r/   new_with_replacementsr#   r,   )r0   r   r   r1   r,   r3   r   r   r   json_openaiN   s6   



r<   )r4   r"   enumr   	functoolsr   typingr   r   r   outlines_core.fsm.json_schemar   pydanticr   outlines.fsm.json_schemar	   r
   outlines.generate.apir   outlines.modelsr   outlines.samplersr   r   r   r.   objectregisterr<   r   r   r   r   <module>   s<    <
