
    gh^                     .   d Z ddlZddlZddlZddlmZ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 ddlmZ ddlmZmZ dd	lmZmZ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% e
rddl&m'Z'  G d dee e!f                   Z(dede)fdZ*dede)fdZ+dS )z:Runnable that can fallback to other Runnables if it fails.    N)AsyncIteratorIteratorSequence)wraps)TYPE_CHECKINGAnyOptionalUnion)	BaseModel
ConfigDict)override)RunnableRunnableSerializable)RunnableConfigensure_config%get_async_callback_manager_for_configget_callback_manager_for_configget_config_listpatch_configset_config_context)ConfigurableFieldSpecInputOutputcoro_with_contextget_unique_config_specs)py_anext)AsyncCallbackManagerForChainRunc                      e Zd ZU dZeeef         ed<   	 eeeef                  ed<   	 e	fZ
eee         df         ed<   	 dZee         ed<   	  ed	          Zeed
ee         fd                        Zeed
ee         fd                        Ze	 d"dee         d
ee         fd            Ze	 d"dee         d
ee         fd            Zeed
ee         fd                        Zeed
efd                        Z eed
ee         fd                        Z!ed
e"eeef                  fd            Z#e	 d"dedee         de$d
efd            Z%e	 d"dedee         dee$         d
efd            Z&e	 d"dddee         dee'eee         f                  dedee$         d
ee         f
d            Z(e	 d"dddee         dee'eee         f                  dedee$         d
ee         f
d            Z)e	 d"dedee         dee$         d
e"e         fd            Z*e	 d"dedee         dee$         d
e+e         fd            Z,d ed
e$fd!Z-dS )#RunnableWithFallbacksu  Runnable that can fallback to other Runnables if it fails.

    External APIs (e.g., APIs for a language model) may at times experience
    degraded performance or even downtime.

    In these cases, it can be useful to have a fallback Runnable that can be
    used in place of the original Runnable (e.g., fallback to another LLM provider).

    Fallbacks can be defined at the level of a single Runnable, or at the level
    of a chain of Runnables. Fallbacks are tried in order until one succeeds or
    all fail.

    While you can instantiate a ``RunnableWithFallbacks`` directly, it is usually
    more convenient to use the ``with_fallbacks`` method on a Runnable.

    Example:

        .. code-block:: python

            from langchain_core.chat_models.openai import ChatOpenAI
            from langchain_core.chat_models.anthropic import ChatAnthropic

            model = ChatAnthropic(
                model="claude-3-haiku-20240307"
            ).with_fallbacks([ChatOpenAI(model="gpt-3.5-turbo-0125")])
            # Will usually use ChatAnthropic, but fallback to ChatOpenAI
            # if ChatAnthropic fails.
            model.invoke('hello')

            # And you can also use fallbacks at the level of a chain.
            # Here if both LLM providers fail, we'll fallback to a good hardcoded
            # response.

            from langchain_core.prompts import PromptTemplate
            from langchain_core.output_parser import StrOutputParser
            from langchain_core.runnables import RunnableLambda

            def when_all_is_lost(inputs):
                return ("Looks like our LLM providers are down. "
                        "Here's a nice 🦜️ emoji for you instead.")

            chain_with_fallback = (
                PromptTemplate.from_template('Tell me a joke about {topic}')
                | model
                | StrOutputParser()
            ).with_fallbacks([RunnableLambda(when_all_is_lost)])
    runnable	fallbacks.exceptions_to_handleNexception_keyT)arbitrary_types_allowedreturnc                     | j         j        S N)r    	InputTypeselfs    _/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_core/runnables/fallbacks.pyr(   zRunnableWithFallbacks.InputTypem   s     }&&    c                     | j         j        S r'   )r    
