Chat Sessions

Sessions represent a conversation. Sessions can be associated with Users in a 1:M relationship.

Chat messages are added to sessions in the form of Memories. Each session can have many messages associated with it.

The SessionID is a string key that accepts arbitrary identifiers. Related data you’d like to store can be persisted as metadata.

Adding a Session

SessionIDs are arbitrary identifiers that you can map to relevant business objects in your app, such as users or a conversation a user might have with your app

Sessions don’t need to be explicitly created

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.

1client = AsyncZep(
2 api_key=API_KEY,
3)
4session_id = uuid.uuid4().hex # A new session identifier
5
6await client.memory.add_session(
7 session_id=session_id,
8 user_id=user_id, # Optionally associate this session with a user
9 metadata={"foo" : "bar"}
10)
Looking to associate a Session with a User? Check out our User Management docs.

Updating Session Metadata

You can update a session’s metadata by providing a Session object with new metadata. Note that metadata is merged, so any existing metadata will be preserved.

1await client.memory.update_session(session_id, metadata={"qax" : "baz"})

Getting a Session

1session = await client.memory.get_session(session_id)
2print(session.dict())

Deleting a Session

Deleting a Session soft-deletes the Session and all associated Memories. The Session and Memories are still available in the database, but are marked as deleted and will not be returned in search results.

If you persist memory to a deleted Session, it will be undeleted. Deleted Memories will, however, remain deleted.

Soft-deleted data is hard-deleted periodically.

1await client.memory.delete(session_id)

Listing Sessions

You can list all Sessions in the Zep Memory Store with page_size and page_number parameters for pagination.

1# List the first 10 Sessions
2result = client.memory.list_sessions(page_size=10, page_number=1)
3for session in result.sessions:
4 print(session)