Skip to content

Commit 3eb054a

Browse files
chore: Update examples
1 parent 9b94884 commit 3eb054a

3 files changed

Lines changed: 70 additions & 110 deletions

File tree

examples/chat_history/memory.py

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
44
This script demonstrates the following functionality:
55
- Creating a user.
6-
- Creating a session associated with the created user.
7-
- Adding messages to the session.
8-
- Searching the session memory for a specific query.
9-
- Searching the session memory with MMR reranking.
10-
- Searching the session memory with a metadata filter.
11-
- optionally deleting the session.
6+
- Creating a thread associated with the created user.
7+
- Adding messages to the thread.
8+
- Searching the thread memory for a specific query.
9+
- Searching the thread memory with MMR reranking.
10+
- Searching the thread memory with a metadata filter.
11+
- optionally deleting the thread.
1212
"""
1313

1414
import asyncio
@@ -53,71 +53,39 @@ async def main() -> None:
5353
metadata={"vip": "true"},
5454
)
5555

56-
# await asyncio.sleep(1)
5756
print(f"User added: {user_id}")
58-
session_id = uuid.uuid4().hex # unique session id. can be any alphanum string
57+
thread_id = uuid.uuid4().hex # unique thread id. can be any alphanum string
5958

60-
# Create session associated with the above user
61-
print(f"\n---Creating session: {session_id}")
59+
# Create thread associated with the above user
60+
print(f"\n---Creating thread: {thread_id}")
6261

63-
await client.memory.add_session(
64-
session_id=session_id,
62+
await client.thread.create(
63+
thread_id=thread_id,
6564
user_id=user_id,
6665
metadata={"foo": "bar"},
6766
)
68-
# await asyncio.sleep(1)
69-
# Update session metadata
70-
print(f"\n---Updating session: {session_id}")
71-
await client.memory.update_session(session_id=session_id, metadata={"bar": "foo"})
72-
# await asyncio.sleep(3)
73-
# Get session
74-
print(f"\n---Getting session: {session_id}")
75-
session = await client.memory.get_session(session_id)
76-
print(f"Session details: {session}")
77-
# await asyncio.sleep(3)
78-
79-
# Add Memory for session
80-
print(f"\n---Add Memory for Session: {session_id}")
67+
68+
print(f"\n---Getting thread: {thread_id}")
69+
thread = await client.thread.get(thread_id)
70+
print(f"thread details: {thread}")
71+
72+
print(f"\n---Add messages to the thread: {thread_id}")
8173
for m in history:
8274
print(f"{m['role']}: {m['content']}")
83-
await client.memory.add(session_id=session_id, messages=[Message(**m)])
75+
await client.thread.add_messages(thread_id=thread_id, messages=[Message(**m)])
8476
# await asyncio.sleep(0.5)
8577

8678
# Wait for the messages to be processed
8779
await asyncio.sleep(50)
8880

89-
# Synthesize a question from most recent messages.
90-
# Useful for RAG apps. This is faster than using an LLM chain.
91-
print("\n---Synthesize a question from most recent messages")
92-
question = await client.memory.synthesize_question(session_id, last_n_messages=3)
93-
print(f"Question: {question}")
94-
95-
# Classify the session.
96-
# Useful for semantic routing, filtering, and many other use cases.
97-
print("\n---Classify the session")
98-
classes = [
99-
"low spender <$50",
100-
"medium spender >=$50, <$100",
101-
"high spender >=$100",
102-
"unknown",
103-
]
104-
classification = await client.memory.classify_session(
105-
session_id, name="spender_category", classes=classes, persist=True
106-
)
107-
print(f"Classification: {classification}")
108-
109-
# Get Memory for session
110-
print(f"\n---Get Perpetual Memory for Session: {session_id}")
111-
memory = await client.memory.get(session_id)
112-
print(f"Memory: {memory}")
113-
print("\n---End of Memory")
11481

115-
print(f"Memory context: {memory.context}")
82+
print(f"\n---Get user context for thread: {thread_id}")
83+
memory = await client.thread.get_user_context(thread_id)
84+
print(f"Context: {memory.context}")
11685

117-
# Delete Memory for session
86+
# Delete thread and wipe thread memory
11887
# Uncomment to run
119-
# print(f"\n6---deleteMemory for Session: {session_id}")
120-
# await client.memory.delete(session_id)
88+
# await client.thread.delete(thread_id)
12189

12290

12391
if __name__ == "__main__":

examples/graph_example/group_graph_example.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
Example of using the Zep Python SDK asynchronously with Graph functionality.
33
44
This script demonstrates the following functionality:
5-
- Creating a group.
6-
- Updating a group.
7-
- Adding episodes to the group (text and JSON).
8-
- Retrieving nodes from the group.
9-
- Retrieving edges from the group.
10-
- Searching the group for specific content.
5+
- Creating a graph.
6+
- Updating a graph.
7+
- Adding episodes to the graph (text and JSON).
8+
- Retrieving nodes from the graph.
9+
- Retrieving edges from the graph.
10+
- Searching the graph for specific content.
1111
1212
The script showcases various operations using the Zep Graph API, including
13-
group management, adding different types of episodes, and querying the graph structure.
13+
graph management, adding different types of episodes, and querying the graph structure.
1414
"""
1515

