U
    h                     @   st   d Z ddl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 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DictListOptional)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)root_validator)get_color_mapping)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< G dd dZ
eee d	d
dZeee d	ddZeddeedddZdeeef ee eeef dddZdeeef ee eeef dddZdS )SequentialChainz=Chain where the outputs of one chain feed directly into next.chainsinput_variablesoutput_variablesF
return_allc                   @   s   e Zd ZdZdZdS )zSequentialChain.ConfigTforbidN__name__
__module____qualname__Zarbitrary_types_allowedextra r   r   ?/tmp/pip-unpacked-wheel-bo69hh5q/langchain/chains/sequential.pyConfig   s   r   returnc                 C   s   | j S )zIReturn expected input keys to the chain.

        :meta private:
        )r   selfr   r   r   
input_keys   s    zSequentialChain.input_keysc                 C   s   | j S z3Return output key.

        :meta private:
        )r   r   r   r   r   output_keys#   s    zSequentialChain.output_keysT)prevaluesr   c           
      C   sT  |d }|d }t  }d|krr|d dk	rr|d j}t|t|rrt|t|@ }tdd| d| dt|| }|D ]l}t|j|}|jr||jj}|rtd	| d
| ||j	}|rtd| |t|j	O }qd|kr(|
ddr||}	n
|d j	}	|	|d< n(t|d |}|rPtd| d|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: .)listZmemory_variablessetintersection
ValueErrorjoinr   
differencer#   r   get)
clsr"   r   r   Zmemory_keysZoverlapping_keysZknown_variableschainZmissing_varsr   r   r   r   validate_chains+   sH    




zSequentialChain.validate_chainsNinputsrun_managerr   c                    s\   |   |pt }t| jD ](\}}| }| d|d} | q fdd| jD S )NTZreturn_only_outputs	callbacksc                    s   i | ]}| | qS r   r   .0kZknown_valuesr   r   
<dictcomp>j   s      z)SequentialChain._call.<locals>.<dictcomp>)copyr   get_noop_manager	enumerater   	get_childupdater   )r   r2   r3   _run_managerir/   r5   outputsr   r9   r   _call_   s    zSequentialChain._callc                    sd   |   |pt }| }t| jD ](\}}|j d|dI d H } | q& fdd| jD S )NTr4   c                    s   i | ]}| | qS r   r   r6   r9   r   r   r:   y   s      z*SequentialChain._acall.<locals>.<dictcomp>)	r;   r   r<   r>   r=   r   Zacallr?   r   )r   r2   r3   r@   r5   rA   r/   rB   r   r9   r   _acalll   s      zSequentialChain._acall)N)N)r   r   r   __doc__r   r
   __annotations__strr   boolr   propertyr   r   r   r   r0   r   r   rC   r   r   rD   r   r   r   r   r      s0   
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< G d	d
 d
Zeee
 dddZeee
 dddZedddeedddZdee
e
f ee ee
e
f dddZdee
ef ee ee
ef d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_keyc                   @   s   e Zd ZdZdZdS )zSimpleSequentialChain.ConfigTr   Nr   r   r   r   r   r      s   r   r   c                 C   s   | j gS )z2Expect input key.

        :meta private:
        )rM   r   r   r   r   r      s    z SimpleSequentialChain.input_keysc                 C   s   | j gS r   )rO   r   r   r   r   r      s    z!SimpleSequentialChain.output_keysT)r    Zskip_on_failurer!   c                 C   sf   |d D ]X}t |jdkr6td| dt |j dt |jdkrtd| dt |j dq|S )z1Validate that chains are all single input/output.r      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.)lenr   r*   r   )r.   r"   r/   r   r   r   r0      s    z%SimpleSequentialChain.validate_chainsNr1   c                 C   s   |p
t  }|| j }tdd tt| jD }t| jD ]P\}}|j||	d|d  d}| j
rp| }|j||t| d| jd q<| j|iS )Nc                 S   s   g | ]}t |qS r   rG   r7   rA   r   r   r   
<listcomp>   s     z/SimpleSequentialChain._call.<locals>.<listcomp>step_rP   r5   
colorendverbose)r   r<   rM   r	   rangerQ   r   r=   runr>   rK   stripon_textrG   r[   rO   r   r2   r3   r@   _inputZcolor_mappingrA   r/   r   r   r   rC      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
rv| }|j||t| d| jdI d H  q<| j|iS )Nc                 S   s   g | ]}t |qS r   rR   rS   r   r   r   rT      s     z0SimpleSequentialChain._acall.<locals>.<listcomp>rU   rP   rV   rW   rX   )r   r<   rM   r	   r\   rQ   r   r=   Zarunr>   rK   r^   r_   rG   r[   rO   r`   r   r   r   rD      s"    
  
  zSimpleSequentialChain._acall)N)N)r   r   r   rE   r   r
   rF   rK   rH   rM   rG   rO   r   rI   r   r   r   r   r0   r   r   rC   r   r   rD   r   r   r   r   rJ   |   s0   

 

 

rJ   N)rE   typingr   r   r   r   Zlangchain_core.callbacksr   r   Zlangchain_core.pydantic_v1r   Zlangchain_core.utils.inputr	   Zlangchain.chains.baser
   r   rJ   r   r   r   r   <module>   s   m