a
    bg~                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZmZmZmZ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m Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d d	l*m+Z+ d d
l,m-Z-m.Z.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4 d dl5m6Z6m7Z7m8Z8 d dl9m:Z: d dl;m<Z<m=Z=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZDmEZEmFZFmGZG d dlHmIZI d dlJmKZKmLZLmMZMmNZNmOZO d dlPmQZQmRZRmSZS eTeUZVd+ddddddZWddddd ZXdd!d"d#d$ZYd%d&d'd(d)ZZG d*d% d%eZ[dS ),    )annotationsN)
itemgetter)AnyAsyncIteratorCallableDictIteratorListMappingOptionalSequenceTypeUnioncast)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LanguageModelInput)BaseChatModel)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageHumanMessageHumanMessageChunkSystemMessageSystemMessageChunkToolMessageToolMessageChunk)OutputParserLike)JsonOutputKeyToolsParserPydanticToolsParsermake_invalid_tool_callparse_tool_call)ChatGenerationChatGenerationChunk
ChatResult)RunnableRunnableMapRunnablePassthrough)BaseTool)convert_to_secret_strget_from_dict_or_envpre_initconvert_to_openai_tool)is_basemodel_subclass)	BaseModel
ConfigDictField	SecretStr)	HTTPError)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponential) agenerate_with_last_element_markcheck_responsegenerate_with_last_element_markFzMapping[str, Any]boolz$Union[BaseMessage, BaseMessageChunk])_dictis_chunkreturnc                 C  s  | d }| d }|dkr0|r&t |dS t|dS |dkrFg }g }d| v rd| d i}t| d D ]\}}|rz2||d d|d d	|d
|d W n ty   Y n0 qdzt|dd}	|	r||	 W qd ty }
 z |t|t	|
 W Y d}
~
qdd}
~
0 0 qdni }|r6t
|||| d
dS t||||dS |dkrj|r`t|dS t|dS |dkri }d| v r| d |d< |rt| dd| d|dS t| dd| d|dS |rt||dS t||dS dS )zConvert a dict to a message.rolecontentuser)rE   	assistant
tool_callsfunctionname	argumentsid)rJ   argsrL   indexT)Z	return_idN)rE   additional_kwargstool_call_chunksrL   )rE   rO   rH   invalid_tool_callssystemtool tool_call_id)rE   rU   rO   rD   rE   )r   r   	enumerateappendgetKeyErrorr%   	Exceptionr$   strr   r   r   r   r    r   r   r   )rA   rB   rD   rE   rH   rQ   rO   rN   valueZparsed_toole r_   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_models/tongyi.pyconvert_dict_to_messageU   s    


.	





ra   r   r   )message_chunkrC   c                   sR   t | ts| S dg t | tr( d | jjd f i  fdd| j D S )z}Convert a message chunk to a message.

    Args:
        chunk: Message chunk to convert.

    Returns:
        Message.
    typerP      c                   s   i | ]\}}| vr||qS r_   r_   ).0kvZignore_keysr_   r`   
<dictcomp>       z4convert_message_chunk_to_message.<locals>.<dictcomp>)
isinstancer   r   rX   	__class____mro____dict__items)rb   r_   rh   r`    convert_message_chunk_to_message   s    	


rp   dict)messagerC   c                 C  s   t | tr| j| jd}nt | tr2d| jd}nt | trbd| jd}d| jv r| jd |d< npt | trzd| jd}nXt | trd| j	| j| j
p| jdd}n,t | trdd	| j| j
d}ntd
|  |S )zConvert a message to a dict.rV   rF   rG   rH   rR   rS   rJ   )rD   rU   rE   rJ   rT   zGot unknown type )rk   r   rD   rE   r   r   rO   r   r   rU   rJ   rY   r   	TypeError)rr   Zmessage_dictr_   r_   r`   convert_message_to_dict   s0    