OutputTyper)   s    r+   r.   z RunnableWithFallbacks.OutputTyper   s     }''r,   configc                 6    | j                             |          S r'   )r    get_input_schemar*   r/   s     r+   r1   z&RunnableWithFallbacks.get_input_schemaw   s     }--f555r,   c                 6    | j                             |          S r'   )r    get_output_schemar2   s     r+   r4   z'RunnableWithFallbacks.get_output_schema}   s     }..v666r,   c                 N    t          d | j        g| j        D                       S )Nc              3   .   K   | ]}|j         D ]}|V  d S r'   )config_specs).0stepspecs      r+   	<genexpr>z5RunnableWithFallbacks.config_specs.<locals>.<genexpr>   sP       '
 '
)'
 '
  '
 '
 '
 '
 '
 '
 '
r,   )r   r    r!   r)   s    r+   r7   z"RunnableWithFallbacks.config_specs   s>     ' '
 '
88'
 '
 '
 
 
 	
r,   c                     dS )NT clss    r+   is_lc_serializablez(RunnableWithFallbacks.is_lc_serializable   s	     tr,   c                 
    g dS )zmGet the namespace of the langchain object.

        Defaults to ["langchain", "schema", "runnable"].
        )	langchainschemar    r=   r>   s    r+   get_lc_namespacez&RunnableWithFallbacks.get_lc_namespace   s     3222r,   c              #   6   K   | j         V  | j        E d{V  dS )z-Iterator over the Runnable and its fallbacks.Nr    r!   r)   s    r+   	runnableszRunnableWithFallbacks.runnables   s6       m>!!!!!!!!!r,   inputkwargsc           	      h   | j         7t          |t                    s"dt          |           d}t	          |          t          |          }t          |          }|                    d ||                    d          p| 	                                |
                    dd                     }d }d }| j        D ]}		 | j         r|
||| j         <   t          ||                                          }
t          |
          5 } |j        |	j        ||fi |}d d d            n# 1 swxY w Y   |                    |           |c S # | j        $ r}||}|}Y d }~d }~wt&          $ r}|                    |            d }~ww xY w|d}t	          |          |                    |           |NXIf 'exception_key' is specified then input must be a dictionary.However found a type of 
 for inputrun_namerun_idnamerO   	callbacksz$No error stored at end of fallbacks.)r#   
isinstancedicttype
ValueErrorr   r   on_chain_startgetget_namepoprG   r   	get_childr   runinvokeon_chain_endr"   BaseExceptionon_chain_error)r*   rH   r/   rI   msgcallback_managerrun_managerfirst_error
last_errorr    child_configcontextoutputes                 r+   r^   zRunnableWithFallbacks.invoke   s_    )*UD2I2I)C+/;;C C C  S//!v&&:6BB&55J'':4==??::h--	 6 
 
 
 	 	H% ;**@0:E$,-+Fk>S>S>U>UVVV'55 (W[   !	 F               ((000 ,   &"#K





    **1--- 8CS//!"";///sI   ?AE
D&E
&D*	*E
-D*	.E


FEF,FFc           	        K   | j         7t          |t                    s"dt          |           d}t	          |          t          |          }t          |          }|                    d ||                    d          p| 	                                |
                    dd                      d {V }d }d }| j        D ]}		 | j         r|
||| j         <   t          ||                                          }
t          |
          5 } |j        |	j        ||fi |}t#          ||           d {V }d d d            n# 1 swxY w Y   |                    |           d {V  |c S # | j        $ r}||}|}Y d }~d }~wt(          $ r!}|                    |           d {V   d }~ww xY w|d}t	          |          |                    |           d {V  |rK   )r#   rT   rU   rV   rW   r   r   rX   rY   rZ   r[   rG   r   r\   r   r]   ainvoker   r_   r"   r`   ra   )r*   rH   r/   rI   rb   rc   rd   re   rf   r    rg   rh   corori   rj   s                  r+   rl   zRunnableWithFallbacks.ainvoke   s      )*UD2I2I)C+/;;C C C  S//!v&&@HH,;;J'':4==??::h--	 < 
 
 
 
 
 
 
 
 
 	 	H% ;**@0:E$,-+Fk>S>S>U>UVVV'55 D&7;x'7QQ&QQD#4T7#C#CCCCCCCFD D D D D D D D D D D D D D D "..v666666666 ,   &"#K





    !00333333333 8CS//!((555555555sI   AE.,E8E.E	E.E	E..
