How to Upload Docs in AI Memory using API?
Langbase lets you create multiple AI memories that can be attached to an AI agent pipe. Each memory can contain multiple documents. You can either upload these documents via Langbase studio or via the API.
Let’s take a look at how you can upload documents to any AI memory in Langbase via API.
Step #1
You will need to generate an API key to authenticate your requests. To generate a User or Org API key, visit the User/Org API key documentation.
Step #2
Uploading a document to a memory requires a signed URL. Send a POST request to this endpoint to get a signed URL and use the PUT method to upload the document.
POST /v1/memory/documents
Here’s how you can send a POST request with Node.js:
async function getSignedUploadUrl() {
const url = 'https://api.langbase.com/v1/memory/documents';
const apiKey = '<YOUR_API_KEY>';
const newDoc = {
memoryName: 'rag-memory',
ownerLogin: 'langbase',
fileName: 'file.pdf',
};
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;
}
In the above requests, add user/org API keys.
Request params
The following are required header parameters of documents endpoint:
Content-Type:application/jsonAuthorization: Replace<YOUR_API_KEY>with your user/org API key.
The following are required body parameters of documents endpoint:
memoryName: The name of the memory where the document will be stored.fileName: The name of the document you are uploading.
Once the request is made, you'll receive a signedUrl in the response and the you can upload the document using the PUT method.
Step #3
Now that you have generated a signed URL, let’s use the PUT method to upload the document in our AI Memory. The signed URL is valid for 2 hours.
Currently, we support application/pdf, text/plain, text/markdown, text/csv, and all major code files as text/plain. For csv, pdf, text, and markdown files, it should correspond to the file type used in the fileName attribute in step 2. For code files, it should be text/plain.
This is how the Node.js request looks like (for cURL and Python visit here):
const fs = require("fs");
async function uploadDocument(signedUrl, filePath) {
const file = fs.readFileSync(filePath);
const response = await fetch(signedUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/pdf',
},
body: file,
});
return response;
}
The following are required header parameters:
Content-Type: The MIME type of the document (e.g.,application/pdf,text/plain).- The body should contain the actual file data.
Following these steps will allow you to successfully upload documents to Langbase's AI memory using the API.
Langbase AI memory is a great way 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.
By following this guide, you'll be able to quickly upload documents to memory using the API, making it easier to build and ship AI features.