Skip to content

Commit 40083c9

Browse files
authored
Merge pull request #126 from Serverless-Devs/dev-0618
Refactor ModelClient to prioritize ModelService over ModelProxy for r…
2 parents f1d1af9 + 43085f8 commit 40083c9

3 files changed

Lines changed: 46 additions & 44 deletions

File tree

agentrun/model/__client_async_template.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,24 +285,25 @@ async def get_async(
285285
ResourceNotExistError: 模型服务不存在
286286
"""
287287

288+
# 优先查 ModelService,未命中再回退 ModelProxy,避免无谓的 404
288289
error: Optional[HTTPError] = None
289-
if backend_type == BackendType.PROXY or backend_type is None:
290+
if backend_type == BackendType.SERVICE or backend_type is None:
290291
try:
291-
result = await self.__control_api.get_model_proxy_async(
292-
model_proxy_name=name, config=config
292+
result = await self.__control_api.get_model_service_async(
293+
model_service_name=name, config=config
293294
)
294-
return ModelProxy.from_inner_object(result)
295+
return ModelService.from_inner_object(result)
295296
except HTTPError as e:
296297
error = e
297298

298-
if backend_type == BackendType.PROXY and error is not None:
299+
if backend_type == BackendType.SERVICE and error is not None:
299300
raise error.to_resource_error("Model", name) from error
300301

301302
try:
302-
result = await self.__control_api.get_model_service_async(
303-
model_service_name=name, config=config
303+
result = await self.__control_api.get_model_proxy_async(
304+
model_proxy_name=name, config=config
304305
)
305-
return ModelService.from_inner_object(result)
306+
return ModelProxy.from_inner_object(result)
306307
except HTTPError as e:
307308
raise e.to_resource_error("Model", name) from e
308309

agentrun/model/client.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,25 @@ async def get_async(
512512
ResourceNotExistError: 模型服务不存在
513513
"""
514514

515+
# 优先查 ModelService,未命中再回退 ModelProxy,避免无谓的 404
515516
error: Optional[HTTPError] = None
516-
if backend_type == BackendType.PROXY or backend_type is None:
517+
if backend_type == BackendType.SERVICE or backend_type is None:
517518
try:
518-
result = await self.__control_api.get_model_proxy_async(
519-
model_proxy_name=name, config=config
519+
result = await self.__control_api.get_model_service_async(
520+
model_service_name=name, config=config
520521
)
521-
return ModelProxy.from_inner_object(result)
522+
return ModelService.from_inner_object(result)
522523
except HTTPError as e:
523524
error = e
524525

525-
if backend_type == BackendType.PROXY and error is not None:
526+
if backend_type == BackendType.SERVICE and error is not None:
526527
raise error.to_resource_error("Model", name) from error
527528

528529
try:
529-
result = await self.__control_api.get_model_service_async(
530-
model_service_name=name, config=config
530+
result = await self.__control_api.get_model_proxy_async(
531+
model_proxy_name=name, config=config
531532
)
532-
return ModelService.from_inner_object(result)
533+
return ModelProxy.from_inner_object(result)
533534
except HTTPError as e:
534535
raise e.to_resource_error("Model", name) from e
535536

@@ -552,24 +553,25 @@ def get(
552553
ResourceNotExistError: 模型服务不存在
553554
"""
554555

556+
# 优先查 ModelService,未命中再回退 ModelProxy,避免无谓的 404
555557
error: Optional[HTTPError] = None
556-
if backend_type == BackendType.PROXY or backend_type is None:
558+
if backend_type == BackendType.SERVICE or backend_type is None:
557559
try:
558-
result = self.__control_api.get_model_proxy(
559-
model_proxy_name=name, config=config
560+
result = self.__control_api.get_model_service(
561+
model_service_name=name, config=config
560562
)
561-
return ModelProxy.from_inner_object(result)
563+
return ModelService.from_inner_object(result)
562564
except HTTPError as e:
563565
error = e
564566

565-
if backend_type == BackendType.PROXY and error is not None:
567+
if backend_type == BackendType.SERVICE and error is not None:
566568
raise error.to_resource_error("Model", name) from error
567569

568570
try:
569-
result = self.__control_api.get_model_service(
570-
model_service_name=name, config=config
571+
result = self.__control_api.get_model_proxy(
572+
model_proxy_name=name, config=config
571573
)
572-
return ModelService.from_inner_object(result)
574+
return ModelProxy.from_inner_object(result)
573575
except HTTPError as e:
574576
raise e.to_resource_error("Model", name) from e
575577

tests/unittests/model/test_client.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -906,13 +906,14 @@ def test_get_auto_detect(self, mock_control_api_class):
906906
mock_control_api_class.return_value = mock_control_api
907907

908908
mock_result = MagicMock()
909-
mock_control_api.get_model_proxy.return_value = mock_result
909+
mock_control_api.get_model_service.return_value = mock_result
910910

911911
client = ModelClient()
912912
result = client.get("test")
913913

914-
# Should try proxy first
915-
mock_control_api.get_model_proxy.assert_called_once()
914+
# Should try service first (ModelService is the primary resource after migration)
915+
mock_control_api.get_model_service.assert_called_once()
916+
mock_control_api.get_model_proxy.assert_not_called()
916917

917918
@patch.dict(
918919
os.environ,
@@ -923,27 +924,25 @@ def test_get_auto_detect(self, mock_control_api_class):
923924
},
924925
)
925926
@patch("agentrun.model.client.ModelControlAPI")
926-
def test_get_auto_detect_falls_back_to_service(
927-
self, mock_control_api_class
928-
):
929-
"""Test fallback to service when proxy not found"""
927+
def test_get_auto_detect_falls_back_to_proxy(self, mock_control_api_class):
928+
"""Test fallback to proxy when service not found"""
930929
mock_control_api = MagicMock()
931930
mock_control_api_class.return_value = mock_control_api
932931

933-
# Proxy get fails
934-
mock_control_api.get_model_proxy.side_effect = HTTPError(
932+
# Service get fails
933+
mock_control_api.get_model_service.side_effect = HTTPError(
935934
status_code=404, message="Not found"
936935
)
937-
# Service get succeeds
936+
# Proxy get succeeds
938937
mock_result = MagicMock()
939-
mock_control_api.get_model_service.return_value = mock_result
938+
mock_control_api.get_model_proxy.return_value = mock_result
940939

941940
client = ModelClient()
942941
result = client.get("test")
943942

944-
mock_control_api.get_model_proxy.assert_called_once()
945943
mock_control_api.get_model_service.assert_called_once()
946-
assert isinstance(result, ModelService)
944+
mock_control_api.get_model_proxy.assert_called_once()
945+
assert isinstance(result, ModelProxy)
947946

948947
@patch.dict(
949948
os.environ,
@@ -1009,26 +1008,26 @@ async def test_get_async_service(self, mock_control_api_class):
10091008
@patch("agentrun.model.client.ModelControlAPI")
10101009
@pytest.mark.asyncio
10111010
async def test_get_async_auto_detect_fallback(self, mock_control_api_class):
1012-
"""Test fallback to service when proxy not found in async get"""
1011+
"""Test fallback to proxy when service not found in async get"""
10131012
mock_control_api = MagicMock()
10141013
mock_control_api_class.return_value = mock_control_api
10151014

1016-
# Proxy get fails
1017-
mock_control_api.get_model_proxy_async = AsyncMock(
1015+
# Service get fails
1016+
mock_control_api.get_model_service_async = AsyncMock(
10181017
side_effect=HTTPError(status_code=404, message="Not found")
10191018
)
1020-
# Service get succeeds
1019+
# Proxy get succeeds
10211020
mock_result = MagicMock()
1022-
mock_control_api.get_model_service_async = AsyncMock(
1021+
mock_control_api.get_model_proxy_async = AsyncMock(
10231022
return_value=mock_result
10241023
)
10251024

10261025
client = ModelClient()
10271026
result = await client.get_async("test")
10281027

1029-
mock_control_api.get_model_proxy_async.assert_called_once()
10301028
mock_control_api.get_model_service_async.assert_called_once()
1031-
assert isinstance(result, ModelService)
1029+
mock_control_api.get_model_proxy_async.assert_called_once()
1030+
assert isinstance(result, ModelProxy)
10321031

10331032
@patch.dict(
10341033
os.environ,

0 commit comments

Comments
 (0)