rt   
ChatTongyizCallable[[Any], Any])llmrC   c                 C  s6   d}d}t dt| jtd||dtttttj	dS )Nrd      T)
multiplierminmax)reraisestopwaitr9   before_sleep)
r9   r;   max_retriesr<   r:   r7   r8   loggerloggingWARNING)rv   Zmin_secondsZmax_secondsr_   r_   r`   _create_retry_decorator   s    
r   c                      s  e Zd ZU dZeddddZdZded< ed	d
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< dZded< dZded< eddZeddd d!Zed"d"d#d$d%Zeddd&d'Zddd(d)d*Zddd(d+d,Zdddd-d.d/Zddd(d0d1Zd]d2d3d4dd5d6d7d8Zd^d2d3d9dd5d6d:d;Zd_d2d3d4dd<d6d=d>Zd`d2d3d9dd?d6d@dAZd2ddddBdCdDZdEdFdGdHdIZe dadddddJdKdLZ!e dMdNdOdPdQZ"dRddSdT fdUdVZ#ddWdXdddYdZd[d\Z$  Z%S )bru   ui  Alibaba Tongyi Qwen chat model integration.

    Setup:
        Install ``dashscope`` and set environment variables ``DASHSCOPE_API_KEY``.

        .. code-block:: bash

            pip install dashscope
            export DASHSCOPE_API_KEY="your-api-key"

    Key init args — completion params:
        model: str
            Name of Qianfan model to use.
        top_p: float
            Total probability mass of tokens to consider at each step.
        streaming: bool
            Whether to stream the results or not.

    Key init args — client params:
        api_key: Optional[str]
            Dashscope API KEY. If not passed in will be read from env var DASHSCOPE_API_KEY.
        max_retries: int
            Maximum number of retries to make when generating.

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

    Instantiate:
        .. code-block:: python

            from langchain_community.chat_models import ChatTongyi

            tongyi_chat = ChatTongyi(
                model="qwen-max",
                # top_p="...",
                # api_key="...",
                # other params...
            )

    Invoke:
        .. code-block:: python

            messages = [
                ("system", "你是一名专业的翻译家，可以将用户的中文翻译为英文。"),
                ("human", "我喜欢编程。"),
            ]
            tongyi_chat.invoke(messages)

        .. code-block:: python

            AIMessage(
                content='I enjoy programming.',
                response_metadata={
                    'model_name': 'qwen-max',
                    'finish_reason': 'stop',
                    'request_id': '0bd14853-4abc-9593-8642-8dbb915bd4df',
                    'token_usage': {
                        'input_tokens': 30,
                        'output_tokens': 4,
                        'total_tokens': 34
                    }
                },
                id='run-533b3688-d12b-40c6-a2f7-52f291f8fa0a-0'
            )

    Stream:
        .. code-block:: python

            for chunk in tongyi_chat.stream(messages):
                print(chunk)

        .. code-block:: python

            content='I' id='run-8fbcce63-42fc-4208-9399-da46ac40c967'
            content=' enjoy' id='run-8fbcce63-42fc-4208-9399-da46ac40c967'
            content=' programming' id='run-8fbcce63-42fc-4208-9399-da46ac40c967'
            content='.' response_metadata={'finish_reason': 'stop', 'request_id': '67aec2b5-72bf-96a4-ae29-5bfebd2e7305', 'token_usage': {'input_tokens': 30, 'output_tokens': 4, 'total_tokens': 34}} id='run-8fbcce63-42fc-4208-9399-da46ac40c967'

    Async:
        .. code-block:: python

            await tongyi_chat.ainvoke(messages)

            # stream:
            # async for chunk in tongyi_chat.astream(messages):
            #    print(chunk)

            # batch:
            # await tongyi_chat.abatch([messages])

        .. code-block:: python

            AIMessage(
                content='I enjoy programming.',
                response_metadata={
                    'model_name': 'qwen-max',
                    'finish_reason': 'stop',
                    'request_id': 'a55a2d6c-a876-9789-9dd9-7b52bf8adde0',
                    'token_usage': {
                        'input_tokens': 30,
                        'output_tokens': 4,
                        'total_tokens': 34
                    }
                },
                id='run-3bffa3ec-e8d9-4043-b57d-348e047d64de-0'
            )

    Tool calling:
        .. code-block:: python

            from pydantic import BaseModel, Field


            class GetWeather(BaseModel):
                '''Get the current weather in a given location'''

                location: str = Field(
                    ..., description="The city and state, e.g. San Francisco, CA"
                )


            class GetPopulation(BaseModel):
                '''Get the current population in a given location'''

                location: str = Field(
                    ..., description="The city and state, e.g. San Francisco, CA"
                )

            chat_with_tools = tongyi_chat.bind_tools([GetWeather, GetPopulation])
            ai_msg = chat_with_tools.invoke(
                "Which city is hotter today and which is bigger: LA or NY?"
            )
            ai_msg.tool_calls

        .. code-block:: python
            [
                {
                    'name': 'GetWeather',
                    'args': {'location': 'Los Angeles, CA'},
                    'id': ''
                }
            ]

    Structured output:
        .. code-block:: python

            from typing import Optional

            from pydantic import BaseModel, Field


            class Joke(BaseModel):
                '''Joke to tell user.'''

                setup: str = Field(description="The setup of the joke")
                punchline: str = Field(description="The punchline to the joke")
                rating: Optional[int] = Field(description="How funny the joke is, from 1 to 10")


            structured_chat = tongyi_chat.with_structured_output(Joke)
            structured_chat.invoke("Tell me a joke about cats")

        .. code-block:: python

            Joke(
                setup='Why did the cat join the band?',
                punchline='Because it wanted to be a solo purr-sonality!',
                rating=None
            )

    Response metadata
        .. code-block:: python

            ai_msg = tongyi_chat.invoke(messages)
            ai_msg.response_metadata

        .. code-block:: python

            {
                'model_name': 'qwen-max',
                'finish_reason': 'stop',
                'request_id': '32a13e4c-370e-99cb-8f9b-4c999d98c57d',
                'token_usage': {
                    'input_tokens': 30,
                    'output_tokens': 4,
                    'total_tokens': 34
                }
            }

    zDict[str, str]rC   c                 C  s   ddiS )Ndashscope_api_keyDASHSCOPE_API_KEYr_   selfr_   r_   r`   
lc_secrets  s    zChatTongyi.lc_secretsNr   clientz
qwen-turbomodel)defaultaliasr\   
model_name)default_factoryzDict[str, Any]model_kwargsg?floattop_papi_key)r   zOptional[SecretStr]r   Fr@   	streaming
   intr   T)Zpopulate_by_namec                 C  s   dS )zReturn type of llm.Ztongyir_   r   r_   r_   r`   	_llm_type  s    zChatTongyi._llm_typer   )valuesrC   c                 C  s   t t|dd|d< zddl}W n ty:   tdY n0 g d}|d |v s\d|d v rz|j|d	< W q ty   td
