AWS Big Data Blog
Empower financial analytics by creating structured knowledge bases using HAQM Bedrock and HAQM Redshift
Traditionally, financial data analysis could require deep SQL expertise and database knowledge. Now with HAQM Bedrock Knowledge Bases integration with structured data, you can use simple, natural language prompts to query complex financial datasets. By combining the AI capabilities of HAQM Bedrock with an HAQM Redshift data warehouse, individuals with varied levels of technical expertise can quickly generate valuable insights, making sure that data-driven decision-making is no longer limited to those with specialized programming skills.
With the support for structured data retrieval using HAQM Bedrock Knowledge Bases, you can now use natural language querying to retrieve structured data from your data sources, such as HAQM Redshift. This enables applications to seamlessly integrate natural language processing capabilities on structured data through simple API calls. Developers can rapidly implement sophisticated data querying features without complex coding—just connect to the API endpoints and let users explore financial data using plain English. From customer portals to internal dashboards and mobile apps, this API-driven approach makes enterprise-grade data analysis accessible to everyone in your organization. Using structured data from a Redshift data warehouse, you can efficiently and quickly build generative AI applications for tasks such as text generation, sentiment analysis, or data translation.
In this post, we showcase how financial planners, advisors, or bankers can now ask questions in natural language, such as, “Give me the name of the customer with the highest number of accounts?” or “Give me details of all accounts for a specific customer.” These prompts will receive precise data from the customer databases for accounts, investments, loans, and transactions. HAQM Bedrock Knowledge Bases automatically translates these natural language queries into optimized SQL statements, thereby accelerating time to insight, enabling faster discoveries and efficient decision-making.
Solution overview
To illustrate the new HAQM Bedrock Knowledge Bases integration with structured data in HAQM Redshift, we will build a conversational AI-powered assistant for financial assistance that is designed to help answer financial inquiries, like “Who has the most accounts?” or “Give details of the customer with the highest loan amount.”
We will build a solution using sample financial datasets and set up HAQM Redshift as the knowledge base. Users and applications will be able to access this information using natural language prompts.
The following diagram provides an overview of the solution.
For building and running this solution, the steps include:
- Load sample financial datasets.
- Enable HAQM Bedrock large language model (LLM) access for HAQM Nova Pro.
- Create an HAQM Bedrock knowledge base referencing structured data in HAQM Redshift.
- Ask queries and get responses in natural language.
To implement the solution, we use a sample financial dataset that is for demonstration purposes only. The same implementation approach can be adapted to your specific datasets and use cases.
Download the SQL script to run the implementation steps in HAQM Redshift Query Editor V2. If you’re using another SQL editor, you can copy and paste the SQL queries either from this post or from the downloaded notebook.
Prerequisites
Make sure your meet the following prerequisites:
- Have an AWS account.
- Create an HAQM Redshift Serverless workgroup or provisioned cluster. For setup instructions, see Creating a workgroup with a namespace or Create a sample HAQM Redshift database, respectively. The HAQM Bedrock integration feature is supported in both HAQM Redshift provisioned and serverless.
- Create an AWS Identity and Access Management (IAM) role. For instructions, see Creating or updating an IAM role for HAQM Redshift ML integration with HAQM Bedrock.
- Associate the IAM role to a Redshift instance.
- Set up the required permissions for HAQM Bedrock Knowledge Bases to connect with HAQM Redshift.
Load sample financial data
To load the finance datasets to HAQM Redshift, complete the following steps:
- Open the HAQM Redshift Query Editor V2 or another SQL editor of your choice and connect to the Redshift database.
- Run the following SQL to create the finance data tables and load sample data:
- Download the sample financial dataset to your local storage and unzip the zipped folder.
- Create an HAQM Simple Storage Service (HAQM S3) bucket with a unique name. For instructions, refer to Creating a general purpose bucket.
- Upload the downloaded files into your newly created S3 bucket.
- Using the following COPY command statements, load the datasets from HAQM S3 into the new tables you created in HAQM Redshift. Replace
<<your_s3_bucket>>
with the name of your S3 bucket and<<your_region>>
with your AWS Region.
Enable LLM access
With HAQM Bedrock, you can access state-of-the-art AI models from providers like Anthropic, AI21 Labs, Stability AI, and HAQM’s own foundation models (FMs). These include Anthropic’s Claude 2, which excels at complex reasoning and content generation; Jurassic-2 from AI21 Labs, known for its multilingual capabilities; Stable Diffusion from Stability AI for image generation; and HAQM Titan models for various text and embedding tasks. For this demo, we use HAQM Bedrock to access the HAQM Nova FMs. Specifically, we use the HAQM Nova Pro model, which is a highly capable multimodal model designed for a wide range of tasks like video summarization, Q&A, mathematical reasoning, software development, and AI agents, including high speed and accuracy for text summarization tasks.
Make sure you have the required IAM permissions to enable access to available HAQM Bedrock Nova FMs. Then complete the following steps to enable model access in HAQM Bedrock:
- On the HAQM Bedrock console, in the navigation pane, choose Model access.
- Choose Enable specific models.
- Search for HAQM Nova models, select Nova Pro, and choose Next.
- Review the selection and choose Submit.
Create an HAQM Bedrock knowledge base referencing structured data in HAQM Redshift
HAQM Bedrock Knowledge Bases uses HAQM Redshift as the query engine to query your data. It reads metadata from your structured data store to generate SQL queries. There are different supported authentication methods to create the HAQM Bedrock knowledge base using HAQM Redshift. For more information, refer to the Set up query engine for your structured data store in HAQM Bedrock Knowledge Bases.
For this post, we create an HAQM Bedrock knowledge base for the Redshift database and sync the data using IAM authentication.
If you’re creating an HAQM Bedrock knowledge base through the AWS Management Console, you can skip the service role setup mentioned in the previous section. It automatically creates one with the necessary permissions for HAQM Bedrock Knowledge Bases to retrieve data from your new knowledge base and generate SQL queries for structured data stores.
When creating an HAQM Bedrock knowledge base using an API, you must attach IAM policies that grant permissions to create and manage knowledge bases with connected data stores. Refer to Prerequisites for creating an HAQM Bedrock Knowledge Base with a structured data store for instructions.
Complete the following steps to create an HAQM Bedrock knowledge base using structured data:
- On the HAQM Bedrock console, choose Knowledge Bases in the navigation pane.
- Choose Create and choose Knowledge Base with structure data store from the dropdown menu.
- Provide the following details for your knowledge base:
- Enter a name and optional description.
- Select HAQM Redshift as the query engine.
- Select Create and use a new service role for resource management.
- Make note of this newly created IAM role.
- Choose Next to proceed to the next part of the setup process.
- Configure the query engine:
- Select Redshift Serverless (HAQM Redshift provisioned is also supported).
- Choose your Redshift workgroup.
- Use the IAM role created earlier.
- Under Default storage metadata, select HAQM Redshift databases and for Database, choose dev.
- You can customize settings by adding specific contexts to enhance the accuracy of the results.
- Choose Next.
- Complete creating your knowledge base.
- Record the generated service role details.
- Next, grant appropriate access to the service role for HAQM Bedrock Knowledge Bases through the HAQM Redshift Query Editor V2. Update <your Service Role name> in the following statements with your service role, and update the value for <your schema>.
Now you can update the knowledge base with the Redshift database.
- On the HAQM Bedrock console, choose Knowledge Bases in the navigation pane.
- Open the knowledge base you created.
- Select the dev Redshift database and choose Sync.
It may take a few minutes for the status to display as COMPLETE.
Ask queries and get responses in natural language
You can set up your application to query the knowledge base or attach the knowledge base to an agent by deploying your knowledge base for your AI application. For this demo, we use a native testing interface on the HAQM Bedrock Knowledge Bases console.
To ask questions in natural language on the knowledge base for Redshift data, complete the following steps:
- On the HAQM Bedrock console, open the details page for your knowledge base.
- Choose Test.
- Choose your category (HAQM), model (Nova Pro), and inference settings (On demand), and choose Apply.
- In the right pane of the console, test the knowledge base setup with HAQM Redshift by asking a few simple questions in natural language, such as “How many tables do I have in the database?” or “Give me list of all tables in the database.”
The following screenshot shows our results.
- To view the generated query from your HAQM Redshift based knowledge base, choose Show details next to the response.
- Next, ask questions related to the financial datasets loaded in HAQM Redshift using natural language prompts, such as, “Give me the name of the customer with the highest number of accounts” or “Give the details of all accounts for customer Deanna McCoy.”
The following screenshot shows the responses in natural language.
Using natural language queries in HAQM Bedrock, you were able to retrieve responses from the structured financial data stored in HAQM Redshift.
Considerations
In this section, we discuss some important considerations when using this solution.
Security and compliance
When integrating HAQM Bedrock with HAQM Redshift, implementing robust security measures is crucial. To protect your systems and data, implement essential safeguards including restricted database roles, read-only database instances, and proper input validation. These measures help prevent unauthorized access and potential system vulnerabilities. For more information, see Allow your HAQM Bedrock Knowledge Bases service role to access your data store.
Cost
You incur a cost for converting natural language to text based on SQL. To learn more, refer to HAQM Bedrock pricing.
Use custom contexts
To improve query accuracy, you can enhance SQL generation by providing custom context in two key ways. First, specify which tables to include or exclude, focusing the model on relevant data structures. Second, supply curated queries as examples, demonstrating the types of SQL queries you expect. These curated queries serve as valuable reference points, guiding the model to generate more accurate and relevant SQL outputs tailored to your specific needs. For more information, refer to Create a knowledge base by connecting to a structured data store.
For different workgroups, you can create separate knowledge bases for each group, with access only to their specific tables. Control data access by setting up role-based permissions in HAQM Redshift, verifying each role can only view and query authorized tables.
Clean up
To avoid incurring future charges, delete the Redshift Serverless instance or provisioned data warehouse created as part of the prerequisite steps.
Conclusion
Generative AI applications provide significant advantages in structured data management and analysis. The key benefits include:
- Using natural language processing – This makes data warehouses more accessible and user-friendly
- Enhancing customer experience – By providing more intuitive data interactions, it boosts overall customer satisfaction and engagement
- Simplifying data warehouse navigation – Users can understand and explore data warehouse content through natural language interactions, improving ease of use
- Improving operational efficiency – By automating routine tasks, it allows human resources to focus on more complex and strategic activities
In this post, we showed how the natural language querying capabilities of HAQM Bedrock Knowledge Bases when integrated with HAQM Redshift enables rapid solution development. This is particularly valuable for the finance industry, where financial planners, advisors, or bankers face challenges in accessing and analyzing large volumes of financial data in a secured and performant manner.
By enabling natural language interactions, you can bypass the traditional barriers of understanding database structures and SQL queries, and quickly access insights and provide real-time support. This streamlined approach accelerates decision-making and drives innovation by making complex data analysis accessible to non-technical users.
For additional details on HAQM Bedrock and HAQM Redshift integration, refer to HAQM Redshift ML integration with HAQM Bedrock.
About the authors
Nita Shah is an Analytics Specialist Solutions Architect at AWS based out of New York. She has been building data warehouse solutions for over 20 years and specializes in HAQM Redshift. She is focused on helping customers design and build enterprise-scale well-architected analytics and decision support platforms.
Sushmita Barthakur is a Senior Data Solutions Architect at HAQM Web Services (AWS), supporting Strategic customers architect their data workloads on AWS. With a background in data analytics, she has extensive experience helping customers architect and build enterprise data lakes, ETL workloads, data warehouses and data analytics solutions, both on-premises and the cloud. Sushmita is based in Florida and enjoys traveling, reading and playing tennis.
Jonathan Katz is a Principal Product Manager – Technical on the HAQM Redshift team and is based in New York. He is a Core Team member of the open source PostgreSQL project and an active open source contributor, including PostgreSQL and the pgvector project.