Skip to content

Commit ff166a3

Browse files
authored
Evict memory stream (#5)
1 parent bdcdd48 commit ff166a3

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

examples/chain/chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewAgentB(llm chatter.Chatter) *AgentB {
6565
agt.registry.Register(command.Return())
6666

6767
agt.Automata = agent.NewAutomata(llm,
68-
memory.NewStream(`
68+
memory.NewStream(memory.INFINITE, `
6969
You are automomous agent who uses tools to perform required tasks.
7070
You are using and remember context from earlier chat history to execute the task.
7171
`),

examples/rainbow/rainbow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func main() {
9797

9898
// Configures memory for the agent. Typically, memory retains all of
9999
// the agent's observations. Here, we use a stream memory that holds all observations.
100-
memory.NewStream("You are agent who remembers and uses earlier chat history."),
100+
memory.NewStream(memory.INFINITE, "You are agent who remembers and uses earlier chat history."),
101101

102102
// Configures the reasoner, which determines the agent's next actions and prompts.
103103
// Here, we use custom (app specific) reasoner. The agent is restricted to execute

examples/script/script.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func main() {
9595

9696
// Configures memory for the agent. Typically, memory retains all of
9797
// the agent's observations. Here, we use a stream memory that holds all observations.
98-
memory.NewStream(`
98+
memory.NewStream(memory.INFINITE, `
9999
You are automomous agent who uses tools to perform required tasks.
100100
You are using and remember context from earlier chat history to execute the task.
101101
`),

memory/stream.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ import (
1717
"github.com/kshard/thinker"
1818
)
1919

20+
const INFINITE = -1
21+
2022
// The stream memory retains all of the agent's observations in the time ordered sequence.
2123
type Stream struct {
2224
mu sync.Mutex
2325
heap map[guid.K]*thinker.Observation
2426
commits []guid.K
2527
stratum chatter.Stratum
28+
cap int
2629
}
2730

2831
// Creates new stream memory that retains all of the agent's observations.
29-
func NewStream(stratum chatter.Stratum) *Stream {
32+
func NewStream(cap int, stratum chatter.Stratum) *Stream {
3033
return &Stream{
3134
heap: make(map[guid.K]*thinker.Observation),
3235
commits: make([]guid.K, 0),
3336
stratum: stratum,
37+
cap: cap,
3438
}
3539
}
3640

@@ -41,6 +45,12 @@ func (s *Stream) Commit(e *thinker.Observation) {
4145

4246
s.heap[e.Created] = e
4347
s.commits = append(s.commits, e.Created)
48+
49+
if s.cap > 0 && len(s.commits) > s.cap {
50+
head := s.commits[0]
51+
s.commits = s.commits[1:]
52+
delete(s.heap, head)
53+
}
4454
}
4555

4656
// Builds the context window for LLM using incoming prompt.

memory/stream_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func TestStream(t *testing.T) {
20-
s := NewStream("role.")
20+
s := NewStream(2, "role.")
2121

2222
t.Run("Commit", func(t *testing.T) {
2323
var prompt chatter.Prompt
@@ -45,4 +45,25 @@ func TestStream(t *testing.T) {
4545
it.Seq(seq).Equal("role.", "prompt.", "reply.", "ask."),
4646
)
4747
})
48+
49+
t.Run("Evict", func(t *testing.T) {
50+
a := thinker.NewObservation(chatter.Prompt{Task: "0."}, chatter.Reply{Text: "0."})
51+
s.Commit(a)
52+
53+
b := thinker.NewObservation(chatter.Prompt{Task: "a."}, chatter.Reply{Text: "a."})
54+
s.Commit(b)
55+
56+
c := thinker.NewObservation(chatter.Prompt{Task: "b."}, chatter.Reply{Text: "b."})
57+
s.Commit(c)
58+
59+
seq := make([]string, 0)
60+
for _, x := range s.Context(chatter.Prompt{Task: "c."}) {
61+
seq = append(seq, x.String())
62+
}
63+
64+
it.Then(t).Should(
65+
it.Seq(seq).Equal("role.", "a.", "a.", "b.", "b.", "c."),
66+
)
67+
})
68+
4869
}

0 commit comments

Comments
 (0)