o
    +i%                     @  s   d 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 ddlmZ ddlmZ ee ZG d	d
 d
eZG dd deZdS )at  Optional caching layer for language models.

Distinct from provider-based [prompt caching](https://docs.langchain.com/oss/python/langchain/models#prompt-caching).

!!! warning "Beta feature"
    This is a beta feature. Please be wary of deploying experimental code to production
    unless you've taken appropriate precautions.

A cache is useful for two reasons:

1. It can save you money by reducing the number of API calls you make to the LLM
    provider if you're often requesting the same completion multiple times.
2. It can speed up your application by reducing the number of API calls you make to the
    LLM provider.
    )annotations)ABCabstractmethod)Sequence)Any)override)
Generation)run_in_executorc                   @  sX   e Zd ZdZedddZedddZedddZdddZdddZ	dddZ
dS )	BaseCacheaJ  Interface for a caching layer for LLMs and Chat models.

    The cache interface consists of the following methods:

    - lookup: Look up a value based on a prompt and `llm_string`.
    - update: Update the cache based on a prompt and `llm_string`.
    - clear: Clear the cache.

    In addition, the cache interface provides an async version of each method.

    The default implementation of the async methods is to run the synchronous
    method in an executor. It's recommended to override the async methods
    and provide async implementations to avoid unnecessary overhead.
    promptstr
llm_stringreturnRETURN_VAL_TYPE | Nonec                 C     dS )a  Look up based on `prompt` and `llm_string`.

        A cache implementation is expected to generate a key from the 2-tuple
        of `prompt` and `llm_string` (e.g., by concatenating them with a delimiter).

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.

                This is used to capture the invocation parameters of the LLM
                (e.g., model name, temperature, stop tokens, max tokens, etc.).

                These invocation parameters are serialized into a string representation.

        Returns:
            On a cache miss, return `None`. On a cache hit, return the cached value.
            The cached value is a list of `Generation` (or subclasses).
        N selfr   r   r   r   R/var/www/html/psymed-ai/venv/lib/python3.10/site-packages/langchain_core/caches.pylookup/       zBaseCache.lookup
return_valRETURN_VAL_TYPENonec                 C  r   )a]  Update cache based on `prompt` and `llm_string`.

        The prompt and llm_string are used to generate a key for the cache.
        The key should match that of the lookup method.

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.

                This is used to capture the invocation parameters of the LLM
                (e.g., model name, temperature, stop tokens, max tokens, etc.).

                These invocation parameters are serialized into a string
                representation.
            return_val: The value to be cached. The value is a list of `Generation`
                (or subclasses).
        Nr   r   r   r   r   r   r   r   updateF   r   zBaseCache.updatekwargsr   c                 K  r   )z7Clear cache that can take additional keyword arguments.Nr   r   r   r   r   r   clear\   r   zBaseCache.clearc                   s   t d| j||I dH S )a  Async look up based on `prompt` and `llm_string`.

        A cache implementation is expected to generate a key from the 2-tuple
        of `prompt` and `llm_string` (e.g., by concatenating them with a delimiter).

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.

                This is used to capture the invocation parameters of the LLM
                (e.g., model name, temperature, stop tokens, max tokens, etc.).

                These invocation parameters are serialized into a string
                representation.

        Returns:
            On a cache miss, return `None`. On a cache hit, return the cached value.
            The cached value is a list of `Generation` (or subclasses).
        N)r	   r   r   r   r   r   alookup`   s   zBaseCache.alookupc                   s   t d| j|||I dH S )ad  Async update cache based on `prompt` and `llm_string`.

        The prompt and llm_string are used to generate a key for the cache.
        The key should match that of the look up method.

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.

                This is used to capture the invocation parameters of the LLM
                (e.g., model name, temperature, stop tokens, max tokens, etc.).

                These invocation parameters are serialized into a string
                representation.
            return_val: The value to be cached. The value is a list of `Generation`
                (or subclasses).
        N)r	   r   r   r   r   r   aupdatex   s   zBaseCache.aupdatec                   s   t d| jfi |I dH S )z=Async clear cache that can take additional keyword arguments.N)r	   r   r   r   r   r   aclear   s   zBaseCache.aclearNr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r    r!   r   r   r   r   r
      s    

r
   c                   @  sd   e Zd ZdZddddd	ZdddZd ddZed!ddZdddZ	d ddZ
ed!ddZdS )"InMemoryCachez#Cache that stores things in memory.N)maxsizer*   
int | Noner   r   c                C  s,   i | _ |dur|dkrd}t||| _dS )a_  Initialize with empty cache.

        Args:
            maxsize: The maximum number of items to store in the cache.
                If `None`, the cache has no maximum size.
                If the cache exceeds the maximum size, the oldest items are removed.

        Raises:
            ValueError: If `maxsize` is less than or equal to `0`.
        Nr   zmaxsize must be greater than 0)_cache
ValueError_maxsize)r   r*   msgr   r   r   __init__   s
   
zInMemoryCache.__init__r   r   r   r   c                 C  s   | j ||fdS )a  Look up based on `prompt` and `llm_string`.

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.

        Returns:
            On a cache miss, return `None`. On a cache hit, return the cached value.
        N)r,   getr   r   r   r   r      s   zInMemoryCache.lookupr   r   c                 C  s>   | j durt| j| j kr| jtt| j= || j||f< dS )a  Update cache based on `prompt` and `llm_string`.

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.
            return_val: The value to be cached. The value is a list of `Generation`
                (or subclasses).
        N)r.   lenr,   nextiterr   r   r   r   r      s   zInMemoryCache.updater   r   c                 K  s
   i | _ dS )zClear cache.N)r,   r   r   r   r   r      s   
zInMemoryCache.clearc                   s   |  ||S )a  Async look up based on `prompt` and `llm_string`.

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.

        Returns:
            On a cache miss, return `None`. On a cache hit, return the cached value.
        )r   r   r   r   r   r      s   zInMemoryCache.alookupc                   s   |  ||| dS )a  Async update cache based on `prompt` and `llm_string`.

        Args:
            prompt: A string representation of the prompt.
                In the case of a chat model, the prompt is a non-trivial
                serialization of the prompt into the language model.
            llm_string: A string representation of the LLM configuration.
            return_val: The value to be cached. The value is a list of `Generation`
                (or subclasses).
        N)r   r   r   r   r   r       s   zInMemoryCache.aupdatec                   s   |    dS )zAsync clear cache.N)r   r   r   r   r   r!      s   zInMemoryCache.aclear)r*   r+   r   r   r"   r#   r$   )r%   r&   r'   r(   r0   r   r   r   r   r   r    r!   r   r   r   r   r)      s    



r)   N)r(   
__future__r   abcr   r   collections.abcr   typingr   typing_extensionsr   langchain_core.outputsr   langchain_core.runnablesr	   r   r
   r)   r   r   r   r   <module>   s    v