
    Ng"                         d dl mZ d dlmZ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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 d dlmZ  G d d	e          Zd
S )    )pre_init)AnyAsyncIteratorDictIteratorListOptionalUnion)root_validator)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)enforce_stop_tokens)GenerationChunkc                       e Zd ZU dZeed<   eed<   	 dZee	eef                  ed<   	 dZ
edee	f         ed<   	 dZeed<   	 ed	e	eef         fd
            Zed	efd            Zede	d	e	fd            Z	 	 ddedeee                  dee         ded	ef
dZ	 	 ddedeee                  dee         ded	ef
dZ	 	 ddedeee                  dee         ded	ee         f
dZ	 	 ddedeee                  dee         ded	ee         f
dZdS )
DeepSparseaH  Neural Magic DeepSparse LLM interface.
    To use, you should have the ``deepsparse`` or ``deepsparse-nightly``
    python package installed. See https://github.com/neuralmagic/deepsparse
    This interface let's you deploy optimized LLMs straight from the
    [SparseZoo](https://sparsezoo.neuralmagic.com/?useCase=text_generation)
    Example:
        .. code-block:: python
            from langchain_community.llms import DeepSparse
            llm = DeepSparse(model="zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base_quant-none")
    pipelinemodelNmodel_configurationgeneration_configF	streamingreturnc                 8    | j         | j        | j        | j        dS )zGet the identifying parameters.)r   model_configr   r   )r   r   r   r   selfs    _/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/deepsparse.py_identifying_paramszDeepSparse._identifying_params1   s)     Z 4!%!7	
 
 	
    c                     dS )zReturn type of llm.
deepsparse r   s    r   	_llm_typezDeepSparse._llm_type;   s	     |r   valuesc                     	 ddl m} n# t          $ r t          d          w xY w|d         pi } |j        d	d|d         d||d<   |S )
z2Validate that ``deepsparse`` package is installed.r   )Pipelinez[Could not import `deepsparse` package. Please install it with `pip install deepsparse[llm]`r   text_generationr   )task
model_pathr   r"   )r!   r&   ImportErrorcreate)clsr$   r&   r   s       r   validate_environmentzDeepSparse.validate_environment@   s    	+++++++ 	 	 	G  	 34:,X_ 
"g
 
 
 
z
 s   	 #promptstoprun_managerkwargsc                     | j         r#d} | j        d|||d|D ]}||j        z  }|}n$ | j        dd|i| j        j        d         j        }|t          ||          }|S )  Generate text from a prompt.
        Args:
            prompt: The prompt to generate text from.
            stop: A list of strings to stop generation when encountered.
        Returns:
            The generated text.
        Example:
            .. code-block:: python
                from langchain_community.llms import DeepSparse
                llm = DeepSparse(model="zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base_quant-none")
                llm.invoke("Tell me a joke.")
         r.   r/   r0   	sequencesr   Nr"   )r   _streamtextr   r   generationsr   r   r.   r/   r0   r1   combined_outputchunkr8   s           r   _callzDeepSparse._callT   s    & > 	 O% Dk EK  . .  5:-"DD III$2HIIQ   &tT22Dr   c                    K   | j         r)d} | j        d|||d|2 3 d{V }||j        z  }6 |}n$ | j        dd|i| j        j        d         j        }|t          ||          }|S )r3   r4   r5   Nr6   r   r"   )r   _astreamr8   r   r   r9   r   r:   s           r   _acallzDeepSparse._acallz   s      & > 	 O,t}  Dk   EK    . . . . . . .e  5:-  #DD III$2HIIQ   &tT22Ds   /c              +      K    | j         d|dd| j        }|D ]C}t          |j        d         j                  }|r|                    |j                   |V  DdS a  Yields results objects as they are generated in real time.
        It also calls the callback manager's on_llm_new_token event with
        similar parameters to the OpenAI LLM class method of the same name.
        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.
        Returns:
            A generator representing the stream of tokens being generated.
        Yields:
            A dictionary like object containing a string token.
        Example:
            .. code-block:: python
                from langchain_community.llms import DeepSparse
                llm = DeepSparse(
                    model="zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base_quant-none",
                    streaming=True
                )
                for chunk in llm.stream("Tell me a joke",
                        stop=["'","
"]):
                    print(chunk, end='', flush=True)  # noqa: T201
        T)r6   r   r   )r8   )tokenNr"   r   r   r   r9   r8   on_llm_new_tokenr   r.   r/   r0   r1   	inferencerC   r<   s           r   r7   zDeepSparse._stream   s      8 "DM 

 
040F
 
	  	 	E#):1)=)BCCCE ?,,5:,>>>KKKK	 	r   c                   K    | j         d|dd| j        }|D ]J}t          |j        d         j                  }|r!|                    |j                   d{V  |W V  KdS rB   rD   rF   s           r   r?   zDeepSparse._astream   s      8 "DM 

 
040F
 
	  	 	E#):1)=)BCCCE E!222DDDDDDDDDKKKKK	 	r   )NN)__name__
__module____qualname____doc__r   __annotations__strr   r	   r   r   r
   r   boolpropertyr   r#   r   r-   r   r   r=   r   r@   r   r   r7   r   r?   r"   r   r   r   r      s        	 	 MMMJJJV48$sCx.1888E 15uT3_-444)
 It8
T#s(^ 
 
 
 X
 3    X $ 4    X, %):>	$ $$ tCy!$ 67	$
 $ 
$ $ $ $R %)?C	$ $$ tCy!$ ;<	$
 $ 
$ $ $ $R %):>	$ $$ tCy!$ 67	$
 $ 
/	"$ $ $ $R %)?C	$ $$ tCy!$ ;<	$
 $ 
	'$ $ $ $ $ $r   r   N)langchain_core.utilsr   typingr   r   r   r   r   r	   r
   pydanticr   langchain_core.callbacksr   r   #langchain_core.language_models.llmsr   langchain_community.llms.utilsr   langchain_core.outputsr   r   r"   r   r   <module>rX      sW   ) ) ) ) ) ) L L L L L L L L L L L L L L L L L L ) ) ) ) ) ) # # # # # # ) ) ) ) ) )        * ) ) ) ) ) 3 3 3 3 3 3 ) ) ) ) ) ) > > > > > > ) ) ) ) ) ) 2 2 2 2 2 2W W W W W W W W W Wr   