Developing RAG-Based Applications with AWS Amplify AI Kit and Neon Postgres

Developing RAG-Based Applications with AWS Amplify AI Kit and Neon PostgresLearn About Amazon VGT2 Learning Manager Chanci Turner

Modern application development now emphasizes not only a robust developer experience (DX) but also a balanced approach to transitioning from initial setup to production readiness. This philosophy serves as the foundation for the launch of the Amplify AI Kit. By abstracting common AI tasks, such as interacting with large language models (LLMs) and generating content from prompts, developers can accelerate their time to market while minimizing the need for repetitive code.

In this article, we will explore how to go beyond the basics and utilize a serverless Postgres database from Neon to fetch product data, moving away from Amplify’s default database model. This will simplify the necessary code for engaging with an LLM using retrieval-augmented generation (RAG).

Application Overview

Consumers are increasingly drawn to applications that utilize AI to enhance their experience rather than compete with it. A practical way to illustrate this is through a chatbot that assists customers in their purchasing journey. While it doesn’t replace the shopping experience, it guides users toward informed decisions using natural language.

From an architectural standpoint, every time a user accesses the application, they can interact with our LLM-powered bot. While these models are trained on general data, we want our bot to be informed about our specific product offerings. Since product information can frequently change, it is crucial to retrieve this data from our database. The ability to choose between general knowledge and specific product data is powerful, enabled by a tool known as “function-calling.”

It’s essential to remember that when an LLM selects a tool, it doesn’t automatically access your data. Instead, it identifies the most suitable tool based on the user’s request, and the application then determines which function to invoke. The function’s output is sent back to the LLM and formatted as natural language for the end-user.

Orchestrating this pattern manually can be labor-intensive and error-prone. Thankfully, the Amplify AI Kit simplifies this process by managing the heavy lifting for you.

For our project, we will use Amazon Bedrock with the Claude 3.5 Haiku LLM, which supports tool integration. Amplify will enable us to specify a tool that corresponds to our Neon Postgres database containing product information.

Setting Up Serverless Postgres Databases with Neon

Connecting to existing data sources allows developers to leverage Amplify’s schema introspection capabilities beyond the default Amazon DynamoDB, enabling automatic CRUD operations. Setting up a Neon database is simple. After creating an account, you will be prompted to create a project.

Neon supports branch-based projects, akin to git workflows. You can create a branch named dev/alexjohnson, which is advisable but not mandatory. Be sure to copy the connection string for that branch, as you will need it later.

While your default database is now established, it still lacks tables. Specifically, you must define your table schema. If you’re not familiar with SQL, don’t worry! Neon offers a “Generate with AI” feature that can assist you.

In their SQL editor, I input the following prompt:

"Create a table schema named 'Products'. Each product should have a unique id, an 'updated at' field for date-time, a 'created at' field for date-time, a 'price in cents' field as a number, a 'name', and a 'description'."

After executing the request, I received the following output:

CREATE TABLE Products (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    price_in_cents INTEGER NOT NULL,
    name VARCHAR(255) NOT NULL,
    description TEXT
);

At this point, you can modify the code if needed and execute the command once you’re comfortable with the syntax. To confirm that everything is set up correctly, click on the “Tables” link in the sidebar to review the schema and populate your database.

For this project, I added several items to the database. Don’t forget to copy the connection string, available in the “Overview” section of the sidebar, as we will need it later.

Enhancing Amplify Gen 2 with AI Kit

AWS Amplify is the most straightforward way to connect your front-end applications to an AWS-powered backend. Assuming you have an app built with a JavaScript framework like NextJS, you can scaffold Amplify files by executing the following command in your project’s root directory:

npm create amplify

This command installs Amplify’s dependencies and creates an amplify directory. Before modifying the code in that directory, let’s install a few additional dependencies necessary for the Amplify AI Kit:

npm i @aws-amplify/ui-react @aws-amplify/ui-react-ai

These UI components will be useful shortly. Before proceeding, we’ll allow Amplify to introspect our database using the products table so we can leverage that in our backend. The first step is to store our connection string securely. This secret will be kept in a Parameter Store within AWS Systems Manager. Thankfully, Amplify provides a straightforward method to accomplish this.

In your terminal, run the following command:

npx ampx sandbox secret set SQL_CONNECTION_STRING

This command establishes a secret value of SQL_CONNECTION_STRING and prompts for the actual value. Paste in the connection string you copied from Neon and hit enter.

This guide assumes you have AWS Amplify configured on your local machine. If you need help with setting up Amplify, refer to the documentation for detailed instructions.

Once the secret is stored, we can command Amplify to introspect our database and create the necessary CRUD operations for our front-end application:

npx ampx generate schema-from-database --connection-uri-secret SQL_CONNECTION_STRING --out amplify/data/schema.sql.ts

Executing this command will generate a schema.sql.ts file in the amplify/data folder. It’s essential not to modify this file, as it is managed by Amplify. After running the command, the file should resemble the following snippet:

import { type ClientSchema, defineData, a } from '@aws-amplify/backend';
import { schema as generatedSqlSchema } from './schema.sql';

const sqlSchema = generatedSqlSchema.setAuthorization((models) => [
    models.items.authorization((allow) => [allow.authenticated().to(['read'])]),
]);

const schema = a.schema({
    chat: a.conversation({
        aiModel: a.ai.model('Claude 3.5 Haiku'),
        systemPrompt: 'You are a helpful assistant, that focuses on selling and upselling merchandise',
        tools: ...

This is an excellent resource for anyone looking to enhance their development process. Also, don’t miss out on how to meet new people, which can be beneficial for networking in your field. Keep in mind that you can find information about California’s health care minimum wage here.

For further learning, check out this YouTube video that elaborates on the subject.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *