使用Serverless.yaml创建Lex Bot时无法添加Bedrock QnA Intent的解决
失败原因分析
- IAM角色权限不足:当前配置的Lex Bot角色仅包含日志操作权限,没有允许调用AWS Bedrock模型的权限,导致Lex无法访问Bedrock服务处理QnA请求。
- Intent类型配置错误:Bedrock QnA属于生成式Intent,不能按照普通自定义Intent或内置Intent的方式配置,必须明确指定
IntentType: Generative,并配置关联Bedrock模型的参数。 - 区域模型兼容性问题:需确认使用的AWS区域(eu-west-2)支持目标Bedrock模型,比如Anthropic Claude 3系列在该区域是可用的。
正确配置实现
1. 更新IAM角色权限
在LexBotRole的PolicyDocument中添加Bedrock访问权限,允许调用指定的Bedrock模型:
Policies: - PolicyName: LexBotPolicy PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - logs:CreateLogGroup - logs:CreateLogStream - logs:PutLogEvents Resource: "arn:aws:logs:*:*:*" - Effect: Allow Action: - bedrock:InvokeModel Resource: "arn:aws:bedrock:eu-west-2::model/anthropic.claude-3-haiku-20240307-v1:0" # 根据需求替换模型ARN
2. 添加Bedrock QnA生成式Intent
在BotLocales的Intents列表中添加生成式QnA Intent,配置IntentType和GenerativeIntentSetting:
# Lex Bot Definition MyLexBot: Type: AWS::Lex::Bot Properties: Name: MySampleBot RoleArn: !GetAtt LexBotRole.Arn DataPrivacy: ChildDirected: false IdleSessionTTLInSeconds: 300 BotLocales: - LocaleId: en_US NluConfidenceThreshold: 0.40 Intents: - Name: GreetingIntent SampleUtterances: - Utterance: Hello - Utterance: Hi IntentClosingSetting: ClosingResponse: MessageGroupsList: - Message: PlainTextMessage: Value: "Hello! How can I help you today?" - Name: FallbackIntent ParentIntentSignature: AMAZON.FallbackIntent IntentClosingSetting: ClosingResponse: MessageGroupsList: - Message: PlainTextMessage: Value: "I'm sorry, I didn't understand that. Can you please rephrase?" # 添加Bedrock QnA生成式Intent - Name: BedrockQnAIntent IntentType: Generative GenerativeIntentSetting: PromptSpecification: PromptTemplates: - PromptTemplate: | Answer the user's question clearly and concisely. User question: {inputTranscript} GenerationSetting: GenerationConfig: MaxTokenCount: 1000 Temperature: 0.7 TopP: 0.9 BedrockModelSetting: ModelArn: "arn:aws:bedrock:eu-west-2::model/anthropic.claude-3-haiku-20240307-v1:0" # 与IAM权限中的模型ARN一致 SampleUtterances: - Utterance: "How do I reset my password?" - Utterance: "What's your return policy?" - Utterance: "Tell me about your services"
关键配置说明
- IntentType: 必须设置为
Generative,标识这是一个生成式Intent,关联Bedrock模型。 - PromptSpecification: 定义引导Bedrock模型生成回答的提示模板,
{inputTranscript}会替换为用户的实际提问内容。 - BedrockModelSetting: 指定要使用的Bedrock模型ARN,需确保该模型在eu-west-2区域可用,且IAM角色拥有该模型的调用权限。
- SampleUtterances: 提供示例提问,帮助Lex识别用户的QnA类请求并路由到该Intent。
内容的提问来源于stack exchange,提问作者Thomas Joseph
相关产品推荐
相关产品推荐

