Upload Document langbase.memories.documents.upload()
Upload documents to a memory in using the langbase.memories.documents.upload()
function.
This function can also be used to replace an existing document in a memory. To do this, you need to provide the same fileName
and memoryName
attributes as the existing document.
You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.
API reference
Function Signature
langbase.memories.documents.upload(options);
// with types.
langbase.memories.documents.upload(options: MemoryUploadDocOptions);
- Name
options
- Type
- MemoryUploadDocOptions
- Description
MemoryUploadDocOptions Object
memoryName: string; documentName: string; document: Buffer | File | FormData | ReadableStream; contentType: | 'application/pdf' | 'text/plain' | 'text/markdown' | 'text/csv'; | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | 'application/vnd.ms-excel'; meta?: Record<string, string>;
Following are the properties of the options object.
- Name
memoryName
- Type
- string
- Required
- Required
- Description
Name of the memory to upload the document to.
- Name
documentName
- Type
- string
- Required
- Required
- Description
Name of the document.
- Name
document
- Type
- Buffer | File | FormData | ReadableStream
- Required
- Required
- Description
The body of the document to be stored in the bucket. It can be
Buffer
File
FormData
ReadableStream
- Name
contentType
- Type
- string
- Required
- Required
- Description
MIME type of the document. Supported types:
application/pdf
: PDF documentstext/plain
: Plain text files and all major code filestext/markdown
: Markdown filestext/csv
: CSV filesapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
: Excel filesapplication/vnd.ms-excel
: Excel files
- Name
meta
- Type
- Record<string, string>
- Description
Custom metadata for the document, limited to string key-value pairs. A maximum of 10 pairs is allowed.
Usage example
Install the SDK
npm i langbase
Environment variables
Environment variables
LANGBASE_API_KEY="<USER/ORG-API-KEY>"
Upload document
Upload document to memory on Langbase
import {Langbase} from 'langbase';
import {readFileSync} from 'fs';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const hasDocumentUploaded = await langbase.memories.documents.upload({
memoryName: 'knowledge-base',
contentType: 'application/pdf',
documentName: 'technical-doc.pdf',
document: readFileSync('document.pdf'),
meta: {
category: 'technical',
section: 'overview',
},
});
if (hasDocumentUploaded.ok) {
console.log('Document uploaded successfully');
}
}
main();
- Name
Response
- Type
- object
- Description
The response object returned by the
langbase.memories.documents.upload()
function.Response
ok: boolean; status: number; statusText: string;
- Name
ok
- Type
- boolean
- Description
Indicates whether the upload was successful.
- Name
status
- Type
- number
- Description
HTTP status code of the upload response.
- Name
statusText
- Type
- string
- Description
HTTP status message corresponding to the status code.
Response of langbase.memories.documents.upload()
{
"ok": true,
"status": 200,
"statusText": "OK"
}