Customize Your Memory Context String
When using graph.search
instead of memory.get
, you need to use the search results to create a custom context string. In this recipe, we will demonstrate how to build a custom memory context string 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 string
Search
For a basic custom context string, we search the graph for edges and nodes relevant to our custom query string, which typically represents a user message. Note that the default memory context string returned by memory.get
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. These are used to seed separate, shallow breadth first searches starting from those episodes, which helps make the search results more relevant to the user’s recent history. We can pass episode IDs instead of node IDs because episodes are represented as nodes under the hood. You can provide BFS nodes both when scope=nodes
or scope=edges
.
These searches can be performed in parallel to reduce latency, using our async Python client, TypeScript promises, or goroutines.
Build the context string
Using the search results and a few helper functions, we can build the context string. 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 string 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 string:
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 string
Using the search results and a few helper functions, we can compose the context string. 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 string template around the custom entity and edge types that we are unpacking into the context string: