Upload Document langbase.memory.documents.upload()

Upload documents to a memory in using the langbase.memory.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.


Generate a User/Org API key

You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.


API reference

langbase.memory.documents.upload(options)

Function Signature

langbase.memory.documents.upload(options);

// with types.
langbase.memory.documents.upload(options: MemoryUploadDocOptions);

options

  • Name
    options
    Type
    MemoryUploadDocOptions
    Description

    MemoryUploadDocOptions Object

    interface MemoryUploadDocOptions {
    	memoryName: string;
    	fileName: string;
    	file: 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
    fileName
    Type
    string
    Required
    Required
    Description

    Name of the document.

  • Name
    file
    Type
    Buffer | File | FormData | ReadableStream
    Required
    Required
    Description

    The body of the file to be stored in the bucket. It can be Buffer, File, FormData, or ReadableStream type.

  • Name
    contentType
    Type
    string
    Required
    Required
    Description

    MIME type of the document. Supported types:

    • application/pdf: PDF documents
    • text/plain: Plain text files and all major code files
    • text/markdown: Markdown files
    • text/csv: CSV files
    • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet: Excel files
    • application/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

.env file

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!,
});

await langbase.memory.documents.upload({
	memoryName: 'knowledge-base',
	contentType: 'application/pdf',
	documentName: 'technical-doc.pdf',
	document: readFileSync('document.pdf'),
	meta: {
		company: 'XYZ Inc.',
		url: 'https://example.com/technical-doc.pdf'
	}
});

Response

  • Name
    Response
    Type
    object
    Description

    The response object returned by the langbase.memory.documents.upload() function.

    Response

    interface 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.memory.documents.upload()

{
	"ok": true,
	"status": 200,
	"statusText": "OK"
}