
    &`i+                         d dl mZmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ dZe G d de                      Ze G d	 d
e                      Ze G d d                      ZdS )    )AnyDictListOptionalUnion)DAGNodeget_dag_node_str)type_to_string)DeveloperAPI__in_context_manager__c            
       2    e Zd ZdZddddeeeeeee	f         ef         f                  f fdZ
dee         dee	ef         dee	ef         d	ee	ef         fd
Zd ZdefdZde	defdZde	fdZde	fdZdeee	f         defdZd Zd Zde	fdZ xZS )	InputNodeao  Ray dag node used in DAG building API to mark entrypoints of a DAG.

    Should only be function or class method. A DAG can have multiple
    entrypoints, but only one instance of InputNode exists per DAG, shared
    among all DAGNodes.

    Example:

    .. code-block::

                m1.forward
                /       \
        dag_input     ensemble -> dag_output
                \       /
                m2.forward

    In this pipeline, each user input is broadcasted to both m1.forward and
    m2.forward as first stop of the DAG, and authored like

    .. code-block:: python

        import ray

        @ray.remote
        class Model:
            def __init__(self, val):
                self.val = val
            def forward(self, input):
                return self.val * input

        @ray.remote
        def combine(a, b):
            return a + b

        with InputNode() as dag_input:
            m1 = Model.bind(1)
            m2 = Model.bind(2)
            m1_output = m1.forward.bind(dag_input[0])
            m2_output = m2.forward.bind(dag_input.x)
            ray_dag = combine.bind(m1_output, m2_output)

        # Pass mix of args and kwargs as input.
        ray_dag.execute(1, x=2) # 1 sent to m1, 2 sent to m2

        # Alternatively user can also pass single data object, list or dict
        # and access them via list index, object attribute or dict key str.
        ray_dag.execute(UserDataObject(m1=1, m2=2))
        # dag_input.m1, dag_input.m2
        ray_dag.execute([1, 2])
        # dag_input[0], dag_input[1]
        ray_dag.execute({"m1": 1, "m2": 2})
        # dag_input["m1"], dag_input["m2"]
    N)
input_type_other_args_to_resolver   c                2   t          |          dk    st          |          dk    rt          d          i | _        || _        |+t	          |t
                    r|i }t          |          |d<   t                                          g i i |           dS )a  InputNode should only take attributes of validating and converting
        input data rather than the input data itself. User input should be
        provided via `ray_dag.execute(user_input)`.

        Args:
            input_type: Describes the data type of inputs user will be giving.
                - if given through singular InputNode: type of InputNode
                - if given through InputAttributeNodes: map of key -> type
                Used when deciding what Gradio block to represent the input nodes with.
            _other_args_to_resolve: Internal only to keep InputNode's execution
                context throughput pickling, replacement and serialization.
                User should not use or pass this field.
        r   z-InputNode should not take any args or kwargs.Nresult_type_string)other_args_to_resolve)	len
ValueErrorinput_attribute_nodesr   
isinstancetyper   super__init__)selfr   r   argskwargs	__class__s        f/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dag/input_node.pyr   zInputNode.__init__C   s    ( t99>>S[[A--LMMM%'"$!jT&B&B!%-)+&;I*;U;U"#78R;QRRRRR    new_args
new_kwargsnew_optionsnew_other_args_to_resolvec                 "    t          |          S )N)r   )r   r   r"   r#   r$   r%   s        r    
_copy_implzInputNode._copy_impld   s     0IJJJJr!   c                     |                                  s
J d            t          |          dk    rt          |          dk    r|d         S t          |i |S )zExecutor of InputNode.zInputNode is a singleton instance that should be only used in context manager for dag building and execution. See the docstring of class InputNode for examples.   r   )_in_context_managerr   DAGInputDatar   r   r   s      r    _execute_implzInputNode._execute_implm   sl     '')) 	
 	
/	
 	
) t99>>c&kkQ..7NT,V,,,r!   returnc                 T    | j         rt          | j         vrdS | j         t                   S )z2Return if InputNode is created in context manager.F)_bound_other_args_to_resolveIN_CONTEXT_MANAGERr   s    r    r+   zInputNode._in_context_manager{   s2     1	I!)JJJ545GHHr!   keyvalc                     || j         |<   dS )zqSet field in parent DAGNode attribute that can be resolved in both
        pickle and JSON serialization
        Nr1   )r   r4   r5   s      r    set_contextzInputNode.set_context   s     25)#...r!   c                 "    t          | d          S )N__InputNode__r	   r3   s    r    __str__zInputNode.__str__   s    o666r!   c                     t          |t                    s
J d            || j        vrt          | |d          | j        |<   | j        |         S )Nz5Please only access dag input attributes with str key.__getattr__)r   strr   InputAttributeNoder   r4   s     r    r=   zInputNode.__getattr__   sr    
 
 	C 	CB	C 	C 
 d000.@c=/ /D&s+ )#..r!   c                    t          |t          t          f          s
J d            d }| j        #|| j        v rt	          | j        |                   }|| j        vrt          | |d|          | j        |<   | j        |         S )NRPlease only use int index or str as first-level key to access fields of dag input.__getitem__)r   r>   intr   r   r   r?   )r   r4   r   s      r    rC   zInputNode.__getitem__   s    #Sz** 	
 	
*	
 	
*
 
?&3$/+A+A'(<==Jd000.@c=*/ /D&s+ )#..r!   c                 <    |                      t          d           | S )NT)r8   r2   r3   s    r    	__enter__zInputNode.__enter__   s    +T222r!   c                     d S N )r   r   s     r    __exit__zInputNode.__exit__   s    r!   c                 2    d| j         v r| j         d         S dS zvGet type of the output of this DAGNode.

        Generated by ray.experimental.gradio_utils.type_to_string().
        r   Nr7   r3   s    r    get_result_typezInputNode.get_result_type   *    
  4#DDD45IJJ EDr!   )__name__
__module____qualname____doc__r   r   r   r   rD   r>   r   r   r   r(   r.   boolr+   r8   r;   r=   rC   rF   rJ   rM   __classcell__r   s   @r    r   r      s       4 4r JN#	S S S U4eCHot.C)D#DEFS S S S S SBKs)K cNK #s(^	K
 $(S>K K K K- - -IT I I I I5s 5 5 5 5 57 7 7 7 7/s / / / //uS#X /3 / / / /     K K K K K K K K Kr!   r   c            
            e Zd ZdZ	 ddedeeef         dedef fdZde	e
         d	eee
f         d
eee
f         deee
f         fdZd ZdefdZdefdZedeeef         fd            Z xZS )r?   a5  Represents partial access of user input based on an index (int),
     object attribute or dict key (str).

    Examples:

        .. code-block:: python

            with InputNode() as dag_input:
                a = dag_input[0]
                b = dag_input.x
                ray_dag = add.bind(a, b)

            # This makes a = 1 and b = 2
            ray_dag.execute(1, x=2)

            with InputNode() as dag_input:
                a = dag_input[0]
                b = dag_input[1]
                ray_dag = add.bind(a, b)

            # This makes a = 2 and b = 3
            ray_dag.execute(2, 3)

            # Alternatively, you can input a single object
            # and the inputs are automatically indexed from the object:
            # This makes a = 2 and b = 3
            ray_dag.execute([2, 3])
    Ndag_input_noder4   accessor_methodr   c           
          || _         || _        || _        t                                          g i i ||||d           d S )N)rW   r4   rX   r   )_dag_input_node_key_accessor_methodr   r   )r   rW   r4   rX   r   r   s        r    r   zInputAttributeNode.__init__   sc      .	 /"0#2 '1 		
 	
 	
 	
 	
r!   r"   r#   r$   r%   c                 V    t          |d         |d         |d         |d                   S )NrW   r4   rX   r   )r?   r'   s        r    r(   zInputAttributeNode._copy_impl   s9     "%&67%e,%&78%&:;	
 
 	
r!   c                 |   t          | j        t                    r| j        | j                 S | j        }t          | j        t                    r:| j        dk    r|| j                 S | j        dk    rt          || j                  S dS t          | j        t                    r|| j                 S t          d          )a0  Executor of InputAttributeNode.

        Args and kwargs are to match base class signature, but not in the
        implementation. All args and kwargs should be resolved and replaced
        with value in bound_args and bound_kwargs via bottom-up recursion when
        current node is executed.
        rC   r=   rB   N)	r   rZ   r,   r[   r>   r\   getattrrD   r   )r   r   r   user_input_python_objects       r    r.   z InputAttributeNode._execute_impl   s     d*L99 	'	22
 (,';$$)S)) (M993DI>>*m;;"#;TYGGG <;DIs++ /	:: 2  r!   r/   c                 4    t          | d| j         d          S )Nz["z"])r
   r[   r3   s    r    r;   zInputAttributeNode.__str__  s     &849&8&8&8999r!   c                 2    d| j         v r| j         d         S dS rL   r7   r3   s    r    rM   z"InputAttributeNode.get_result_type  rN   r!   c                     | j         S rH   )r[   r3   s    r    r4   zInputAttributeNode.key&  s
    yr!   rH   )rO   rP   rQ   rR   r   r   rD   r>   r   r   r   r   r(   r.   r;   rM   propertyr4   rT   rU   s   @r    r?   r?      sO        D 
 
!
 38_
 	

 
 
 
 
 
 
2
s)
 cN
 #s(^	

 $(S>
 
 
 
  :: : : : :K K K K K U38_    X    r!   r?   c                   8    e Zd ZdZd Zdeeef         defdZ	dS )r,   zIf user passed multiple args and kwargs directly to dag.execute(), we
    generate this wrapper for all user inputs as one object, accessible via
    list index or object attribute key.
    c                 <    t          |          | _        || _        d S rH   )list_args_kwargsr-   s      r    r   zDAGInputData.__init__2  s    $ZZ
r!   r4   r/   c                     t          |t                    r| j        |         S t          |t                    r| j        |         S t          d          )NrB   )r   rD   rh   r>   ri   r   r@   s     r    rC   zDAGInputData.__getitem__6  sV    c3 
	:c?"S!! 	<$$.  r!   N)
rO   rP   rQ   rR   r   r   rD   r>   r   rC   rI   r!   r    r,   r,   +  sW         
  uS#X 3      r!   r,   N)typingr   r   r   r   r   ray.dagr   ray.dag.format_utilsr
   ray.experimental.gradio_utilsr   ray.util.annotationsr   r2   r   r?   r,   rI   r!   r    <module>rp      sA   3 3 3 3 3 3 3 3 3 3 3 3 3 3       1 1 1 1 1 1 8 8 8 8 8 8 - - - - - --  iK iK iK iK iK iK iK iKX o o o o o o o od          r!   