
    Ng(                        d dl mZ d dlZd dlmZ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 d dlmZmZmZ d dlmZmZ  ej        e          Z G d	 d
e          ZdS )    )annotationsN)AnyAsyncIteratorDictIteratorListOptional)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)GenerationChunk)convert_to_secret_strget_from_dict_or_envpre_init)Field	SecretStrc                      e Zd ZU dZ ee          Zded<   	  ee          Zded<   	 dZ	ded<    edd	
          Z
ded<    edd
          Zded<   dZded<   	  ed          Zded<   	 dZded<   	  edd
          Zded<   	 dZded<   dZded<   dZded <   	 ed8d$            Zed9 fd%            Zed:d'            Zed9d(            Zd;d,Z	 	 d<d=d1Z	 	 d<d>d3Z	 	 d<d?d5Z	 	 d<d@d7Z xZS )AQianfanLLMEndpointun  Baidu Qianfan completion model integration.

    Setup:
        Install ``qianfan`` and set environment variables ``QIANFAN_AK``, ``QIANFAN_SK``.

        .. code-block:: bash

            pip install qianfan
            export QIANFAN_AK="your-api-key"
            export QIANFAN_SK="your-secret_key"

    Key init args — completion params:
        model: str
            Name of Qianfan model to use.
        temperature: Optional[float]
            Sampling temperature.
        endpoint: Optional[str]
            Endpoint of the Qianfan LLM
        top_p: Optional[float]
            What probability mass to use.

    Key init args — client params:
        timeout: Optional[int]
            Timeout for requests.
        api_key: Optional[str]
            Qianfan API KEY. If not passed in will be read from env var QIANFAN_AK.
        secret_key: Optional[str]
            Qianfan SECRET KEY. If not passed in will be read from env var QIANFAN_SK.

    See full list of supported init args and their descriptions in the params section.

    Instantiate:
        .. code-block:: python

            from langchain_community.llms import QianfanLLMEndpoint

            llm = QianfanLLMEndpoint(
                model="ERNIE-3.5-8K",
                # api_key="...",
                # secret_key="...",
                # other params...
            )

    Invoke:
        .. code-block:: python

            input_text = "用50个字左右阐述，生命的意义在于"
            llm.invoke(input_text)

        .. code-block:: python

            '生命的意义在于体验、成长、爱与被爱、贡献与传承，以及对未知的勇敢探索与自我超越。'

    Stream:
        .. code-block:: python

            for chunk in llm.stream(input_text):
                print(chunk)

        .. code-block:: python

            生命的意义 | 在于不断探索 | 与成长 | ，实现 | 自我价值，| 给予爱 | 并接受 | 爱， | 在经历 | 中感悟 | ，让 | 短暂的存在 | 绽放出无限 | 的光彩 | 与温暖 | 。

        .. code-block:: python

            stream = llm.stream(input_text)
            full = next(stream)
            for chunk in stream:
                full += chunk
            full

        .. code-block::

            '生命的意义在于探索、成长、爱与被爱、贡献价值、体验世界之美，以及在有限的时间里追求内心的平和与幸福。'

    Async:
        .. code-block:: python

            await llm.ainvoke(input_text)

            # stream:
            # async for chunk in llm.astream(input_text):
            #    print(chunk)

            # batch:
            # await llm.abatch([input_text])

        .. code-block:: python

            '生命的意义在于探索、成长、爱与被爱、贡献社会，在有限的时间里追寻无限的可能，实现自我价值，让生活充满色彩与意义。'

    )default_factoryDict[str, Any]init_kwargsmodel_kwargsNr   clientapi_key)defaultaliaszOptional[SecretStr]
qianfan_ak
secret_key
qianfan_skFzOptional[bool]	streamingr   zOptional[str]modelendpoint<   timeoutzOptional[int]request_timeoutg?zOptional[float]top_pgffffff?temperature   penalty_scorevaluesr   returnc                n   t          t          |ddgdd                    |d<   t          t          |ddgdd                    |d<   i |                    d	i           d
|d
         i}|d                                         dk    r|d                                         |d<   |d                                         dk    r|d                                         |d<   |d         |d         dk    r|d         |d<   	 dd l} |j        di ||d<   n# t          $ r t          d          w xY w|S )Nr   r   
QIANFAN_AK r!   r   r   
QIANFAN_SKr   r"   akskr#   r   r   zGqianfan package not found, please install it with `pip install qianfan` )r   r   getget_secret_valueqianfan
CompletionImportError)clsr+   paramsr6   s       k/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/baidu_qianfan_endpoint.pyvalidate_environmentz'QianfanLLMEndpoint.validate_environment   s   4 y)	   
  
|  5 |,	   
  
|
jj++
VG_
 
 ,0022b88!,/@@BBF4L,0022b88!,/@@BBF4L*)fZ.@B.F.F!'
!3F:	NNN1w1;;F;;F8 	 	 	(  	
 s   D D2c                L    i | j         | j        dt                      j        S )N)r#   r"   )r#   r"   super_identifying_params)self	__class__s    r;   r?   z&QianfanLLMEndpoint._identifying_params   s0    
