-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path__model_proxy_async_template.py
More file actions
234 lines (191 loc) · 6.29 KB
/
__model_proxy_async_template.py
File metadata and controls
234 lines (191 loc) · 6.29 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""Model Proxy 高层 API / Model Proxy High-Level API
此模块定义模型代理资源的高级API。
This module defines the high-level API for model proxy resources.
"""
from typing import List, Optional
import pydash
from agentrun.model.api.data import BaseInfo, ModelDataAPI
from agentrun.model.api.model_api import ModelAPI
from agentrun.utils.config import Config
from agentrun.utils.model import Status
from agentrun.utils.resource import ResourceBase
from .model import (
BackendType,
ModelProxyCreateInput,
ModelProxyImmutableProps,
ModelProxyListInput,
ModelProxyMutableProps,
ModelProxySystemProps,
ModelProxyUpdateInput,
PageableInput,
ProxyMode,
)
class ModelProxy(
ModelProxyImmutableProps,
ModelProxyMutableProps,
ModelProxySystemProps,
ModelAPI,
ResourceBase,
):
"""模型服务"""
_data_client: Optional[ModelDataAPI] = None
@classmethod
def __get_client(cls):
from .client import ModelClient
return ModelClient()
@classmethod
async def create_async(
cls, input: ModelProxyCreateInput, config: Optional[Config] = None
):
"""创建模型服务(异步)
Args:
input: 模型服务输入参数
config: 配置
Returns:
ModelProxy: 创建的模型服务对象
"""
return await cls.__get_client().create_async(input, config=config)
@classmethod
async def delete_by_name_async(
cls, model_Proxy_name: str, config: Optional[Config] = None
):
"""根据名称删除模型服务(异步)
Args:
model_Proxy_name: 模型服务名称
config: 配置
"""
return await cls.__get_client().delete_async(
model_Proxy_name, backend_type=BackendType.PROXY, config=config
)
@classmethod
async def update_by_name_async(
cls,
model_proxy_name: str,
input: ModelProxyUpdateInput,
config: Optional[Config] = None,
):
"""根据名称更新模型服务(异步)
Args:
model_Proxy_name: 模型服务名称
input: 模型服务更新输入参数
config: 配置
Returns:
ModelProxy: 更新后的模型服务对象
"""
return await cls.__get_client().update_async(
model_proxy_name, input, config=config
)
@classmethod
async def get_by_name_async(
cls, model_proxy_name: str, config: Optional[Config] = None
):
"""根据名称获取模型服务(异步)
Args:
model_Proxy_name: 模型服务名称
config: 配置
Returns:
ModelProxy: 模型服务对象
"""
return await cls.__get_client().get_async(
model_proxy_name, backend_type=BackendType.PROXY, config=config
)
@classmethod
async def _list_page_async(
cls, page_input: PageableInput, config: Config | None = None, **kwargs
):
return await cls.__get_client().list_async(
input=ModelProxyListInput(
**kwargs,
**page_input.model_dump(),
),
config=config,
)
@classmethod
async def list_all_async(
cls,
*,
proxy_mode: Optional[str] = None,
status: Optional[Status] = None,
config: Optional[Config] = None,
) -> List["ModelProxy"]:
return await cls._list_all_async(
lambda m: m.model_proxy_id or "",
config=config,
proxy_mode=proxy_mode,
status=status,
)
async def update_async(
self, input: ModelProxyUpdateInput, config: Optional[Config] = None
):
"""更新模型服务(异步)
Args:
input: 模型服务更新输入参数
config: 配置
Returns:
ModelProxy: 更新后的模型服务对象
"""
if self.model_proxy_name is None:
raise ValueError(
"model_Proxy_name is required to update a ModelProxy"
)
result = await self.update_by_name_async(
self.model_proxy_name, input, config=config
)
self.update_self(result)
return self
async def delete_async(self, config: Optional[Config] = None):
"""删除模型服务(异步)
Args:
config: 配置
"""
if self.model_proxy_name is None:
raise ValueError(
"model_Proxy_name is required to delete a ModelProxy"
)
return await self.delete_by_name_async(
self.model_proxy_name, config=config
)
async def get_async(self, config: Optional[Config] = None):
"""刷新模型服务信息(异步)
Args:
config: 配置
Returns:
ModelProxy: 刷新后的模型服务对象
"""
if self.model_proxy_name is None:
raise ValueError(
"model_Proxy_name is required to refresh a ModelProxy"
)
result = await self.get_by_name_async(
self.model_proxy_name, config=config
)
self.update_self(result)
return self
async def refresh_async(self, config: Optional[Config] = None):
"""刷新模型服务信息(异步)
Args:
config: 配置
Returns:
ModelProxy: 刷新后的模型服务对象
"""
return await self.get_async(config=config)
def model_info(self, config: Optional[Config] = None) -> BaseInfo:
cfg = Config.with_configs(self._config, config)
if self._data_client is None:
self._data_client = ModelDataAPI(
self.model_proxy_name or "",
credential_name=self.credential_name,
config=cfg,
)
self._data_client.update_model_name(
model_proxy_name=self.model_proxy_name,
model_name=(
pydash.get(self, "proxy_config.endpoints[0].model_names[0]")
if self.proxy_mode == ProxyMode.SINGLE
else self.model_proxy_name
)
or "",
credential_name=self.credential_name,
config=cfg,
)
return self._data_client.model_info()