Agent memory
What it shows
The example stores the long-term memory of an AI agent as a graph: a conversation with its messages, the entities the messages mention and the facts linking the entities. It then recalls the facts reachable from an entity as context for the next prompt, finds the messages mentioning an entity and forgets the conversation while keeping what was learned from it. See agdb in the AI era for the background.
Run it
cargo run -p agdb --example agent_memory
It prints the recalled facts, the messages mentioning agdb and the number of nodes left after forgetting the conversation:
agdb_server is built on agdb
agdb is written in Rust
[Message { db_id: Some(DbId(5)), role: "assistant", text: "Noted, and agdb is written in Rust." }, Message { db_id: Some(DbId(4)), role: "user", text: "agdb_server is built on agdb." }]
Nodes after forgetting: 5
The model
Messages and entities are typed structs. The text of a message is only ever a value - it is never parsed as a query so it cannot inject anything into it:
#[derive(Debug, DbType)]
struct Message {
db_id: Option<DbId>,
role: String,
text: String,
}
#[derive(Debug, DbType)]
struct Entity {
db_id: Option<DbId>,
kind: String,
name: String,
}
The entities are indexed by name so that a name appearing in a new message can be looked up directly. The messages point to the entities they mention and the facts are edges between the entities:
// Which message mentions which entity.
t.exec_mut(
QueryBuilder::insert()
.edges()
.from([message1, message1, message2, message2])
.to([server, database, database, rust])
.query(),
)?;
// The facts are edges between the entities.
t.exec_mut(
QueryBuilder::insert()
.edges()
.from([server, database])
.to([database, rust])
.values([
[("relation", "is built on").into()],
[("relation", "is written in").into()],
])
.query(),
)?;
Recall
Starting at the entity, the search follows only the outgoing edges, so it traverses the facts and not the messages (they point to the entities, not the other way round). The distance condition selects the edges up to two hops away and also stops the search there:
let facts = t.exec(
QueryBuilder::select()
.values("relation")
.search()
.from(*start)
.where_()
.edge()
.and()
.distance_less_than(4)
.query(),
)?;
Each returned edge has its from and to ids, so the names of both entities are selected to turn the fact into a line of text for the prompt.
This recalls only the chain of facts starting at the entity, along the outgoing edges (recall("Rust") finds nothing). The facts pointing to the entity need a second search with .to(entity) filtered by .keys("relation") and bounded by a distance condition, as the incoming edges include the ones from the messages.
Provenance
The messages mentioning an entity are among its direct neighbours along the incoming edges (to). The neighbor() condition (distance 2) stops the search at them - without it the search would continue past them, e.g. to the messages mentioning agdb_server, which has a fact about agdb. The neighbours are the messages, the entities root and the entities with facts about this one; the role key keeps only the messages:
let sources: Vec<Message> = db
.exec(
QueryBuilder::select()
.elements::<Message>()
.search()
.to(database)
.where_()
.neighbor()
.and()
.keys("role")
.query(),
)?
.try_into()?;
This is provenance per entity. To trace each fact to the message it was learned from, store the id of that message as a value on the fact edge (e.g. ("source", message_id)).
Forgetting
Removing the conversation node and its messages (the nodes at distance 0 and 2) also removes their edges. The entities and the facts between them stay:
let conversation = t.exec(
QueryBuilder::search()
.from(alias)
.where_()
.node()
.and()
.distance_less_than(3)
.query(),
)?;
t.exec_mut(QueryBuilder::remove().ids(conversation).query())?;
Learn more
- Search and its conditions in the queries reference.
- Transactions - the memory is written and recalled in transactions.
- Indexes - looking up elements by an indexed key.