aws lambda - aws QnA intent cloudformation template not working - Stack Overflow

I want to create a lex bot with serverless.yaml and I want to use the aws bedrocks builtin intent qnain

I want to create a lex bot with serverless.yaml and I want to use the aws bedrocks builtin intent qnaintent. I am able to create the bot with intents from the serverless.yaml but I am unable to add the qna intent with it. Why is that and how can I do that?

service: my-lex-bot

provider:
  name: aws
  region: eu-west-2

resources:
  Resources:
    # IAM Role for Lex Bot
    LexBotRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Principal:
                Service: lexv2.amazonaws
              Action: sts:AssumeRole
        Policies:
          - PolicyName: LexBotPolicy
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Effect: Allow
                  Action:
                    - logs:CreateLogGroup
                    - logs:CreateLogStream
                    - logs:PutLogEvents
                  Resource: "arn:aws:logs:*:*:*"

    # 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?"

I want to add a qnaintent of bedrock with it how can I do that?

I want to create a lex bot with serverless.yaml and I want to use the aws bedrocks builtin intent qnaintent. I am able to create the bot with intents from the serverless.yaml but I am unable to add the qna intent with it. Why is that and how can I do that?

service: my-lex-bot

provider:
  name: aws
  region: eu-west-2

resources:
  Resources:
    # IAM Role for Lex Bot
    LexBotRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Principal:
                Service: lexv2.amazonaws
              Action: sts:AssumeRole
        Policies:
          - PolicyName: LexBotPolicy
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Effect: Allow
                  Action:
                    - logs:CreateLogGroup
                    - logs:CreateLogStream
                    - logs:PutLogEvents
                  Resource: "arn:aws:logs:*:*:*"

    # 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?"

I want to add a qnaintent of bedrock with it how can I do that?

Share Improve this question edited Mar 26 at 2:06 Robert 8,71355 gold badges134 silver badges174 bronze badges asked Mar 24 at 7:10 Thomas JosephThomas Joseph 31 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 0
  1. QnAIntent is not part of the predefined AMAZON.* intents

  2. It's part of an integration between Lex and Bedrock QnA capabilities.

  3. Currently, CloudFormation does not support adding QnAIntent directly — it can only be done through:

    AWS Console, AWS CLI Or SDK (Boto3 / AWS SDKs)

example awscli

aws lexv2-models create_intent \
  --bot-id <your-bot-id> \
  --bot-version DRAFT \
  --locale-id en_US \
  --intent-name QnAIntent \
  --parent-intent-signature "QnAIntent.Bedrock"

i did it with the fall back intent insted of the qnaintent when it goes to fallback i triggered a lambda function to retrive the data from the knowledge base

service: claude-lex-bedrock-bot

provider:
  name: aws
  runtime: nodejs22.x
  region: eu-west-2
  environment:
    KNOWLEDGE_BASE_ID: ${env:KNOWLEDGE_BASE_ID, 'M86AHPZ6CL'}
  iam:
    role:
      statements:
        - Effect: Allow
          Action:
            - bedrock:InvokeModel
            - bedrock:Retrieve
            - bedrock:RetrieveAndGenerate
            - logs:CreateLogGroup
            - logs:CreateLogStream
            - logs:PutLogEvents
            - lambda:InvokeFunction
            - lex:*
          Resource: "*"

functions:
  LexBedrockHandler:
    handler: handler.handler
    name: claude-lex-bedrock-bot-dev-LexBedrockHandler
    timeout: 15  
    events:
      - httpApi:
          path: /chat
          method: post
  BotInputHandler:
    handler: lexHandler.handler
    name: lex-chat
    environment:
      BOT_ID: !Ref MyLexBot
      BOT_ALIAS_ID: !GetAtt MyLexBotAlias.BotAliasId
      LOCALE_ID: "en_US"
    events:
      - httpApi:
          path: /bot
          method: post

