|
3 | 3 |
|
4 | 4 | This script demonstrates the following functionality: |
5 | 5 | - 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. |
12 | 12 | """ |
13 | 13 |
|
14 | 14 | import asyncio |
@@ -53,71 +53,39 @@ async def main() -> None: |
53 | 53 | metadata={"vip": "true"}, |
54 | 54 | ) |
55 | 55 |
|
56 | | - # await asyncio.sleep(1) |
57 | 56 | 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 |
59 | 58 |
|
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}") |
62 | 61 |
|
63 | | - await client.memory.add_session( |
64 | | - session_id=session_id, |
| 62 | + await client.thread.create( |
| 63 | + thread_id=thread_id, |
65 | 64 | user_id=user_id, |
66 | 65 | metadata={"foo": "bar"}, |
67 | 66 | ) |
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}") |
81 | 73 | for m in history: |
82 | 74 | 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)]) |
84 | 76 | # await asyncio.sleep(0.5) |
85 | 77 |
|
86 | 78 | # Wait for the messages to be processed |
87 | 79 | await asyncio.sleep(50) |
88 | 80 |
|
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") |
114 | 81 |
|
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}") |
116 | 85 |
|
117 | | - # Delete Memory for session |
| 86 | + # Delete thread and wipe thread memory |
118 | 87 | # 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) |
121 | 89 |
|
122 | 90 |
|
123 | 91 | if __name__ == "__main__": |
|
0 commit comments