F18FF1F,,F1F)return_exceptionsinputsrn   c                    ddl m  j        At          d |D                       s(dt	          |d                    d}t          |          |sg S t          |t          |                    fdD             } fdt          ||          D             i }t          t          |                    }i }	d }
 j        D ]l} |j        d t          |                                          D             fd	t          |          D             fd
di|}t          t          |                                                                          |          D ]\  \  }}}t!          |t"                    r7t!          | j                  s"|s|
p|}
n||	|<   |                    |           Tt!          | j                  r j        r
|| j        <   ||	|<   |                             |           |||<   |                    |           |	                    |d            |
r|
|s nnt          |	                                          }|D ] \  }}|                             |           !|s|r|d         d         |                    |	           d t          |                                          D             S )Nr   )CallbackManagerc              3   @   K   | ]}t          |t                    V  d S r'   rT   rU   r8   rH   s     r+   r;   z.RunnableWithFallbacks.batch.<locals>.<genexpr>  =       6
 6
(-Jud##6
 6
 6
 6
 6
 6
r,   YIf 'exception_key' is specified then inputs must be dictionaries.However found a type of rM   c                     g | ]W}                     |                    d           dd|                    d          d|                    d          d          XS rS   NFtagsmetadata)inheritable_callbackslocal_callbacksverboseinheritable_tags
local_tagsinheritable_metadatalocal_metadata	configurerY   )r8   r/   rq   s     r+   
<listcomp>z/RunnableWithFallbacks.batch.<locals>.<listcomp>  sy     
 
 
  %%&,jj&=&= $!'F!3!3%+ZZ
%;%;# &  
 
 
r,   c                     g | ]s\  }}}|                     d t          |t                    r|nd|i|                    d          p                                |                    dd                     tS )NrH   rN   rO   rP   )rX   rT   rU   rY   rZ   r[   r8   cmrH   r/   r*   s       r+   r   z/RunnableWithFallbacks.batch.<locals>.<listcomp>*  s     
 
 
 "E6 #E400Fw6FZZ
++>t}}zz(D11	   
 
 
r,   c                     g | ]\  }}|S r=   r=   r8   _rH   s      r+   r   z/RunnableWithFallbacks.batch.<locals>.<listcomp>:      AAA81eAAAr,   c                 n    g | ]1}t          |         |                                                    2S rR   r   r\   r8   iconfigsrun_managerss     r+   r   z/RunnableWithFallbacks.batch.<locals>.<listcomp>;  L         !|A7P7P7R7RSSS  r,   rn   T   c                     g | ]\  }}|S r=   r=   r8   r   ri   s      r+   r   z/RunnableWithFallbacks.batch.<locals>.<listcomp>`      BBB91fBBBr,   ) langchain_core.callbacks.managerrq   r#   allrV   rW   r   lenziprU   	enumeraterG   batchsorteditemscopyrT   r`   r"   r[   r_   ra   update)r*   ro   r/   rn   rI   rb   callback_managers	to_return	run_againhandled_exceptionsfirst_to_raiser    outputsr   rH   ri   sorted_handled_exceptionserrorrq   r   r   s   `                 @@@r+   r   zRunnableWithFallbacks.batch  s    	EDDDDD)# 6
 6
176
 6
 6
 3
 3
)G+/q	??G G G  S//! 	I "&#f++66
 
 
 
 "
 
 

 
 
 
 &)):FG%L%L
 
 
 %'	6**++	79  	  	H$hnAAvioo.?.?'@'@AAA     $I..  	 	 #'	 	 	G '*&1A1A1G1G1I1I*J*JG&T&T 4 4"
