如何通过InvokeModelCommand调用AWS Bedrock带变量的Prompt Management提示词?
const bedrockClient = new BedrockRuntimeClient({});
...
const command = new InvokeModelCommand({
modelId: "arn:aws:bedrock:us-east-1:12345678:prompt/ABCDEFG:2",
body: JSON.stringify({
promptVariables: {
myInput: myInputValue
}
})
});
const response = await bedrockClient.send(command);
指定modelId正常,但传递提示词变量时出现错误: > ValidationException: Malformed request for promptVariablesreceived --- # 解决方案 直接通过`InvokeModelCommand`调用Prompt Management存储的提示词是可行的,问题出在变量传递的格式错误。正确格式需要将变量嵌套在`promptConfiguration`对象内,同时必须包含基础模型的推理配置参数。 修正后的代码示例: ```javascript import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime"; ... const bedrockClient = new BedrockRuntimeClient({}); ... const command = new InvokeModelCommand({ modelId: "arn:aws:bedrock:us-east-1:12345678:prompt/ABCDEFG:2", body: JSON.stringify({ promptConfiguration: { promptVariables: { myInput: myInputValue }, // 根据关联的基础模型添加对应推理参数,以下为Claude 3示例 inferenceConfig: { temperature: 0.7, maxTokens: 1000 } } }) }); const response = await bedrockClient.send(command);
关键要点
- 提示词变量必须放在
promptConfiguration对象的promptVariables字段中,不能直接作为body的顶层属性 - 必须包含
inferenceConfig(或对应模型的参数字段),因为调用提示词ARN时,Bedrock需要基于关联的基础模型执行推理,必须明确配置推理行为 - 确认提示词ARN的版本号正确,避免引用旧版本的提示词
内容的提问来源于stack exchange,提问作者Adam
相关产品推荐
相关产品推荐

