Central data structure in Zep's Memory Store

You are viewing the Zep Open Source v0.x documentation. This version is no longer supported, and documentation is provided as a reference only.

The current documentation for Zep Community Edition is available here.

A Memory is the central data structure in Zep’s Memory Store. It contains a list of Messages and a Summary (if created). The Memory and Summary are returned with UUIDs, entities extracted from the conversation, token counts, timestamps, and other metadata.

Memories are associated with a Session in a many-to-one relationship.

Persisting a Memory to a Session

A Memory may include a single message or a series of messages. Each Message has a role and content field, with role being the identifiers for your human and AI/agent users and content being the text of the message.

Additionally, you can even store custom metadata with each Message.

Note: Sessions are created automatically when adding Memories. If the SessionID already exists, then the Memory is upserted into the Session.

Manually creating a session can be useful if you want to associate it with a user or add metadata

Python

1session_id = uuid.uuid4().hex # A new session identifier
2
3history = [
4 { "role": "human", "content": "Who was Octavia Butler?" },
5 {
6 "role": "ai",
7 "content":
8 "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" +
9 " science fiction author.",
10 },
11 {
12 "role": "human",
13 "content": "Which books of hers were made into movies?",
14 "metadata":{"foo": "bar"},
15 }
16]
17
18messages = [Message(role=m["role"], content=m["content"]) for m in history]
19memory = Memory(messages=messages)
20result = await client.memory.aadd_memory(session_id, memory)

TypeScript

1const sessionID = randomUUID();
2
3const history = [
4 { role: "human", content: "Who was Octavia Butler?" },
5 {
6 role: "ai",
7 content:
8 "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" +
9 " science fiction author.",
10 },
11 {
12 role: "human",
13 content: "Which books of hers were made into movies?",
14 metadata: { foo: "bar" },
15 },
16];
17
18const messages = history.map(
19 ({ role, content }) => new Message({ role, content })
20);
21const memory = new Memory({ messages });
22
23await zepClient.memory.addMemory(sessionID, memory);

Getting a Session’s Memory

Python

1async with ZepClient(base_url, api_key) as client:
2 try:
3 memory = await client.memory.aget_memory(session_id)
4 for message in memory.messages:
5 print(message.to_dict())
6 except NotFoundError:
7 print("Memory not found")
1{
2 "uuid": "7291333f-2e01-4b06-9fe0-3efc59b3399c",
3 "created_at": "2023-05-16T21:59:11.057919Z",
4 "role": "ai",
5 "content": "Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.",
6 "token_count": 56
7}
8{
9 "uuid": "61f862c5-945b-49b1-b87c-f9338518b7cb",
10 "created_at": "2023-05-16T21:59:11.057919Z",
11 "role": "human",
12 "content": "Write a short synopsis of Butler's book, Parable of the Sower. What is it about?",
13 "token_count": 23
14}

TypeScript

1const memory = await zepClient.memory.getMemory(sessionID);
2
3if (memory.messages.length === 0) {
4 console.debug("No messages found for session ", sessionID);
5} else {
6 memory.messages.forEach((message) => {
7 console.debug(JSON.stringify(message));
8 });
9}
1{
2 "uuid": "7291333f-2e01-4b06-9fe0-3efc59b3399c",
3 "created_at": "2023-05-16T21:59:11.057919Z",
4 "role": "ai",
5 "content": "Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.",
6 "token_count": 56
7}
8{
9 "uuid": "61f862c5-945b-49b1-b87c-f9338518b7cb",
10 "created_at": "2023-05-16T21:59:11.057919Z",
11 "role": "human",
12 "content": "Write a short synopsis of Butler's book, Parable of the Sower. What is it about?",
13 "token_count": 23
14}