How to Create an AI Memory in Langbase using API?
Langbase AI memory allows you to store, organize, and retrieve information. It can be used to build powerful Retrieval Augmented Generation (RAG) based AI agents which can use your data to assist with your queries.
AI memories can be attached to AI agent pipes. Each memory can contain multiple documents. You can either create an AI memory using Langbase's studio or via the memory API.
This guide walks you through the steps that you need to follow to create an AI memory using the API.
Let’s take a look at how it’s done:
Step #1
You will need to generate a user/org API key to authenticate all your API requests to Langbase. For more information, visit the User/Org API key documentation for more details.
Step #2
Create a new memory by sending the memory data inside the request body. For this you'll need to send a POST request to the API endpoint.
POST /v1/memory
Next, send the memory data inside the request body. Here’s how you can send a request with Node.js:
async function createNewMemory() {
const url = 'https://api.langbase.com/v1/memory';
const apiKey = '<YOUR_API_KEY>';
const memory = {
name: 'rag-memory',
description: 'RAG memory',
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(memory),
});
const newMemory = await response.json();
return newMemory;
}
In the above requests, add user/org API keys.
Request Parameters
Add the following request parameters to the endpoint:
Add the following request parameters to the endpoint:
Headers:
Content-Type:application/jsonAuthorization: Replace<YOUR_API_KEY>with your user/org API key.
Body Parameters:
name: The name of the memory you want to create (required)description: A short description of the memory (optional)
Once the request is successful, the response will include the memory's details, including the memory’s URL.
After creating a memory, the API response will return the memory's details, such as:
{
"name": "rag-memory",
"description": "RAG memory",
"owner_login": "langbase",
"url": "https://langbase.com/memorysets/langbase/rag-memory"
}
The url will be the link to your newly created memory.
By following these steps, you can easily create an AI memory in Langbase using the API. This memory can then be used to store documents and assist your AI agents in providing more accurate responses through Retrieval Augmented Generation (RAG).