Skip to content

Commit

Permalink
docs(memory): add missing links
Browse files Browse the repository at this point in the history
Ref: #102
Signed-off-by: Tomas Dvorak <toomas2d@gmail.com>
  • Loading branch information
Tomas2D committed Oct 19, 2024
1 parent 65de51a commit 09b1e2c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
32 changes: 18 additions & 14 deletions docs/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Memory in the context of an agent refers to the system's capability to store, re

### Capabilities showcase

<!-- embedme examples/memory/unconstrainedMemory.ts -->
<!-- embedme examples/memory/base.ts -->

```ts
import { UnconstrainedMemory } from "bee-agent-framework/memory/unconstrainedMemory";
Expand All @@ -22,17 +22,14 @@ const memory = new UnconstrainedMemory();
await memory.add(
BaseMessage.of({
role: "system",
text: `You are a helpful and funny assistant.`,
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: `Get it? I'm a helpful assistant, I can answer questions, provide information, and even tell jokes!`,
}),
BaseMessage.of({ role: "assistant", text: `Everything!` }),
]);

console.info(memory.isEmpty()); // false
Expand All @@ -41,11 +38,7 @@ console.info(memory.asReadOnly()); // returns a NEW read only instance
memory.reset(); // removes all messages
```

_Source: [examples/memory/unconstrainedMemory.ts](/examples/memory/unconstrainedMemory.ts)_

> [!TIP]
>
> You can create a new read-only memory instance by calling the `asReadOnly` method on the existing one.
_Source: [examples/memory/base.ts](/examples/memory/base.ts)_

### Usage with LLMs

Expand Down Expand Up @@ -129,18 +122,27 @@ The framework provides multiple out-of-the-box memory implementations.

Unlimited in size.

<!-- embedme examples/memory/unconstrainedMemory.ts -->

```ts
import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat";
import { UnconstrainedMemory } from "bee-agent-framework/memory/unconstrainedMemory";
import { BaseMessage } from "bee-agent-framework/llms/primitives/message";

const memory = new UnconstrainedMemory();
await memory.add(BaseMessage.of({ role: "user", text: `Give me first 5 prime numbers.` }));
await memory.add(
BaseMessage.of({
role: "user",
text: `Hello world!`,
}),
);

console.log(memory.isEmpty()); // false
console.info(memory.isEmpty()); // false
console.log(memory.messages.length); // 1
console.log(memory.messages);
```

_Source: [examples/memory/unconstrainedMemory.ts](/examples/memory/unconstrainedMemory.ts)_

### SlidingMemory

Keeps last `k` entries in the memory. The oldest ones are deleted (unless specified otherwise).
Expand Down Expand Up @@ -209,6 +211,8 @@ console.log(memory.stats()); // prints statistics
await memory.sync(); // calculates real token usage for all messages marked as "dirty"
```

_Source: [examples/memory/tokenMemory.ts](/examples/memory/tokenMemory.ts)_

### SummarizeMemory

Only a single summarization of the conversation is preserved. Summarization is updated with every new message.
Expand Down
23 changes: 23 additions & 0 deletions examples/memory/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { UnconstrainedMemory } from "bee-agent-framework/memory/unconstrainedMemory";
import { BaseMessage } from "bee-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
20 changes: 4 additions & 16 deletions examples/memory/unconstrainedMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,13 @@ import { UnconstrainedMemory } from "bee-agent-framework/memory/unconstrainedMem
import { BaseMessage } from "bee-agent-framework/llms/primitives/message";

const memory = new UnconstrainedMemory();

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

// Multiple messages
await memory.addMany([
BaseMessage.of({ role: "user", text: `What can you do?` }),
BaseMessage.of({
role: "assistant",
text: `Get it? I'm a helpful assistant, I can answer questions, provide information, and even tell jokes!`,
}),
]);

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
console.log(memory.messages.length); // 1
console.log(memory.messages);

0 comments on commit 09b1e2c

Please sign in to comment.