resources:
  Resources:
    MyLexBot:
      Type: AWS::Lex::Bot
      Properties:
        Name: MyBot
        DataPrivacy:
          ChildDirected: false
        IdleSessionTTLInSeconds: 300
        RoleArn: !GetAtt LexBotRole.Arn
        BotLocales:
          - LocaleId: en_US
            NluConfidenceThreshold: 0.40
            Intents:
              - Name: GreetingIntent
                SampleUtterances:
                  - Utterance: "Hi"
                  - Utterance: "Hello"
                  - Utterance: "Good morning"
                  - Utterance: "Hey"
                IntentClosingSetting:
                  ClosingResponse:
                    MessageGroupsList:
                      - Message:
                          PlainTextMessage:
                            Value: "Hello! How can I assist you today?"
              - Name: FallbackIntent
                ParentIntentSignature: AMAZON.FallbackIntent
                FulfillmentCodeHook:
                  Enabled: true  # Invoke Lambda for dynamic response

    MyLexBotVersion:
      Type: AWS::Lex::BotVersion
      Properties:
        BotId: !Ref MyLexBot
        BotVersionLocaleSpecification:
          - LocaleId: en_US
            BotVersionLocaleDetails:
              SourceBotVersion: "DRAFT"
      DependsOn: MyLexBot

    MyLexBotAlias:
      Type: AWS::Lex::BotAlias
      Properties:
        BotAliasName: prod
        BotId: !Ref MyLexBot
        BotVersion: "1"
        BotAliasLocaleSettings:
          - LocaleId: en_US
            BotAliasLocaleSetting:
              Enabled: true
              CodeHookSpecification:
                LambdaCodeHook:
                  CodeHookInterfaceVersion: "1.0"
                  LambdaArn: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:claude-lex-bedrock-bot-dev-LexBedrockHandler"
      DependsOn: MyLexBotVersion

    LexBotRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Principal:
                Service: lexv2.amazonaws
              Action: sts:AssumeRole
        Policies:
          - PolicyName: LexBotAccessPolicy
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Effect: Allow
                  Action:
                    - logs:CreateLogGroup
                    - logs:CreateLogStream
                    - logs:PutLogEvents
                    - lambda:InvokeFunction
                    - bedrock:InvokeModel
                    - bedrock:Retrieve
                    - bedrock:RetrieveAndGenerate
                  Resource: "*"

    LexLambdaPermission:
      Type: AWS::Lambda::Permission
      Properties:
        Action: lambda:InvokeFunction
        FunctionName: claude-lex-bedrock-bot-dev-LexBedrockHandler
        Principal: lexv2.amazonaws
        SourceArn: !Sub "arn:aws:lex:${AWS::Region}:${AWS::AccountId}:bot-alias/${MyLexBot}/${MyLexBotAlias.BotAliasId}"

  Outputs:
    BotId:
      Description: The ID of the Lex Bot
      Value: !Ref MyLexBot
    AliasId:
      Description: The ID of the Lex Bot Alias
      Value: !GetAtt MyLexBotAlias.BotAliasId

plugins:
  - serverless-api-gateway-throttling
  - serverless-dotenv-plugin

custom:
  apiGatewayThrottling:
    maxRequestsPerSecond: 10
    maxConcurrentRequests: 5

package:
  exclude:
    - node_modules/**
    - .git/**
    - .gitignore
    - test/**
    - "*.md"

also you can add the qnaintent by the cli create the bot through the yaml file and then add the qnaintent through the cli it will work

#!/bin/bash

# Set your variables
BOT_ID=""             # Your hardcoded bot ID
LOCALE_ID="en_US"                # Bot locale
KNOWLEDGE_BASE_ID=""   # Your knowledge base ID
REGION="eu-west-2"               # AWS region
ACCOUNT_ID=""        # Your AWS account ID (from your Serverless YAML)
BOT_VERSION="DRAFT"              # Lex bot version

# Construct the full ARN for the knowledge base
KNOWLEDGE_BASE_ARN="arn:aws:bedrock:$REGION:$ACCOUNT_ID:knowledge-base/$KNOWLEDGE_BASE_ID"

# Create QnAIntent configuration file
echo "Creating configuration..."
cat > qna_config.json << EOL
{
  "intentName": "QnAIntent",
  "description": "Q&A via Knowledge Base",
  "parentIntentSignature": "AMAZON.QnAIntent",
  "qnAIntentConfiguration": {
    "dataSourceConfiguration": {
      "bedrockKnowledgeStoreConfiguration": {
        "bedrockKnowledgeBaseArn": "$KNOWLEDGE_BASE_ARN"
      }
    },
    "bedrockModelConfiguration": {
      "modelArn": "arn:aws:bedrock:$REGION::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0"
    }
  }
}
EOL

# Delete the existing QnAIntent (if it exists)
echo "Deleting existing QnAIntent..."
aws lexv2-models delete-intent \
  --bot-id "$BOT_ID" \
  --bot-version "$BOT_VERSION" \
  --locale-id "$LOCALE_ID" \
  --intent-id "4L9UIT0PFW" || echo "No existing intent to delete"

# Create the updated QnAIntent
echo "Creating QnAIntent..."
aws lexv2-models create-intent \
  --bot-id "$BOT_ID" \
  --bot-version "$BOT_VERSION" \
  --locale-id "$LOCALE_ID" \
  --cli-input-json file://qna_config.json || exit 1

# Rebuild the bot locale
echo "Rebuilding bot locale..."
aws lexv2-models build-bot-locale \
  --bot-id "$BOT_ID" \
  --bot-version "$BOT_VERSION" \
  --locale-id "$LOCALE_ID" || exit 1

echo "QnAIntent added successfully!"



发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744256517a4565425.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信