
    ghu@                         d Z ddlmZmZmZmZmZ ddl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mZmZmZ ddlmZmZmZmZmZ ddlmZmZm Z m!Z!  G d	 d
eee f                   Z"dS )z?Runnable that selects which branch to run based on a condition.    )AsyncIterator	AwaitableIteratorMappingSequence)AnyCallableOptionalUnioncast)	BaseModel
ConfigDict)override)RunnableRunnableLikeRunnableSerializablecoerce_to_runnable)RunnableConfigensure_config%get_async_callback_manager_for_configget_callback_manager_for_configpatch_config)ConfigurableFieldSpecInputOutputget_unique_config_specsc            
           e Zd ZU dZeeeeef         eee	f         f                  e
d<   eee	f         e
d<   deeeeeef         eegef         eegee         f         f         ef         ef         ddf fdZ ed          Zedefd	            Zeedee         fd
                        Ze	 ddee         dee         f fd            Zeedee         fd                        Z	 ddedee         de de	fdZ!	 ddedee         de de	fdZ"	 ddedee         dee          de#e	         fdZ$	 ddedee         dee          de%e	         fdZ& xZ'S )RunnableBrancha  Runnable that selects which branch to run based on a condition.

    The Runnable is initialized with a list of (condition, Runnable) pairs and
    a default branch.

    When operating on an input, the first condition that evaluates to True is
    selected, and the corresponding Runnable is run on the input.

    If no condition evaluates to True, the default branch is run on the input.

    Parameters:
        branches: A list of (condition, Runnable) pairs.
        default: A Runnable to run if no condition is met.

    Examples:

        .. code-block:: python

            from langchain_core.runnables import RunnableBranch

            branch = RunnableBranch(
                (lambda x: isinstance(x, str), lambda x: x.upper()),
                (lambda x: isinstance(x, int), lambda x: x + 1),
                (lambda x: isinstance(x, float), lambda x: x * 2),
                lambda x: "goodbye",
            )

            branch.invoke("hello") # "HELLO"
            branch.invoke(None) # "goodbye"
    branchesdefaultreturnNc           	         t          |          dk     rd}t          |          |d         }t          |t          t          t
          f          sd}t          |          t          dt          t          d|                              }g }|dd         D ]}t          |t          t          f          s!dt          |           }t          |          t          |          dk    r!d	t          |           }t          |          |\  }}t          d
t          |                    }t          |          }|                    ||f           t                                          ||           dS )a  A Runnable that runs one of two branches based on a condition.

        Args:
            *branches: A list of (condition, Runnable) pairs.
                Defaults a Runnable to run if no condition is met.

        Raises:
            ValueError: If the number of branches is less than 2.
            TypeError: If the default branch is not Runnable, Callable or Mapping.
            TypeError: If a branch is not a tuple or list.
            ValueError: If a branch is not of length 2.
           z-RunnableBranch requires at least two branchesz=RunnableBranch default must be Runnable, callable or mapping.zRunnable[Input, Output]r   Nz5RunnableBranch branches must be tuples or lists, not zARunnableBranch branches must be tuples or lists of length 2, not zRunnable[Input, bool])r   r    )len
ValueError
isinstancer   r	   r   	TypeErrorr   r   tuplelisttypeappendsuper__init__)
selfr   msgr    default_	_branchesbranch	conditionrunnable	__class__s
            \/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_core/runnables/branch.pyr.   zRunnableBranch.__init__G   s   4 x==1ACS//!2,x)
 
 	! RCC.. %'9$~w:W:W'X'X
 
 	ssm 	4 	4Ffudm44 %;,0LL; ;   nn$6{{aF8;FF F  !oo%"(Ix46H6S6STTI)(33Hi23333 	 	
 	
 	
 	
 	
    T)arbitrary_types_allowedc                     dS )zDRunnableBranch is serializable if all its branches are serializable.T clss    r7   is_lc_serializablez!RunnableBranch.is_lc_serializable   s	     tr8   c                 
    g dS )N)	langchainschemar5   r;   r<   s    r7   get_lc_namespacezRunnableBranch.get_lc_namespace   s     3222r8   configc                 L   | j         gd | j        D             z   d | j        D             z   }|D ]T}|                    |                                                              d          	 |                    |          c S Ut                                          |          S )Nc                     g | ]\  }}|S r;   r;   .0_rs      r7   
