File size: 5,090 Bytes
cbc00ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.

from dashscope.client.base_api import (
    CreateMixin,
    DeleteMixin,
    GetMixin,
    ListMixin,
    PutMixin,
    StreamEventMixin,
)
from dashscope.customize.customize_types import (
    Deployment,
    DeploymentDelete,
    DeploymentList,
)


class Deployments(
    CreateMixin,
    DeleteMixin,
    ListMixin,
    GetMixin,
    StreamEventMixin,
    PutMixin,
):
    SUB_PATH = "deployments"
    """Deploy a model.
    """

    @classmethod
    # type: ignore[override]
    def call(  # pylint: disable=arguments-renamed  # type: ignore[override]
        cls,
        model: str,
        capacity: int,
        version: str = None,
        suffix: str = None,
        api_key: str = None,
        workspace: str = None,
        **kwargs,
    ) -> Deployment:
        """Call to deployment a model service.

        Args:
            model (str): The model name.
            version (str, optional): The model version, unnecessary
                for fine-tuned model. Defaults to None.
            suffix (str, optional): The name suffix of the model deployment,
                If specified, the final model name will be model_suffix.
                Defaults to None.
            capacity (int, optional): The model service capacity.
            api_key (str, optional): The api-key. Defaults to None.
            workspace (str): The dashscope workspace id.

        Returns:
            Deployment: _description_
        """
        req = {"model_name": model, "capacity": capacity}

        if version is not None:
            req["model_version"] = version
        if suffix is not None:
            req["suffix"] = suffix
        response = super().call(
            req,
            api_key=api_key,
            workspace=workspace,
            **kwargs,
        )
        return Deployment(**response)

    @classmethod
    def list(  # type: ignore[override]
        cls,
        page_no=1,
        page_size=10,
        api_key: str = None,
        workspace: str = None,
        **kwargs,
    ) -> DeploymentList:
        """List deployments.

        Args:
            api_key (str, optional): The api api_key, if not present,
                will get by default rule(TODO: api key doc). Defaults to None.
            page_no (int, optional): Page number. Defaults to 1.
            page_size (int, optional): Items per page. Defaults to 10.
            workspace (str): The dashscope workspace id.

        Returns:
            Deployment: The deployment list.
        """
        response = super().list(
            page_no=page_no,
            page_size=page_size,
            api_key=api_key,
            workspace=workspace,
            **kwargs,
        )
        return DeploymentList(**response)

    @classmethod
    def get(  # type: ignore[override]
        cls,
        deployed_model: str,
        api_key: str = None,
        workspace: str = None,
        **kwargs,
    ) -> Deployment:
        """Get model deployment information.

        Args:
            deployed_model (str): The deployment_id.
            api_key (str, optional): The api key. Defaults to None.
            workspace (str): The dashscope workspace id.

        Returns:
            Deployment: The deployment information.
        """
        response = super().get(
            deployed_model,
            api_key=api_key,
            workspace=workspace,
            **kwargs,
        )
        return Deployment(**response)

    @classmethod
    def delete(  # type: ignore[override]
        cls,
        deployed_model: str,
        api_key: str = None,
        workspace: str = None,
        **kwargs,
    ) -> DeploymentDelete:
        """Delete model deployment.

        Args:
            deployed_model (str): The deployment id.
            api_key (str, optional): The api key. Defaults to None.
            workspace (str): The dashscope workspace id.

        Returns:
            Deployment: The delete result.
        """
        response = super().delete(
            deployed_model,
            api_key=api_key,
            workspace=workspace,
            **kwargs,
        )
        return DeploymentDelete(**response)

    @classmethod
    def scale(
        cls,
        deployed_model: str,
        capacity: int,
        api_key: str = None,
        workspace: str = None,
        **kwargs,
    ) -> Deployment:
        """Scaling model deployment.

        Args:
            deployment_id (str): The deployment id.
            capacity (int): The target service capacity.
            api_key (str, optional): The api key. Defaults to None.

        Returns:
            Deployment: The delete result.
        """
        req = {"deployed_model": deployed_model, "capacity": capacity}
        path = f"{cls.SUB_PATH.lower()}/{deployed_model}/scale"
        response = super().put(
            deployed_model,
            req,
            path=path,
            api_key=api_key,
            workspace=workspace,
            **kwargs,
        )
        return Deployment(**response)