Guide: How to insert text into Memory

A step-by-step guide to insert raw text into Memory using the Langbase API.


In this guide, 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 #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 #1Create a Memory

We will use the create endpoint of user memory API to create a memory. This memory will store the text file.

async function createNewMemory() {
  const url = 'https://api.langbase.com/beta/user/memorysets';
  const apiKey = '<USER_API_KEY>';

  const memory = {
    name: "text-based-memory",
    description: "This memory contains files created from text",
  };

  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;
}

Response will contain the newly created memory object; for example:

{
  name: 'text-based-memory',
  description: 'This memory contains files created from text',
  owner_login: 'langbase',
  url: 'https://langbase.com/memorysets/langbase/text-based-memory'
}

Step #2Create 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.

async function getSignedUploadUrl() {
  const url = 'https://api.langbase.com/beta/user/memorysets/documents';
  const apiKey = '<USER_API_KEY>';

  const newDoc = {
    memoryName: 'text-based-memory',
    ownerLogin: 'langbase',
    fileName: 'file-from-text.txt',
  };

  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${apiKey}`,
    },
    body: JSON.stringify(newDoc),
  });

  const res = await response.json();

  return res;
}

Response will contain the signed URL; something like this:

{
  "signedUrl": "https://b.langbase.com/..."
}

Step #3Upload 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.

async function uploadDocument(signedUrl) {

  const text = "Hello, World!";
  const file = new Blob([text], { type: "text/plain" });

  const response = await fetch(signedUrl, {
    method: "PUT",
    headers: {
      "Content-Type": "text/plain",
    },
    body: file,
  });

  return response;
}

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 the owner and memoryName in the URL, and use the API key to get the list of documents in the memory.

async function listMemorySets() {
  const url = 'https://api.langbase.com/beta/memorysets/{owner}/{memoryName}/documents';
  const apiKey = '<YOUR_API_KEY>';

  const response = await fetch(url, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${apiKey}`
    },
  });

  const memoryDocumentsList = await response.json();
  return memoryDocumentsList;
}

We will get a response with the list of documents in the memory.

"docs": [
    {
      "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.