Y q0 n*z|j|d	< W n ty   tdY n0 |S )z?Validate that api key and python package exists in environment.r   r   r   NzdCould not import dashscope python package. Please install it with `pip install dashscope --upgrade`.)z
qwen-vl-v1zqwen-vl-chat-v1zqwen-audio-turbozqwen-vl-pluszqwen-vl-maxr   Zvlr   z`dashscope` has no `MultiModalConversation` attribute, this is likely due to an old version of the dashscope package. Try upgrading it with `pip install --upgrade dashscope`.z`dashscope` has no `Generation` attribute, this is likely due to an old version of the dashscope package. Try upgrading it with `pip install --upgrade dashscope`.)r-   r.   	dashscopeImportErrorZMultiModalConversationAttributeError
ValueErrorZ
Generation)clsr   r   Zdashscope_multimodal_modelsr_   r_   r`   validate_environment  s6    




zChatTongyi.validate_environmentc                 C  s$   | j | jtt| j dd| jS )z7Get the default parameters for calling Tongyi Qwen API.rr   )r   r   r   Zresult_format)r   r   r   r6   r   Zget_secret_valuer   r   r_   r_   r`   _default_params  s    zChatTongyi._default_params)kwargsrC   c                   s.   t  }|ddd fdd}|f i |S )*Use tenacity to retry the completion call.r   _kwargsrC   c                    s    j jf i | }t|S N)r   callr>   )r   respr   r_   r`   _completion_with_retry  s    z@ChatTongyi.completion_with_retry.<locals>._completion_with_retryr   )r   r   retry_decoratorr   r_   r   r`   completion_with_retry  s    z ChatTongyi.completion_with_retryc                   s.   t  }|ddd fdd}|f i |S )r   r   r   c            	      ;  s    j jf i | }d }|D ]}| dr| ddstt|}|dr|d dr|d d d }|d }t|dtrd	d
d |d D }||d< |}|d u r|}n 	||}|}t
|V  qt
|V  qd S )Nstreamincremental_outputFoutputchoicesr   rr   rE   rT   c                 s  s$   | ]}t |tr|d dV  qdS )textrT   N)rk   rq   rY   )re   itemr_   r_   r`   	<genexpr>,  s   
zaChatTongyi.stream_completion_with_retry.<locals>._stream_completion_with_retry.<locals>.<genexpr>)r   r   rY   jsonloadsdumpsrk   listjoinsubtract_client_responser>   )	r   	responses	prev_respr   	resp_copychoicerr   Zcontent_textZ
delta_respr   r_   r`   _stream_completion_with_retry  s,    
zNChatTongyi.stream_completion_with_retry.<locals>._stream_completion_with_retryr   )r   r   r   r   r_   r   r`   stream_completion_with_retry  s     z'ChatTongyi.stream_completion_with_retry)r   r   rC   c                 C  s   t t |}|d d d }|d }t t |}|d d d }|d }|d |d d|d< |drt|d D ]Z\}	}
|
d }|dr~|d |	 d }|d	 |d	 d|d	< |d
 |d
 d|d
< q~|S )zvSubtract prev response from curr response.

        Useful when streaming without `incremental_output = True`
        r   r   r   rr   rE   rT   rH   rI   rJ   rK   )r   r   r   replacerY   rW   )r   r   r   r   r   rr   Zprev_resp_copyZprev_choiceZprev_messagerN   Z	tool_callrI   Zprev_functionr_   r_   r`   r   >  s&    


