Working with Memory

Search Over Memory Artifacts

Find relevant Facts, Summaries, or individual Messages across a Session, User, or more.

Searching for Facts, Summaries, or Messages

Zep offers similarity search over Facts, Summaries, and Messages stored within a Session, select Sessions, across all Sessions of a User, or across your entire Zep Project. This enables you to populate prompts with past conversations and conversation artifacts that are contextually similar to a specific query, organizing the results by a similarity Score.

Choosing Between Facts, Summaries, and Messages

Zep supports searching over Facts, Summaries, and Messages. Since individual messages might have little contextual information (consider a message containing just “yes”), Facts and Summaries typically yield higher quality search results.

MMR Reranking for Summaries

Summaries can sometimes overlap in information, especially when the Message Window is set low. In such cases, employing Maximum Marginal Relevance (MMR) to rerank search results can be beneficial. Zep includes built-in, hardware-accelerated support for MMR, making it simple and easy to use.

Constructing Search Queries

Zep’s Collection and Memory search support semantic search queries, JSONPath-based metadata filters, and a combination of both.

Memory search also supports querying by message creation date.

Read more about constructing search queries.

1from zep_cloud.client import AsyncZep
3client = AsyncZep(
4 api_key=API_KEY,
7# This uniquely identifies the user's session
8session_id = "my_session_id"
10data = await client.memory.search_sessions(
11 session_ids=[session_id],
12 text="Is Lauren Olamina a character in a book?",
13 search_scope="facts", # This could be facts, summaries, or messages
14 search_type="mmr", # remove this if you'd prefer not to rerank results
15 mmr_lambda=0.5, # tune diversity vs relevance
18for search_result in data.results:
19 # Uncomment for message search
20 # print(search_result.messsage.dict())
21 print(search_result.summary.dict())
2 "summary": {
3 "uuid": "b47b83da-16ae-49c8-bacb-f7d049f9df99",
4 "created_at": "2023-11-02T18:22:10.103867Z",
5 "content": "The human asks the AI to explain the book Parable of the Sower by Octavia Butler. The AI responds by explaining that Parable of the Sower is a science fiction novel by Octavia Butler. The book 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": 66
7 },
8 "metadata": null,
9 "dist": 0.8440576791763306

Hybrid Search for Chat History with Metadata Filters

Besides the vector similarity search for Facts, Summaries, and Messages stored in Zep, you can also use metadata filters for your searches. You also have the option to conduct searches based purely on metadata.

2 session_ids=[session_id],
3 text="I enjoy reading science fiction.",
4 record_filter={
5 "where": {"jsonpath": '$[*] ? ( == "bar")'},
6 },
2 "dist": 0.7170433826192629,
3 "message": {
4 "content": "I've read many books written by Octavia Butler.",
5 "created_at": "2023-06-03T22:00:43.034056Z",
6 "metadata": {
7 "foo": "bar",
8 "system": {
9 "entities": [
10 {
11 "Label": "PERSON",
12 "Matches": [
13 {
14 "End": 46,
15 "Start": 32,
16 "Text": "Octavia Butler"
17 }
18 ],
19 "Name": "Octavia Butler"
20 }
21 ]
22 }
23 },
24 "role": "human",
25 "token_count": 13,
26 "uuid": "8f3a06dd-0625-41da-a2af-b549f2056b3f"
27 },
28 "metadata": null,
29 "summary": null