AWS Bedrock
Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon through a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI.
Using AWS Bedrock with Nile
AWS Bedrock’s foundation models can be used with Nile to build B2B applications using RAG (Retrieval Augmented Generation) architectures.
Below, we show a simple example of how to use AWS Bedrock’s embedding models with Nile. However, keep in mind that AWS Bedrock offers a wide range of models and capabilities. All of them can be used with Nile to build powerful AI-native applications, using similar patterns to the one shown below.
We’ll walk you through the setup steps and then explain the code line by line. The entire script is available here.
Setting Up Nile
Once you’ve signed up for Nile, you’ll be promoted to create your first database. Go ahead and do so. You’ll be redirected to the “Query Editor” page of your new database. This is a good time to create the table we’ll be using in this example:
Once you’ve created the table, you’ll see it on the left-hand side of the screen. You’ll also see the tenants
table that is built-in to Nile.
Next, you’ll want to pick up your database connection string: Navigate to the “Settings” page, select “Connections” and click “Generate credentials”. Copy the connection string and keep it in a secure location.
To use Nile in your application, you’ll also need to install Psycopg2, a Python library for interacting with Postgres. And since we’ll be using vector embeddings, it helps to have pgvector’s Python client installed as well. You can install it with the following command:
Setting Up AWS Bedrock
The first thing you’ll need to do is to request access to the models you’ll be using. AWS model availability varies by region, so make sure you select the region where your application will be deployed and request access to models in that region.
Note that it takes some time for AWS to approve access requests, so make sure to do this well ahead of time. In this example, we’ll be using Titan Text Embeddings V2
model from Amazon.
You’ll also need to install boto3
, which is AWS’s Python SDK. You can install it with the following command:
The simplest way to use boto3 on your local machine is using AWS profile and credentials. If you already have them configured, thats great! Otherwise, you’ll need to use AWS CLI to configure them. AWS Boto3 documentation explains how to do this as well as other ways you can authenticate.
Quickstart
Now that we have everything set up, we can start writing some code (or alternatively, you can download the entire script here and follow along.
First, we’ll need to import the libraries we’ll be using and setting up the Boto3 Bedrock client:
Next, we’ll set up the connection to the Nile database, register the pgvector client with the cursor, and create a tenant who will own the todo items:
Now we’ll generate embeddings for a few todo items and insert them into Nile:
Now we’ll use Nile’s RAG capabilities to retrieve todo items related to a given query:
Run the script with the following command:
And if everything went well, you should see the following output:
Seems like a good answer to the question!
Full application
Coming soon!