Skip to content

Commit 784526b

Browse files
Threads API (#267)
* chore: Fix fact ratings table * chore: Bump version * SDK regeneration * SDK regeneration * SDK regeneration * feat: Add support for custom entity edges * simplify ontology client * SDK regeneration * SDK regeneration * chore: Update doc string * feat: Update sync version of set entity type method * SDK regeneration * SDK regeneration * chore: Bump version * SDK regeneration * SDK regeneration * SDK regeneration * SDK regeneration * SDK regeneration * chore: Set up set ontology method * chore: Bump version * SDK regeneration * SDK regeneration * chore: Bump version * SDK regeneration * SDK regeneration * SDK regeneration * chore: Add deprecation notice to structured data extraction * feat: Create context string utility function * chore: Bump version * SDK regeneration * SDK regeneration * SDK regeneration * chore: Bump version * SDK regeneration * SDK regeneration * chore: Make memory example thread focused * chore: Update user graph example --------- Co-authored-by: fern-api <115122769+fern-api[bot]@users.noreply.github.com>
1 parent 0020d26 commit 784526b

16 files changed

Lines changed: 1562 additions & 110 deletions

examples/chat_history/memory.py

Lines changed: 20 additions & 56 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, FactRatingInstruction, FactRatingExamples
23+
from zep_cloud.types import Message
2424

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

3737
# Create a user
3838
user_id = uuid.uuid4().hex # unique user id. can be any alphanum string
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-
)
39+
4840
await client.user.add(
4941
user_id=user_id,
5042
email="user@example.com",
@@ -55,69 +47,41 @@ async def main() -> None:
5547

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

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

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

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

8672
# Wait for the messages to be processed
8773
await asyncio.sleep(50)
8874

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")
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)
11478

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

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

12286

12387
if __name__ == "__main__":

examples/graph_example/user_graph_example.py

Lines changed: 10 additions & 29 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]:
4646
await client.memory.add(
47-
session_id,
47+
thread_id,
4848
messages=[
4949
Message(
5050
role_type=message["role_type"],
@@ -55,19 +55,9 @@ 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)
70-
print("Getting episodes for user")
58+
print("Getting user context for thread")
59+
thread_context = await client.thread.get_user_context(thread_id)
60+
print(thread_context)
7161
episode_result = await client.graph.episode.get_by_user_id(user_id, lastn=3)
7262
episodes = episode_result.episodes
7363
print(f"Episodes for user {user_id}:")
@@ -154,15 +144,6 @@ async def main() -> None:
154144
scope="nodes",
155145
)
156146
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-
)
166147

167148
# Uncomment to delete the user
168149
# 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.18.0"
3+
version = "2.19.0"
44
description = ""
55
readme = "README.md"
66
authors = []

0 commit comments

Comments
 (0)