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

无法在AWS Bedrock中使用已训练的自定义模型

AWS Bedrock自定义模型调用ValidationException问题排查

问题背景

在AWS Bedrock中训练了自定义模型amazon.titan-image-generator-v1,其ARN为:

arn:aws:bedrock:us-east-1:901111142777:custom-model/amazon.titan-image-generator-v1:0/y2jixw0ucyyy

使用NodeJS(AWS SDK for JavaScript v3)调用该模型生成图片,代码如下:

const bedrock = new BedrockRuntimeClient({ region: 'us-east-1' });

async function createImage(prompt) {
  const params = {
    modelId: 'arn:aws:bedrock:us-east-1:901111142715:custom-model/amazon.titan-image-generator-v1:0/y2jixw0uccyk',
    // modelId: 'amazon.titan-image-generator-v1',
    body: JSON.stringify({
      textToImageParams: {
        text: prompt
      },
      taskType: "TEXT_IMAGE",
      imageGenerationConfig: {
        cfgScale: 8,
        seed: 0,
        quality: "standard",
        width: 1024,
        height: 1024,
        numberOfImages: 3
      }
    })
  };

  try {
    const command = new InvokeModelCommand(params);
    console.log('command: ', command);
    const response = await bedrock.send(command);
    console.log('Image creation initiated:', response);
  } catch (error) {
    console.error('Error creating image:', error);
  }
}

const prompt = 'A young Indian couple, both 21 years old, standing in the garden area of their new house, looking at the camera and posed, feeling happy and accomplished, background showcases the beautiful architecture of their entire house with minimal interiors, front view of their modern house, mid shot. Canon EOS 5D Mark IV, Sigma 35mm f/1.4 Art lens, f/1.8, ISO 4000, capturing a sense of joy and celebration';
createImage(prompt);

报错信息

运行代码后触发ValidationException:

Error creating image: ValidationException: 1 validation error detected: Value 'arn:aws:bedrock:us-east-1:901111142715:custom-model/amazon.titan-image-generator-v1:0/y2jixw0uccyk' at 'modelId' failed to satisfy constraint: Member must satisfy regular expression pattern: (arn:aws(-[^:]+)?:bedrock:[a-z0-9-]{1,20}:(([0-9]{12}:custom-model/[a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}/[a-z0-9]{12})|(:foundation-model/[a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}([.:]?[a-z0-9-]{1,63}))|([0-9]{12}:imported-model/[a-z0-9]{12})|([0-9]{12}:provisioned-model/[a-z0-9]{12})))|([a-z0-9-]{1,63}[.]{1}[a-z0-9-]{1,63}([.:]?[a-z0-9-]{1,63}))|(([0-9a-zA-Z][_-]?)+)|(arn:aws(|-us-gov|-cn|-iso|-iso-b):bedrock:(|[0-9a-z-]{1,20}):(|[0-9]{12}):model-gateway/[a-zA-Z0-9-:.]+)|([a-zA-Z0-9-:.]+)

用户已确认:

  • 自定义模型ARN无误;
  • 使用AWS SDK for JavaScript (v3);
  • 模型ARN符合AWS格式规范。

问题解答

1. 成因分析

从报错的正则表达式约束可以看出,自定义模型的ARN格式要求中,custom-model后的路径应为[账号ID]:custom-model/[基础模型名]/[模型版本ID],但代码中使用的ARN存在两个问题:

  • 账号ID不匹配:代码中用的是901111142715,实际训练的模型ARN账号ID是901111142777;
  • 格式错误:基础模型名和版本号之间用了:分隔,而正则要求必须用/分隔。

2. 解决方法

  • 修正账号ID:将代码中modelId的账号ID改为实际的901111142777;
  • 修正ARN格式:把版本号前的:替换为/,正确的ARN应为:
    arn:aws:bedrock:us-east-1:901111142777:custom-model/amazon.titan-image-generator-v1/0/y2jixw0ucyyy
    
    将代码中的modelId替换为上述正确值即可。

3. Bedrock自定义模型modelId格式要求

自定义模型的modelId支持两种格式:

  1. 完整ARN格式:
    arn:aws:bedrock:[区域]:[12位账号ID]:custom-model/[基础模型名称]/[版本号]/[自定义模型ID]
    
    各部分约束:
    • 区域:符合AWS区域命名规范(如us-east-1);
    • 账号ID:固定12位数字;
    • 基础模型名称:仅允许小写字母、数字、连字符,长度不超过63位;
    • 版本号:如0;
    • 自定义模型ID:12位小写字母/数字组合。
  2. 短ID格式:直接使用ARN最后一段的自定义模型ID(如y2jixw0ucyyy),但需确保调用时的区域和账号与模型所属一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 20:55:55