U
    h                     @   s|   d dl mZ d dlmZmZmZmZ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 G dd	 d	eZd
S )    )partial)AnyDictListMappingOptionalSet)CallbackManagerForLLMRun)LLM)Field)pre_init)enforce_stop_tokensc                   @   s\  e Zd ZU dZeed< edddZee ed< edddZ	e
ed< edd	dZe
ed	< ed
ddZe
ed< edddZeed< edddZeed< edddZeed< edddZeed< edddZeed< edddZee
 ed< dZee
 ed< dZee ed< dZee ed< dZee
 ed< dZee ed< g Zeee  ed< dZee
 ed< d Zee ed!< ed"d#dZe
ed#< dZeed$< dZ eed%< ed&d'dZ!ee ed'< dZ"e#ed(< G d)d* d*Z$e%e&e d+d,d-Z'e(ee#f d+d.d/Z)e*e(e(d0d1d2Z+e,e-ee#f d+d3d4Z.e,ed+d5d6Z/d:eeee  ee0 e#ed7d8d9Z1dS );GPT4Alla  GPT4All language models.

    To use, you should have the ``gpt4all`` python package installed, the
    pre-trained model file, and the model's config information.

    Example:
        .. code-block:: python

            from langchain_community.llms import GPT4All
            model = GPT4All(model="./models/gpt4all-model.bin", n_threads=8)

            # Simplest invocation
            response = model.invoke("Once upon a time, ")
    modelNbackend)alias   
max_tokensn_partsr   seedFf16_kv
logits_all
vocab_only	use_mlock	embedding   	n_threads   	n_predictgffffff?tempg?top_p(   top_kechostop@   repeat_last_ngzG?repeat_penalty   n_batch	streamingallow_downloadcpudeviceclientc                   @   s   e Zd ZdZdS )zGPT4All.ConfigZforbidN)__name__
__module____qualname__extra r4   r4   D/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/gpt4all.pyConfigc   s   r6   )returnc                	   C   s   ddddddddd	h	S )
Nr   r   r#   r!   r    r*   r(   r'   r+   r4   r4   r4   r4   r5   _model_param_namesf   s    zGPT4All._model_param_namesc              
   C   s*   | j | j| j| j| j| j| j| j| jd	S )N	r   r   r#   r!   r    r*   r(   r'   r+   r9   selfr4   r4   r5   _default_paramst   s    zGPT4All._default_params)valuesr7   c                 C   s   zddl m} W n tk
r,   tdY nX |d }|d\}}}||7 }|||pXd|d |d |d	 d
|d< |d dk	r|d j|d  z|d j|d< W n$ tk
r   |d jj|d< Y nX |S )z;Validate that the python package exists in the environment.r   )r   zVCould not import gpt4all python package. Please install it with `pip install gpt4all`.r   /Nr   r,   r.   )
model_path
model_typer,   r.   r/   r   )gpt4allr   ImportError
rpartitionr   Zset_thread_countr@   AttributeError)clsr=   ZGPT4AllModel	full_pathr?   	delimiterZ
model_namer4   r4   r5   validate_environment   s.    

zGPT4All.validate_environmentc                    s(   d j i   fdd j D S )zGet the identifying parameters.r   c                    s"   i | ]\}}|   kr||qS r4   )r8   ).0kvr:   r4   r5   
<dictcomp>   s      z/GPT4All._identifying_params.<locals>.<dictcomp>)r   r<   __dict__itemsr:   r4   r:   r5   _identifying_params   s     
zGPT4All._identifying_paramsc                 C   s   dS )zReturn the type of llm.rA   r4   r:   r4   r4   r5   	_llm_type   s    zGPT4All._llm_type)promptr%   run_managerkwargsr7   c           	      K   sh   d}|rt |j| jd}d}|  |}| jj|f|D ]}|rH|| ||7 }q8|dk	rdt||}|S )a  Call out to GPT4All's generate method.

        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

                prompt = "Once upon a time, "
                response = model.invoke(prompt, n_predict=55)
        N)verbose )r   Zon_llm_new_tokenrT   r<   r/   generater   )	r;   rQ   r%   rR   rS   Ztext_callbacktextparamstokenr4   r4   r5   _call   s    

zGPT4All._call)NN)2r0   r1   r2   __doc__str__annotations__r   r   r   r   intr   r   r   boolr   r   r   r   r   r   r    floatr!   r#   r$   r%   r   r'   r(   r*   r+   r,   r.   r/   r   r6   staticmethodr   r8   r   r<   r   rH   propertyr   rO   rP   r	   rZ   r4   r4   r4   r5   r      sX   
!
  
r   N)	functoolsr   typingr   r   r   r   r   r   Zlangchain_core.callbacksr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.pydantic_v1r   Zlangchain_core.utilsr   Zlangchain_community.llms.utilsr   r   r4   r4   r4   r5   <module>   s    