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.
Additionally, note that below 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 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:
As we do in Example 1, we pass episode IDs as BFS node IDs:
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: