
    &`iJ                     n   d dl Z d dlZd dl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 d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZmZ d d
lmZ  e j         e          Z! ed           G d d                      Z" ed           G d d                      Z#de#defdZ$dede#fdZ%dS )    N)deepcopy)AnyCallableDictListOptionalTupleUnion)DeploymentConfigReplicaConfigRequestRouterConfighandle_num_replicas_auto)SERVE_LOGGER_NAME)ServeUsageTag)DEFAULTDefault)AutoscalingConfig)DeploymentSchemaLoggingConfigRayActorOptionsSchema)	PublicAPIstable)	stabilityc                       e Zd ZdZddZdS )Applicationa  One or more deployments bound with arguments that can be deployed together.

    Can be passed into another `Deployment.bind()` to compose multiple deployments in a
    single application, passed to `serve.run`, or deployed via a Serve config file.

    For example, to define an Application and run it in Python:

        .. code-block:: python

            from ray import serve
            from ray.serve import Application

            @serve.deployment
            class MyDeployment:
                pass

            app: Application = MyDeployment.bind(OtherDeployment.bind())
            serve.run(app)

    To run the same app using the command line interface (CLI):

        .. code-block:: bash

            serve run python_file:app

    To deploy the same app via a config file:

        .. code-block:: yaml

            applications:
                my_app:
                    import_path: python_file:app

    bound_deployment
Deploymentc                     || _         d S N)_bound_deployment)selfr   s     h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/deployment.py__init__zApplication.__init__;   s    !1    N)r   r   )__name__
__module____qualname____doc__r#    r$   r"   r   r      s3        ! !F2 2 2 2 2 2r$   r   c            1       R   e Zd ZdZ	 	 d4dedededee         ddf
d	Zdefd
Z	e
defd            Ze
dee         fd            Ze
deeef         fd            Ze
defd            Ze
defd            Ze
defd            Ze
defd            Ze
d             Ze
dee         fd            Ze
dee         fd            Ze
dee         fd            Ze
dee         fd            Ze
defd            ZdefdZd Zde fdZ!de"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        e"j#        de"j#        fdee         de$e         de$e         de$eeeef                           de$eedf                  de$ee                  d e$e%eee&f                           d!e$e         d"e$e         d#e$ee                  d$e$e         d%e$e         d&e$eee'df                  d'e$e&         d(e$e&         d)e$e&         d*e$e&         de$eee(df                  d+e$eee)df                  d,e$ee                  d-e$eeef                  d.e*d/e$e         dd f0d0Z+d1 Z,d2 Z-d3 Z.dS )5r   a  Class (or function) decorated with the `@serve.deployment` decorator.

    This is run on a number of replica actors. Requests to those replicas call
    this class.

    One or more deployments can be composed together into an `Application` which is
    then run via `serve.run` or a config file.

    Example:

    .. code-block:: python

        @serve.deployment
        class MyDeployment:
            def __init__(self, name: str):
                self._name = name

            def __call__(self, request):
                return "Hello world!"

        app = MyDeployment.bind()
        # Run via `serve.run` or the `serve run` CLI command.
        serve.run(app)

    NFnamedeployment_configreplica_configversionreturnc                     |st          d          |                     |           |$t          |t                    st	          d          || _        || _        || _        || _        d S )NzZThe Deployment constructor should not be called directly. Use `@serve.deployment` instead.zversion must be a string.)	RuntimeError_validate_name
isinstancestr	TypeError_name_version_deployment_config_replica_config)r!   r+   r,   r-   r.   	_internals         r"   r#   zDeployment.__init__\   s      	=   	D!!!:gs#;#;7888
"3-r$   c                     t          |t                    st          d          d|v rt          j        d| d           d S d S )Nzname must be a string.#zvDeployment names should not contain the '#' character, this will raise an error starting in Ray 2.46.0. Current name: .)r3   r4   r5   warningswarn)r!   r+   s     r"   r2   zDeployment._validate_namer   sj    $$$ 	64555 $;;M)!%) ) )     ;r$   c                     | j         S )zUnique name of this deployment.r6   r!   s    r"   r+   zDeployment.name}   s     zr$   c                     | j         S r   )r7   rB   s    r"   r.   zDeployment.version   s
    }r$   c                     | j         j        S )z8Underlying class or function that this deployment wraps.)r9   deployment_defrB   s    r"   func_or_classzDeployment.func_or_class   s     #22r$   c                     | j         j        S )zTarget number of replicas.)r8   num_replicasrB   s    r"   rH   zDeployment.num_replicas   s     &33r$   c                     | j         j        S )z%Dynamic user-provided config options.)r8   user_configrB   s    r"   rJ   zDeployment.user_config   s     &22r$   c                     | j         j        S )z4Max number of requests a replica can handle at once.)r8   max_ongoing_requestsrB   s    r"   rL   zDeployment.max_ongoing_requests   s     &;;r$   c                     | j         j        S )zDMax number of requests that can be queued in each deployment handle.)r8   max_queued_requestsrB   s    r"   rN   zDeployment.max_queued_requests   s     &::r$   c                      t          d          )N`route_prefix` can no longer be specified at the deployment level. Pass it to `serve.run` or in the application config instead.)
ValueErrorrB   s    r"   route_prefixzDeployment.route_prefix   s    K
 
 	
r$   c                     | j         j        S )z:Actor options such as resources required for each replica.)r9   ray_actor_optionsrB   s    r"   rT   zDeployment.ray_actor_options   s     #55r$   c                     | j         j        S r   )r9   	init_argsrB   s    r"   rV   zDeployment.init_args   s    #--r$   c                     | j         j        S r   )r9   init_kwargsrB   s    r"   rX   zDeployment.init_kwargs   s    #//r$   c                 :    t                               d           d S )NzUDeprecationWarning: `Deployment.url` is deprecated and will be removed in the future.)loggerwarningrB   s    r"   urlzDeployment.url   s$    1	
 	
 	
 tr$   c                     | j         j        S r   r8   logging_configrB   s    r"   r_   zDeployment.logging_config   s    &55r$   r_   c                     || j         _        d S r   r^   )r!   r_   s     r"   set_logging_configzDeployment.set_logging_config   s    1?...r$   c                      t          d          )NzNDeployments cannot be constructed directly. Use `deployment.deploy() instead.`)r1   rB   s    r"   __call__zDeployment.__call__   s    1
 
 	
r$   c                 J    t          |                     ||                    S )zBind the arguments to the deployment and return an Application.

        The returned Application can be deployed using `serve.run` (or via
        config file) or bound to another deployment for composition.
        )
_init_args_init_kwargs)r   options)r!   argskwargss      r"   bindzDeployment.bind   s"     4<<4f<MMNNNr$   rF   rH   rR   rT   placement_group_bundlesplacement_group_strategymax_replicas_per_noderJ   rL   rN   autoscaling_configgraceful_shutdown_wait_loop_sgraceful_shutdown_timeout_shealth_check_period_shealth_check_timeout_srequest_router_configre   rf   r:   max_constructor_retry_countc           	         |t           j        urt          d          |t          d          |dk    r4d}t          ||          \  }}t          j                            d           d t                                                      D             }t          | j
                  }|s|j                            |           |t           j        ddfvr|t           j        dfvrt          d          |dk    rt          d	          |s)|t           j        urt                              d
           n|t           j        dfvr||_        |
t           j        ur|
|_        |t           j        ur||_        |t           j        ur||_        |t           j        ur||_        || j        j        }|t           j        u r| j        }|t           j        u r| j        }|t           j        u r| j        j        }|t           j        u r| j        j        }|t           j        u r| j        j        }|t           j        u r| j        j        }|t           j        u r| j        j        }|	t           j        u r| j        j        }	|t           j        ur||_        |t           j        ur||_        |t           j        ur||_         |t           j        ur||_!        |t           j        ur||_"        |t           j        ur||_#        |t           j        ur0tI          |tJ                    r|&                                }||_'        tQ          j)        |||||||	          }tU          ||||d          S )a  Return a copy of this deployment with updated options.

        Only those options passed in will be updated, all others will remain
        unchanged from the existing deployment.

        Refer to the `@serve.deployment` decorator docs for available arguments.
        rP   Nz2`max_ongoing_requests` must be non-null, got None.auto1c                 <    g | ]\  }}|d vr|t           j        u|S )>   r!   r:   rF   )r   VALUE).0optionvalues      r"   
<listcomp>z&Deployment.options.<locals>.<listcomp>  sA     (
 (
 (
CCCW]**  +**r$   zQManually setting num_replicas is not allowed when autoscaling_config is provided.r   z)num_replicas is expected to larger than 0zDeprecationWarning: `version` in `Deployment.options()` has been deprecated. Explicitly specifying version will raise an error in the future!)rV   rX   rT   rk   rl   rm   T)r.   r:   )+r   ry   rQ   r   r   AUTO_NUM_REPLICAS_USEDrecordlocalsitemsr   r8   user_configured_option_namesupdaterZ   r[   rH   rJ   rL   rN   rt   r9   rE   r6   r7   rV   rX   rT   rk   rl   rm   rn   rs   ro   rp   rq   rr   r3   r   dictr_   r   creater   )r!   rF   r+   r.   rH   rR   rT   rk   rl   rm   rJ   rL   rN   rn   ro   rp   rq   rr   r_   rs   re   rf   r:   rt   r   new_deployment_confignew_replica_configs                              r"   rg   zDeployment.options   s   J w},,O    'QRRR6!!L7O$&88 84 "4 077<<<(
 (
!'!1!1(
 (
 (
$ !))@ A A 	!>EE,   M 
 
 
 !M)
 
 
 2  
 1HIII 	>WGM99NN    '-!6661=!.gm++0;!-w}449M!6gm338K!5&gm;;+ "=   0?M7=  :Dgm##mG&&-7J7=((/;L-- $ 4 F"gm33&*&:&R##w}44'+';'T$ GM11$($8$N!W]227I!4 55:O!7(==- "? 'gm;;+ "= !55:O!7!66;Q!8...-88 7!/!4!4!6!63A!0*1 $/$;%="7
 
 
 !
 
 
 	
r$   c           	         t          | j        |j        k    | j        |j        k    | j        |j        k    | j        j        |j        j        k    | j        j        |j        j        k    | j        j        |j        j        k    g          S r   )allr6   r7   r8   r9   rV   rX   rT   )r!   others     r"   __eq__zDeployment.__eq__  s~    
ek)/'5+CC$.%2G2QQ$0E4I4UU$6(:;

 

 
	
r$   c                     d| j          dS )NzDeployment(name=)rA   rB   s    r"   __str__zDeployment.__str__  s    /$*////r$   c                      t          |           S r   )r4   rB   s    r"   __repr__zDeployment.__repr__  s    4yyr$   )NF)/r%   r&   r'   r(   r4   r   r   r   r#   r2   propertyr+   r.   r
   r   rF   intrH   r   rJ   rL   rN   rR   r   rT   r	   rV   rX   r\   r_   ra   rc   r   rj   r   ry   r   r   floatr   r   r   boolrg   r   r   r   r)   r$   r"   r   r   @   sw        > "&. .. ,. &	.
 #. 
. . . .,	3 	 	 	 	 c    X #    X 3uXs]3 3 3 3 X3 4c 4 4 4 X4 3S 3 3 3 X3 <c < < < X< ;S ; ; ; X; 
 
 X
 68D> 6 6 6 X6 .5: . . . X. 0U3Z 0 0 0 X0 Xc]    X 6 6 6 6 X6@ @ @ @ @
 
 
O{ O O O O -1$] ';B=29-5<]CJ=18.5m.5m-4],3M M8?6=m0718DKM M*1-074;M9v
 v
)v
 clv
 	v

 huS#X78v
 eCI./v
 #8D>2v
 ")d3:.>)?!@v
 #*#,v
  's|v
 Xc]+v
 &clv
 %S\v
 $$)4/0
v
" (/u~#v
$ &-U^%v
&  'u~'v
( !()v
*  dM4&? @A+v
,  '$+T12 
-v
2 E#J'3v
4 d38n-5v
6 7v
8 &-S\9v
: 
;v
 v
 v
 v
p
 
 
0 0 0    r$   r   dr/   c                 P   | j         t          j        | j                   }nd}i d| j        d| j        j        rdn| j        d| j        d| j        d| j	        d| j        j        d| j        j
        d	| j        j        d
| j        j        d| j        j        d|d| j        j        d| j        j        d| j        j        d| j        j        d| j        j        }t)          |                                          D ]}|dk    r|| j        j        vr||= t/          di |S )zConverts a live deployment object to a corresponding structured schema.

    Args:
        d: Deployment object to convert
    Nr+   rH   rL   rN   rJ   rn   ro   rp   rq   rr   rT   rl   rk   rm   r_   rs   r)   )rT   r   	parse_objr+   r8   rn   rH   rL   rN   rJ   ro   rp   rq   rr   r9   rl   rk   rm   r_   rs   listkeysr   r   )r   ray_actor_options_schemadeployment_optionsr{   s       r"   deployment_to_schemar     s    	&#8#B1CV#W#W  #' 2^	
 	 6 	q4 	q} 	a2E 	()=)[ 	&q';'W 	 !5!K 	!!"6"M 	5 	#A$5$N 	"1#4#L  	 !2!H!" 	!.=#$ 	 !5!K%2 )..0011 + +fa2OOO"6* 110111r$   sc                 V   | j         t          j        u rd}n| j                             d          }| j        t          j        u rd}n| j        }| j        t          j        u rd}n| j        }| j        t          j        u rd}n| j        }t          j        | j	        | j
        | j        | j        | j        | j        | j        | j        | j        | j        | j                  }|                                 |_        t-          j        ddi ||||          }t1          | j        ||d          S )	aM  Creates a deployment with parameters specified in schema.

    The returned deployment CANNOT be deployed immediately. It's func_or_class
    value is an empty string (""), which is not a valid import path. The
    func_or_class value must be overwritten with a valid function or class
    before the deployment can be deployed.
    NT)exclude_unset)rH   rJ   rL   rN   rn   ro   rp   rq   rr   r_   rs    r)   )rE   rV   rX   rT   rk   rl   rm   )r+   r,   r-   r:   )rT   r   ry   r   rk   rl   rm   r   from_defaultrH   rJ   rL   rN   rn   ro   rp   rq   rr   r_   rs   !_get_user_configured_option_namesr   r   r   r   r+   )r   rT   rk   rl   rm   r,   r-   s          r"   schema_to_deploymentr     sc    	gm++ /4444HH GM11"&"#";!W]22#'  #$#= '-// $ ! 7(5^M31/&'&E$%$A5 7'5   	
++-- 2 #)+ 7!93  N V+%	   r$   )&loggingr>   copyr   typingr   r   r   r   r   r	   r
   ray.serve._private.configr   r   r   r   ray.serve._private.constantsr   ray.serve._private.usager   ray.serve._private.utilsr   r   ray.serve.configr   ray.serve.schemar   r   r   ray.util.annotationsr   	getLoggerrZ   r   r   r   r   r)   r$   r"   <module>r      s           D D D D D D D D D D D D D D D D D D            ; : : : : : 2 2 2 2 2 2 5 5 5 5 5 5 5 5 . . . . . . S S S S S S S S S S * * * * * *		,	-	- X&2 &2 &2 &2 &2 &2 &2 &2R XX X X X X X X Xv
02J 02+; 02 02 02 02f=, = = = = = = =r$   