Customize Your Context Block
When searching the graph instead of using Zep’s Context Block, you need to use the search results to create a custom context block. In this recipe, we will demonstrate how to build a custom Context Block using the graph search API. We will also use the custom entity and edge types feature, though using this feature is optional.
Add data
First, we define our custom entity and edge types, create a user, and add some example data:
Example 1: Basic custom context block
Search
For a basic custom context block, we search the graph for edges and nodes relevant to our custom query string, which typically represents a user message. Note that the default Context Block returned by thread.get_user_context uses the past few messages as the query instead.
These searches can be performed in parallel to reduce latency, using our async Python client, TypeScript promises, or goroutines.
Build the context block
Using the search results and a few helper functions, we can build the context block. Note that for nodes, we typically want to unpack the node name and node summary, and for edges we typically want to unpack the fact and the temporal validity information:
Example 2: Utilizing custom entity and edge types
Search
For a custom context block that uses custom entity and edge types, we perform multiple searches (with our custom query string) filtering to the custom entity or edge type we want to include in the context block:
These searches can be performed in parallel to reduce latency, using our async Python client, TypeScript promises, or goroutines.
Build the context block
Using the search results and a few helper functions, we can compose the context block. Note that in this example, we focus on unpacking the custom attributes of the nodes and edges, but this is a design choice that you can experiment with for your use case.
Note also that we designed the context block template around the custom entity and edge types that we are unpacking into the context block:
Example 3: Basic custom context block with BFS
Search
For a more advanced custom context block, we can enhance the search results by using Breadth-First Search (BFS) to make them more relevant to the user’s recent history. In this example, we retrieve the past several episodes and use those episode IDs as the BFS node IDs. We use BFS here to make the search results more relevant to the user’s recent history. You can read more about how BFS works in the Breadth-First Search section of our searching the graph documentation.
These searches can be performed in parallel to reduce latency, using our async Python client, TypeScript promises, or goroutines.
Build the context block
Using the search results and a few helper functions, we can build the context block. Note that for nodes, we typically want to unpack the node name and node summary, and for edges we typically want to unpack the fact and the temporal validity information:
Example 4: Using user summary in context block
Get user node
You can retrieve the user node and use its summary to create a simple, personalized context block. This approach is particularly useful when you want to include high-level user information generated from user summary instructions.
About the user node
Each user has a single unique user node in their graph representing the user themselves. The user summary generated from user summary instructions lives on this user node. When you call client.user.get_node(), you are retrieving this special node that contains the user’s summary.
Build the context block
Using the user summary, you can create a simple context block that provides personalized user information: