In this blog, we will learn how to insert raw text into Memory using the Langbase API. This way you can turn your text into a file and store it in Memory for RAG.
Step 0: Get 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 1: Create a Memory
We will use the create endpoint of user memory API to create a memory. This memory will store the text file.
1async function createNewMemory() {
2 const url = 'https://api.langbase.com/v1/memory';
3 const apiKey = '<YOUR_API_KEY>';
4
5 const memory = {
6 name: "text-based-memory",
7 description: "This memory contains files created from text",
8 };
9
10 const response = await fetch(url, {
11 method: 'POST',
12 headers: {
13 'Content-Type': 'application/json',
14 Authorization: `Bearer ${apiKey}`,
15 },
16 body: JSON.stringify(memory),
17 });
18
19 const newMemory = await response.json();
20 return newMemory;
21}Response will contain the newly created memory object; for example:
1{
2 name: 'text-based-memory',
3 description: 'This memory contains files created from text',
4 owner_login: 'langbase',
5 url: 'https://langbase.com/memory/langbase/text-based-memory'
6}Step 2: Create a Signed URL
Next we create a signed URL using the upload endpoint of Memory API. This signed URL will be used to upload the text. This text is stored as a file in the memory.
Make sure to replace the API key and the ownerLogin.
1async function getSignedUploadUrl() {
2 const url = 'https://api.langbase.com/v1/memory/documents';
3 const apiKey = '<YOUR_API_KEY>';
4
5 const newDoc = {
6 memoryName: 'text-based-memory',
7 ownerLogin: 'langbase',
8 fileName: 'file-from-text.txt',
9 };
10
11 const response = await fetch(url, {
12 method: 'POST',
13 headers: {
14 'Content-Type': 'application/json',
15 Authorization: `Bearer ${apiKey}`,
16 },
17 body: JSON.stringify(newDoc),
18 });
19
20 const res = await response.json();
21
22 return res;
23}
24Response will contain the signed URL; something like this:
1{
2 "signedUrl": "https://b.langbase.com/..."
3}Step 3: Upload the text
Now that we have the signed URL, we can use it to upload text to the memory. We will use the PUT method to upload the text as a file on the signed URL.
1async function uploadDocument(signedUrl) {
2
3 const text = "Hello, World!";
4 const file = new Blob([text], { type: "text/plain" });
5
6 const response = await fetch(signedUrl, {
7 method: "PUT",
8 headers: {
9 "Content-Type": "text/plain",
10 },
11 body: file,
12 });
13
14 return response;
15}That's it! You have successfully created a file from text and uploaded it to Memory using the Langbase API.
You can see the text as a file using the UI or using the list endpoint of Documents API in Langbase. Let's list the documents in the memory to see the uploaded file.
Replace memoryName with your memory name and <YOUR_API_KEY> with your user/org API key to get the list of documents in the following code.
1async function listMemorySets() {
2 const url = 'https://api.langbase.com/v1/memory/{memoryName}/documents';
3 const apiKey = '<YOUR_API_KEY>';
4
5 const response = await fetch(url, {
6 method: 'GET',
7 headers: {
8 'Content-Type': 'application/json',
9 Authorization: `Bearer ${apiKey}`
10 },
11 });
12
13 const memoryDocumentsList = await response.json();
14 return memoryDocumentsList;
15}We will get a response with the list of documents in the memory.
1[
2 {
3 "name": "file-from-text.txt",
4 "status": "completed",
5 "status_message": null,
6 "metadata": {
7 "size": 13,
8 "type": "text/plain"
9 },
10 "enabled": true,
11 "chunk_size": 10000,
12 "chunk_overlap": 2048,
13 "owner_login": "saqib"
14 }
15]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.
