o
    پi{                     @   s   d dl Zd dlmZ d dlmZmZmZmZm	Z	 d dl
Z
d dlmZmZ er2d dlmZ d dlmZ eG dd dZd	efd
dZdededee fddZdS )    N)	dataclass)TYPE_CHECKINGCallableOptionalTupleUnion)generatemodels)SequenceGenerator)Promptc                   @   sl   e Zd ZU dZded< eeeef ed< eed< dZ	e
d ed< edd
edefddZdd Zdd ZdS )Functiona)  Represents an Outlines function.

    Functions are a convenient way to encapsulate a prompt template, a language
    model and a Pydantic model that define the output structure. Once defined,
    the function can be called with arguments that will be used to render the
    prompt template.

    r   prompt_templateschema
model_nameNr
   	generatorfnprogram_pathfunction_namec                 C   s   t |}t||}|S )z Load a function stored on GitHub)download_from_githubextract_function_from_file)clsr   r   program_contentfunction r   E/home/ubuntu/.local/lib/python3.10/site-packages/outlines/function.pyfrom_github   s   
zFunction.from_githubc                 C   s    t | j}t|| j| _dS )z,Load the model and initialize the generator.N)r	   transformersr   r   jsonr   r   )selfmodelr   r   r   init_generator&   s   zFunction.init_generatorc                 O   s,   | j du r	|   | j|i |}|  |S )a>  Call the function.

        .. warning::

           This currently does not support batching.

        Parameters
        ----------
        args
            Values to pass to the prompt template as positional arguments.
        kwargs
            Values to pass to the prompt template as keyword arguments.

        N)r   r    r   )r   argskwargspromptr   r   r   __call__+   s   

zFunction.__call__)r   )__name__
__module____qualname____doc____annotations__r   strr   objectr   r   classmethodr   r    r$   r   r   r   r   r      s   
 	r   
short_pathc           	      C   s   d}d}|  d}t|dk rtd| dd dkrtd	|d
 }|d }|dd }d||||g| d }t|}|jdkrG|jS |jdkrTtd| d|  dS )z<Download the file in which the function is stored on GitHub.z!https://raw.githubusercontent.commain/   zNPlease provide a valid path in the form {USERNAME}/{REPO_NAME}/{PATH_TO_FILE}.Nz.pyz6Do not append the `.py` extension to the program name.r            i  zProgram could not be found at zf. Please make sure you entered the GitHub username, repository name and path to the program correctly.)	splitlen
ValueErrorjoinrequestsgetstatus_codetextraise_for_status)	r-   GITHUB_BASE_URLBRANCHpathusernamerepopath_to_fileurlresultr   r   r   r   A   s*   




r   contentr   returnc                 C   sz   t jjdddd}|dur;t j|}t| |j zt||}W n ty+   tdw t||j	j
s;td| d|S )z1Extract a function object from a downloaded file.outlines_functionNgithub)loaderoriginztCould not find an `outlines.Function` instance in the remote file. Make sure that the path you specified is correct.zThe `zD` variable in the program must be an instance of `outlines.Function`)	importlibutilspec_from_loadermodule_from_specexec__dict__getattrAttributeError
isinstanceoutlinesr   	TypeError)rF   r   specmoduler   r   r   r   r   _   s$   
r   )importlib.utilrL   dataclassesr   typingr   r   r   r   r   r9   rU   r   r	   outlines.generate.apir
   outlines.promptsr   r   r*   r   r   r   r   r   r   <module>   s    2