1616
import asyncio
@@ -33,68 +33,68 @@ async def main() -> None:
3333
api_key=API_KEY,
3434
)
3535

36-
group_id = f"slack:{uuid.uuid4().hex}"
37-
print(f"Creating group {group_id}...")
38-
group = await client.group.add(
39-
group_id=group_id,
40-
name="My Group",
41-
description="This is my group",
36+
graph_id = f"slack:{uuid.uuid4().hex}"
37+
print(f"Creating graph {graph_id}...")
38+
graph = await client.graph.create(
39+
graph_id=graph_id,
40+
name="My Graph",
41+
description="This is my graph",
4242
)
4343
await asyncio.sleep(2)
44-
print(f"Group {group_id} created {group}")
44+
print(f"graph {graph_id} created {graph}")
4545

46-
print(f"Adding episode to group {group_id}...")
46+
print(f"Adding episode to graph {graph_id}...")
4747
await client.graph.add(
48-
group_id=group_id,
48+
graph_id=graph_id,
4949
data="This is a test episode",
5050
type="text",
5151
)
5252
await asyncio.sleep(2)
53-
print(f"Adding more meaningful episode to group {group_id}...")
53+
print(f"Adding more meaningful episode to graph {graph_id}...")
5454
await client.graph.add(
55-
group_id=group_id,
55+
graph_id=graph_id,
5656
data="Eric Clapton is a rock star",
5757
type="text",
5858
)
5959
await asyncio.sleep(2)
60-
print(f"Adding a JSON episode to group {group_id}...")
60+
print(f"Adding a JSON episode to graph {graph_id}...")
6161
json_string = '{"name": "Eric Clapton", "age": 78, "genre": "Rock"}'
6262
await client.graph.add(
63-
group_id=group_id,
63+
graph_id=graph_id,
6464
data=json_string,
6565
type="json",
6666
)
6767
await asyncio.sleep(20)
6868

6969
# TODO: Need to enable non-message episodic content retrieval
70-
print(f"Getting episodes from group {group_id}...")
71-
results = await client.graph.episode.get_by_group_id(group_id, lastn=2)
70+
print(f"Getting episodes from graph {graph_id}...")
71+
results = await client.graph.episode.get_by_graph_id(graph_id, lastn=2)
7272
await asyncio.sleep(2)
73-
print(f"Episodes from group {group_id} {results.episodes}")
73+
print(f"Episodes from graph {graph_id} {results.episodes}")
7474
episode = await client.graph.episode.get(results.episodes[0].uuid_)
7575
await asyncio.sleep(2)
76-
print(f"Episode {episode.uuid_} from group {group_id} {episode}")
76+
print(f"Episode {episode.uuid_} from graph {graph_id} {episode}")
7777

78-
print(f"Getting nodes from group {group_id}...")
79-
nodes = await client.graph.node.get_by_group_id(group_id)
78+
print(f"Getting nodes from graph {graph_id}...")
79+
nodes = await client.graph.node.get_by_graph_id(graph_id)
8080
await asyncio.sleep(2)
81-
print(f"Nodes from group {group_id} {nodes}")
81+
print(f"Nodes from graph {graph_id} {nodes}")
8282

83-
print(f"Getting edges from group {group_id}...")
84-
edges = await client.graph.edge.get_by_group_id(group_id)
83+
print(f"Getting edges from graph {graph_id}...")
84+
edges = await client.graph.edge.get_by_graph_id(graph_id)
8585
await asyncio.sleep(2)
86-
print(f"Edges from group {group_id} {edges}")
86+
print(f"Edges from graph {graph_id} {edges}")
8787

88-
print(f"Searching group {group_id}...")
88+
print(f"Searching graph {graph_id}...")
8989
search_results = await client.graph.search(
90-
group_id=group_id,
90+
graph_id=graph_id,
9191
query="Eric Clapton",
9292
)
9393
await asyncio.sleep(2)
94-
print(f"Search results from group {group_id} {search_results}")
94+
print(f"Search results from graph {graph_id} {search_results}")
9595

96-
# await client.group.delete(group_id)
97-
print(f"Group {group_id} deleted")
96+
# await client.graph.delete(graph_id)
97+
# print(f"graph {graph_id} deleted")
9898

9999

100100
if __name__ == "__main__":

examples/graph_example/user_graph_example.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
44
This script demonstrates the following functionality:
55
- Creating a user.
6-
- Creating a session associated with the created user.
7-
- Adding messages to the session.
6+
- Creating a thread associated with the created user.
7+
- Adding messages to the thread.
88
- Retrieving episodes, edges, and nodes for a user.
99
- Searching the user's graph memory.
1010
- Adding text and JSON episodes to the graph.
1111
- Performing a centered search on a specific node.
1212
1313
The script showcases various operations using the Zep Graph API, including
14-
user and session management, adding different types of episodes, and querying
14+
user and thread management, adding different types of episodes, and querying
1515
the graph structure.
1616
"""
1717

@@ -37,14 +37,14 @@ async def main() -> None:
3737
api_key=API_KEY,
3838
)
3939
user_id = uuid.uuid4().hex
40-
session_id = uuid.uuid4().hex
40+
thread_id = uuid.uuid4().hex
4141
await client.user.add(user_id=user_id, first_name="Paul")
4242
print(f"User {user_id} created")
43-
await client.memory.add_session(session_id=session_id, user_id=user_id)
44-
print(f"Session {session_id} created")
43+
await client.thread.create(thread_id=thread_id, user_id=user_id)
44+
print(f"thread {thread_id} created")
4545
for message in history[2]:
46-
await client.memory.add(
47-
session_id,
46+
await client.thread.add_messages(
47+
thread_id,
4848
messages=[
4949
Message(
5050
role_type=message["role_type"],
@@ -55,18 +55,10 @@ async def main() -> None:
5555

5656
print("Waiting for the graph to be updated...")
5757
await asyncio.sleep(30)
58-
print("Getting memory for session")
59-
session_memory = await client.memory.get(session_id)
60-
print(session_memory)
61-
print("Searching user memory...")
62-
search_results = await client.memory.search_sessions(
63-
text="What is the weather in San Francisco?",
64-
user_id=user_id,
65-
search_scope="facts",
66-
)
67-
print(search_results)
68-
sessions = await client.user.get_sessions(user_id)
69-
print(sessions)
58+
print("Getting memory for thread")
59+
thread_memory = await client.thread.get_user_context(thread_id)
60+
print(thread_memory)
61+
7062
print("Getting episodes for user")
7163
episode_result = await client.graph.episode.get_by_user_id(user_id, lastn=3)
7264
episodes = episode_result.episodes

0 commit comments

Comments
 (0)