EFfm44 4ZD5> > 4 - 7)7)A606*1-MM!$$$$(ABB 4) ;4:d01,2&q)) O00888#)IaLMM!$$$&**1d3333 %$$  %++=+C+C+E+E$F$F!1 	2 	2HAuO**51111  	2%> 	2+A.q11+,,,BBy/@/@(A(ABBBBr,   c                   K   ddl m  j        At          d |D                       s(dt	          |d                    d}t          |          |sg S t          |t          |                    fdD             }t          j	         fdt          ||          D               d {V i }t          t          |                    }i }	d }
 j        D ]x} |j        d t          |                                          D             fd	t          |          D             fd
di| d {V }t          t          |                                                                          |          D ]\  \  }}}t%          |t&                    r7t%          | j                  s"|s|
p|}
n||	|<   |                    |           Tt%          | j                  r j        r
|| j        <   ||	|<   |||<   |                             |           d {V  |                    |           |	                    |d            |
r|
|s nzt          |	                                          }t          j	        fd|D               d {V  |s|r|d         d         |                    |	           d t          |                                          D             S )Nr   )AsyncCallbackManagerc              3   @   K   | ]}t          |t                    V  d S r'   rs   rt   s     r+   r;   z/RunnableWithFallbacks.abatch.<locals>.<genexpr>m  ru   r,   rv   rM   c                     g | ]W}                     |                    d           dd|                    d          d|                    d          d          XS rx   r   )r8   r/   r   s     r+   r   z0RunnableWithFallbacks.abatch.<locals>.<listcomp>{  sy     
 
 
  !**&,jj&=&= $!'F!3!3%+ZZ
%;%;# +  
 
 
r,   c           
   3      K   | ]\\  }}}|                     d ||                    d          p                                |                    dd                     V  ]d S )NrN   rO   rP   )rX   rY   rZ   r[   r   s       r+   r;   z/RunnableWithFallbacks.abatch.<locals>.<genexpr>  s         &Bv !!J//B4==??!::h55	 "       r,   c                     g | ]\  }}|S r=   r=   r   s      r+   r   z0RunnableWithFallbacks.abatch.<locals>.<listcomp>  r   r,   c                 n    g | ]1}t          |         |                                                    2S r   r   r   s     r+   r   z0RunnableWithFallbacks.abatch.<locals>.<listcomp>  r   r,   rn   Tc              3   T   K   | ]"\  }}|                              |          V  #d S r'   )ra   )r8   r   r   r   s      r+   r;   z/RunnableWithFallbacks.abatch.<locals>.<genexpr>  sM        Au Q..u55     r,   r   c                     g | ]\  }}|S r=   r=   r   s      r+   r   z0RunnableWithFallbacks.abatch.<locals>.<listcomp>  r   r,   )r   r   r#   r   rV   rW   r   r   asynciogatherr   rU   r   rG   abatchr   r   r   rT   r`   r"   r[   r_   r   )r*   ro   r/   rn   rI   rb   r   r   r   r   r   r    r   r   rH   ri   r   r   r   r   s   `                @@@r+   r   zRunnableWithFallbacks.abatchb  sA      	JIIIII)# 6
 6
176
 6
 6
 3
 3
)G+/q	??G G G  S//! 	I "&#f++66
 
 
 
 "
 
 
 ELN    *-->)P)P  
E
 
?
 
?
 
?
 
?
 
?
 
?
 	6**++	79 "	 "	H+HOAAvioo.?.?'@'@AAA     $I..  	 	 #'	 	 	 	 	 	 	 	 	G '*&1A1A1G1G1I1I*J*JG&T&T 4 4"
EFfm44 4ZD5> > 4 - 7)7)A606*1-MM!$$$$(ABB 4) ;4:d01,2&q))#)IaL&q/66v>>>>>>>>>MM!$$$&**1d3333 %$$  %++=+C+C+E+E$F$F!n    9  
 	
 	
 	
 	
 	
 	
 	
 ! 	2%> 	2+A.q11+,,,BBy/@/@(A(ABBBBr,   c           	   +   (  K   | j         7t          |t                    s"dt          |           d}t	          |          t          |          }t          |          }|                    d ||                    d          p| 	                                |
                    dd                     }d }d }| j        D ]}		 | j         r|
||| j         <   t          ||                                          }
t          |
          5 } |j        |	j        |fi |}|                    t"          |          }d d d            n# 1 swxY w Y   d } nB# | j        $ r}||n|}|}Y d }~d }~wt&          $ r}|                    |            d }~ww xY w|r|                    |           ||V  |}	 |D ]}|V  	 ||z   }# t*          $ r d }Y w xY wn(# t&          $ r}|                    |            d }~ww xY w|                    |           d S NrL   rM   rN   rO   rP   rR   )r#   rT   rU   rV   rW   r   r   rX   rY   rZ   r[   rG   r   r\   r   r]   streamnextr"   r`   ra   	TypeErrorr_   r*   rH   r/   rI   rb   rc   rd   re   rf   r    rg   rh   r   chunkrj   ri   s                   r+   r   zRunnableWithFallbacks.stream  s      )*UD2I2I)C+/;;C C C  S//!v&&:6BB&55J'':4==??::h--	 6 
 
 
 	 	H% ;**@0:E$,-+Fk>S>S>U>UVVV'55 >(W[   ! F
 %,KKf$=$=E> > > > > > > > > > > > > > > # ,   #.#6aaK





    **1---  	&&{333#(		 " ""#e^FF  " " "!FFF"	"  	 	 	&&q)))	 	  (((((s   AE0E6EE	E	E	
E
FE((F5FF3G <GG GG GG 
G:G55G:c           	     z  K   | j         7t          |t                    s"dt          |           d}t	          |          t          |          }t          |          }|                    d ||                    d          p| 	                                |
                    dd                      d {V }d }d }| j        D ]}		 | j         r|
||| j         <   t          ||                                          }
t          |
          5 } |	j        ||
fi |}t!          t#          |          |           d {V }d d d            n# 1 swxY w Y   d } nH# | j        $ r}||n|}|}Y d }~d }~wt&          $ r!}|                    |           d {V   d }~ww xY w|r|                    |           d {V  ||W V  |}	 |2 3 d {V }|W V  	 ||z   }# t*          $ r d }Y !w xY w6 n.# t&          $ r!}|                    |           d {V   d }~ww xY w|                    |           d {V  d S r   )r#   rT   rU   rV   rW   r   r   rX   rY   rZ   r[   rG   r   r\   r   astreamr   r   r"   r`   ra   r   r_   r   s                   r+   r   zRunnableWithFallbacks.astream  s      )*UD2I2I)C+/;;C C C  S//!v&&@HH,;;J'':4==??::h--	 < 
 
 
 
 
 
 
 
 
 	 	H% ;**@0:E$,-+Fk>S>S>U>UVVV'55 O-X-$  ! F
 #4HV4D4Dg"N"NNNNNNNEO O O O O O O O O O O O O O O # ,   #.#6aaK





    !00333333333  	,,[999999999#(		% " " " " " " "e"#e^FF  " " "!FFF"	  &v  	 	 	,,Q/////////	 &&v...........s   AE3E?EE	EE	E
F$E11F>FF	G2 G0G2 GG2 G-*G2 ,G--G2 2
H<HHrQ   c                      t           j                  t                    r4t                    dt          dt          dt          f fd            }|S S )a  Get an attribute from the wrapped Runnable and its fallbacks.

        Returns:
            If the attribute is anything other than a method that outputs a Runnable,
            returns getattr(self.runnable, name). If the attribute is a method that
            does return a new Runnable (e.g. llm.bind_tools([...]) outputs a new
            RunnableBinding) then self.runnable and each of the runnables in
            self.fallbacks is replaced with getattr(x, name).

        Example:
            .. code-block:: python

                from langchain_openai import ChatOpenAI
                from langchain_anthropic import ChatAnthropic

                gpt_4o = ChatOpenAI(model="gpt-4o")
                claude_3_sonnet = ChatAnthropic(model="claude-3-sonnet-20240229")
                llm = gpt_4o.with_fallbacks([claude_3_sonnet])

                llm.model_name
                # -> "gpt-4o"

                # .bind_tools() is called on both ChatOpenAI and ChatAnthropic
                # Equivalent to:
                # gpt_4o.bind_tools([...]).with_fallbacks([claude_3_sonnet.bind_tools([...])])
                llm.bind_tools([...])
                # -> RunnableWithFallbacks(
                    runnable=RunnableBinding(bound=ChatOpenAI(...), kwargs={"tools": [...]}),
                    fallbacks=[RunnableBinding(bound=ChatAnthropic(...), kwargs={"tools": [...]})],
                )

        argsrI   r%   c                       | i |}g }j         D ]-}t          |          }|                     || i |           . j        di i                                 ||dS )NrF   r=   )r!   getattrappend	__class__
