
    7|h"                         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y
)    )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y)
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                 `    | j                   | j                  | j                  | j                  dS )zGet the identifying parameters.)r   model_configr   r   )r   r   r   r   selfs    b/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/deepsparse.py_identifying_paramszDeepSparse._identifying_params1   s.     ZZ 44!%!7!7	
 	
    c                      y)zReturn type of llm.
deepsparse r   s    r   	_llm_typezDeepSparse._llm_type;   s     r   valuesc                     	 ddl m} |d   xs i } |j                  d	d|d   d||d<   |S # t        $ r t        d      w xY w)
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@   su    	+ 34:,X__ 
"g
 
z
   	G 	s	   . Apromptstoprun_managerkwargsc                    | j                   r/d} | j                  d|||d|D ]  }||j                  z  } |}n5 | 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   r"   )r   _streamtextr   r   generationsr   r   r.   r/   r0   r1   combined_outputchunkr8   s           r   _callzDeepSparse._callT   s    & >> O% DkEK .  5::-. #D II$2H2HIQ   &tT2Dr   c                   K   | j                   r1d} | j                  d|||d|2 3 d{   }||j                  z  } | j                  dd|i| j                  j
                  d   j                  }|t        ||      }|S 7 Y6 |}w)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::- II$2H2HIQ   &tT2D.   #Ds&   %BBBBABBBc              +      K    | j                   d|dd| j                  }|D ]G  }t        |j                  d   j                        }|r|j                  |j                         | I yw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   sy     8 "DMM 

040F0F
	  	E#):):1)=)B)BCE,,5::,>K	s   A-A/c                   K    | j                   d|dd| j                  }|D ]P  }t        |j                  d   j                        }|r$|j                  |j                         d{    | R y7 wrB   rD   rF   s           r   r?   zDeepSparse._astream   s     8 "DMM 

040F0F
	  	E#):):1)=)B)BCE!222DDDK	 Es   A)A:+A8,A:)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   	 MJV48$sCx.18E 15uT3_-4)
 It8
T#s(^ 
 
 3   $ 4  , %):>	$$ 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      sA    ) L L L ) # ) * 3 ) > ) 2W Wr   