Logo
Classes

Memory

Defined in: packages/core/src/memory/memory.ts:43

Type Parameters

TAdapters

TAdapters extends Record<string, MessageAdapter<unknown, TMessageOptions>> = Record<string, never>

TMessageOptions

TMessageOptions extends object = object

Constructors

Constructor

new Memory<TAdapters, TMessageOptions>(messages, options): Memory<TAdapters, TMessageOptions>

Defined in: packages/core/src/memory/memory.ts:80

Parameters

messages

MemoryMessage<TMessageOptions>[] = []

options

MemoryOptions<TMessageOptions> = {}

Returns

Memory<TAdapters, TMessageOptions>

Methods

add()

add(message): Promise<void>

Defined in: packages/core/src/memory/memory.ts:112

Add a message to the memory

Parameters

message

unknown

The message to add to the memory

Returns

Promise<void>


get()

get<K>(options): Promise<K extends keyof TAdapters | keyof BuiltinAdapters<TMessageOptions> ? ReturnType<TAdapters & BuiltinAdapters<TMessageOptions>[K<K>]["fromMemory"]>[] : never>

Defined in: packages/core/src/memory/memory.ts:140

Get the messages of specific type from the memory

Type Parameters

K

K extends string | number | symbol = "llamaindex"

Parameters

options

The options for the get method

type?

K

transientMessages?

ChatMessage<TMessageOptions>[]

Returns

Promise<K extends keyof TAdapters | keyof BuiltinAdapters<TMessageOptions> ? ReturnType<TAdapters & BuiltinAdapters<TMessageOptions>[K<K>]["fromMemory"]>[] : never>

The messages of specific type


getLLM()

getLLM(llm, transientMessages?): Promise<ChatMessage[]>

Defined in: packages/core/src/memory/memory.ts:189

Get the messages from the memory, optionally including transient messages. only return messages that are within context window of the LLM

Parameters

llm

To fit the result messages to the context window of the LLM (fallback to default llm if not provided). If llm is not specified in both the constructor and the method, the default token limit will be used.

undefined | LLM<object, object>

transientMessages?

ChatMessage<TMessageOptions>[]

Optional transient messages to include.

Returns

Promise<ChatMessage[]>

The messages from the memory, optionally including transient messages.


manageMemoryBlocks()

manageMemoryBlocks(): Promise<void>

Defined in: packages/core/src/memory/memory.ts:327

Manage the memory blocks This method processes new messages into memory blocks when short-term memory exceeds its token limit. It uses a cursor system to track which messages have already been processed into long-term memory.

Returns

Promise<void>


clear()

clear(): Promise<void>

Defined in: packages/core/src/memory/memory.ts:398

Clear all the messages in the memory

Returns

Promise<void>


snapshot()

snapshot(): string

Defined in: packages/core/src/memory/memory.ts:409

Creates a snapshot of the current memory state Note: Memory blocks are not included in snapshots as they may contain non-serializable content. Memory blocks should be recreated when loading from snapshot.

Returns

string

A JSON-serializable object containing the memory state