|
| 1 | +## Intro |
| 2 | + |
| 3 | +### Knowledge bases and agents |
| 4 | + |
| 5 | +Knowledge bases and agents are essential tools in enhancing organizational efficiency and decision-making. Knowledge bases serve as centralized repositories of information, enabling quick access to accurate data, which reduces search times, improves productivity, and supports consistent communication across teams. Knowledge-based agents, on the other hand, utilize this information to make informed decisions and take appropriate actions based on their understanding of the environment, thereby facilitating learning and adaptation through continuous knowledge updates. Together, they streamline operations, reduce training time for new employees, and enhance customer support by providing instant access to resources, ultimately leading to improved organizational performance. |
| 6 | + |
| 7 | +### Amazon Bedrock Knowledge bases and agents |
| 8 | + |
| 9 | +Amazon Bedrock Knowledge Bases and Agents offer powerful tools for ingesting documentation and answering questions intelligently. This example demonstrates how to leverage these capabilities effectively. |
| 10 | +Amazon Bedrock Knowledge Bases are used for implementing Retrieval Augmented Generation (RAG), a technique that enhances AI responses by incorporating contextual information from your company's data sources. The knowledge base ingests and processes your documentation, converting it into vector embeddings that can be efficiently searched. |
| 11 | +Amazon Bedrock Agents, built on top of the knowledge base, provide a natural language interface to interact with the stored information3. These agents can be configured to not only retrieve relevant data but also to execute various actions, including leveraging third-party APIs, in response to user queries. |
| 12 | + |
| 13 | +By combining these technologies, you can create a system that: |
| 14 | + |
| 15 | +1. Ingests and processes your documentation semi-automatically |
| 16 | +2. Retrieves relevant information based on user queries |
| 17 | +3. Generates accurate and contextually appropriate responses |
| 18 | +4. Performs additional actions or API calls as needed to supplement the information |
| 19 | + |
| 20 | +This approach enables the creation of highly customized, intelligent applications that can efficiently answer questions about your specific documentation while also integrating external data sources when necessary |
| 21 | + |
| 22 | +#### AWS Powertools |
| 23 | + |
| 24 | +For this examples we leverage [AWS Powertools](https://docs.powertools.aws.dev/lambda/python/latest/) for lambda. |
| 25 | +The reason is, AWS Powertools is essential for optimizing AWS Lambda functions, providing a suite of utilities that enhance performance, monitoring, and security while reducing the complexity of serverless application development. |
| 26 | +By implementing best practices such as structured logging, custom metrics, and distributed tracing, AWS Powertools enables developers to focus on business logic rather than boilerplate code, streamlining the development process. |
| 27 | +Additionally, it supports multiple programming languages and integrates seamlessly with AWS services, making it a versatile tool for teams looking to improve their serverless architecture and operational efficiency. |
| 28 | + |
| 29 | +### Architecture |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +To understand the architecture of this solution, let’s break it down step by step, moving from left to right in the system's workflow: |
| 34 | + |
| 35 | +#### **User Interaction and REST API** |
| 36 | + |
| 37 | +- **User Interaction**: The system begins with user interaction through REST API calls. These calls can be initiated from a **Web UI**, enabling users to interact with the solution seamlessly. |
| 38 | +- **Security with Amazon Cognito**: All REST API interactions are secured using **Amazon Cognito**. This ensures that only authenticated and authorized users can access the APIs. Users must register and authenticate via Cognito to obtain valid tokens, which are included in the API requests for validation. |
| 39 | + |
| 40 | +#### **API Gateway** |
| 41 | + |
| 42 | +- The **Amazon API Gateway** acts as the entry point for all REST API requests. It exposes the system's functionality by routing requests to the appropriate backend services. |
| 43 | +- API Gateway integrates with Amazon Cognito to validate user credentials before forwarding requests, ensuring robust security and fine-grained access control. |
| 44 | + |
| 45 | +#### **AWS Lambda** |
| 46 | + |
| 47 | +- Once the request passes through the API Gateway, it is processed by **AWS Lambda** functions. These serverless functions handle business logic and interact with other components of the architecture. |
| 48 | +- Lambda functions are responsible for querying and interacting with: |
| 49 | + - **Knowledge Bases**: These could include databases or other structured repositories of information. |
| 50 | + - **Agents**: External or internal systems that perform specific tasks or provide additional data. |
| 51 | + |
| 52 | +#### **Data Storage in Amazon S3** |
| 53 | + |
| 54 | +- The data to be processed or searched by the knowledge base and agents is stored in an **Amazon S3 bucket**. |
| 55 | +- For this specific use case, it is assumed that users manually upload documents to the S3 bucket. This allows the system to process these documents as part of its operations. |
| 56 | + |
| 57 | +#### **Key Features of the Architecture** |
| 58 | + |
| 59 | +1. **Security**: By leveraging Amazon Cognito and JWT-based authentication, the architecture ensures secure access to APIs without requiring session management overhead[6]. |
| 60 | +2. **Scalability**: With serverless components like AWS Lambda and S3, the system is highly scalable, capable of handling varying workloads efficiently. |
| 61 | +3. **Modularity**: Each component (API Gateway, Lambda, Cognito, S3) is independent yet seamlessly integrated, allowing for easy updates or replacements without disrupting the entire system. |
| 62 | + |
| 63 | +This architecture demonstrates a robust design for handling secure user interactions, efficient data processing, and scalable storage using AWS services. It aligns with best practices in solution architecture by ensuring security, scalability, and maintainability. |
| 64 | + |
| 65 | +## Deploying the CDK Stack: A Step-by-Step Guide |
| 66 | + |
| 67 | +Follow the these steps to install the example: |
| 68 | + |
| 69 | +### 1. Install Dependencies |
| 70 | + |
| 71 | +Begin by installing the necessary npm dependencies. Navigate to the top-level directory of your project in your terminal and run: |
| 72 | + |
| 73 | +``` |
| 74 | +npm install |
| 75 | +``` |
| 76 | + |
| 77 | +This command will fetch and install all required packages specified in your `package.json` file. |
| 78 | + |
| 79 | +### 2. Bootstrap CDK |
| 80 | + |
| 81 | +Before deploying your stack, you need to bootstrap CDK in your AWS account and region. This process sets up the necessary resources for CDK to manage deployments. Execute the following command, replacing `<account>` and `<region>` with your AWS account ID and desired region: |
| 82 | + |
| 83 | +``` |
| 84 | +npx cdk bootstrap aws://<account>/<region> |
| 85 | +``` |
| 86 | + |
| 87 | +For example, if your account ID is 123456789012 and you're deploying to us-east-1, the command would be: |
| 88 | + |
| 89 | +``` |
| 90 | +npx cdk bootstrap aws://123456789012/us-east-1 |
| 91 | +``` |
| 92 | + |
| 93 | +### 3. Deploy the Stack |
| 94 | + |
| 95 | +With the dependencies installed and CDK bootstrapped, you're ready to deploy your stack. Simply run: |
| 96 | + |
| 97 | +``` |
| 98 | +npx cdk deploy |
| 99 | +``` |
| 100 | + |
| 101 | +This command synthesizes a CloudFormation template from your CDK code and deploys it to your AWS account, creating or updating the specified resources. |
| 102 | + |
| 103 | +By following these steps, you'll have your CDK stack up and running in your AWS environment, ready to manage your infrastructure efficiently. |
| 104 | + |
| 105 | +### 4. Knowledge Base Configuration and Document Processing |
| 106 | + |
| 107 | +After completing the installation process, you'll be able to access your newly created Knowledge Base within Amazon Bedrock. To view and manage your Knowledge Base: |
| 108 | + |
| 109 | +1. Navigate to the AWS Management Console |
| 110 | +2. Locate and select the Amazon Bedrock service |
| 111 | +3. Choose "Knowledge and datasource" from the sidebar menu |
| 112 | + |
| 113 | +This will display your Knowledge Base and its associated S3 bucket, which serves as the repository for documents used in your Knowledge Base. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +### Document Upload and Synchronization |
| 118 | + |
| 119 | +To populate your Knowledge Base with relevant information: |
| 120 | + |
| 121 | +1. Select an appropriate document for ingestion. For this exercise, we recommend using the [Amazon S3 user manual](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-userguide.pdf), which provides a comprehensive overview of S3 functionality. |
| 122 | +2. Upload the chosen document to the S3 bucket directory specified in your Knowledge Base configuration. |
| 123 | +3. After successful upload, locate and press the "Synchronize" button within the Bedrock console. |
| 124 | + |
| 125 | +The synchronization process is a crucial step that accomplishes the following: |
| 126 | + |
| 127 | +- It triggers the creation of embeddings for the uploaded documents. |
| 128 | +- These embeddings are vector representations of the text content, enabling efficient semantic search and retrieval. |
| 129 | +- The system uses a specified embeddings model to convert the textual data into these high-dimensional vectors. |
| 130 | + |
| 131 | +### Behind the Scenes |
| 132 | + |
| 133 | +During synchronization, Amazon Bedrock performs several important tasks: |
| 134 | + |
| 135 | +1. **Document Chunking**: The uploaded content is divided into manageable chunks, optimizing it for processing and retrieval. |
| 136 | + |
| 137 | +2. **Embedding Generation**: Each chunk is processed through the selected embeddings model, transforming the text into numerical vectors. |
| 138 | + |
| 139 | +3. **Vector Storage**: The generated embeddings are stored in the configured vector database, which could be Amazon OpenSearch Serverless or another compatible service. |
| 140 | + |
| 141 | +4. **Metadata Association**: Any relevant metadata from the original documents is preserved and associated with the corresponding embeddings. |
| 142 | + |
| 143 | +This process enables your Knowledge Base to efficiently search and retrieve relevant information when queried, forming the foundation for powerful retrieval-augmented generation (RAG) applications. |
| 144 | + |
| 145 | +By following these steps, you'll have successfully initialized and populated your Amazon Bedrock Knowledge Base, preparing it for use in various AI-driven tasks and applications. |
| 146 | + |
| 147 | +### Test the Knowledge Base with the AWS console |
| 148 | + |
| 149 | +To interact with Amazon Bedrock Knowledge Bases using the AWS console, you first need to navigate to the Amazon Bedrock service and select "Knowledge bases" from the left navigation pane. |
| 150 | +You will notice the new created knowledge base named: `KBBedrockKb.....` select it and then use the knowledge base chat to query information or generate AI responses based on the ingested data. For this step you need to choose a model, for this exercise we sued Anthropic/Claude 3 Haiku. |
| 151 | + |
| 152 | +If you choose to use the `Amazon S3 user manual` you can test the Knowledge Base you can ask for questions about S3. |
| 153 | + |
| 154 | +#### Amazon Bedrock Model access |
| 155 | + |
| 156 | +In order to be able to use foundation models you need to request access for. In order to do this navigate to the Amazon Bedrock service and select "Model access" from the left navigation pane. |
| 157 | +On the Model access page, users can review the End User License Agreement (EULA) for each model and then click "Modify model access" to select the desired models. After selecting the models, users should review the terms, submit any required use case details (such as for Anthropic models), and click "Submit" to request access, which may take several minutes to be reflected in the console. |
| 158 | + |
| 159 | +For this exercise we recommend you to request access for all Anthropic/Claude 3 models. |
| 160 | + |
| 161 | +## Test it with AWS Console |
| 162 | + |
| 163 | +The interaction/functionality from section `Test the Knowledge Base with the AWS console` is can be done programmatic as well as code. |
| 164 | +For this exercise we prepared two distinct functionalities, the Knowledge Base RAG query and Agent query. |
| 165 | +The codebase provide two Amazon Powertools lambdas let's try to explore them. |
| 166 | + |
| 167 | +### Knowledge Base RAG query and agent query |
| 168 | + |
| 169 | +The code is available in the `api/example-powertools/example.py` file. |
| 170 | + |
| 171 | +The AWS Lambda function in this code is designed to be triggered by API Gateway when a REST request is received. |
| 172 | +API Gateway routes incoming HTTP requests to the appropriate Lambda function based on the configured API endpoints. |
| 173 | +When a request matches a defined route, API Gateway invokes the Lambda function, passing the request details as an event object. |
| 174 | + |
| 175 | +You can test the lambda function from the AWS console using the event the predefined events with the Lambda AWS console as follow steps. |
| 176 | + |
| 177 | +First navigate to the Lambda function `BedrockKbAgentStack-ExampleLambdaFn....` in the AWS console and click on the "Test" tab. |
| 178 | +Create a new test event by selecting "Configure test event" from the dropdown menu, then enter `..test/lambda-events/ask-kb.json` JSON payload in the event body. Finally, click "Test" to execute the Lambda function with your specified event, and view the results in the console's execution output. |
| 179 | + |
| 180 | +Same logic for agent query, with the difference that for the agent query you need to use `..test/lambda-events/ask-agent.json` |
| 181 | + |
| 182 | +This is how the `ask-kb` event response looks like |
| 183 | + |
| 184 | +```json |
| 185 | +{ |
| 186 | + "statusCode": 200, |
| 187 | + "body": "{\"conv_id\":\"0b772d3a-3b56-4bb4-ac8f-ee5d4272dc89\",\"question\":\"what is s3?\",\"answer\":\"Amazon S3 (Simple Storage Service) is an object storage service offered by Amazon Web Services (AWS). It allows users to store and retrieve data of any amount or type, from anywhere on the web. Some key features of Amazon S3 include:\\n\\n- Storage classes: S3 offers a range of storage classes designed for different use cases, such as frequently accessed data, infrequently accessed data, and archived data.\\n- Data protection: S3 is designed to provide 99.999999999% durability and 99.99% availability of objects over a given year.\\n- Lifecycle management: Users can configure lifecycle rules to automatically transition objects between storage classes or expire objects that reach the end of their lifetimes.\\n- Access management: S3 provides various access management tools to control who can access the stored data and what actions they can perform.\"}", |
| 188 | + "isBase64Encoded": false, |
| 189 | + "headers": { |
| 190 | + "Content-Type": "application/json" |
| 191 | + }, |
| 192 | + "cookies": [] |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +Let's try to understand it |
| 197 | + |
| 198 | +1. for the exercise we ingest the `S3 user manual` and this means we can ask S# specific questions, for this case we send the question: `what is s3?` |
| 199 | +2. the Knowledge Base RAG answer is: `Amazon S3 (Simple Storage Service) is an object storage ....` |
| 200 | +3. the method returns also conversation id, useful if you want to create a longer conversation. |
| 201 | + |
| 202 | +This is how the `ask-kb` event response looks like |
| 203 | + |
| 204 | +```json |
| 205 | +{ |
| 206 | + "statusCode": 200, |
| 207 | + "body": "{\"conv_id\":\"adc56b78a40b4a99bf82c5236596bc01\",\"question\":\"how is the weather in London?\",\"answer\":\"The weather in London is rainy\"}", |
| 208 | + "isBase64Encoded": false, |
| 209 | + "headers": { |
| 210 | + "Content-Type": "application/json" |
| 211 | + }, |
| 212 | + "cookies": [] |
| 213 | +} |
| 214 | +``` |
| 215 | + |
| 216 | +Let's try to understand it. |
| 217 | + |
| 218 | +This event is routed to the Knowledge Base Agent, the source code for this is in the `./api/example-agent/example.py`. |
| 219 | +This file s is simple implementation of a Knowledge Base Agent. |
| 220 | + |
| 221 | +The Knowledge Base Agent, implemented in example.py, is designed to recognize and respond to weather-related questions for specific locations. This agent utilizes a llm mechanisms to determine the location mentioned in a query and provides a mock weather forecast for that location. |
| 222 | +The implementation includes a /weather endpoint that generates a random weather condition (either "rainy" or "sunny") for the given location, demonstrating a basic example of how a knowledge-based system can process and respond to user queries. |
| 223 | + |
| 224 | +## Destroy |
| 225 | + |
| 226 | +In order to remove this example run: |
| 227 | + |
| 228 | +``` |
| 229 | +npx cdk destroy |
| 230 | +``` |
0 commit comments