z#ChatTongyi.subtract_client_responsec                 K s>   G dd d}|| j f i |d2 z3 dH W }|V  q$6 dS )z}Because the dashscope SDK doesn't provide an async API,
        we wrap `stream_generate_with_retry` with an async generator.c                   @  sD   e Zd ZddddZddddZddd	d
ZddddZdS )zEChatTongyi.astream_completion_with_retry.<locals>._AioTongyiGeneratorr   	generatorc                 S  s
   || _ d S r   r   )r   r   r_   r_   r`   __init__c  s    zNChatTongyi.astream_completion_with_retry.<locals>._AioTongyiGenerator.__init__zAsyncIterator[Any]r   c                 S  s   | S r   r_   r   r_   r_   r`   	__aiter__f  s    zOChatTongyi.astream_completion_with_retry.<locals>._AioTongyiGenerator.__aiter__c                   s,   t  d | jI d H }|d ur$|S td S r   )asyncioget_running_looprun_in_executor
_safe_nextStopAsyncIteration)r   r]   r_   r_   r`   	__anext__i  s    
zOChatTongyi.astream_completion_with_retry.<locals>._AioTongyiGenerator.__anext__c                 S  s&   zt | jW S  ty    Y d S 0 d S r   )nextr   StopIterationr   r_   r_   r`   r   r  s    zPChatTongyi.astream_completion_with_retry.<locals>._AioTongyiGenerator._safe_nextN)__name__
__module____qualname__r   r   r   r   r_   r_   r_   r`   _AioTongyiGeneratorb  s   	r   r   N)r   )r   r   r   chunkr_   r_   r`   astream_completion_with_retry^  s
    z(ChatTongyi.astream_completion_with_retryzList[BaseMessage]zOptional[List[str]]z"Optional[CallbackManagerForLLMRun]r(   )messagesr|   run_managerr   rC   c           
      K  s   g }| j r`d }| j|f||d|D ]}|d u r8|}q&||7 }q&|d usNJ || | n@| jf ||d|}| jf i |}	|tf i | |	 t|d| j	idS N)r|   r   )r   r|   r   )generationsZ
llm_output)
r   _streamrX   _chunk_to_generation_invocation_paramsr   r&   _chat_generation_from_qwen_respr(   r   )
r   r   r|   r   r   r   Zgeneration_chunkr   paramsr   r_   r_   r`   	_generate}  s:    

