2020from chat_history_shoe_purchase import history
2121
2222from zep_cloud .client import AsyncZep
23- from zep_cloud .types import Message
23+ from zep_cloud .types import Message , FactRatingInstruction , FactRatingExamples
2424
2525load_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
87123if __name__ == "__main__" :
0 commit comments