
    dh'8                         S SK r S SKrS SKJrJrJrJrJrJr  S SK	r	S SK
JrJr  S SKJr  S SKJr  S SKJr  \R&                  " \5      r " S S\5      rg)	    N)AnyAsyncIteratorDictIteratorListOptional)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)GenerationChunk)Fieldc                      \ rS rSr% Sr\\S'    Sr\\   \S'    Sr	\\
   \S'    \" SS	S
9r\\S	'    Sr\\   \S'    Sr\\   \S'    Sr\\   \S'    Sr\\   \S'    Sr\\   \S'    Sr\\   \S'    Sr\\   \S'    Sr\\
   \S'    Sr\\
   \S'    Sr\\
   \S'    Sr\\   \S'    Sr\\   \S'    \" SSS
9r\\S'    \" SS S
9r\
\S '    \" SS!S
9r\\S!'    S"r\\
   \S#'    \" SS$S
9r\\S$'    \" SS%S
9r \\S%'    / r!\\"\      \S&'    Sr#\\S''    \$S(\%\\&4   4S) j5       r'\$S(\%\\&4   4S* j5       r(\$S(\4S+ j5       r)S6S,\\"\      S(\%\\&4   4S- jjr*  S7S.\S,\\"\      S/\\+   S0\&S(\4
S1 jjr,  S7S.\S,\\"\      S/\\-   S0\&S(\4
S2 jjr.  S7S.\S,\\"\      S/\\+   S0\&S(\/\0   4
S3 jjr1  S7S.\S,\\"\      S/\\-   S0\&S(\2\0   4
S4 jjr3S5r4g)8TextGen   aN  Text generation models from WebUI.

To use, you should have the text-generation-webui installed, a model loaded,
and --api added as a command-line option.

Suggested installation, use one-click installer for your OS:
https://github.com/oobabooga/text-generation-webui#one-click-installers

Parameters below taken from text-generation-webui api example:
https://github.com/oobabooga/text-generation-webui/blob/main/api-examples/api-example.py

Example:
    .. code-block:: python

        from langchain_community.llms import TextGen
        llm = TextGen(model_url="http://localhost:8500")
	model_urlNpreset   max_new_tokensT	do_sample)aliasg?temperatureg?top_p   	typical_pr   epsilon_cutoff
eta_cutoffgzG?repetition_penalty(   top_k
min_lengthno_repeat_ngram_size	num_beamspenalty_alphalength_penaltyFearly_stoppingseedadd_bos_tokeni   truncation_lengthban_eos_tokenskip_special_tokensstopping_strings	streamingreturnc                    0 SU R                   _SU R                  _SU R                  _SU R                  _SU R                  _SU R
                  _SU R                  _SU R                  _S	U R                  _S
U R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                  _SU R                   _U R"                  U R$                  U R&                  U R(                  S.E$ )z/Get the default parameters for calling textgen.r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r'   r(   )r)   r*   r+   r,   )r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r'   r(   r)   r*   r+   r,   selfs    X/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/textgen.py_default_paramsTextGen._default_paramsv   sU   
d11

 4++
 TZZ	

 
 d11
 $//
 !$"9"9
 TZZ
 $//
 #D$=$=
 
 T//
 d11
 d11
  DII!
" T//#
$ "&!7!7!//#'#;#; $ 5 5+
 	
    c                 :    0 SU R                   0EU R                  E$ )zGet the identifying parameters.r   )r   r3   r0   s    r2   _identifying_paramsTextGen._identifying_params   s$     I;/H43G3GHHr5   c                     g)zReturn type of llm.textgen r0   s    r2   	_llm_typeTextGen._llm_type   s     r5   stopc                     U R                   (       a  Ub  [        S5      eU R                  c  U R                  nOSU R                  0nU R                   =(       d    U=(       d    / US'   U$ )z
Performs sanity check, preparing parameters in format needed by textgen.

Args:
    stop (Optional[List[str]]): List of stop sequences for textgen.

Returns:
    Dictionary containing the combined parameters.
z2`stop` found in both the input and default params.r   r,   )r,   
ValueErrorr   r3   )r1   r>   paramss      r2   _get_parametersTextGen._get_parameters   sc       T%5QRR;;))F,F &*%:%:%Hd%Hb!"r5   promptrun_managerkwargsc                    U R                   (       a0  SnU R                  " SXUS.UD6 H  nXVR                  -  nM     UnU$ U R                   S3nU R	                  U5      n	U	R                  5       n
XS'   [        R                  " XS9nUR                  S:X  a  UR                  5       S   S   S	   nU$ [        S
U 35        SnU$ )  Call the textgen web API and return the output.

Args:
    prompt: The prompt to use for generation.
    stop: A list of strings to stop generation when encountered.

Returns:
    The generated text.

