Memory

[!TIP]

Location within the framework Hive-agent-framework/memory.

Memory in the context of an agent refers to the system's capability to store, recall, and utilize information from past interactions. This enables the agent to maintain context over time, improve its responses based on previous exchanges, and provide a more personalized experience.

Usage

Capabilities showcase

import { UnconstrainedMemory } from "Hive-agent-framework/memory/unconstrainedMemory";
import { BaseMessage } from "Hive-agent-framework/llms/primitives/message";

const memory = new UnconstrainedMemory();

// Single message
await memory.add(
  BaseMessage.of({
    role: "system",
    text: `You are a helpful assistant.`,
  }),
);

// Multiple messages
await memory.addMany([
  BaseMessage.of({ role: "user", text: `What can you do?` }),
  BaseMessage.of({ role: "assistant", text: `Everything!` }),
]);

console.info(memory.isEmpty()); // false
console.info(memory.messages); // prints all saved messages
console.info(memory.asReadOnly()); // returns a NEW read only instance
memory.reset(); // removes all messages

Source: examples/memory/base.ts

Usage with LLMs

Source: examples/memory/llmMemory.ts

[!TIP]

Memory for non-chat LLMs works exactly the same way.

Usage with agents

Source: examples/memory/agentMemory.ts

[!TIP]

If your memory already contains the user message, run the agent with prompt: null.

[!NOTE]

Hive Agent internally uses TokenMemory to store intermediate steps for a given run.

[!NOTE]

Agent typically works with a memory similar to what was just shown.

Memory types

The framework provides multiple out-of-the-box memory implementations.

UnconstrainedMemory

Unlimited in size.

Source: examples/memory/unconstrainedMemory.ts

SlidingMemory

Keeps last k entries in the memory. The oldest ones are deleted (unless specified otherwise).

Source: examples/memory/slidingMemory.ts

TokenMemory

Ensures that the token sum of all messages is below the given threshold. If overflow occurs, the oldest message will be removed.

Source: examples/memory/tokenMemory.ts

SummarizeMemory

Only a single summarization of the conversation is preserved. Summarization is updated with every new message.

Source: examples/memory/summarizeMemory.ts

Creating a custom memory provider

To create your memory implementation, you must implement the BaseMemory class.

Source: examples/memory/custom.ts

The simplest implementation is UnconstrainedMemory, which can be found here.