Saturday, July 6, 2024
HomeArtificial Intelligence (AI) NewsIntroducing guardrails to Amazon Bedrock knowledge bases

Introducing guardrails to Amazon Bedrock knowledge bases

Amazon Bedrock Knowledge Base is a fully managed feature that enables you to securely connect Amazon Bedrock Foundation Models (FMs) to your enterprise data using Retrieval Augmented Generation (RAG). This feature streamlines the entire RAG workflow, from ingestion to retrieval to prompt enhancements, eliminating the need for custom data source integration and data flow management.

We recently announced the general availability of Guardrails for Amazon Bedrock, enabling you to implement safety controls in your generative artificial intelligence (AI) applications that are customized to your use case and responsible AI policy. You can create multiple guardrails for different use cases and apply them to multiple FMs, allowing you to standardize safety controls across your generative AI applications.

Today, we are introducing guardrails to Amazon Bedrock knowledge bases to enhance the safety and compliance of your generative AI RAG applications. This new capability provides industry-leading safeguards that filter harmful content and protect sensitive information in documents, improving user experience and complying with organizational standards.

Solution overview

Using the Amazon Bedrock knowledge base, you can configure your RAG application to query the knowledge base using the RetrieveAndGenerate API and generate a response from the retrieved information.

By default, the knowledge base allows the RAG application to query the entire Vector database and access all records to retrieve relevant results. This may result in the generation of inappropriate or unwanted content or provide sensitive information, violating certain policies and guidelines set by your company. Integrating guardrails into the knowledge base provides a mechanism to filter and control the generated output in compliance with predefined rules and regulations.

The following diagram shows an example workflow:

Knowledge Base Workflow Guardrails

Test your knowledge base using the Amazon Bedrock console, or RetrieveAndGenerate In an API that uses one of the AWS SDKs, the system generates a query embedding and performs a semantic search to retrieve similar documents from the vector store.

The query is then expanded to include the retrieved document chunks, a prompt, and a guardrail configuration. The guardrails are applied to check for rejected topics and filter harmful content before the expanded query is sent to the InvokeModel API. Finally, InvokeModel The API generates responses from large-scale language models (LLMs) and ensures that the output is free of unwanted content.

In the next section, we’ll show you how to create a knowledge base with guardrails, and compare query results using the same knowledge base with and without guardrails.

Guardrails use case with knowledge base for Amazon Bedrock

Common use cases for integrating guardrails into your knowledge base include:

  • Internal knowledge management for law firms — This allows legal professionals to search case files, cases, and client correspondence. Guardrails can prevent capturing confidential client information and filter out inappropriate language. For example, when a lawyer asks, “What are the key takeaways from the latest intellectual property case?”, guardrails can prevent sensitive client details and inappropriate language from being included in the response, maintaining the integrity and confidentiality of the information.
  • Conversational Search for Financial Services — This allows financial advisors to search investment portfolios, trading history, and market analysis. Guardrails can prevent obtaining unauthorized investment advice and filter out content that violates regulatory compliance. An example query could be, “What are the recent performance metrics for our high net worth clients?” and guardrails ensure only authorized information is shared.
  • E-commerce platform customer support — This allows customer service representatives to access order history, customer inquiries, and product details. Guardrails can block sensitive customer data (such as name, email address, or postal address) from being exposed in responses. For example, if a representative asks, “Can you summarize some of your recent complaints about our new product line?”, guardrails will redact any personally identifiable information (PII), enhancing compliance with privacy and data protection regulations.

Prepare a dataset for your Amazon Bedrock knowledge base

This post uses a sample dataset that contains several fictitious emergency room reports, including detailed procedure notes, pre- and post-operative diagnoses, and patient history. These records demonstrate how you can integrate your knowledge base with guardrails and query effectively.

  1. If you follow the steps in your AWS account, download the files. Each medical record is a Word document.
  2. The dataset is stored in an Amazon Simple Storage Service (Amazon S3) bucket. For instructions on creating a bucket, see Creating a Bucket.
  3. Upload the unzipped files to this S3 bucket.

Creating a Knowledge Base for Amazon Bedrock

For steps to create a new knowledge base, see Create a Knowledge Base. For this example, use the following settings:

  1. upper Configure the Data Source Bottom of page Amazon S3Select the S3 bucket that contains your dataset.
  2. under Chunking Strategyselect No chunks This is because the documents in the dataset are preprocessed to fit within a certain length.
  3. In Embedding Model Select a section and model Titan G1 Embedding – Text.
  4. In Vector Database Select by section Quickly create a new vector store.

Create a knowledge base

Synchronizing a Dataset with the Knowledge Base

