Memories are the central data structure in Zep's Memory Store. They contain a list of Messages and Relevant Facts.

Persisting a Memory to a Session

A Memory may include a single message or a series of messages. Each Message has a role, role_type 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 store custom metadata with each Message.

Sessions don’t need to be explicitly created

Sessions are created automatically when adding Memories. If the SessionID is 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.

1from zep_cloud.client import AsyncZep
2from zep_cloud.types import Message
3
4import uuid
5
6API_KEY = "<your_api_key>"
7client = AsyncZep(
8 api_key=API_KEY,
9)
10session_id = uuid.uuid4().hex # A new session identifier
11
12history = [
13 {"role": "Jane", "role_type": "user", "content": "Who was Octavia Butler?"},
14 {"role": "HistoryBot", "role_type": "assistant", "content": "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American science fiction author."},
15 {"role": "Jane", "role_type": "user", "content": "Which books of hers were made into movies?", "metadata": {"foo": "bar"}},
16 {"role": "HistoryBot", "role_type": "assistant", "content": "As of now, none of Octavia Butler's books have been made into full-length feature films. However, there have been some adaptations and projects in development. For example, Ava DuVernay is working on adapting Butler's 'Dawn' into a television series for Amazon. Additionally, FX is developing a series based on her novel 'Kindred'."},
17 {"role": "Jane", "role_type": "user", "content": "That's interesting! Can you tell me more about 'Kindred'?"},
18]
19
20messages = [
21 Message(
22 role=m["role"],
23 role_type=m["role_type"],
24 content=m["content"],
25 metadata=m.get("metadata"),
26 ) for m in history
27]
28result = await client.memory.add(session_id, messages=messages)

Retrieving a Session’s Memory

The example below uses Zep’s async API and a context manager.

1client = AsyncZep(
2 api_key=API_KEY,
3)
4memory = await client.memory.get(session_id)
5
6for message in memory.messages:
7 print(message)
Sample Output:
1[
2 {
3 "uuid": "7291333f-2e01-4b06-9fe0-3efc59b3399c",
4 "created_at": "2023-05-16T21:59:11.057919Z",
5 "role": "HistoryBot",
6 "role_type": "assistant",
7 "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.",
8 "token_count": 56
9 },
10 {
11 "uuid": "61f862c5-945b-49b1-b87c-f9338518b7cb",
12 "created_at": "2023-05-16T21:59:11.057919Z",
13 "role": "Jane",
14 "role_type": "user",
15 "content": "Write a short synopsis of Butler's book, Parable of the Sower. What is it about?",
16 "token_count": 23
17 }
18]