You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python CDK部署Bedrock Agent遇模型调用错误,如何自动配置推理配置文件?

问题描述

我使用Python CDK部署了一个以anthropic.claude-3-5-sonnet-20241022-v2:0为基础模型的Bedrock Agent,代码如下:

support_agent = bedrock.CfnAgent(self, "HorizonIQSupportAgent",
    agent_name="support-agent",
    agent_resource_role_arn=agent_role.attr_arn,
    auto_prepare=True,
    description="support-agent",
    foundation_model=foundation_model_name,
    idle_session_ttl_in_seconds=600,
    instruction=prompt,
    knowledge_bases=[bedrock.CfnAgent.AgentKnowledgeBaseProperty(
        description=kb_description,
        knowledge_base_id=knowledge_base.attr_knowledge_base_id,
    )]
)

Agent成功部署后,在控制台测试时出现错误:

Invocation of model ID anthropic.claude-3-5-sonnet-20241022-v2:0 with on-demand throughput isn’t supported. Retry your request with the ID or ARN of an inference profile that contains this model

目前需手动编辑模型并选择包含该模型的推理配置文件才能正常使用,请问如何通过Python CDK实现此操作的自动化?

解决方案

要通过Python CDK自动完成推理配置文件的关联,需要修改CfnAgent的配置参数,核心是用inference_configuration字段指定推理配置文件,而非直接设置foundation_model。

  • 步骤1:确认推理配置文件的标识
    找到包含anthropic.claude-3-5-sonnet-20241022-v2:0的推理配置文件,获取它的ARN或ID。AWS提供的默认推理配置文件ARN格式为arn:aws:bedrock:<你的区域>::inference-profile/anthropic.claude-3-5-sonnet-20241022-v2:0,替换<你的区域>为实际部署的AWS区域即可。

  • 步骤2:修改CDK代码
    在CfnAgent定义中添加inference_configuration属性,同时移除原来的foundation_model参数(推理配置文件已包含模型信息)。修改后的代码示例:

support_agent = bedrock.CfnAgent(self, "HorizonIQSupportAgent",
    agent_name="support-agent",
    agent_resource_role_arn=agent_role.attr_arn,
    auto_prepare=True,
    description="support-agent",
    idle_session_ttl_in_seconds=600,
    instruction=prompt,
    knowledge_bases=[bedrock.CfnAgent.AgentKnowledgeBaseProperty(
        description=kb_description,
        knowledge_base_id=knowledge_base.attr_knowledge_base_id,
    )],
    # 配置推理配置文件
    inference_configuration=bedrock.CfnAgent.InferenceConfigurationProperty(
        inference_profile_arn="arn:aws:bedrock:us-east-1::inference-profile/anthropic.claude-3-5-sonnet-20241022-v2:0"
        # 也可使用ID:inference_profile_id="anthropic.claude-3-5-sonnet-20241022-v2:0"
    )
)
  • 步骤3:检查权限
    确保Agent关联的IAM角色(agent_role)拥有访问该推理配置文件的权限,需在角色策略中添加以下权限:
{
    "Effect": "Allow",
    "Action": [
        "bedrock:InvokeModel",
        "bedrock:InvokeModelWithResponseStream"
    ],
    "Resource": "<推理配置文件ARN>"
}

内容的提问来源于stack exchange,提问作者Naxi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:16:16