o
    پi                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dlZd dlm	Z	m
Z
 d dlmZmZmZmZ daG dd de
Zedd	d
 Zddee fddZdd Zdd Zejdd ZdS )    N)CallableOptional)CacheDisk)ENOVALUNKNOWNargs_to_key	full_nameTc                       sR   e Zd Zd fdd	Z fddZ fddZef fdd		Z fd
dZ  Z	S )CloudpickleDisk   c                    s   || _ t j|fi | d S N)compress_levelsuper__init__)self	directoryr   kwargs	__class__ D/home/ubuntu/.local/lib/python3.10/site-packages/outlines/caching.pyr      s   zCloudpickleDisk.__init__c                    s   t |}t |S r   )cloudpickledumpsr   put)r   keydatar   r   r   r      s   
zCloudpickleDisk.putc                    s   t  ||}t|S r   )r   getr   loads)r   r   rawr   r   r   r   r      s   
zCloudpickleDisk.getc                    s    |st |}t j|||dS )N)r   )r   r   r   store)r   valuereadr   r   r   r   r      s   
zCloudpickleDisk.storec                    s$   t  ||||}|st|}|S r   )r   fetchr   r   )r   modefilenamer    r!   r   r   r   r   r"       s   
zCloudpickleDisk.fetch)r   )
__name__
__module____qualname__r   r   r   r   r   r"   __classcell__r   r   r   r   r
      s    r
   r   c                  C   s^   ddl m}  tjd}tjd| d}t|ddtd}| |dkr)|	  | |d< |S )	a  Get the context object that contains previously-computed return values.

    The cache is used to avoid unnecessary computations and API calls, which can
    be long and expensive for large models.

    The cache directory defaults to `HOMEDIR/.cache/outlines`, but this choice
    can be overridden by the user by setting the value of the `OUTLINES_CACHE_DIR`
    environment variable.

    r   )__version__~OUTLINES_CACHE_DIRz/.cache/outlinesnone)eviction_policy
cull_limitdiskr)   )
outlines._versionr)   ospath
expanduserenvironr   r   r
   clear)outlines_versionhome_dir	cache_dirmemoryr   r   r   	get_cache'   s   r:   Fr   expirec                    s   dt f fdd}|S )a4  Caching decorator for memoizing function calls.

    The cache key is created based on the values returned by the key_function callable
    if provided or based on the arguments of the decorated function directly otherwise

    This is based on `diskcache`'s `memoize`.

    Parameters
    ----------
    expire
        Seconds until arguments expire.
    typed
        Cache different types separately.
    ignore
        Positional or keyword arguments to ignore.

    Returns
    -------
        A decorator function that can be applied to other functions.
    cached_functionc                    sb   t  }tf trfddnfdd fdd}|_|__S )Nc                     sp   t s | i |I d H S j| i |}jj|tdd}|tu r6 | i |I d H }jj||dd |S NT)defaultretry)r?   _caching_enabled__cache_key__
__memory__r   r   setargsr   	cache_keyresultr<   r;   wrapperr   r   rJ   c   s   z)cache.<locals>.decorator.<locals>.wrapperc                     sb   t s	 | i |S j| i |}jj|tdd}|tu r/ | i |}jj||dd |S r=   r@   rE   rI   r   r   rJ   r   s   c                     s   t  | |S )z,Make key for cache given function arguments.)r   )rF   r   )baseignoretypedr   r   rB      s   z/cache.<locals>.decorator.<locals>.__cache_key__)r:   r	   asyncioiscoroutinefunctionrB   rC   __wrapped__)r<   r9   rB   r;   rL   rM   )rK   r<   rJ   r   	decorator\   s   

zcache.<locals>.decorator)r   )r;   rM   rL   rR   r   rQ   r   cacheF   s   -rS   c                   C   s   da dS )au  Disable the cache for this session.

    Generative models output different results each time they are called when
    sampling. This can be a desirable property for some workflows, in which case
    one can call `outlines.call.disable` to disable the cache for the session.

    This function does not delete the cache, call `outlines.cache.clear`
    instead. It also does not overwrite the cache with the values returned
    during the session.

    Example
    -------

    `outlines.cache.disable` should be called right after importing outlines:

    >>> import outlines.caching as cache
    >>> cache.disable_cache()

    FNrA   r   r   r   r   disable_cache   s   rU   c                  C   s   t  } |   dS )zErase the cache completely.N)r:   r5   )r9   r   r   r   clear_cache   s   rV   c                  c   s"    t } da zd V  W | a d S | a w )NFrT   )original_stater   r   r   cache_disabled   s   rX   )NFr   )rN   
contextlib	functoolsr1   typingr   r   r   	diskcacher   r   diskcache.corer   r   r   r	   rA   r
   	lru_cacher:   floatrS   rU   rV   contextmanagerrX   r   r   r   r   <module>   s"    
F