79552585

Date: 2025-04-03 10:00:26
Score: 1.5
Natty:
Report link

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.com
              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.com
        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"
Reasons:
  • Blacklisted phrase (0.5): How can I
  • Blacklisted phrase (1): Good morning
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Thomas Joseph