zChatTongyi._generatez'Optional[AsyncCallbackManagerForLLMRun]c           
        s   g }| j rjd }| j|f||d|2 z"3 d H W }|d u r@|}q&||7 }q&6 |d usXJ || | nV| jf ||d|}t d tj	| j
fi |I d H }	|tf i | |	 t|d| jidS r   )r   _astreamrX   r   r   r   r   r   	functoolspartialr   r&   r   r(   r   )
r   r   r|   r   r   r   Z
generationr   r   r   r_   r_   r`   
_agenerate  s@    
zChatTongyi._ageneratezIterator[ChatGenerationChunk]c              	   k  s   | j f ||dd|}t| jf i |D ]v\}}|d d d }|d }	|d dkrn|	d	 d
krnd|	vrnq,tf i | j|d|d}
|r|j|
j|
d |
V  q,d S )NTr   r|   r   r   r   r   rr   finish_reasonnullrE   rT   rH   rB   is_last_chunkr   )r   r?   r   r'   r   on_llm_new_tokenr   )r   r   r|   r   r   r   stream_respr   r   rr   r   r_   r_   r`   r     s2    

zChatTongyi._streamz"AsyncIterator[ChatGenerationChunk]c           	   	   K s~   | j f ||dd|}t| jf i |2 zJ3 d H W \}}tf i | j|d|d}|rp|j|j|dI d H  |V  q,6 d S )NTr   r   r   )r   r=   r   r'   r   r   r   )	r   r   r|   r   r   r   r   r   r   r_   r_   r`   r     s     zChatTongyi._astream)r   r|   r   rC   c                 K  s   i | j |}|d ur||d< |dr:|ds:d|d< dd |D }dd t|D }t|d	kr||d
 d
kr|tdnt|d	krtd||d< |S )Nr|   r   toolsTr   c                 S  s   g | ]}t |qS r_   )rt   )re   mr_   r_   r`   
<listcomp>  rj   z1ChatTongyi._invocation_params.<locals>.<listcomp>c                 S  s    g | ]\}}|d  dkr|qS )rD   rR   r_   )re   ir   r_   r_   r`   r     s   rd   r   z-System message can only be the first message.z-There can be only one system message at most.r   )r   rY   rW   lenr   )r   r   r|   r   r   Zmessage_dictsZsystem_message_indicesr_   r_   r`   r     s    
zChatTongyi._invocation_paramszList[Optional[dict]]rq   )llm_outputsrC   c                 C  s   |d d u ri S |d S )Nr   r_   )r   r   r_   r_   r`   _combine_llm_outputs  s    zChatTongyi._combine_llm_outputs)r   rB   r   rC   c                 C  sX   | d d d }t |d |d}|rJt|t|d | d t| d d	d
S t|dS d S )Nr   r   r   rr   )rB   r   
request_idusage)r   r   Ztoken_usagerr   generation_info)rr   )ra   rq   )r   rB   r   r   rr   r_   r_   r`   r     s    
	z*ChatTongyi._chat_generation_from_qwen_respr'   r&   )r   rC   c                 C  s   t t| j| jdS )Nr   )r&   rp   rr   r   r   r_   r_   r`   r   2  s    zChatTongyi._chunk_to_generationzDSequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]]z)Runnable[LanguageModelInput, BaseMessage])r   r   rC   c                   s$   dd |D }t  jf d|i|S )a  Bind tool-like objects to this chat model.

        Args:
            tools: A list of tool definitions to bind to this chat model.
                Can be  a dictionary, pydantic model, callable, or BaseTool. Pydantic
                models, callables, and BaseTools will be automatically converted to
                their schema dictionary representation.
            **kwargs: Any additional parameters to pass to the
                :class:`~langchain.runnable.Runnable` constructor.
        c                 S  s   g | ]}t |qS r_   r0   )re   rS   r_   r_   r`   r   I  rj   z)ChatTongyi.bind_tools.<locals>.<listcomp>r   )superbind)r   r   r   Zformatted_toolsrl   r_   r`   
