o
    `+ i:                     @   s|   d Z ddlmZmZ ddlmZmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ G dd	 d	eZG d
d deZdS )zEChain pipeline where the outputs of one step feed directly into next.    )AnyOptional)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)get_color_mapping)
ConfigDictmodel_validator)Self)Chainc                	   @   s   e Zd ZU dZee ed< ee ed< ee ed< dZe	ed< e
ddd	Zed
ee fddZed
ee fddZeddeded
efddZ	ddeeef dee d
eeef fddZ	ddeeef dee d
eeef fddZdS )SequentialChainz=Chain where the outputs of one chain feed directly into next.chainsinput_variablesoutput_variablesF
return_allTforbidZarbitrary_types_allowedextrareturnc                 C      | j S )zIReturn expected input keys to the chain.

        :meta private:
        )r   self r   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain/chains/sequential.py
input_keys      zSequentialChain.input_keysc                 C   r   z3Return output key.

        :meta private:
        )r   r   r   r   r   output_keys%   r   zSequentialChain.output_keysbeforemodevaluesc                 C   s`  |d }|d }g }d|v r;|d dur;	 |d j }t|t|r;t|t|@ }dd| d| d}t|t|| }|D ]:}t|j|}	|jrW|	|jj }	|	red	|	 d
| }t|||j}|rvd| }t||t|jO }qCd|vr|	ddr||}
n|d j}
|
|d< |S t|d |}	|	rd|	 d}t||S )z6Validate that the correct inputs exist for all chains.r   r   memoryNzThe input key(s)  z are found in the Memory keys (z8) - please use input and memory keys that don't overlap.zMissing required input keys: z, only had z(Chain returned keys that already exist: r   r   Fz/Expected output variables that were not found: .)
Zmemory_variablessetintersectionjoin
ValueErrorr   
differencer!   r   get)clsr    r   r   Zmemory_keysZoverlapping_keysmsgZknown_variableschainZmissing_varsr   r   r   r   validate_chains-   sP   


zSequentialChain.validate_chainsNinputsrun_managerc                    s\   |   |p	t }t| jD ]\}}| }| d|d} | q fdd| jD S )NTZreturn_only_outputs	callbacksc                       i | ]}| | qS r   r   .0kZknown_valuesr   r   
<dictcomp>m       z)SequentialChain._call.<locals>.<dictcomp>)copyr   get_noop_manager	enumerater   	get_childupdater   )r   r/   r0   _run_manager_ir-   r2   outputsr   r7   r   _callb   s   zSequentialChain._callc                    sf   |   |p
t }| }t| jD ]\}}|j d|dI d H } | q fdd| jD S )NTr1   c                    r3   r   r   r4   r7   r   r   r8   ~   r9   z*SequentialChain._acall.<locals>.<dictcomp>)	r:   r   r;   r=   r<   r   Zacallr>   r   )r   r/   r0   r?   r2   r@   r-   rA   r   r7   r   _acallo   s   zSequentialChain._acallN)__name__
__module____qualname____doc__listr
   __annotations__strr   boolr   model_configpropertyr   r   r   classmethoddictr   r.   r   r   rB   r   rC   r   r   r   r   r      sB   
 6




r   c                	   @   s   e Zd ZU dZee ed< dZeed< dZ	e
ed< dZe
ed< ed	d
dZedee
 fddZedee
 fddZedddefddZ	ddee
e
f dee dee
e
f fddZ	ddee
ef dee dee
ef fddZdS )SimpleSequentialChainzCSimple chain where the outputs of one step feed directly into next.r   Fstrip_outputsinput	input_keyoutput
output_keyTr   r   r   c                 C      | j gS )z2Expect input key.

        :meta private:
        )rT   r   r   r   r   r         z SimpleSequentialChain.input_keysc                 C   rW   r   )rV   r   r   r   r   r      rX   z!SimpleSequentialChain.output_keysafterr   c                 C   sl   | j D ]0}t|jdkrd| dt|j d}t|t|jdkr3d| dt|j d}t|q| S )z1Validate that chains are all single input/output.   z=Chains used in SimplePipeline should all have one input, got z with z inputs.z>Chains used in SimplePipeline should all have one output, got z	 outputs.)r   lenr   r(   r   )r   r-   r,   r   r   r   r.      s&   
z%SimpleSequentialChain.validate_chainsNr/   r0   c                 C   s   |pt  }|| j }tdd tt| jD }t| jD ](\}}|j||	d|d  d}| j
r8| }|j||t| d| jd q| j|iS )Nc                 S      g | ]}t |qS r   rK   r5   ir   r   r   
<listcomp>       z/SimpleSequentialChain._call.<locals>.<listcomp>step_rZ   r2   
colorendverbose)r   r;   rT   r   ranger[   r   r<   runr=   rR   stripon_textrK   rh   rV   r   r/   r0   r?   _inputZcolor_mappingr_   r-   r   r   r   rB      s"   


zSimpleSequentialChain._callc                    s   |pt  }|| j }tdd tt| jD }t| jD ].\}}|j||	d|d  dI d H }| j
r<| }|j||t| d| jdI d H  q| j|iS )Nc                 S   r\   r   r]   r^   r   r   r   r`      ra   z0SimpleSequentialChain._acall.<locals>.<listcomp>rb   rZ   rc   rd   re   )r   r;   rT   r   ri   r[   r   r<   Zarunr=   rR   rk   rl   rK   rh   rV   rm   r   r   r   rC      s$   


zSimpleSequentialChain._acallrD   )rE   rF   rG   rH   rI   r
   rJ   rR   rL   rT   rK   rV   r   rM   rN   r   r   r   r	   r.   rP   r   r   rB   r   r   rC   r   r   r   r   rQ      s@   
 




rQ   N)rH   typingr   r   Zlangchain_core.callbacksr   r   Zlangchain_core.utils.inputr   Zpydanticr   r   Ztyping_extensionsr	   Zlangchain.chains.baser
   r   rQ   r   r   r   r   <module>   s    q