Customizing Graph Structure

Zep enables the use of rich, domain-specific data structures in graphs through Entity Types and Edge Types, replacing generic graph nodes and edges with detailed models.

Zep classifies newly created nodes/edges as one of the default or custom types or leaves them unclassified. For example, a node representing a preference is classified as a Preference node, and attributes specific to that type are automatically populated. You may restrict graph queries to nodes/edges of a specific type, such as Preference.

The default entity types are applied to all graphs by default, but you may define additional custom types as needed.

Each node/edge is classified as a single type only. Multiple classifications are not supported.

Default Entity Types

Definition

The default entity types are:

  • User: A human that is part of the current chat thread.
  • Preference: One of the User’s preferences.
  • Procedure: A multi-step instruction informing the agent how to behave (e.g. ‘When the user asks for code, respond only with code snippets followed by a bullet point explanation’)

Default entity types only apply to user graphs (not group graphs). All nodes in any user graph will be classified into one of these types or none.

Adding Data

When we add data to the graph, default entity types are automatically created:

1from zep_cloud.types import Message
2
3message = {"role": "John Doe", "role_type": "user", "content": "I really like pop music, and I don't like metal"}
4
5client.memory.add(session_id=session_id, messages=[Message(**message)])

Searching

When searching nodes in the graph, you may provide a list of types to filter the search by. The provided types are ORed together. Search results will only include nodes that satisfy one of the provided types:

1search_results = client.graph.search(
2 user_id=user_id,
3 query="the user's music preferences",
4 scope="nodes",
5 search_filters={
6 "node_labels": ["Preference"]
7 }
8)
9for i, node in enumerate(search_results.nodes):
10 preference = node.attributes
11 print(f"Preference {i+1}:{preference}")
Preference 1: {'category': 'Music', 'description': 'Pop Music is a genre of music characterized by its catchy melodies and widespread appeal.', 'labels': ['Entity', 'Preference']}
Preference 2: {'category': 'Music', 'description': 'Metal Music is a genre of music characterized by its heavy sound and complex compositions.', 'labels': ['Entity', 'Preference']}

Custom Entity and Edge Types

Definition

In addition to the default entity types, you may specify your own custom entity and custom edge types. You need to provide a description of the type and a description for each of the fields. The syntax for this is different for each language.

You may not create more than 10 custom entity types and 10 custom edge types per project. The limit of 10 custom entity types does not include the default types. Each model may have up to 10 fields.

When creating custom entity or edge types, you may not use the following attribute names (including in Go struct tags), as they conflict with default node attributes: uuid, name, group_id, name_embedding, summary, and created_at.

1from zep_cloud.external_clients.ontology import EntityModel, EntityText, EdgeModel, EntityBoolean
2from pydantic import Field
3
4class Restaurant(EntityModel):
5 """
6 Represents a specific restaurant.
7 """
8 cuisine_type: EntityText = Field(description="The cuisine type of the restaurant, for example: American, Mexican, Indian, etc.", default=None)
9 dietary_accommodation: EntityText = Field(description="The dietary accommodation of the restaurant, if any, for example: vegetarian, vegan, etc.", default=None)
10
11class Audiobook(EntityModel):
12 """
13 Represents an audiobook entity.
14 """
15 genre: EntityText = Field(description="The genre of the audiobook, for example: self-help, fiction, nonfiction, etc.", default=None)
16
17class RestaurantVisit(EdgeModel):
18 """
19 Represents the fact that the user visited a restaurant.
20 """
21 restaurant_name: EntityText = Field(description="The name of the restaurant the user visited", default=None)
22
23class AudiobookListen(EdgeModel):
24 """
25 Represents the fact that the user listened to or played an audiobook.
26 """
27 audiobook_title: EntityText = Field(description="The title of the audiobook the user listened to or played", default=None)
28
29class DietaryPreference(EdgeModel):
30 """
31 Represents the fact that the user has a dietary preference or dietary restriction.
32 """
33 preference_type: EntityText = Field(description="Preference type of the user: anything, vegetarian, vegan, peanut allergy, etc.", default=None)
34 allergy: EntityBoolean = Field(description="Whether this dietary preference represents a user allergy: True or false", default=None)

