Documentation
¶
Overview ¶
Package memory provides ShortTermMemory and LongTermMemory implementations for the goagent framework.
The design separates two orthogonal concerns:
- Storage (memory/storage): where messages live — load and save the full slice.
- Policy (memory/policy): which messages the model sees — filter at read time.
Short-term memory ¶
ShortTermMemory maintains the active conversation context across Run calls. Construct one with NewShortTerm, combining any Storage with any Policy:
mem := memory.NewShortTerm(
memory.WithStorage(storage.NewInMemory()),
memory.WithPolicy(policy.NewTokenWindow(4096)),
)
agent := goagent.New(goagent.WithShortTermMemory(mem))
Long-term memory ¶
LongTermMemory retrieves semantically relevant messages across sessions using vector similarity search.
This package does not ship a VectorStore or Embedder implementation. Both are interfaces defined in the root goagent package; the caller must supply concrete implementations (e.g. a pgvector client, a Chroma adapter, or any embedding API). NewLongTerm returns an error if either is missing.
mem, err := memory.NewLongTerm(
memory.WithVectorStore(myStore), // caller-provided
memory.WithEmbedder(myEmbedder), // caller-provided
)
Example ¶
Example demonstrates appending messages to a ShortTermMemory and reading them back via Messages.
package main
import (
"context"
"fmt"
"log"
"github.com/Germanblandin1/goagent"
"github.com/Germanblandin1/goagent/memory"
)
func main() {
mem := memory.NewShortTerm()
ctx := context.Background()
if err := mem.Append(ctx,
goagent.UserMessage("hello"),
goagent.AssistantMessage("hi"),
); err != nil {
log.Fatal(err)
}
msgs, err := mem.Messages(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(len(msgs))
fmt.Println(msgs[0].TextContent())
}
Output: 2 hello
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingVectorStore is returned by NewLongTerm when no VectorStore // is provided via WithVectorStore. ErrMissingVectorStore = errors.New("memory: NewLongTerm requires WithVectorStore") // ErrMissingEmbedder is returned by NewLongTerm when no Embedder is // provided via WithEmbedder. ErrMissingEmbedder = errors.New("memory: NewLongTerm requires WithEmbedder") )
Functions ¶
func NewLongTerm ¶
func NewLongTerm(opts ...LongTermOption) (goagent.LongTermMemory, error)
NewLongTerm creates a LongTermMemory with the given options. Both WithVectorStore and WithEmbedder are required.
Possible errors:
- ErrMissingVectorStore — no VectorStore was provided
- ErrMissingEmbedder — no Embedder was provided
Example ¶
ExampleNewLongTerm shows how to construct a LongTermMemory. No Output: is provided because both WithVectorStore and WithEmbedder require real implementations (a vector database and an embedding model).
package main
import (
"context"
"log"
"github.com/Germanblandin1/goagent"
"github.com/Germanblandin1/goagent/memory"
)
func main() {
_, err := memory.NewLongTerm(
memory.WithVectorStore(stubVectorStore{}),
memory.WithEmbedder(stubEmbedder{}),
memory.WithTopK(5),
)
if err != nil {
log.Fatal(err)
}
}
type stubVectorStore struct{}
func (stubVectorStore) Upsert(_ context.Context, _ string, _ []float32, _ goagent.Message) error {
return nil
}
func (stubVectorStore) Search(_ context.Context, _ []float32, _ int, _ ...goagent.SearchOption) ([]goagent.ScoredMessage, error) {
return nil, nil
}
func (stubVectorStore) Delete(_ context.Context, _ string) error { return nil }
type stubEmbedder struct{}
func (stubEmbedder) Embed(_ context.Context, _ []goagent.ContentBlock) ([]float32, error) {
return []float32{0.1, 0.2, 0.3}, nil
}
Output:
func NewShortTerm ¶
func NewShortTerm(opts ...ShortTermOption) goagent.ShortTermMemory
NewShortTerm creates a ShortTermMemory with the given options. Defaults: in-process storage + NoOp policy (full history sent to provider).
Example (WithPolicy) ¶
ExampleNewShortTerm_withPolicy shows how a FixedWindow policy limits the number of messages returned by Messages, even when more are stored.
package main
import (
"context"
"fmt"
"log"
"github.com/Germanblandin1/goagent"
"github.com/Germanblandin1/goagent/memory"
"github.com/Germanblandin1/goagent/memory/policy"
"github.com/Germanblandin1/goagent/memory/storage"
)
func main() {
mem := memory.NewShortTerm(
memory.WithStorage(storage.NewInMemory()),
memory.WithPolicy(policy.NewFixedWindow(2)),
)
ctx := context.Background()
if err := mem.Append(ctx,
goagent.UserMessage("one"),
goagent.AssistantMessage("two"),
goagent.UserMessage("three"),
goagent.AssistantMessage("four"),
goagent.UserMessage("five"),
); err != nil {
log.Fatal(err)
}
visible, err := mem.Messages(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(len(visible))
fmt.Println(visible[0].TextContent())
}
Output: 2 four
Types ¶
type LongTermOption ¶
type LongTermOption func(*longTermConfig)
LongTermOption configures a LongTermMemory created by NewLongTerm.
func WithChunker ¶
func WithChunker(c vector.Chunker) LongTermOption
WithChunker sets the chunking strategy used by Store before embedding. Default: NoOpChunker — one message produces exactly one chunk. For long documents or text that may exceed the embedding model's context window, use TextChunker or BlockChunker.
func WithEmbedder ¶
func WithEmbedder(e goagent.Embedder) LongTermOption
WithEmbedder sets the embedding model used to vectorize messages. Required.
func WithTopK ¶
func WithTopK(k int) LongTermOption
WithTopK sets the default number of messages returned by Retrieve. Default: 3. Overridden by the topK argument passed to Retrieve directly.
func WithVectorStore ¶
func WithVectorStore(s goagent.VectorStore) LongTermOption
WithVectorStore sets the vector store backend. Required.
func WithWritePolicy ¶
func WithWritePolicy(p goagent.WritePolicy) LongTermOption
WithWritePolicy sets the function that decides whether a turn should be stored. Default: StoreAlways.
type ShortTermOption ¶
type ShortTermOption func(*shortTermConfig)
ShortTermOption configures a ShortTermMemory created by NewShortTerm.
func WithPolicy ¶
func WithPolicy(p policy.Policy) ShortTermOption
WithPolicy sets the read policy for a ShortTermMemory. Default: NoOp (all messages are passed to the provider unchanged).
func WithStorage ¶
func WithStorage(s storage.Storage) ShortTermOption
WithStorage sets the storage backend for a ShortTermMemory. Default: in-process slice (storage.NewInMemory).
Directories
¶
| Path | Synopsis |
|---|---|
|
Package policy provides Policy implementations for the goagent memory system.
|
Package policy provides Policy implementations for the goagent memory system. |
|
Package storage provides Storage implementations for the goagent memory system.
|
Package storage provides Storage implementations for the goagent memory system. |
|
Package vector provides embeddings, chunking, similarity search, and an in-process vector store for goagent's long-term memory subsystem.
|
Package vector provides embeddings, chunking, similarity search, and an in-process vector store for goagent's long-term memory subsystem. |
|
pgvector
module
|
|
|
sqlitevec
module
|