Skip to content

Commit 94d84f9

Browse files
Merge branch 'main' of github.com:getzep/zep-python
2 parents 43e3b64 + 05d4ffa commit 94d84f9

16 files changed

Lines changed: 110 additions & 1566 deletions

examples/chat_history/memory.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from chat_history_shoe_purchase import history
2121

2222
from zep_cloud.client import AsyncZep
23-
from zep_cloud.types import Message
23+
from zep_cloud.types import Message, FactRatingInstruction, FactRatingExamples
2424

2525
load_dotenv(
2626
dotenv_path=find_dotenv()
@@ -36,7 +36,15 @@ async def main() -> None:
3636

3737
# Create a user
3838
user_id = uuid.uuid4().hex # unique user id. can be any alphanum string
39-
39+
fact_rating_instruction = """Rate the facts by poignancy. Highly poignant
40+
facts have a significant emotional impact or relevance to the user.
41+
Facts with low poignancy are minimally relevant or of little emotional
42+
significance."""
43+
fact_rating_examples = FactRatingExamples(
44+
high="The user received news of a family member's serious illness.",
45+
medium="The user completed a challenging marathon.",
46+
low="The user bought a new brand of toothpaste.",
47+
)
4048
await client.user.add(
4149
user_id=user_id,
4250
email="user@example.com",
@@ -47,41 +55,69 @@ async def main() -> None:
4755

4856
# await asyncio.sleep(1)
4957
print(f"User added: {user_id}")
50-
thread_id = uuid.uuid4().hex # unique session id. can be any alphanum string
58+
session_id = uuid.uuid4().hex # unique session id. can be any alphanum string
5159

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

55-
await client.thread.create(
56-
thread_id=thread_id,
63+
await client.memory.add_session(
64+
session_id=session_id,
5765
user_id=user_id,
66+
metadata={"foo": "bar"},
5867
)
59-
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)
6073
# Get session
61-
print(f"\n---Getting thread: {thread_id}")
62-
session = await client.thread.get(thread_id)
63-
print(f"Thread details: {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)
6478

65-
# Add messages to the thread
66-
print(f"\n---Add messages to the thread: {thread_id}")
79+
# Add Memory for session
80+
print(f"\n---Add Memory for Session: {session_id}")
6781
for m in history:
6882
print(f"{m['role']}: {m['content']}")
69-
await client.thread.add_messages(thread_id=thread_id, messages=[Message(**m)])
83+
await client.memory.add(session_id=session_id, messages=[Message(**m)])
7084
# await asyncio.sleep(0.5)
7185

7286
# Wait for the messages to be processed
7387
await asyncio.sleep(50)
7488

75-
# Get user context for thread
76-
print(f"\n---Get user context for thread: {thread_id}")
77-
user_context = await client.thread.get_user_context(thread_id)
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")
78114

79-
print(f"User context: {user_context.context}")
115+
print(f"Memory context: {memory.context}")
80116

81-
# Delete thread and clear context
117+
# Delete Memory for session
82118
# Uncomment to run
83-
# print(f"\n6---delete thread memory : {thread_id}")
84-
# await client.thread.delete(thread_id)
119+
# print(f"\n6---deleteMemory for Session: {session_id}")
120+
# await client.memory.delete(session_id)
85121

86122

87123
if __name__ == "__main__":

examples/graph_example/user_graph_example.py

Lines changed: 29 additions & 10 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 thread associated with the created user.
7-
- Adding messages to the thread.
6+
- Creating a session associated with the created user.
7+
- Adding messages to the session.
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 thread management, adding different types of episodes, and querying
14+
user and session 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-
thread_id = uuid.uuid4().hex
40+
session_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.thread.create(thread_id=thread_id, user_id=user_id)
44-
print(f"thread {thread_id} created")
43+
await client.memory.add_session(session_id=session_id, user_id=user_id)
44+
print(f"Session {session_id} created")
4545
for message in history[2]:
4646
await client.memory.add(
47-
thread_id,
47+
session_id,
4848
messages=[
4949
Message(
5050
role_type=message["role_type"],
@@ -55,9 +55,19 @@ async def main() -> None:
5555

5656
print("Waiting for the graph to be updated...")
5757
await asyncio.sleep(30)
58-
print("Getting user context for thread")
59-
thread_context = await client.thread.get_user_context(thread_id)
60-
print(thread_context)
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)
70+
print("Getting episodes for user")
6171
episode_result = await client.graph.episode.get_by_user_id(user_id, lastn=3)
6272
episodes = episode_result.episodes
6373
print(f"Episodes for user {user_id}:")
@@ -144,6 +154,15 @@ async def main() -> None:
144154
scope="nodes",
145155
)
146156
print(search_results.nodes)
157+
print("Getting all user facts")
158+
result = await client.user.get_facts(user_id)
159+
print(result.facts)
160+
161+
for fact in result.facts:
162+
if fact.valid_at or fact.invalid_at:
163+
print(
164+
f"Fact {fact.fact} is valid at {fact.valid_at} and invalid at {fact.invalid_at}\n "
165+
)
147166

148167
# Uncomment to delete the user
149168
# await client.user.delete(user_id)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zep-cloud"
3-
version = "2.19.0"
3+
version = "2.21.0"
44
description = ""
55
readme = "README.md"
66
authors = []

0 commit comments

Comments
 (0)