Setting Entity and Edge Types

You can then set these custom entity and edge types as the graph ontology for your current Zep project. Note that for custom edge types, you can require the source and destination nodes to be a certain type, or allow them to be any type:

1from zep_cloud import EntityEdgeSourceTarget
2
3client.graph.set_ontology(
4 entities={
5 "Restaurant": Restaurant,
6 "Audiobook": Audiobook,
7 },
8 edges={
9 "RESTAURANT_VISIT": (
10 RestaurantVisit,
11 [EntityEdgeSourceTarget(source="User", target="Restaurant")]
12 ),
13 "AUDIOBOOK_LISTEN": (
14 AudiobookListen,
15 [EntityEdgeSourceTarget(source="User", target="Audiobook")]
16 ),
17 "DIETARY_PREFERENCE": (
18 DietaryPreference,
19 [EntityEdgeSourceTarget(source="User")]
20 ),
21 }
22)

Adding Data

Now, when you add data to the graph, new nodes and edges are classified into exactly one of the overall set of entity or edge types respectively, or no type:

1from zep_cloud import Message
2import uuid
3
4messages_session1 = [
5 Message(content="Take me to a lunch place", role_type="user", role="John Doe"),
6 Message(content="How about Panera Bread, Chipotle, or Green Leaf Cafe, which are nearby?", role_type="assistant", role="Assistant"),
7 Message(content="Do any of those have vegetarian options? I’m vegetarian", role_type="user", role="John Doe"),
8 Message(content="Yes, Green Leaf Cafe has vegetarian options", role_type="assistant", role="Assistant"),
9 Message(content="Let’s go to Green Leaf Cafe", role_type="user", role="John Doe"),
10 Message(content="Navigating to Green Leaf Cafe", role_type="assistant", role="Assistant"),
11]
12
13messages_session2 = [
14 Message(content="Play the 7 habits of highly effective people", role_type="user", role="John Doe"),
15 Message(content="Playing the 7 habits of highly effective people", role_type="assistant", role="Assistant"),
16]
17
18user_id = f"user-{uuid.uuid4()}"
19client.user.add(user_id=user_id, first_name="John", last_name="Doe", email="[email protected]")
20
21session1_id = f"session-{uuid.uuid4()}"
22session2_id = f"session-{uuid.uuid4()}"
23client.memory.add_session(session_id=session1_id, user_id=user_id)
24client.memory.add_session(session_id=session2_id, user_id=user_id)
25
26client.memory.add(session_id=session1_id, messages=messages_session1, ignore_roles=["assistant"])
27client.memory.add(session_id=session2_id, messages=messages_session2, ignore_roles=["assistant"])

Searching/Retrieving

Now that a graph with custom entity and edge types has been created, you may filter node search results by entity type, or edge search results by edge type.

Below, you can see the examples that were created from our data of each of the entity and edge types that we defined:

