a
    bg(                     @  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                      s  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d!d!d"d#d$Zedd% fd&d'Zed(d%d)d*Zedd%d+d,Zd(dd-d.d/d0Zd?d(d1d2dd(d3d4d5Zd@d(d1d6dd(d3d7d8ZdAd(d1d2dd9d3d:d;ZdBd(d1d6dd<d3d=d>Z  ZS )C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_factoryz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_scorer   )values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rt|d  |d< |d  dkr|d  |d< |d d ur|d dkr|d |d< z dd l}|jf i ||d< W n ty   tdY n0 |S )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-    r1   }/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/baidu_qianfan_endpoint.pyvalidate_environment   sF    	

z'QianfanLLMEndpoint.validate_environment)r)   c                   s   i | j | jdt jS )N)r    r   )r    r   super_identifying_paramsself	__class__r1   r2   r5      s
    z&QianfanLLMEndpoint._identifying_paramsstrc                 C  s   dS )zReturn type of llm.zbaidu-qianfan-endpointr1   r6   r1   r1   r2   	_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   )r7   Znormal_paramsr1   r1   r2   _default_params   s    
z"QianfanLLMEndpoint._default_paramsdict)promptkwargsr)   c                 K  s0   d|v r| d|d< i || jd| j|S )Nr   r<   )r?   r   )popr   r=   )r7   r?   r@   r1   r1   r2   _convert_prompt_msg_params   s    
z-QianfanLLMEndpoint._convert_prompt_msg_paramszOptional[List[str]]z"Optional[CallbackManagerForLLMRun])r?   stoprun_managerr@   r)   c           	      K  sh   | j r4d}| j|||fi |D ]}||j7 }q |S | j|fi |}||d< | jjf 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*   rC   result)r   _streamtextrB   r   do	r7   r?   rC   rD   r@   
completionchunkr0   Zresponse_payloadr1   r1   r2   _call   s    zQianfanLLMEndpoint._callz'Optional[AsyncCallbackManagerForLLMRun]c           	        sx   | j r>d}| j|||fi |2 z3 d H W }||j7 }q 6 |S | j|fi |}||d< | jjf i |I d H }|d S )Nr*   rC   rE   )r   _astreamrG   rB   r   adorI   r1   r1   r2   _acall
  s    "zQianfanLLMEndpoint._acallzIterator[GenerationChunk]c                 k  sj   | j |fi i |ddi}||d< | jjf i |D ],}|r8t|d d}|r^||j |V  q8d S Nr<   TrC   rE   )rG   )rB   r   rH   r   on_llm_new_tokenrG   r7   r?   rC   rD   r@   r0   resrK   r1   r1   r2   rF     s    zQianfanLLMEndpoint._streamzAsyncIterator[GenerationChunk]c                 K s   | j |fi i |ddi}||d< | jjf i |I d H 2 z:3 d H W }|r>t|d d}|rr||jI d H  |V  q>6 d S rP   )rB   r   rN   r   rQ   rG   rR   r1   r1   r2   rM   -  s    $zQianfanLLMEndpoint._astream)NN)NN)NN)NN)__name__
__module____qualname____doc__r   r>   r   __annotations__r   r   r   r   r   r   r    r#   r$   r%   r'   r   r3   propertyr5   r;   r=   rB   rL   rO   rF   rM   __classcell__r1   r1   r8   r2   r      sD   
]
'         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   	getLoggerrT   loggerr   r1   r1   r1   r2   <module>   s    	
