chore: 添加虚拟环境到仓库

- 添加 backend_service/venv 虚拟环境
- 包含所有Python依赖包
- 注意:虚拟环境约393MB,包含12655个文件
This commit is contained in:
2025-12-03 10:19:25 +08:00
parent a6c2027caa
commit c4f851d387
12655 changed files with 3009376 additions and 0 deletions

View File

@@ -0,0 +1,192 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
from dataclasses import dataclass
from http import HTTPStatus
from typing import Dict, List
from dashscope.common.base_type import BaseObjectMixin
__all__ = ['Deployment', 'FineTune', 'DeploymentList', 'FineTuneList']
@dataclass(init=False)
class DashScopeBaseList(BaseObjectMixin):
page_no: int
page_size: int
total: int
def __init__(self, **kwargs):
super().__init__(**kwargs)
@dataclass(init=False)
class DashScopeBase(BaseObjectMixin):
status_code: int
request_id: str
code: str
message: str
def __init__(self, **kwargs):
super().__init__(**kwargs)
@dataclass(init=False)
class FineTuneOutput(BaseObjectMixin):
job_id: str
job_name: str
status: str
model: str
base_model: str
finetuned_output: str
training_file_ids: List[str]
validation_file_ids: List[str]
hyper_parameters: Dict
training_type: str
create_time: str
end_time: str
user_identity: str
modifier: str
creator: str
group: str
usage: int
def __init__(self, **kwargs):
super().__init__(**kwargs)
@dataclass(init=False)
class FineTune(DashScopeBase):
output: FineTuneOutput
usage: Dict
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = FineTuneOutput(**kwargs.pop('output', {}))
super().__init__(**kwargs)
@dataclass(init=False)
class FineTuneListOutput(DashScopeBaseList):
jobs: List[FineTuneOutput]
def __init__(self, **kwargs):
self.jobs = []
for job in kwargs.pop('jobs', []):
self.jobs.append(FineTuneOutput(**job))
super().__init__(**kwargs)
@dataclass(init=False)
class FineTuneList(DashScopeBase):
output: FineTuneListOutput
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = FineTuneListOutput(**kwargs.pop('output', {}))
super().__init__(**kwargs)
@dataclass(init=False)
class CancelDeleteStatus(BaseObjectMixin):
status: str
@dataclass(init=False)
class FineTuneCancel(DashScopeBase):
output: CancelDeleteStatus
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = CancelDeleteStatus(**kwargs.pop('output', {}))
super().__init__(**kwargs)
@dataclass(init=False)
class FineTuneDelete(DashScopeBase):
output: CancelDeleteStatus
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = CancelDeleteStatus(**kwargs.pop('output', {}))
super().__init__(**kwargs)
@dataclass(init=False)
class FineTuneEvent(DashScopeBase):
output: str
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = kwargs.pop('output', {})
super().__init__(**kwargs)
@dataclass(init=False)
class DeploymentOutput(BaseObjectMixin):
deployed_model: str
gmt_create: str
gmt_modified: str
status: str
model_name: str
base_model: str
base_capacity: int
capacity: int
ready_capacity: int
workspace_id: str
charge_type: str
modifier: str
creator: str
def __init__(self, **kwargs):
super().__init__(**kwargs)
@dataclass(init=False)
class Deployment(DashScopeBase):
output: DeploymentOutput
def __init__(self, **kwargs):
output = kwargs.pop('output', {})
if output:
self.output = DeploymentOutput(**output)
else:
self.output = None
super().__init__(**kwargs)
@dataclass(init=False)
class DeploymentListOutput(DashScopeBaseList):
deployments: List[DeploymentOutput]
def __init__(self, **kwargs):
self.deployments = []
for job in kwargs.pop('deployments', []):
self.deployments.append(DeploymentOutput(**job))
super().__init__(**kwargs)
@dataclass(init=False)
class DeploymentList(BaseObjectMixin):
output: DeploymentListOutput
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = DeploymentListOutput(**kwargs.pop('output', {}))
super().__init__(**kwargs)
@dataclass(init=False)
class DeploymentDelete(DashScopeBase):
output: CancelDeleteStatus
def __init__(self, **kwargs):
status_code = kwargs.get('status_code', None)
if status_code == HTTPStatus.OK:
self.output = CancelDeleteStatus(**kwargs.pop('output', {}))
super().__init__(**kwargs)

View File