<listcomp>z3RunnableBranch.get_input_schema.<locals>.<listcomp>       +++TQq+++r8   c                     g | ]\  }}|S r;   r;   rG   rI   rH   s      r7   rJ   z3RunnableBranch.get_input_schema.<locals>.<listcomp>   rK   r8   r+   )r    r   get_input_schemamodel_json_schemagetr-   )r/   rC   	runnablesr5   r6   s       r7   rN   zRunnableBranch.get_input_schema   s    
 \N++T]+++,++T]+++, 	 " 	9 	9H))&11CCEEII&QQ  0088888
 ww''///r8   c                     ddl mm t          d | j        gd | j        D             z   d | j        D             z   D                       }t          fd|D                       rd}t          |          |S )Nr   )CONTEXT_CONFIG_PREFIXCONTEXT_CONFIG_SUFFIX_SETc              3   .   K   | ]}|j         D ]}|V  d S N)config_specs)rG   stepspecs      r7   	<genexpr>z.RunnableBranch.config_specs.<locals>.<genexpr>   sR       (
 (

 )(
 (
  (
 (
 (
 (
 (
 (
 (
r8   c                     g | ]\  }}|S r;   r;   rF   s      r7   rJ   z/RunnableBranch.config_specs.<locals>.<listcomp>       ///A1///r8   c                     g | ]\  }}|S r;   r;   rM   s      r7   rJ   z/RunnableBranch.config_specs.<locals>.<listcomp>   r\   r8   c              3      K   | ]8}|j                                       o|j                                       V  9d S rV   )id
startswithendswith)rG   srS   rT   s     r7   rZ   z.RunnableBranch.config_specs.<locals>.<genexpr>   s_       
 
  DOO122 9788
 
 
 
 
 
r8   z.RunnableBranch cannot contain context setters.)%langchain_core.beta.runnables.contextrS   rT   r   r    r   anyr&   )r/   specsr0   rS   rT   s      @@r7   rW   zRunnableBranch.config_specs   s    	
 	
 	
 	
 	
 	
 	
 	

 ( (
 (
 /////0/////0(
 (
 (
 
 
  
 
 
 
 
 
 
 
 
 
 	"
 CCS//!r8   inputkwargsc                    t          |          }t          |          }|                    d||                    d          p|                                 |                    dd                    }	 t          | j                  D ]\  }}|\  }}	|                    |t          ||
                    d|dz                        	          }
|
r; |	j        |fd
t          ||
                    d|dz                        i|} n9 | j        j        |fd
t          ||
                    d                    i|}n(# t          $ r}|                    |            d}~ww xY w|                    |           |S )ad  First evaluates the condition, then delegate to true or false branch.

        Args:
            input: The input to the Runnable.
            config: The configuration for the Runnable. Defaults to None.
            kwargs: Additional keyword arguments to pass to the Runnable.

        Returns:
            The output of the branch that was run.
        Nrun_namerun_idnamerj   
condition:   tag	callbacksrC   rC   branch:branch:default)r   r   on_chain_startrP   get_namepop	enumerater   invoker   	get_childr    BaseExceptionon_chain_erroron_chain_endr/   rf   rC   rg   callback_managerrun_manageridxr3   r4   r5   expression_valueoutputes                r7   rz   zRunnableBranch.invoke   s    v&&:6BB&55J'':4==??::h--	 6 
 
 	(77  V&,#	8#,#3#3'"-"7"7<Rq<R<R"7"S"S   $4 $ $  $ 	,X_ +"&1&;&;@S#PQ'@S@S&;&T&T      ! F E	 -, '+*?*?DT*?*U*U  
    	 	 	&&q)))	 	  (((s   4CE	 	
E.E))E.c                 \  K   t          |          }t          |          }|                    d||                    d          p|                                 |                    dd                     d{V }	 t          | j                  D ]\  }}|\  }}	|                    |t          ||
                    d|dz                        	           d{V }
|
rA |	j        |fd
t          ||
                    d|dz                        i| d{V } n? | j        j        |fd
t          ||
                    d                    i| d{V }n.# t          $ r!}|                    |           d{V   d}~ww xY w|                    |           d{V  |S )zAsync version of invoke.Nri   rj   rk   rm   rn   ro   rq   rs   rC   rt   ru   )r   r   rv   rP   rw   rx   ry   r   ainvoker   r{   r    r|   r}   r~   r   s                r7   r   zRunnableBranch.ainvoke  s      v&&@HH,;;J'':4==??::h--	 < 
 
 
 
 
 
 
 
 	(77  V&,#	8)2):):'"-"7"7<Rq<R<R"7"S"S   *; * * $ $ $ $ $ $  $ 	#38#3$ $+"&1&;&;@S#PQ'@S@S&;&T&T     $ !$ $      F E	  4t|3   '+*?*?DT*?*U*U   
            	 	 	,,Q/////////	 &&v.........s   <C&E# #
F-F		Fc              +     K   t          |          }t          |          }|                    d||                    d          p|                                 |                    dd                    }d}d}	 t          | j                  D ]\  }}	|	\  }
}|
                    |t          ||
                    d|dz              	          
          }|rd |j        |fdt          ||
                    d|dz              	          i|D ](}|V  |r ||}	 ||z   }# t          $ r d}d}Y $w xY w) nb | j        j        |fdt          ||
                    d          	          i|D ](}|V  |r ||}	 ||z   }# t          $ r d}d}Y $w xY w)n(# t          $ r}|                    |            d}~ww xY w|                    |           dS a  First evaluates the condition, then delegate to true or false branch.

        Args:
            input: The input to the Runnable.
            config: The configuration for the Runnable. Defaults to None.
            kwargs: Additional keyword arguments to pass to the Runnable.

        Yields:
            The output of the branch that was run.

        Raises:
            BaseException: If an error occurs during the execution of the Runnable.
        Nri   rj   rk   Trm   rn   ro   rq   rs   rC   rt   Fru   )r   r   rv   rP   rw   rx   ry   r   rz   r   r{   streamr(   r    r|   r}   r~   r/   rf   rC   rg   r   r   final_outputfinal_output_supportedr   r3   r4   r5   r   chunkr   s                  r7   r   zRunnableBranch.stream1  s     & v&&:6BB&55J'':4==??::h--	 6 
 
 *.!%5	(77 1? 1?V&,#	8#,#3#3'"-"7"7<Rq<R<R"7"S"S   $4 $ $  $ !0" "+"&1&;&;@S#PQ'@S@S&;&T&T     " !" " C C $1 C+3/4!C3?%3GLL'0 !C !C !C37L=B$:$:$:!CC E'* 1T\0 '"-"7"7<L"7"M"M     ? ?E  KKK- ?'/+0LL?/;e/C#, ? ? ?/39> 6 6 6??  	 	 	&&q)))	 	  .....sb   :B&F! !D'&F! 'D85F! 7D88AF! F
F! FF! FF! !
G+GGc                  K   t          |          }t          |          }|                    d||                    d          p|                                 |                    dd                     d{V }d}d}	 t          | j                  D ]\  }}	|	\  }
}|
                    |t          ||
                    d|dz              	          
           d{V }|rk |j        |fdt          ||
                    d|dz              	          i|2 3 d{V }|W V  |r ||}	 ||z   }# t          $ r d}d}Y *w xY w/6  ni | j        j        |fdt          ||
                    d          	          i|2 3 d{V }|W V  |r ||}	 ||z   }# t          $ r d}d}Y *w xY w/6 n.# t          $ r!}|                    |           d{V   d}~ww xY w|                    |           d{V  dS r   )r   r   rv   rP   rw   rx   ry   r   r   r   r{   astreamr(   r    r|   r}   r~   r   s                  r7   r   zRunnableBranch.astream  s     & v&&@HH,;;J'':4==??::h--	 < 
 
 
 
 
 
 
 
 *.!%5	(77 1? 1?V&,#	8)2):):'"-"7"7<Rq<R<R"7"S"S   *; * * $ $ $ $ $ $  $ '7x'7( (+"&1&;&;@S#PQ'@S@S&;&T&T     ( !( ( C C C C C C Ce $1 C+3/4!C3?%3GLL'0 !C !C !C37L=B$:$:$:!CC($ E'* $84<#7$ $'"-"7"7<L"7"M"M  $ $ $ ? ? ? ? ? ? ?%  KKKK- ?'/+0LL?/;e/C#, ? ? ?/39> 6 6 6??$$  	 	 	,,Q/////////	 &&|44444444444sz    BF; E%F; 3D98F; 9E
F; 	E

A F; 
F9F; F$#F; $F52F; 4F55F; ;
G&G!!G&rV   )(__name__
__module____qualname____doc__r   r)   r   r   boolr   __annotations__r   r	   r   r   r.   r   model_configclassmethodr>   r   r*   strrB   r
   r   r+   r   rN   propertyr   rW   r   rz   r   r   r   r   r   __classcell__)r6   s   @r7   r   r   $   sC         > uXeTk2HUF]4KKLMMMMeVm$$$$C
UD[)eWd]+eWio568
  

C
 
C
 C
 C
 C
 C
 C
J : $  L 4    [ 3c 3 3 3 X [3 150 0~.0	i0 0 0 0 0 X0$ d#89    X X2 @D8 88$,^$<8OR8	8 8 8 8v @D. ..$,^$<.OR.	. . . .f ,0T/ T/T/ (T/ 3-	T/
 
&	T/ T/ T/ T/r ,0T5 T5T5 (T5 3-	T5
 
v	T5 T5 T5 T5 T5 T5 T5 T5r8   r   N)#r   collections.abcr   r   r   r   r   typingr   r	   r
   r   r   pydanticr   r   typing_extensionsr   langchain_core.runnables.baser   r   r   r   langchain_core.runnables.configr   r   r   r   r   langchain_core.runnables.utilsr   r   r   r   r   r;   r8   r7   <module>r      s   E E Q Q Q Q Q Q Q Q Q Q Q Q Q Q              + * * * * * * * & & & & & &                                   w5 w5 w5 w5 w5)%-8 w5 w5 w5 w5 w5r8   