Skip to content

Commit 14ae7e4

Browse files
committed
fix: enhance exception handling and improve example implementations
Updated HTTPError exception handling to include status code 409 for resource already exists scenarios. Improved example code with better resource filtering and added execution role ARN configuration for model proxy operations. The changes enhance error handling robustness and provide more reliable resource management in example implementations. 更新了 HTTPError 异常处理以包含状态码 409 来处理资源已存在的情况。 改进了示例代码中的资源过滤功能,并为模型代理操作添加了执行角色 ARN 配置。 这些更改增强了错误处理的健壮性,并在示例实现中提供了更可靠的 资源管理。 Change-Id: I5245d48ababac407f46b59c13bbea0237f071139 Signed-off-by: OhYee <[email protected]>
1 parent 6f2d7b8 commit 14ae7e4

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

agentrun/model/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ProxyConfigTokenRateLimiter(BaseModel):
131131

132132
class ProxyConfigAIGuardrailConfig(BaseModel):
133133
"""AI 防护配置"""
134+
134135
check_request: Optional[bool] = None
135136
check_response: Optional[bool] = None
136137

agentrun/utils/exception.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ def to_resource_error(
7878
"does not exist" in self.message or "not found" in self.message
7979
):
8080
return ResourceNotExistError(resource_type, resource_id)
81-
elif (self.status_code == 400 or self.status_code == 500) and (
82-
"already exists" in self.message
83-
):
81+
elif (
82+
self.status_code == 400
83+
or self.status_code == 409
84+
or self.status_code == 500
85+
) and ("already exists" in self.message):
8486
# TODO: ModelProxy already exists returns 500
8587
return ResourceAlreadyExistError(resource_type, resource_id)
8688
else:

examples/agent_runtime.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ def create_or_get_agentruntime():
7777
),
7878
)
7979
)
80-
except ResourceAlreadyExistError:
81-
logger.info("已存在,获取已有资源")
82-
83-
ar = client.list(
84-
AgentRuntimeListInput(agent_runtime_name=agent_runtime_name)
80+
except ResourceAlreadyExistError as e:
81+
logger.info("已存在,获取已有资源", e)
82+
83+
ar = list(
84+
filter(
85+
lambda a: a.agent_runtime_name == agent_runtime_name,
86+
client.list(
87+
AgentRuntimeListInput(agent_runtime_name=agent_runtime_name)
88+
),
89+
)
8590
)[0]
8691

8792
ar.wait_until_ready_or_failed()

examples/model.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from logging import config
12
import os
23
import re
34
import time
@@ -135,21 +136,25 @@ def create_or_get_model_proxy():
135136
"""
136137
logger.info("创建或获取已有的资源")
137138

139+
from agentrun.utils.config import Config
140+
141+
cfg = Config()
142+
138143
try:
139144
cred = client.create(
140145
ModelProxyCreateInput(
141146
model_proxy_name=model_proxy_name,
142147
description="测试模型治理",
143148
model_type=model.ModelType.LLM,
149+
execution_role_arn=f"acs:ram::{cfg.get_account_id()}:role/aliyunagentrundefaultrole",
144150
proxy_config=model.ProxyConfig(
145151
endpoints=[
146152
model.ProxyConfigEndpoint(
147153
model_names=[model_name],
148-
model_service_name="test-model-service",
154+
model_service_name=model_service_name,
149155
)
150156
for model_name in model_names
151157
],
152-
policies={},
153158
),
154159
)
155160
)
@@ -172,9 +177,16 @@ def update_model_proxy(mp: ModelProxy):
172177
"""
173178
logger.info("更新描述为当前时间")
174179

180+
from agentrun.utils.config import Config
181+
182+
cfg = Config()
183+
175184
# 也可以使用 client.update
176185
mp.update(
177-
ModelProxyUpdateInput(description=f"当前时间戳:{time.time()}"),
186+
ModelProxyUpdateInput(
187+
execution_role_arn=f"acs:ram::{cfg.get_account_id()}:role/aliyunagentrundefaultrole",
188+
description=f"当前时间戳:{time.time()}",
189+
),
178190
)
179191
mp.wait_until_ready_or_failed()
180192
if mp.status != Status.READY:

0 commit comments

Comments
 (0)