o
    Zh(                     @  s   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eZG d	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                      sd  e Zd ZU dZeedZded< 	 eedZded< 	 dZ	ded< edd	d
Z
ded< eddd
Zded< dZded< 	 eddZded< 	 dZded< 	 eddd
Zded< 	 dZded< dZded< dZded < 	 edAd$d%ZedB fd&d'ZedCd)d*ZedBd+d,ZdDd0d1Z		dEdFd6d7Z		dEdGd9d:Z		dEdHd<d=Z		dEdId?d@Z  ZS )J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                 C  s   t t|ddgddd|d< t t|ddgddd|d< i |d	i d
|d
 i}|d  dkr:|d  |d< |d  dkrJ|d  |d< |d d ur\|d dkr\|d |d< zdd l}|jdi ||d< W |S  tyx   tdw )Nr   r   Z
QIANFAN_AK r   r   r   Z
QIANFAN_SKr   r    Zakskr!   r   r   zGqianfan package not found, please install it with `pip install qianfan` )r   r   getZget_secret_valueqianfanZ
CompletionImportError)clsr)   paramsr/   r-   r-   f/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/baidu_qianfan_endpoint.pyvalidate_environment   sH   	
z'QianfanLLMEndpoint.validate_environmentc                   s   i | j | jdt jS )N)r!   r    )r!   r    super_identifying_paramsself	__class__r-   r3   r6      s
   z&QianfanLLMEndpoint._identifying_paramsstrc                 C  s   dS )zReturn type of llm.zbaidu-qianfan-endpointr-   r7   r-   r-   r3   	_llm_type   s   zQianfanLLMEndpoint._llm_typec                 C  s0   | 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   )r8   Znormal_paramsr-   r-   r3   _default_params   s   
z"QianfanLLMEndpoint._default_paramspromptkwargsdictc                 K  s0   d|v r| d|d< i || jd| j|S )Nr   r=   )r?   r    )popr    r>   )r8   r?   r@   r-   r-   r3   _convert_prompt_msg_params   s   
z-QianfanLLMEndpoint._convert_prompt_msg_paramsstopOptional[List[str]]run_manager"Optional[CallbackManagerForLLMRun]c           	      K  sh   | j rd}| j|||fi |D ]}||j7 }q|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+   rD   resultNr-   )r   _streamtextrC   r   do	r8   r?   rD   rF   r@   
completionchunkr2   Zresponse_payloadr-   r-   r3   _call   s   zQianfanLLMEndpoint._call'Optional[AsyncCallbackManagerForLLMRun]c           	        sz   | j r d}| j|||fi |2 z3 d H W }||j7 }q6 |S | j|fi |}||d< | jjdi |I d H }|d S )Nr+   rD   rH   r-   )r   _astreamrJ   rC   r   adorL   r-   r-   r3   _acall
  s   "zQianfanLLMEndpoint._acallIterator[GenerationChunk]c                 k  sl    | j |fi i |ddi}||d< | jjdi |D ]}|r3t|d d}|r0||j |V  qd S Nr=   TrD   rH   )rJ   r-   )rC   r   rK   r   on_llm_new_tokenrJ   r8   r?   rD   rF   r@   r2   resrN   r-   r-   r3   rI     s   zQianfanLLMEndpoint._streamAsyncIterator[GenerationChunk]c                 K s   | j |fi i |ddi}||d< | jjdi |I d H 2 z3 d H W }|r=t|d d}|r:||jI d H  |V  q 6 d S rU   )rC   r   rR   r   rV   rJ   rW   r-   r-   r3   rQ   -  s   $zQianfanLLMEndpoint._astream)r)   r   r*   r   )r*   r   )r*   r;   )r?   r;   r@   r   r*   rA   )NN)
r?   r;   rD   rE   rF   rG   r@   r   r*   r;   )
r?   r;   rD   rE   rF   rP   r@   r   r*   r;   )
r?   r;   rD   rE   rF   rG   r@   r   r*   rT   )
r?   r;   rD   rE   rF   rP   r@   r   r*   rY   )__name__
__module____qualname____doc__r   rA   r   __annotations__r   r   r   r   r   r    r!   r$   r%   r&   r(   r   r4   propertyr6   r<   r>   rC   rO   rS   rI   rQ   __classcell__r-   r-   r9   r3   r      sT   
 ]	'
 r   )
__future__r   loggingtypingr   r   r   r   r   r   Zlangchain_core.callbacksr	   r
   Z#langchain_core.language_models.llmsr   Zlangchain_core.outputsr   Zlangchain_core.utilsr   r   r   Zpydanticr   r   	getLoggerrZ   loggerr   r-   r-   r-   r3   <module>   s     	