=4:>>
gg)
 	
    strc                    dS )zReturn type of llm.zbaidu-qianfan-endpointr3   )r@   s    r;   	_llm_typezQianfanLLMEndpoint._llm_type   s
     ('rB   c                r    | j         | j        | j        | j        | j        | j        | j        d}i || j        S )z3Get the default parameters for calling Qianfan API.)r"   r#   streamr&   r'   r(   r*   )r"   r#   r    r&   r'   r(   r*   r   )r@   normal_paramss     r;   _default_paramsz"QianfanLLMEndpoint._default_params   sK     Zn#3Z+!/
 
 6-54#455rB   promptkwargsdictc                d    d|v r|                     d          |d<   i || j        d| j        |S )Nr    rG   )rJ   r"   )popr"   rI   )r@   rJ   rK   s      r;   _convert_prompt_msg_paramsz-QianfanLLMEndpoint._convert_prompt_msg_params   sV    
 &  %zz+66F8
$*55
"
 
 	
rB   stopOptional[List[str]]run_manager"Optional[CallbackManagerForLLMRun]c                    | j         r!d} | j        |||fi |D ]}||j        z  }|S  | j        |fi |}||d<    | j        j        di |}|d         S )a  Call out to an qianfan models endpoint for each generation with a prompt.
        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.
        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python
                response = qianfan_model.invoke("Tell me a joke.")
        r/   rP   resultr3   )r    _streamtextrO   r   do	r@   rJ   rP   rR   rK   
completionchunkr:   response_payloads	            r;   _callzQianfanLLMEndpoint._call   s    $ > 	J%fdKJJ6JJ ) )ej(

00BB6BBv)4;>33F33))rB   'Optional[AsyncCallbackManagerForLLMRun]c                   K   | j         r'd} | j        |||fi |2 3 d {V }||j        z  }6 |S  | j        |fi |}||d<    | j        j        di | d {V }|d         S )Nr/   rP   rU   r3   )r    _astreamrW   rO   r   adorY   s	            r;   _acallzQianfanLLMEndpoint._acall
  s       > 	J,t}VT;QQ&QQ ) ) ) ) ) ) )eej(

  R00BB6BBv!0!:!:6!:!:::::::))s   .Iterator[GenerationChunk]c              +     K    | j         |fi i |ddi}||d<    | j        j        di |D ]:}|r6t          |d                   }|r|                    |j                   |V  ;d S NrG   TrP   rU   )rW   r3   )rO   r   rX   r   on_llm_new_tokenrW   r@   rJ   rP   rR   rK   r:   resr[   s           r;   rV   zQianfanLLMEndpoint._stream  s       10VV;Uf;UhPT;U;UVVv!4;>++F++ 	 	C 'S];;; =00<<<	 	rB   AsyncIterator[GenerationChunk]c                  K    | j         |fi i |ddi}||d<    | j        j        di | d {V 2 3 d {V }|r=t          |d                   }|r |                    |j                   d {V  |W V  G6 d S re   )rO   r   ra   r   rf   rW   rg   s           r;   r`   zQianfanLLMEndpoint._astream-  s       10VV;Uf;UhPT;U;UVVv.t{8888888888 	 	 	 	 	 	 	# 'S];;; C%66uzBBBBBBBBB 988s   A;)r+   r   r,   r   )r,   r   )r,   rC   )rJ   rC   rK   r   r,   rL   )NN)
rJ   rC   rP   rQ   rR   rS   rK   r   r,   rC   )
rJ   rC   rP   rQ   rR   r^   rK   r   r,   rC   )
rJ   rC   rP   rQ   rR   rS   rK   r   r,   rc   )
rJ   rC   rP   rQ   rR   r^   rK   r   r,   ri   )__name__
__module____qualname____doc__r   rL   r   __annotations__r   r   r   r   r    r"   r#   r&   r'   r(   r*   r   r<   propertyr?   rE   rI   rO   r]   rb   rV   r`   __classcell__)rA   s   @r;   r   r      s        [ [z #(%"="="=K====@ $)5#>#>#>L>>>>8F&+eD	&J&J&JJJJJJ&+eD&M&M&MJMMMM %I%%%%/ 5...E.... #H""""E%*U2Y%G%G%GOGGGG0 E    #'K''''%&M&&&& % % % X%N 
 
 
 
 
 X
 ( ( ( X( 6 6 6 X6
 
 
 
  %):>	* * * * *@ %)?C	* * * * *, %):>	    & %)?C	        rB   r   )
__future__r   loggingtypingr   r   r   r   r   r	   langchain_core.callbacksr
   r   #langchain_core.language_models.llmsr   langchain_core.outputsr   langchain_core.utilsr   r   r   pydanticr   r   	getLoggerrk   loggerr   r3   rB   r;   <module>r|      sD   " " " " " "                        4 3 3 3 3 3 2 2 2 2 2 2 V V V V V V V V V V % % % % % % % %		8	$	$b b b b b b b b b brB   