bind_tools9  s    zChatTongyi.bind_tools)include_rawzUnion[Dict, Type[BaseModel]]z4Runnable[LanguageModelInput, Union[Dict, BaseModel]])schemar   r   rC   c                K  s   |rt d| t|to"t|}| |g}|rDt|gdd}nt|d d }t|dd}|rtj	t
d|B dd	 d
}tj	dd	 d}	|j|	gdd}
t|d|
B S ||B S dS )a  Model wrapper that returns outputs formatted to match the given schema.

        Args:
            schema: The output schema as a dict or a Pydantic class. If a Pydantic class
                then the model output will be an object of that class. If a dict then
                the model output will be a dict. With a Pydantic class the returned
                attributes will be validated, whereas with a dict they will not be. If
                `method` is "function_calling" and `schema` is a dict, then the dict
                must match the OpenAI function-calling spec.
            include_raw: If False then only the parsed structured output is returned. If
                an error occurs during model output parsing it will be raised. If True
                then both the raw model response (a BaseMessage) and the parsed model
                response will be returned. If an error occurs during output parsing it
                will be caught and returned as well. The final output is always a dict
                with keys "raw", "parsed", and "parsing_error".

        Returns:
            A Runnable that takes any ChatModel input and returns as output:

                If include_raw is True then a dict with keys:
                    raw: BaseMessage
                    parsed: Optional[_DictOrPydantic]
                    parsing_error: Optional[BaseException]

                If include_raw is False then just _DictOrPydantic is returned,
                where _DictOrPydantic depends on the schema:

                If schema is a Pydantic class then _DictOrPydantic is the Pydantic
                    class.

                If schema is a dict then _DictOrPydantic is a dict.

        zReceived unsupported arguments T)r   first_tool_onlyrI   rJ   )key_namer   rawc                 S  s   d S r   r_   _r_   r_   r`   <lambda>  rj   z3ChatTongyi.with_structured_output.<locals>.<lambda>)parsedparsing_errorc                 S  s   d S r   r_   r   r_   r_   r`   r     rj   )r   r   )Zexception_key)r   N)r   rk   rc   r2   r   r#   r1   r"   r+   Zassignr   Zwith_fallbacksr*   )r   r   r   r   Zis_pydantic_schemarv   Zoutput_parserr   Zparser_assignZparser_noneZparser_with_fallbackr_   r_   r`   with_structured_outputL  s.    (z!ChatTongyi.with_structured_output)NN)NN)NN)NN)FT)&r   r   r   __doc__propertyr   r   __annotations__r5   r   rq   r   r   r   r   r   r4   Zmodel_configr   r/   r   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   __classcell__r_   r_   r   r`   ru      s\   
 ?	*
' "  %  (  #   )F)\
__future__r   r   r   r   r   operatorr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   Zlangchain_core.callbacksr   r   Zlangchain_core.language_modelsr   Z*langchain_core.language_models.chat_modelsr   Zlangchain_core.messagesr   r   r   r   r   r   r   r   r   r   r   r   r    Z"langchain_core.output_parsers.baser!   Z*langchain_core.output_parsers.openai_toolsr"   r#   r$   r%   Zlangchain_core.outputsr&   r'   r(   Zlangchain_core.runnablesr)   r*   r+   Zlangchain_core.toolsr,   Zlangchain_core.utilsr-   r.   r/   Z%langchain_core.utils.function_callingr1   Zlangchain_core.utils.pydanticr2   Zpydanticr3   r4   r5   r6   Zrequests.exceptionsr7   tenacityr8   r9   r:   r;   r<   Zlangchain_community.llms.tongyir=   r>   r?   	getLoggerr   r   ra   rp   rt   r   ru   r_   r_   r_   r`   <module>   s:   8<
 Z!