Once you have created your knowledge base and your data files are stored in your S3 bucket, you can begin incremental ingestion by following the steps in Synchronize a data source into your knowledge base.

While you’re waiting for the sync job to complete, you can move on to the next section, where you create guardrails.

Creating guardrails in the Amazon Bedrock console

To create a guardrail, follow these steps:

  1. On the Amazon Bedrock console, guardrail In the navigation pane.
  2. choose Create guardrails.
  3. upper Provide guardrail details Bottom of page Guardrail DetailsEnter a name for the guardrail and an optional description.
  4. In Rejected Topics In the section, add the information for the two topics, as shown in the following screenshot.
  5. In Add a sensitive information filter Under Section Types of personal informationAdd all PII types.
  6. choose Create guardrails.

Build a guardrail

Querying the Knowledge Base in the Amazon Bedrock Console

Now let’s test our knowledge base with guardrails.

  1. On the Amazon Bedrock console, Knowledge Base In the navigation pane.
  2. Select the knowledge base you created.
  3. choose Testing Knowledge Base.
  4. please select composition Click on the icon and scroll down guardrail.

The following screenshots show a side-by-side comparison of querying a knowledge base without guardrails (left) and with guardrails (right).

The first example shows a query on a rejected topic.

Querying with and without the guardrail knowledge base

Then, query the data that contains PII.

Querying with and without the guardrail knowledge base

Finally, run a query on another rejected topic.

Querying with and without the guardrail knowledge base

Querying the Knowledge Base using the AWS SDK

To query a knowledge base with guardrails using the AWS SDK for Python (Boto3), you can use the following example code:

import boto3
client = boto3.client('bedrock-agent-runtime')
response = client.retrieve_and_generate(
    input={
        'text': 'Example input text'
    },
    retrieveAndGenerateConfiguration={
        'knowledgeBaseConfiguration': {
            'generationConfiguration': {
                'guardrailConfiguration': {
                    'guardrailId': 'your-guardrail-id',
                    'guardrailVersion': 'your-guardrail-version'
                }
            },
            'knowledgeBaseId': 'your-knowledge-base-id',
            'modelArn': 'your-model-arn'
        },
        'type': 'KNOWLEDGE_BASE'
    },
    sessionId='your-session-id'
)

cleaning

To clean up resources, follow these steps:

  1. Delete the knowledge base.
    1. On the Amazon Bedrock console, Knowledge Base under Orchestration In the navigation pane.
    2. Select the knowledge base you created.
    3. Note the AWS Identity and Access Management (IAM) service role name. Knowledge Base Overview
    4. In Vector Database In the section, note the Amazon OpenSearch Serverless collection ARN.
    5. choose erasethen enter delete to confirm.
  2. Delete the vector database.
    1. In the Amazon OpenSearch Service console, collection under Serverless In the navigation pane.
    2. Enter the collection ARN you saved in the search bar.
    3. Select a collection erase.
    4. Enter “confirm” in the confirmation prompt, erase.
  3. Delete the IAM service role.
    1. In the IAM console, role In the navigation pane.
    2. Search for the role name you noted earlier.
    3. Select a role and click erase.
    4. In the confirmation prompt, enter the role name to delete the role.
  4. Delete the sample dataset.
    1. In the Amazon S3 console, navigate to the S3 bucket that you used.
    2. Select the prefix and file, erase.
    3. To delete, enter “permanently delete” at the confirmation prompt.

Conclusion

In this post, we discussed the integration of Guardrails with Amazon Bedrock knowledge bases, allowing you to benefit from a robust and customizable safety framework that fits the specific requirements of your application and responsible AI practices. The goal of this integration is to enhance the overall security, compliance, and responsible use of the underlying models in your knowledge base ecosystem, providing more control and trust for your AI-driven applications.

For pricing information, see Amazon Bedrock Pricing. To get started with your Amazon Bedrock knowledge base, see Creating a Knowledge Base. For more technical content and to learn how the Builder community is using Amazon Bedrock in their solutions, visit the community.aws website.


About the Author

Hardik Vasa Hardik is a Sr. Solutions Architect at AWS. He focuses on Generative AI and Serverless technologies and helps customers make the most of AWS services. Hardik enjoys sharing his knowledge at various conferences and workshops. In his spare time, he enjoys learning about new technologies, playing video games and spending time with his family.

Vani Sharma He is a Sr. Solutions Architect with Amazon Web Services (AWS) based in Denver, Colorado. As a Solutions Architect, he works with many SMEs providing technical guidance and solutions on AWS. He has deep knowledge in containers, modernization, and is currently working on deepening Generative AI. Prior to joining AWS, he held various technical roles at a major telecommunications provider and worked as a senior developer at a multinational bank.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

error: Content is protected !!