a
    bg}                     @   sd   d dl mZ d dlmZmZmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ G dd deZdS )	    )partial)AnyDictListOptionalSequence)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)pre_initc                   @   s   e Zd ZU dZeed< eed< dZee ed< dZ	ee ed< dZ
eeeef  ed< dZee ed< eeeef d	d
dZeed	ddZeeedddZdeeee  ee eedddZdeeee  ee eedddZdS )CTransformersa_  C Transformers LLM models.

    To use, you should have the ``ctransformers`` python package installed.
    See https://github.com/marella/ctransformers

    Example:
        .. code-block:: python

            from langchain_community.llms import CTransformers

            llm = CTransformers(model="/path/to/ggml-gpt-2.bin", model_type="gpt2")
    clientmodelN
model_type
model_fileconfiglib)returnc                 C   s   | j | j| j| jdS )zGet the identifying parameters.r   r   r   r   r   self r   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/ctransformers.py_identifying_params-   s
    z!CTransformers._identifying_paramsc                 C   s   dS )zReturn type of llm.ctransformersr   r   r   r   r   	_llm_type7   s    zCTransformers._llm_type)valuesr   c                 C   sj   zddl m} W n ty*   tdY n0 |d p6i }|j|d f|d |d |d d	||d
< |S )z5Validate that ``ctransformers`` package is installed.r   )AutoModelForCausalLMz\Could not import `ctransformers` package. Please install it with `pip install ctransformers`r   r   r   r   r   )r   r   r   r   )r   r   ImportErrorZfrom_pretrained)clsr   r   r   r   r   r   validate_environment<   s"    

z"CTransformers.validate_environment)promptstoprun_managerkwargsr   c                 K   sJ   g }|pt  }| j||ddD ]}|| |j|| jd q d|S )aV  Generate text from a prompt.

        Args:
            prompt: The prompt to generate text from.
            stop: A list of sequences to stop generation when encountered.

        Returns:
            The generated text.

        Example:
            .. code-block:: python

                response = llm.invoke("Tell me a joke.")
        Tr"   streamverbose )r	   Zget_noop_managerr   appendon_llm_new_tokenr(   join)r   r!   r"   r#   r$   textZ_run_managerchunkr   r   r   _callQ   s    
zCTransformers._callc                    sP   d}|rt |j| jd}d}| j||ddD ]}|rB||I dH  ||7 }q,|S )a  Asynchronous Call out to CTransformers generate method.
        Very helpful when streaming (like with websockets!)

        Args:
            prompt: The prompt to pass into the model.
            stop: A list of strings to stop generation when encountered.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python
                response = llm.invoke("Once upon a time, ")
        Nr'   r)   Tr%   )r   r+   r(   r   )r   r!   r"   r#   r$   Ztext_callbackr-   tokenr   r   r   _acallm   s    
zCTransformers._acall)NN)NN)__name__
__module____qualname____doc__r   __annotations__strr   r   r   r   r   r   propertyr   r   r   r    r   r	   r/   r   r   r1   r   r   r   r   r      s>   
	  
  
r   N)	functoolsr   typingr   r   r   r   r   Zlangchain_core.callbacksr   r	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.utilsr   r   r   r   r   r   <module>   s
   