recallrai 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of recallrai might be problematic. Click here for more details.

@@ -0,0 +1,439 @@
1
+ Metadata-Version: 2.3
2
+ Name: recallrai
3
+ Version: 0.2.0
4
+ Summary: Official Python SDK for RecallrAI - Revolutionary contextual memory system that enables AI assistants to form meaningful connections between conversations, just like human memory.
5
+ License: MIT
6
+ Keywords: ai,memory,context,llm,mem0,getzep,zep
7
+ Author: Devasheesh Mishra
8
+ Author-email: devasheeshmishra4@gmail.com
9
+ Requires-Python: >=3.9,<3.14
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Requires-Dist: httpx (>=0.28.1,<0.29.0)
20
+ Requires-Dist: pydantic (>=2.11.1,<3.0.0)
21
+ Project-URL: Homepage, https://recallrai.com
22
+ Project-URL: Repository, https://github.com/recallrai/sdk-python
23
+ Description-Content-Type: text/markdown
24
+
25
+ # RecallrAI Python SDK
26
+
27
+ Official Python SDK for RecallrAI – a revolutionary contextual memory system that enables AI assistants to form meaningful connections between conversations, just like human memory.
28
+
29
+ ## Installation
30
+
31
+ Install the SDK via Poetry or pip:
32
+
33
+ ```bash
34
+ poetry add recallrai
35
+ # or
36
+ pip install recallrai
37
+ ```
38
+
39
+ ## Initialization
40
+
41
+ Create a client instance with your API key and project ID:
42
+
43
+ ```python
44
+ from recallrai import RecallrAI
45
+
46
+ api_key = "rai_yourapikey"
47
+ project_id = "project-uuid"
48
+ client = RecallrAI(api_key=api_key, project_id=project_id)
49
+ ```
50
+
51
+ ## User Management
52
+
53
+ ### Create a User
54
+
55
+ ```python
56
+ from recallrai.exceptions import UserAlreadyExistsError
57
+ try:
58
+ user = client.create_user(user_id="user123", metadata={"name": "John Doe"})
59
+ print(f"Created user: {user.user_id}")
60
+ print(f"User metadata: {user.metadata}")
61
+ print(f"Created at: {user.created_at}")
62
+ except UserAlreadyExistsError as e:
63
+ print(f"Error: {e}")
64
+ ```
65
+
66
+ ### Get a User
67
+
68
+ ```python
69
+ from recallrai.exceptions import UserNotFoundError
70
+ try:
71
+ user = client.get_user("user123")
72
+ print(f"User metadata: {user.metadata}")
73
+ print(f"Last active: {user.last_active_at}")
74
+ except UserNotFoundError as e:
75
+ print(f"Error: {e}")
76
+ ```
77
+
78
+ ### List Users
79
+
80
+ ```python
81
+ user_list = client.list_users(offset=0, limit=10)
82
+ print(f"Total users: {user_list.total}")
83
+ print(f"Has more users: {user_list.has_more}")
84
+
85
+ for user in user_list.users:
86
+ print(f"User ID: {user.user_id}")
87
+ print(f"Metadata: {user.metadata}")
88
+ print(f"Created at: {user.created_at}")
89
+ print(f"Last active: {user.last_active_at}")
90
+ print("---")
91
+ ```
92
+
93
+ ### Update a User
94
+
95
+ ```python
96
+ from recallrai.exceptions import UserNotFoundError, UserAlreadyExistsError
97
+ try:
98
+ user = client.get_user("user123")
99
+ updated_user = user.update(
100
+ new_metadata={"name": "John Doe", "role": "admin"},
101
+ new_user_id="john_doe"
102
+ )
103
+ print(f"Updated user ID: {updated_user.user_id}")
104
+ print(f"Updated metadata: {updated_user.metadata}")
105
+ except UserNotFoundError as e:
106
+ print(f"Error: {e}")
107
+ except UserAlreadyExistsError as e:
108
+ print(f"Error: {e}")
109
+ ```
110
+
111
+ ### Delete a User
112
+
113
+ ```python
114
+ from recallrai.exceptions import UserNotFoundError
115
+ try:
116
+ user = client.get_user("john_doe")
117
+ user.delete()
118
+ print("User deleted successfully")
119
+ except UserNotFoundError as e:
120
+ print(f"Error: {e}")
121
+ ```
122
+
123
+ ## Session Management
124
+
125
+ ### Create a Session
126
+
127
+ ```python
128
+ from recallrai.exceptions import UserNotFoundError
129
+ from recallrai.session import Session
130
+
131
+ try:
132
+ # First, get the user
133
+ user = client.get_user("user123")
134
+
135
+ # Create a session for the user; auto_process_after_minutes set to -1 disables auto-processing
136
+ session: Session = user.create_session(auto_process_after_minutes=5)
137
+ print("Created session id:", session.session_id)
138
+ except UserNotFoundError as e:
139
+ print(f"Error: {e}")
140
+ ```
141
+
142
+ ### Get an Existing Session
143
+
144
+ ```python
145
+ from recallrai.exceptions import UserNotFoundError, SessionNotFoundError
146
+
147
+ try:
148
+ # First, get the user
149
+ user = client.get_user("user123")
150
+
151
+ # Retrieve an existing session by its ID
152
+ session = user.get_session(session_id="session-uuid")
153
+ print("Session status:", session.get_status())
154
+ except UserNotFoundError as e:
155
+ print(f"Error: {e}")
156
+ except SessionNotFoundError as e:
157
+ print(f"Error: {e}")
158
+ ```
159
+
160
+ ### List Sessions
161
+
162
+ ```python
163
+ from recallrai.exceptions import UserNotFoundError
164
+
165
+ try:
166
+ # First, get the user
167
+ user = client.get_user("user123")
168
+
169
+ # List sessions for this user
170
+ session_list = user.list_sessions(offset=0, limit=10)
171
+ for session in session_list.sessions:
172
+ print(session.session_id, session.status)
173
+ except UserNotFoundError as e:
174
+ print(f"Error: {e}")
175
+ ```
176
+
177
+ ### Session – Adding Messages
178
+
179
+ ```python
180
+ from recallrai.exceptions import UserNotFoundError, SessionNotFoundError, InvalidSessionStateError
181
+
182
+ try:
183
+ # Add a user message
184
+ session.add_user_message("Hello! How are you?")
185
+
186
+ # Add an assistant message
187
+ session.add_assistant_message("I'm an assistant. How can I help you?")
188
+ except UserNotFoundError as e:
189
+ print(f"Error: {e}")
190
+ except SessionNotFoundError as e:
191
+ print(f"Error: {e}")
192
+ except InvalidSessionStateError as e:
193
+ print(f"Error: {e}")
194
+ ```
195
+
196
+ ### Session – Retrieving Context
197
+
198
+ ```python
199
+ from recallrai.exceptions import UserNotFoundError, SessionNotFoundError
200
+
201
+ try:
202
+ context = session.get_context()
203
+ print("Memory used:", context.memory_used)
204
+ print("Context:", context.context)
205
+ except UserNotFoundError as e:
206
+ print(f"Error: {e}")
207
+ except SessionNotFoundError as e:
208
+ print(f"Error: {e}")
209
+ ```
210
+
211
+ ### Session – Process Session
212
+
213
+ ```python
214
+ from recallrai.exceptions import UserNotFoundError, SessionNotFoundError, InvalidSessionStateError
215
+
216
+ try:
217
+ session.process()
218
+ except UserNotFoundError as e:
219
+ print(f"Error: {e}")
220
+ except SessionNotFoundError as e:
221
+ print(f"Error: {e}")
222
+ except InvalidSessionStateError as e:
223
+ print(f"Error: {e}")
224
+ ```
225
+
226
+ ### Session – Get Status and Messages
227
+
228
+ ```python
229
+ from recallrai.exceptions import UserNotFoundError, SessionNotFoundError
230
+ from recallrai.models import SessionStatus
231
+
232
+ try:
233
+ status = session.get_status()
234
+ print("Session status:", status)
235
+
236
+ # Check if the session is in a specific state
237
+ if status == SessionStatus.PROCESSED:
238
+ print("Session has been processed")
239
+
240
+ messages = session.get_messages()
241
+ for message in messages:
242
+ print(f"{message.role}: {message.content} at {message.timestamp}")
243
+ except UserNotFoundError as e:
244
+ print(f"Error: {e}")
245
+ except SessionNotFoundError as e:
246
+ print(f"Error: {e}")
247
+ ```
248
+
249
+ ## Example Usage with LLMs
250
+
251
+ ```python
252
+ import openai
253
+ from recallrai import RecallrAI
254
+ from recallrai.exceptions import UserNotFoundError
255
+
256
+ # Initialize RecallrAI and OpenAI clients
257
+ rai_client = RecallrAI(
258
+ api_key="rai_yourapikey",
259
+ project_id="your-project-uuid"
260
+ )
261
+ oai_client = openai.OpenAI(api_key="your-openai-api-key")
262
+
263
+ def chat_with_memory(user_id, session_id=None):
264
+ # Get or create user
265
+ try:
266
+ user = rai_client.get_user(user_id)
267
+ except UserNotFoundError:
268
+ user = rai_client.create_user(user_id)
269
+
270
+ # Create a new session or get an existing one
271
+ if session_id:
272
+ session = user.get_session(session_id=session_id)
273
+ else:
274
+ session = user.create_session(auto_process_after_minutes=30)
275
+ print(f"Created new session: {session.session_id}")
276
+
277
+ print("Chat session started. Type 'exit' to end the conversation.")
278
+
279
+ while True:
280
+ # Get user input
281
+ user_message = input("You: ")
282
+ if user_message.lower() == 'exit':
283
+ break
284
+
285
+ # Add the user message to RecallrAI
286
+ session.add_user_message(user_message)
287
+
288
+ # Get context from RecallrAI after adding the user message
289
+ context = session.get_context()
290
+
291
+ # Create a system prompt that includes the context
292
+ system_prompt = f"""You are a helpful assistant with memory of previous conversations.
293
+
294
+ MEMORIES ABOUT THE USER:
295
+ {context.context}
296
+
297
+ You can use the above memories to provide better responses to the user.
298
+ Don't mention that you have access to memories unless you are explicitly asked."""
299
+
300
+ # Get previous messages
301
+ previous_messages = session.get_messages()
302
+ previous_messages = [{"role": message.role, "content": message.content} for message in previous_messages]
303
+
304
+ # Call the LLM with the system prompt and conversation history
305
+ response = oai_client.chat.completions.create(
306
+ model="gpt-4o-mini",
307
+ messages=[
308
+ {"role": "system", "content": system_prompt},
309
+ **previous_messages,
310
+ ],
311
+ temperature=0.7
312
+ )
313
+
314
+ assistant_message = response.choices[0].message.content
315
+
316
+ # Print the assistant's response
317
+ print(f"Assistant: {assistant_message}")
318
+
319
+ # Add the assistant's response to RecallrAI
320
+ session.add_assistant_message(assistant_message)
321
+
322
+ # Process the session at the end of the conversation
323
+ print("Processing session to update memory...")
324
+ session.process()
325
+ print(f"Session ended. Session ID: {session.session_id}")
326
+ return session.session_id
327
+
328
+ # Example usage
329
+ if __name__ == "__main__":
330
+ user_id = "user123"
331
+ # To continue a previous session, uncomment below and provide the session ID
332
+ # previous_session_id = "previously-saved-session-uuid"
333
+ # session_id = chat_with_memory(user_id, previous_session_id)
334
+
335
+ # Start a new session
336
+ session_id = chat_with_memory(user_id)
337
+ print(f"To continue this conversation later, use session ID: {session_id}")
338
+ ```
339
+
340
+ ## Exception Handling
341
+
342
+ The RecallrAI SDK implements a comprehensive exception hierarchy to help you handle different error scenarios gracefully:
343
+
344
+ ### Base Exception
345
+
346
+ - **RecallrAIError**: The base exception for all SDK-specific errors. All other exceptions inherit from this.
347
+
348
+ ### Authentication Errors
349
+
350
+ - **AuthenticationError**: Raised when there's an issue with your API key or project ID authentication.
351
+
352
+ ### Network-Related Errors
353
+
354
+ - **NetworkError**: Base exception for all network-related issues.
355
+ - **TimeoutError**: Occurs when a request takes too long to complete.
356
+ - **ConnectionError**: Happens when the SDK cannot establish a connection to the RecallrAI API.
357
+
358
+ ### Server Errors
359
+
360
+ - **ServerError**: Base class for server-side errors.
361
+ - **InternalServerError**: Raised when the RecallrAI API returns a 5xx error code.
362
+
363
+ ### User-Related Errors
364
+
365
+ - **UserError**: Base for all user-related exceptions.
366
+ - **UserNotFoundError**: Raised when attempting to access a user that doesn't exist.
367
+ - **UserAlreadyExistsError**: Occurs when creating a user with an ID that already exists.
368
+
369
+ ### Session-Related Errors
370
+
371
+ - **SessionError**: Base for all session-related exceptions.
372
+ - **SessionNotFoundError**: Raised when attempting to access a non-existent session.
373
+ - **InvalidSessionStateError**: Occurs when performing an operation that's not valid for the current session state (e.g., adding a message to a processed session).
374
+
375
+ ### Input Validation Errors
376
+
377
+ - **ValidationError**: Raised when provided data doesn't meet the required format or constraints.
378
+
379
+ ### Importing Exceptions
380
+
381
+ You can import exceptions directly from the `recallrai.exceptions` module:
382
+
383
+ ```python
384
+ # Import specific exceptions
385
+ from recallrai.exceptions import UserNotFoundError, SessionNotFoundError
386
+
387
+ # Import all exceptions
388
+ from recallrai.exceptions import (
389
+ RecallrAIError,
390
+ AuthenticationError,
391
+ NetworkError,
392
+ TimeoutError,
393
+ ConnectionError,
394
+ ServerError,
395
+ InternalServerError,
396
+ SessionError,
397
+ SessionNotFoundError,
398
+ InvalidSessionStateError,
399
+ UserError,
400
+ UserNotFoundError,
401
+ UserAlreadyExistsError,
402
+ ValidationError,
403
+ )
404
+ ```
405
+
406
+ ### Best Practices for Error Handling
407
+
408
+ When implementing error handling with the RecallrAI SDK, consider these best practices:
409
+
410
+ 1. **Handle specific exceptions first**: Catch more specific exceptions before general ones.
411
+
412
+ ```python
413
+ try:
414
+ # SDK operation
415
+ except UserNotFoundError:
416
+ # Specific handling
417
+ except RecallrAIError:
418
+ # General fallback
419
+ ```
420
+
421
+ 2. **Implement retry logic for transient errors**: Network and timeout errors might be temporary.
422
+
423
+ 3. **Log detailed error information**: Exceptions contain useful information for debugging.
424
+
425
+ 4. **Handle common user flows**: For example, check if a user exists before operations, or create them if they don't:
426
+
427
+ ```python
428
+ try:
429
+ user = client.get_user(user_id)
430
+ except UserNotFoundError:
431
+ user = client.create_user(user_id)
432
+ ```
433
+
434
+ For more detailed information on specific exceptions, refer to the API documentation.
435
+
436
+ ## Conclusion
437
+
438
+ This README outlines the basic usage of the RecallrAI SDK functions for user and session management. For additional documentation and advanced usage, please see the [official documentation](https://recallrai.com) or the source code repository on [GitHub](https://github.com/recallrai/sdk-python).
439
+