Retrieve from Memory langbase.memory.retrieve()

Retrieve similar chunks from an AI memory on Langbase for a query using the langbase.memory.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.memory.retrieve(options)

Function Signature

langbase.memory.retrieve(options);

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

options

  • Name
    options
    Type
    MemoryRetrieveOptions
    Description

    MemoryRetrieveOptions Object

    interface MemoryRetrieveOptions {
    	query: string;
    	memory: {
    		name: string;
    	}[];
    	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<{name: string}>
    Required
    Required
    Description

    An array of memory objects from which to retrieve similar chunks.

  • 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

.env file

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

Retrieve from memory

Retrieve from memory on Langbase

import {Langbase} from 'langbase';

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

const results = await langbase.memory.retrieve({
	query: "What are the key features?",
	memory: [{ name: "product-docs" }]
});

Response

  • Name
    MemoryRetrieveResponse[]
    Type
    array
    Description

    The response array returned by the langbase.memory.retrieve() function.

    MemoryRetrieveResponse

    interface MemoryRetrieveResponse {
    	text: string;
    	similarity: number;
    	meta: Record<string, string>;
    }
    
    • Name
      text
      Type
      string
      Description

      Retrieved text segment from memory.

    • Name
      similarity
      Type
      number
      Description

      Similarity score between the query and retrieved text (0-1 range).

    • Name
      meta
      Type
      Record<string, string>
      Description

      Additional metadata associated with the retrieved text.

Response of langbase.memory.retrieve()

[
	{
		"text": "Key features include real-time collaboration, version control, and automated testing.",
		"similarity": 0.92,
		"meta": {
			"source": "features.md",
			"section": "overview"
		}
	},
	{
		"text": "The platform supports multiple programming languages and frameworks.",
		"similarity": 0.85,
		"meta": {
			"source": "technical-specs.md",
			"section": "compatibility"
		}
	}
]