Example:
    .. code-block:: python

        from langchain_community.llms import TextGen
        llm = TextGen(model_url="http://localhost:5000")
        llm.invoke("Write a story about llamas.")
 rD   r>   rE   /api/v1/generaterD   json   resultsr   textERROR: response: r;   )r-   _streamrP   r   rB   copyrequestspoststatus_coderM   printr1   rD   r>   rE   rF   combined_text_outputchunkresulturlrA   requestresponses               r2   _callTextGen._call   s    . >>#%  kEK %

2$ *F  ^^$$45C))$/FkkmG &H}}S7H##s*!3A6v>
  )(45r5   c                   #    U R                   (       a0  SnU R                  " SXUS.UD6  Sh  vN nXVR                  -  nM  U R                   S3nU R	                  U5      n	U	R                  5       n
XS'   [        R                  " XS9nUR                  S:X  a  UR                  5       S   S	   S
   nU$ [        SU 35        SnU$  N
 UnU$ 7f)rH   rI   rJ   NrK   rD   rL   rN   rO   r   rP   rQ   r;   )r-   _astreamrP   r   rB   rS   rT   rU   rV   rM   rW   rX   s               r2   _acallTextGen._acall   s     . >>#% #}}  k EK  3e %

2$ ^^$$45C))$/FkkmG &H}}S7H##s*!3A6v>
  )(45'3   *F s&   )CCC	CBC	CCc              +   >  #     SSK n0 U R                  U5      EUEnU R                   S3nUR	                  5       nXS'   UR                  5       n	U	R                  U5        U	R                  [        R                  " U5      5         U	R                  5       n
[        R                  " U
5      n
U
S   S:X  a2  [        U
S   SS	9nU(       a  UR                  UR                  S
9  Uv   OU
S   S:X  a  U	R                  5         gM}  ! [         a    [        S5      ef = f7f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 prompts 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 objects containing a string token and metadata.
            See text-generation-webui docs and below for more.

        Example:
            .. code-block:: python

                from langchain_community.llms import TextGen
                llm = TextGen(
                    model_url = "ws://localhost:5005"
                    streaming=True
                )
                for chunk in llm.stream("Ask 'Hi, how are you?' like a pirate:'",
                        stop=["'","
"]):
                    print(chunk, end='', flush=True)  # noqa: T201

        r   Nz9The `websocket-client` package is required for streaming.z/api/v1/streamrD   eventtext_streamrP   )rP   generation_info)token
stream_end	websocketImportErrorrB   r   rS   	WebSocketconnectsendrM   dumpsrecvloadsr   on_llm_new_tokenrP   closer1   rD   r>   rE   rF   rm   rA   r\   r]   websocket_clientr[   rZ   s               r2   rR   TextGen._stream  s     H	 :D((.9&9 /++-"$..0  %djj12%**,FZZ'Fg-/'$( 00uzz0BL0 &&( %  	K 	s   DD C<DDDc                T  #     SSK n0 U R                  U5      EUEnU R                   S3nUR	                  5       nXS'   UR                  5       n	U	R                  U5        U	R                  [        R                  " U5      5         U	R                  5       n
[        R                  " U
5      n
U
S   S:X  a;  [        U
S   SS	9nU(       a!  UR                  UR                  S
9I Sh  vN   U7v   OU
S   S:X  a  U	R                  5         gM  ! [         a    [        S5      ef = f N?7frf   rl   rw   s               r2   rb   TextGen._astreamY  s-    H	 :D((.9&9 /++-"$..0  %djj12%**,FZZ'Fg-/'$( %66UZZ6HHHL0 &&( %  	K 	8 Is(   D(D CD(&D&'&D(D##D(r;   )N)NN)5__name__
__module____qualname____firstlineno____doc__str__annotations__r   r   r   intr   r   boolr   floatr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r'   r(   r)   r*   r+   r,   r   r-   propertyr   r   r3   r7   r<   rB   r
   r_   r	   rc   r   r   rR   r   rb   __static_attributes__r;   r5   r2   r   r      s   $ NJ FHSM 1$'NHSM'3D4It4#&K%&M !E8E? G "#Ix"8 '(NHUO'"#J#*..F  E8E?? !"J!.*+(3-+<
  !Ix} %&M8E?&&'NHUO' .>?ND?b'D#'O<M4<: (,x}+T  _=M4=V %d2G HHD,.htCy).@It8
c3h 
 
4 IT#s(^ I I 3  HT#Y$7 4S> : %):>	,, tCy!, 67	,
 , 
,b %)?C	,, tCy!, ;<	,
 , 
,b %):>	FF tCy!F 67	F
 F 
/	"FV %)?C	FF tCy!F ;<	F
 F 
	'F Fr5   r   )rM   loggingtypingr   r   r   r   r   r   rT   langchain_core.callbacksr	   r
   #langchain_core.language_models.llmsr   langchain_core.outputsr   pydanticr   	getLoggerr|   loggerr   r;   r5   r2   <module>r      sB      E E  4 2 			8	$Nc Nr5   