Guide: How to insert text into Memory
A step-by-step guide to insert raw text into Memory using the Langbase SDK.
In this guide, we will learn how to insert raw text into Memory using the Langbase SDK. This way you can turn your text into a file and store it in Memory for RAG.
Step #0Get API Key
You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.
Step #1Install Langbase SDK
Run the following command to install the Langbase SDK:
Install the SDK
npm i langbase
Step #2Add Langbase API Key
Add your Langbase API key to the .env
file:
LANGBASE_API_KEY=YOUR_API_KEY
Step #3Create a Memory
We will use langbase.memory.create()
function to create a new memory. This memory will store the text file.
import {Langbase} from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!
});
await langbase.memory.create({
name: 'text-based-memory',
description: 'This memory contains documents created from text'
});
Response will contain the newly created memory object; for example:
{
name: 'text-based-memory',
description: 'This memory contains documents created from text',
owner_login: 'langbase',
url: 'https://langbase.com/memorysets/langbase/text-based-memory'
}
Step #4Upload the text
Now that we have the memory, we can use it to upload text to the memory. We will use langbase.memory.documents.upload()
function to upload the text as a document to the memory.
import {Langbase} from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
const text = "Hello, World!";
const document = new Blob([text], { type: "text/plain" });
await langbase.memory.documents.upload({
memoryName: 'text-based-memory',
contentType: 'text/plain',
documentName: 'file-from-text.txt',
document: document,
});
That's it! You have successfully created a document from text and uploaded it to Memory using the Langbase SDK.
You can see the text as a document using the UI or using langbase.memory.documents.list()
function from Langbase SDK. Let's list the documents in the memory to see the uploaded document.
import {Langbase} from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
const memoryDocumentsList = await langbase.memory.documents.list({
memoryName: 'text-based-memory'
});
console.log(memoryDocumentsList);
We will get a response with the list of documents in the memory.
[
{
"name": "file-from-text.txt",
"status": "completed",
"status_message": null,
"metadata": {
"size": 13,
"type": "text/plain"
},
"enabled": true,
"chunk_size": 1024,
"chunk_overlap": 256,
"owner_login": "saqib"
}
]
We can see the uploaded file file-from-text.txt
in the memory. You can now use this file in your RAG chatbot or any other application that uses Langbase Memory.