Retrieve Memories langbase.memories.retrieve()

Retrieve memories on Langbase using the langbase.memories.retrieve() function.


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.memories.retrieve()

Function Signature

langbase.memories.retrieve(options);

// with types.
langbase.memories.retrieve(options: MemoryRetrieveOptions);

options

  • Name
    options
    Type
    MemoryRetrieveOptions
    Description

    MemoryRetrieveOptions Object

    query: string;
    memory:  Memory[];
    topK?: number;
    

    Following are the properties of the options object.


  • Name
    query
    Type
    string
    Required
    Required
    Description

    The search query for retrieving similar chunks.

  • Name
    memory
    Type
    Array<Memory>
    Required
    Required
    Description

    An array of memory objects from which to retrieve similar chunks. Each object can include optional filters to narrow down the search.

    Memory Object

    name: string;
    filters?: MemoryFilters;
    

  • Name
    topK
    Type
    number
    Description

    The number of top similar chunks to return from memory.

    Default is 20, minimum is 1, and maximum is 100.

Usage example

Install the SDK

npm i langbase

Environment variables

Environment variables

LANGBASE_API_KEY="<USER/ORG-API-KEY>"

Retrieve memories

Retrieve from memory on Langbase

import {Langbase} from 'langbase';

const langbase = new Langbase({
	apiKey: process.env.LANGBASE_API_KEY!,
});

async function main() {
	const chunks = await langbase.memories.retrieve({
		query: "What are the key features?",
		memory: [{
			name: "knowledge-base"
		}]
	});

	console.log('Memory chunk:', chunks);
}

main();

Response

  • Name
    MemoryRetrieveResponse
    Type
    object
    Description

    The response object returned by the langbase.memories.retrieve() function.

    MemoryRetrieveResponse Object

    text: string;
    similarity: number;
    meta: Record<string, string>;
    
    • Name
      text
      Type
      string
      Description

      The retrieved memory text.

    • Name
      similarity
      Type
      number
      Description

      The similarity score of the retrieved memory.

    • Name
      meta
      Type
      Record<string, string>
      Description

      Metadata associated with the retrieved memory.

Response Examples

[
	{
		"text": "Key features of Langbase include: semantic search capabilities, flexible memory management, and scalable architecture for handling large datasets.",
		"similarity": 0.92,
		"meta": {
			"category": "features",
			"section": "overview"
		}
	},
	{
		"text": "Our platform offers advanced features like real-time memory updates, custom metadata filtering, and enterprise-grade security.",
		"similarity": 0.87,
		"meta": {
			"category": "updates",
			"section": "highlights"
		}
	},
	{
		"text": "Platform highlights include AI-powered memory retrieval, customizable embedding models, and advanced filtering capabilities.",
		"similarity": 0.85,
		"meta": {
			"category": "features",
			"section": "highlights"
		}
	}
]