Quickstart: Build efficient RAG with Langbase
A step-by-step guide to building a RAG solution with Langbase
In this guide, you will learn how to build a highly scalable RAG powered AI support agent. You will:
- Create an AI Memory agent that will contain FAQs data.
- Create an AI support agent pipe configured with
gpt-4o-mini
model. - Connect the AI Memory agent to the AI support agent pipe to create a RAG solution
Let's get started
There are two ways to follow this guide:
Step #1Generate Langbase API key
Every request you send to Langbase needs an API key. This guide assumes you already have one. In case, you do not have an API key, please check the instructions below.
Step #2Add LLM API keys
If you have set up LLM API keys in your profile, the Pipe will automatically use them. If not, navigate to LLM API keys page and add keys for different providers like OpenAI, TogetherAI, Anthropic, etc.
Step #3Setup your project
Create a new directory for your project and navigate to it.
Project setup
mkdir ai-support-agent && cd ai-support-agent
Initialize the project
Create a new Node.js project.
Initialize project
npm init -y
Install dependencies
You will use the Langbase SDK to connect to the AI agent pipes and dotenv
to manage environment variables. So, let's install these dependencies.
Install dependencies
npm i langbase dotenv
Create an env file
Create a .env
file in the root of your project and add the following environment variables:
.env
LANGBASE_API_KEY=xxxxxxxxx
Replace xxxxxxxxx
with your Langbase API key.
Step #4Create an AI Memory agent
To create an AI Memory agent, you will use the Langbase SDK to create a new Memory. Create a new file create-memory.ts
and add the following code:
create-memory.ts
import 'dotenv/config';
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const memory = await langbase.memories.create({
name: 'support-memory-agent',
description: 'Support chatbot memory agent',
});
console.log('Memory:', memory);
}
main();
Now run the following command in your terminal to create an AI Memory agent:
npx tsx create-memory.ts
Step #5Upload FAQs data
You will use the langbase.memories.upload()
function to upload content to the memory agent. Let's upload FAQs to the memory agent.
Create a new file upload-doc.ts
and add the following code:
upload-doc.ts
import 'dotenv/config';
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main(content: string) {
// Create a Blob object with the FAQs content
const documentBlob = new Blob([content], { type: 'text/plain' });
// Create a File object from the Blob object
const document = new File([documentBlob], 'langbase-faqs.txt', { type: 'text/plain' });
// Upload the document to the memory agent
const response = await langbase.memories.documents.upload({
document,
memoryName: 'support-memory-agent',
contentType: 'text/plain',
documentName: 'langbase-faqs.txt',
});
console.log('Document uploaded:', response);
}
// FAQs content
const langbaseFAQs = `
How to reset password?
Step 1: Access Profile Settings
1. Log in to your Langbase account.
2. Navigate to the [Profile Settings page](https://langbase.com/settings/profile)
Step 2: Change Your Password
1. Scroll to the Reset Password section.
2. Enter your new password in the New Password box.
3. Click Set Password to confirm the change.
How to edit user profile?
Step 1: Access Profile Settings
1. Log in to your Langbase account.
2. Navigate to the Profile Settings page.
Step 2: Edit Your Profile
1. Click on Change Profile Picture to upload an image from your computer.
• Note: The image should be in PNG or JPG format and must not exceed 1MB in size.
2. Enter a name for your profile in the Name field.
3. Create a username for your profile using a hyphen-separated format in the Username field.
4. Write a short bio in the Bio field.
Step 3: Save Your Changes
1. Click Save Changes to apply your edits.
How to upgrade organization plan?
Step 1: Access the Organization Billing Page
1. Log in to your Langbase account.
2. Navigate to your Organization Profile page.
3. Click the Billing button in the top-right corner.
Step 2: Review Current Billing Status
1. In the Subscription section, view your current plan, term, and status details.
2. The Usage section summarizes your organization’s activity, including the total number of pipes, requests, and memory usage.
Step 3: Upgrade to Pro or Enterprise
1. Go to the Plans section to compare and explore available subscription options.
2. Click Subscribe or Let’s Talk and follow the prompts to complete the upgrade.
`;
main(langbaseFAQs);
You can also import a file using fs
module and pass it as a value to document
.
Let's run the script to upload the FAQs data to the memory agent:
npx tsx upload-doc.ts
Step #6Retrieval testing of FAQs data
Let's do a retrieval test to check if the FAQs data was uploaded successfully. You will use the langbase.memories.retrieve()
function to list the documents in the memory agent.
Create a new file retrieval.ts
and add the following code:
retrieval.ts
import 'dotenv/config';
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const chunks = await langbase.memories.retrieve({
memory: [
{
name: 'support-memory-agent',
},
],
query: 'How to reset password?',
topK: 1
});
console.log('Memory chunks:', chunks);
}
main();
Run the script to test the retrieval of FAQs data:
npx tsx retrieval.ts
This will print the memory chunks containing the FAQs data on the console.
Retrieval output
[
{
"text": 'How to reset password?\n' +
'Step 1: Access Profile Settings\n' +
'\t1.\tLog in to your Langbase account.\n' +
'\t2.\tNavigate to the [Profile Settings page](https://langbase.com/settings/profile)\n' +
'Step 2: Change Your Password\n' +
'\t1.\tScroll to the Reset Password section.\n' +
'\t2.\tEnter your new password in the New Password box.\n' +
'\t3.\tClick Set Password to confirm the change.\n' +
'\n' +
'How to edit user profile?\n' +
'Step 1: Access Profile Settings\n' +
'\t1.\tLog in to your Langbase account.\n' +
'\t2.\tNavigate to the Profile Settings page.\n' +
'\n' +
'Step 2: Edit Your Profile\n' +
'\t1.\tClick on Change Profile Picture to upload an image from your computer.\n' +
'\t\t\tNote: The image should be in PNG or JPG format and must not exceed 1MB in size.\n' +
'\t2.\tEnter a name for your profile in the Name field.\n' +
'\t3.\tCreate a username for your profile using a hyphen-separated format in the Username field.\n' +
'\t4.\tWrite a short bio in the Bio field.\n' +
'\n' +
'Step 3: Save Your Changes\n' +
'\t1.\tClick Save Changes to apply your edits.\n' +
'\n' +
'How to upgrade organization plan?',
similarity: 0.5440677404403687,
meta: { documentName: 'langbase-faqs.txt' }
}
]
Step #6Create an AI support agent pipe
You need to create an AI support agent pipe. It will use the gpt-4o-mini
model and the AI memory you created earlier to answer user queries.
Create a new file create-pipe.ts
and add the following code:
create-pipe.ts
import 'dotenv/config';
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const supportAgent = await langbase.pipes.create({
name: `ai-support-agent`,
description: `An AI agent to support users with their queries.`,
messages: [
{
role: `system`,
content: `You're a helpful AI assistant. You will assist users with their queries about Langbase. Always ensure that you provide accurate and to the point information.`,
},
],
memory: [{ name: 'support-memory-agent' }],
});
console.log('Support agent:', supportAgent);
}
main();
You have defined a system prompt and linked the memory agent to the pipe by specifying the memory agent's name while creating the pipe. Run the script to create the AI support agent pipe:
npx tsx create-pipe.ts
Step #7Run the AI support agent pipe
Now that you have created the AI support agent pipe, let's run it to test the agent. You will use the langbase.pipes.run()
function to run the pipe.
Create a new file run-pipe.ts
and add the following code:
run-pipe.ts
import 'dotenv/config';
import { Langbase, getRunner } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const { stream } = await langbase.pipes.run({
name: `ai-support-agent`,
stream: true,
messages: [
{
role: `user`,
content: `How to request payment API?`,
},
],
});
const runner = getRunner(stream);
runner.on('content', content => {
process.stdout.write(content);
});
}
main();
You are using the stream
option to get real-time completions from the AI model. Now let's run the above file to see the AI generate completions for the user message:
npx tsx run-pipe.ts
The AI support agent will generate a response to the user query using OpenAI gpt-4o-mini
model based on the FAQs data uploaded to the memory agent.
LLM generation
To reset your password on Langbase, follow these steps:
Step 1: Access Profile Settings
1. Log in to your Langbase account.
2. Navigate to the [Profile Settings page](https://langbase.com/settings/profile).
Step 2: Change Your Password
1. Scroll to the Reset Password section.
2. Enter your new password in the New Password box.
3. Click Set Password to confirm the change.
You can also ask other questions like:
- How to edit user profile?
- How to upgrade organization plan?
And the AI support agent will generate responses based on the FAQs data uploaded to the memory agent.
✨ That's it! You have successfully created an AI support agent that uses AI Memory (RAG) to answer user queries. You can now integrate this agent into your application to provide instant support to your users.
Next Steps
- Explore SDK & API reference to learn more about Langbase APIs.
- Join our Discord community for feedback, requests, and support.