Guide: How to replace a document in Memory

A step-by-step guide to replace an existing document in Memory using the Langbase API.


In this guide, we will learn how to replace an existing document in Memory using the Langbase API. This is useful when you need to update the content of a file that is already stored in a memory.


Step #0Get API Key

You will need to generate an API key to authenticate your requests. Visit User/Org API key documentation page to learn more.

Step #1Create a Signed URL for Replacement

We'll use the upload endpoint of the Memory API to create a signed URL for replacing the document. This process is similar to uploading a new document, but we'll use the same filename as the existing document.

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

  const replaceDoc = {
    memoryName: 'your-memory-name',
    ownerLogin: 'your-username',
    fileName: 'existing-document-name.pdf',  // Use the name of the existing document
  };

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

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

The response will contain the signed URL, which looks like this:

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

Step #2Upload the Replacement Document

Now that we have the signed URL, we can use it to upload the replacement document. We'll use the PUT method to upload the new file, overwriting the existing document.

const fs = require('fs');

async function replaceDocument(signedUrl, filePath) {
  const file = fs.readFileSync(filePath);

  const response = await fetch(signedUrl, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/pdf',  // Adjust based on your file type
    },
    body: file,
  });

  return response;
}

Step #3Verify the Replacement

After replacing the document, you can verify that the update was successful by listing the documents in the memory again.

async function listMemoryDocuments() {
  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;
}

The response will show the updated document with the same name but potentially different metadata:

"docs": [
  {
    "name": "existing-document-name.pdf",
    "status": "completed",
    "status_message": null,
    "metadata": {
      "size": 1048576,  // This might be different
      "type": "application/pdf"
    },
    "enabled": true,
    "chunk_size": 1024,
    "chunk_overlap": 256,
    "owner_login": "your-username"
  }
]

That's it! You have successfully replaced an existing document in Memory using the Langbase API. The document retains its original name, but its content has been updated. This process allows you to keep your Memory up-to-date without creating duplicate entries.

Remember that replacing a document will trigger a re-processing of the content, including re-chunking and re-embedding. This ensures that your RAG system uses the most current information when responding to queries.