Customize Your Memory Context String

In this recipe, we will demonstrate how to build a custom memory context string using the graph search API. This is necessary when using group graphs, or if you want to further customize the context string. We will create and add data to a group graph, and then search the edges and nodes to create a custom context string.

First, we create a group and add some example data:

1import os
2import uuid
3import time
4from dotenv import find_dotenv, load_dotenv
5from zep_cloud.client import Zep
6import json
7
8load_dotenv(dotenv_path=find_dotenv())
9API_KEY = os.environ.get("ZEP_API_KEY") or "YOUR_API_KEY"
10
11client = Zep(api_key=API_KEY)
12uuid_value = uuid.uuid4().hex
13group_id = "MyGroup" + uuid_value
14group = client.group.add(group_id=group_id)
15
16car_inventory_description = {
17 "entity": "Car inventory",
18 "description": "This is a list of cars in the inventory. Each car has a maker, model, and gas mileage."
19}
20car_inventory_list = [
21 {
22 "maker": "Toyota",
23 "model": "Sedan-01",
24 "gas_mileage": "25 mpg",
25 },
26 {
27 "maker": "Ford",
28 "model": "SUV-01",
29 "gas_mileage": "20 mpg",
30 },
31 {
32 "maker": "Ford",
33 "model": "SUV-02",
34 "gas_mileage": "22 mpg",
35 },
36 {
37 "maker": "Chevrolet",
38 "model": "Truck-01",
39 "gas_mileage": "25 mpg",
40 },
41]
42
43json_string = json.dumps(car_inventory_description)
44client.graph.add(
45 group_id=group_id,
46 type="json",
47 data=json_string,
48)
49for item in car_inventory_list:
50 # This helps contextualize the car items in the graph
51 item["description"] = "This is one of the cars in the car inventory"
52 json_string = json.dumps(item)
53 client.graph.add(
54 group_id=group_id,
55 type="json",
56 data=json_string,
57 )
58
59# Wait a minute or two
60time.sleep(120)

Next, we search the graph for edges and nodes relevant to our custom query. Note that the default memory context string returned by memory.get uses the past few messages as the query instead.

1query = "What Ford cars do we have in stock and what are there gas mileages?"
2node_search_results = client.graph.search(
3 group_id=group_id,
4 query=query,
5 scope="nodes",
6 limit=3,
7 reranker="cross_encoder",
8)
9edge_search_results = client.graph.search(
10 group_id=group_id,
11 query=query,
12 scope="edges",
13 limit=3,
14 reranker="cross_encoder",
15)

Lastly, we use the search results to build a custom context string:

1custom_context_string = "Below are facts and entities related to what Ford cars we have in stock:\n"
2
3custom_context_string += "<FACTS>\n"
4for edge in edge_search_results.edges:
5 custom_context_string += f"- {edge.fact}\n"
6custom_context_string += "</FACTS>\n"
7
8custom_context_string += "<ENTITIES>\n"
9for node in node_search_results.nodes:
10 custom_context_string += f"- {node.name}: {node.summary}\n"
11custom_context_string += "</ENTITIES>\n"
12
13print(custom_context_string)

This recipe demonstrated how to build a custom memory context string using the graph search API. We created a group and added some example data, and then searched the edges and nodes to create a custom context string.

Built with