aws-lambdaaws-cloudformationserverlessamazon-lexamazon-bedrock

aws QnA intent cloudformation template not working


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.com
              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?


Solution

  • 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!"