@@ -0,0 +1,146 @@
# 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
def call(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(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(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(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 = '%s/%s/scale' % (cls.SUB_PATH.lower(), deployed_model)
response = super().put(deployed_model,
req,
path=path,
api_key=api_key,
workspace=workspace,
**kwargs)
return Deployment(**response)

View File

@@ -0,0 +1,234 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
import time
from http import HTTPStatus
from typing import Iterator, Union
from dashscope.client.base_api import (CancelMixin, CreateMixin, DeleteMixin,
GetStatusMixin, ListMixin, LogMixin,
StreamEventMixin)
from dashscope.common.constants import TaskStatus
from dashscope.customize.customize_types import (FineTune, FineTuneCancel,
FineTuneDelete, FineTuneEvent,
FineTuneList)
class FineTunes(CreateMixin, CancelMixin, DeleteMixin, ListMixin,
GetStatusMixin, StreamEventMixin, LogMixin):
SUB_PATH = 'fine-tunes'
@classmethod
def call(cls,
model: str,
training_file_ids: Union[list, str],
validation_file_ids: Union[list, str] = None,
mode: str = None,
hyper_parameters: dict = {},
api_key: str = None,
workspace: str = None,
**kwargs) -> FineTune:
"""Create fine-tune job
Args:
model (str): The model to be fine-tuned
training_file_ids (list, str): Ids of the fine-tune training data,
which can be pre-uploaded using the File API.
validation_file_ids ([list,str], optional): Ids of the fine-tune
validating data, which can be pre-uploaded using the File API.
mode (str): The fine-tune mode, sft or efficient_sft.
hyper_parameters (dict, optional): The fine-tune hyper parameters.
Defaults to empty.
api_key (str, optional): The api key. Defaults to None.
workspace (str): The dashscope workspace id.
Returns:
FineTune: The request result.
"""
if isinstance(training_file_ids, str):
training_file_ids = [training_file_ids]
if validation_file_ids and isinstance(validation_file_ids, str):
validation_file_ids = [validation_file_ids]
request = {
'model': model,
'training_file_ids': training_file_ids,
'validation_file_ids': validation_file_ids,
'hyper_parameters': hyper_parameters if hyper_parameters else {},
}
if mode is not None:
request['training_type'] = mode
if 'finetuned_output' in kwargs:
request['finetuned_output'] = kwargs['finetuned_output']
resp = super().call(request,
api_key=api_key,
workspace=workspace,
**kwargs)
return FineTune(**resp)
@classmethod
def cancel(cls,
job_id: str,
api_key: str = None,
workspace: str = None,
**kwargs) -> FineTuneCancel:
"""Cancel a running fine-tune job.
Args:
job_id (str): The fine-tune job id.
api_key (str, optional): The api api_key, can be None,
if None, will get by default rule(TODO: api key doc).
workspace (str): The dashscope workspace id.
Returns:
FineTune: The request result.
"""
rsp = super().cancel(job_id,
api_key=api_key,
workspace=workspace,
**kwargs)
return FineTuneCancel(**rsp)
@classmethod
def list(cls,
page_no=1,
page_size=10,
api_key: str = None,
workspace: str = None,
**kwargs) -> FineTuneList:
"""List fine-tune job.
Args:
api_key (str, optional): The api key
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:
FineTune: The fine-tune jobs in the result.
"""
response = super().list(page_no=page_no,
page_size=page_size,
api_key=api_key,
workspace=workspace,
**kwargs)
return FineTuneList(**response)
@classmethod
def get(cls,
job_id: str,
api_key: str = None,
workspace: str = None,
**kwargs) -> FineTune:
"""Get fine-tune job information.
Args:
job_id (str): The fine-tune job id
api_key (str, optional): The api key. Defaults to None.
workspace (str): The dashscope workspace id.
Returns:
FineTune: The job info
"""
response = super().get(job_id,
api_key=api_key,
workspace=workspace,
**kwargs)
return FineTune(**response)
@classmethod
def delete(cls,
job_id: str,
api_key: str = None,
workspace: str = None,
**kwargs) -> FineTuneDelete:
"""Delete a fine-tune job.
Args:
job_id (str): The fine-tune job id.
api_key (str, optional): The api key. Defaults to None.
workspace (str): The dashscope workspace id.
Returns:
FineTune: The delete result.
"""
rsp = super().delete(job_id,
api_key=api_key,
workspace=workspace,
**kwargs)
return FineTuneDelete(**rsp)
@classmethod
def stream_events(cls,
job_id: str,
api_key: str = None,
workspace: str = None,
**kwargs) -> Iterator[FineTuneEvent]:
"""Get fine-tune job events.
Args:
job_id (str): The fine-tune job id
api_key (str, optional): the api key. Defaults to None.
workspace (str): The dashscope workspace id.
Returns:
FineTune: The job log events.
"""
responses = super().stream_events(job_id,
api_key=api_key,
workspace=workspace,
**kwargs)
for rsp in responses:
yield FineTuneEvent(**rsp)
@classmethod
def logs(cls,
job_id: str,
*,
offset=1,
line=1000,
api_key: str = None,
workspace: str = None,
**kwargs) -> FineTune:
"""Get log of the job.
Args:
job_id (str): The job id(used for fine-tune)
offset (int, optional): start log line. Defaults to 1.
line (int, optional): total line return. Defaults to 1000.
api_key (str, optional): The api key. Defaults to None.
workspace (str): The dashscope workspace id.
Returns:
FineTune: The response
"""
return super().logs(job_id,
offset=offset,
line=line,
workspace=workspace,
api_key=api_key)
@classmethod
def wait(cls,
job_id: str,
api_key: str = None,
workspace: str = None,
**kwargs):
try:
while True:
rsp = FineTunes.get(job_id,
api_key=api_key,
workspace=workspace,
**kwargs)
if rsp.status_code == HTTPStatus.OK:
if rsp.output['status'] in [
TaskStatus.FAILED, TaskStatus.CANCELED,
TaskStatus.SUCCEEDED
]:
return rsp
else:
time.sleep(30)
else:
return rsp
except Exception:
raise Exception(
'You can stream output via: dashscope fine_tunes.stream -j %s'
% job_id)