1search_results_restaurants = client.graph.search(
2 user_id=user_id,
3 query="Take me to a restaurant",
4 scope="nodes",
5 search_filters={
6 "node_labels": ["Restaurant"]
7 },
8 limit=1,
9)
10node = search_results_restaurants.nodes[0]
11print(f"Node name: {node.name}")
12print(f"Node labels: {node.labels}")
13print(f"Cuisine type: {node.attributes.get('cuisine_type')}")
14print(f"Dietary accommodation: {node.attributes.get('dietary_accommodation')}")
Node name: Green Leaf Cafe
Node labels: Entity,Restaurant
Cuisine type: undefined
Dietary accommodation: vegetarian
1search_results_audiobook_nodes = client.graph.search(
2 user_id=user_id,
3 query="Play an audiobook",
4 scope="nodes",
5 search_filters={
6 "node_labels": ["Audiobook"]
7 },
8 limit=1,
9)
10node = search_results_audiobook_nodes.nodes[0]
11print(f"Node name: {node.name}")
12print(f"Node labels: {node.labels}")
13print(f"Genre: {node.attributes.get('genre')}")
Node name: 7 habits of highly effective people
Node labels: Entity,Audiobook
Genre: undefined
1search_results_visits = client.graph.search(
2 user_id=user_id,
3 query="Take me to a restaurant",
4 scope="edges",
5 search_filters={
6 "edge_types": ["RESTAURANT_VISIT"]
7 },
8 limit=1,
9)
10edge = search_results_visits.edges[0]
11print(f"Edge fact: {edge.fact}")
12print(f"Edge type: {edge.name}")
13print(f"Restaurant name: {edge.attributes.get('restaurant_name')}")
Edge fact: User John Doe is going to Green Leaf Cafe
Edge type: RESTAURANT_VISIT
Restaurant name: Green Leaf Cafe
1search_results_audiobook_listens = client.graph.search(
2 user_id=user_id,
3 query="Play an audiobook",
4 scope="edges",
5 search_filters={
6 "edge_types": ["AUDIOBOOK_LISTEN"]
7 },
8 limit=1,
9)
10edge = search_results_audiobook_listens.edges[0]
11print(f"Edge fact: {edge.fact}")
12print(f"Edge type: {edge.name}")
13print(f"Audiobook title: {edge.attributes.get('audiobook_title')}")
Edge fact: John Doe requested to play the audiobook '7 habits of highly effective people'
Edge type: AUDIOBOOK_LISTEN
Audiobook title: 7 habits of highly effective people
1search_results_dietary_preference = client.graph.search(
2 user_id=user_id,
3 query="Find some food around here",
4 scope="edges",
5 search_filters={
6 "edge_types": ["DIETARY_PREFERENCE"]
7 },
8 limit=1,
9)
10edge = search_results_dietary_preference.edges[0]
11print(f"Edge fact: {edge.fact}")
12print(f"Edge type: {edge.name}")
13print(f"Preference type: {edge.attributes.get('preference_type')}")
14print(f"Allergy: {edge.attributes.get('allergy')}")
Edge fact: User states 'I'm vegetarian' indicating a dietary preference.
Edge type: DIETARY_PREFERENCE
Preference type: vegetarian
Allergy: false

Additionally, you can provide multiple types in search filters, and the types will be ORed together:

1search_results_dietary_preference = client.graph.search(
2 user_id=user_id,
3 query="Find some food around here",
4 scope="edges",
5 search_filters={
6 "edge_types": ["DIETARY_PREFERENCE", "RESTAURANT_VISIT"]
7 },
8 limit=2,
9)
10for edge in search_results_dietary_preference.edges:
11 print(f"Edge fact: {edge.fact}")
12 print(f"Edge type: {edge.name}")
13 if edge.name == "DIETARY_PREFERENCE":
14 print(f"Preference type: {edge.attributes.get('preference_type')}")
15 print(f"Allergy: {edge.attributes.get('allergy')}")
16 elif edge.name == "RESTAURANT_VISIT":
17 print(f"Restaurant name: {edge.attributes.get('restaurant_name')}")
18 print("\n")
Edge fact: User John Doe is going to Green Leaf Cafe
Edge type: RESTAURANT_VISIT
Restaurant name: Green Leaf Cafe
Edge fact: User states 'I'm vegetarian' indicating a dietary preference.
Edge type: DIETARY_PREFERENCE
Preference type: vegetarian
Allergy: false

Important Notes/Tips

Some notes regarding custom entity and edge types:

  • The set_ontology method overwrites any previously defined custom entity and edge types, so the set of custom entity and edge types is always the list of types provided in the last set_ontology method call
  • The overall set of entity types for a project includes both the custom entity types you set and the default entity types
  • There are no default edge types
  • You can overwrite the default entity types by providing custom entity types with the same names
  • Changing the custom entity or edge types will not update previously created nodes or edges. The classification and attributes of existing nodes and edges will stay the same. The only thing that can change existing classifications or attributes is adding data that provides new information.
  • When creating custom entity or edge types, avoid using the following attribute names (including in Go struct tags), as they conflict with default attributes: uuid, name, group_id, name_embedding, summary, and created_at
  • Tip: Design custom entity types to represent entities/nouns, and design custom edge types to represent relationships/verbs. Otherwise, your type might be represented in the graph as an edge more often than as a node or vice versa.
  • Tip: If you have overlapping entity or edge types (e.g. ‘Hobby’ and ‘Hiking’), you can prioritize one type over another by mentioning which to prioritize in the entity or edge type descriptions