model_dump)	r   rI   new_runnablenew_fallbacksfallbackfallback_attrattrrQ   r*   s	         r+   wrappedz2RunnableWithFallbacks.__getattr__.<locals>.wrappedl  s    #tT4V44 " $ I IH$+Hd$;$;M!(()G)G)GHHHH%t~  //++$0%2    r,   )r   r    _returns_runnabler   r   )r*   rQ   r   r   s   `` @r+   __getattr__z!RunnableWithFallbacks.__getattr__H  s    B t}d++T"" 	4[[s c c        [ Nr,   r'   ).__name__
__module____qualname____doc__r   r   r   __annotations__r   	Exceptionr"   tuplerV   r`   r#   r	   strr   model_configpropertyr   r(   r.   r   r   r1   r4   listr   r7   classmethodboolr@   rD   r   rG   r   r^   rl   r
   r   r   r   r   r   r   r=   r,   r+   r   r   )   sC        . .` uf}%%%%$/0000)=FL%] 3S 89HHH $(M8C='''.
 : $  L '4; ' ' ' X X' (DL ( ( ( X X( 156 6~.6	i6 6 6 X6
 157 7~.7	i7 7 7 X7
 
d#89 
 
 
 X X
 4    X [ 3c 3 3 3 X [3 "8HUF]$;< " " " X"
 ?C/ //$,^$</OR/	/ / / X/b  ,0/ // (/ 3-	/
 
/ / / X/b  IM[C
 #([C [C [CU[C ~tN/CCDE[C
  [C 3-[C 
f[C [C [C X[Cz  IMcC
 #(cC cC cCUcC ~tN/CCDEcC
  cC 3-cC 
fcC cC cC XcCJ  ,0=) =)=) (=) 3-	=)
 
&	=) =) =) X=)~  ,0=/ =/=/ (=/ 3-	=/
 
v	=/ =/ =/ X=/~6 6 6 6 6 6 6 6r,   r   r   r%   c                     t          |           sdS t          j        |                               d          }t	          |ot          |                    S )NFr%   )callabletypingget_type_hintsrY   r   _is_runnable_type)r   return_types     r+   r   r     sO    D>> u'--11(;;K> 1+ > >???r,   type_c                 $   t          j        |           rt          | t                    S t	          | dd           }t          j        |          rt          |t                    S |t
          j        u rt          d | j        D                       S dS )N
__origin__c              3   4   K   | ]}t          |          V  d S r'   )r   )r8   ts     r+   r;   z$_is_runnable_type.<locals>.<genexpr>  s+      @@A$Q''@@@@@@r,   F)	inspectisclass
issubclassr   r   r   r
   r   __args__)r   origins     r+   r   r     s    u +%***UL$//Fv ,&(+++@@@@@@@@5r,   ),r   r   r   r   collections.abcr   r   r   	functoolsr   r   r   r	   r
   pydanticr   r   typing_extensionsr   langchain_core.runnables.baser   r   langchain_core.runnables.configr   r   r   r   r   r   r   langchain_core.runnables.utilsr   r   r   r   r   langchain_core.utils.aiterr   r   r   r   r   r   r   r=   r,   r+   <module>r      s6   @ @    = = = = = = = = = =                  + * * * * * * * & & & & & & H H H H H H H H                               0 / / / / / QPPPPPPU	 U	 U	 U	 U	0? U	 U	 U	p@C @D @ @ @ @S T      r,   