personal-ai-tutor.mdx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ---
  2. title: Personalized AI Tutor
  3. ---
  4. You can create a personalized AI Tutor using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
  5. ## Overview
  6. The Personalized AI Tutor leverages Mem0 to retain information across interactions, enabling a tailored learning experience. By integrating with OpenAI's GPT-4 model, the tutor can provide detailed and context-aware responses to user queries.
  7. ## Setup
  8. Before you begin, ensure you have the required dependencies installed. You can install the necessary packages using pip:
  9. ```bash
  10. pip install openai mem0ai
  11. ```
  12. ## Full Code Example
  13. Below is the complete code to create and interact with a Personalized AI Tutor using Mem0:
  14. ```python
  15. from openai import OpenAI
  16. from mem0 import Memory
  17. # Initialize the OpenAI client
  18. client = OpenAI()
  19. class PersonalAITutor:
  20. def __init__(self):
  21. """
  22. Initialize the PersonalAITutor with memory configuration and OpenAI client.
  23. """
  24. config = {
  25. "vector_store": {
  26. "provider": "qdrant",
  27. "config": {
  28. "host": "localhost",
  29. "port": 6333,
  30. }
  31. },
  32. }
  33. self.memory = Memory.from_config(config)
  34. self.client = client
  35. self.app_id = "app-1"
  36. def ask(self, question, user_id=None):
  37. """
  38. Ask a question to the AI and store the relevant facts in memory
  39. :param question: The question to ask the AI.
  40. :param user_id: Optional user ID to associate with the memory.
  41. """
  42. # Start a streaming chat completion request to the AI
  43. stream = self.client.chat.completions.create(
  44. model="gpt-4",
  45. stream=True,
  46. messages=[
  47. {"role": "system", "content": "You are a personal AI Tutor."},
  48. {"role": "user", "content": question}
  49. ]
  50. )
  51. # Store the question in memory
  52. self.memory.add(question, user_id=user_id, metadata={"app_id": self.app_id})
  53. # Print the response from the AI in real-time
  54. for chunk in stream:
  55. if chunk.choices[0].delta.content is not None:
  56. print(chunk.choices[0].delta.content, end="")
  57. def get_memories(self, user_id=None):
  58. """
  59. Retrieve all memories associated with the given user ID.
  60. :param user_id: Optional user ID to filter memories.
  61. :return: List of memories.
  62. """
  63. return self.memory.get_all(user_id=user_id)
  64. # Instantiate the PersonalAITutor
  65. ai_tutor = PersonalAITutor()
  66. # Define a user ID
  67. user_id = "john_doe"
  68. # Ask a question
  69. ai_tutor.ask("I am learning introduction to CS. What is queue? Briefly explain.", user_id=user_id)
  70. ```
  71. ### Fetching Memories
  72. You can fetch all the memories at any point in time using the following code:
  73. ```python
  74. memories = ai_tutor.get_memories(user_id=user_id)
  75. for m in memories:
  76. print(m['text'])
  77. ```
  78. ### Key Points
  79. - **Initialization**: The PersonalAITutor class is initialized with the necessary memory configuration and OpenAI client setup.
  80. - **Asking Questions**: The ask method sends a question to the AI and stores the relevant information in memory.
  81. - **Retrieving Memories**: The get_memories method fetches all stored memories associated with a user.
  82. ### Conclusion
  83. As the conversation progresses, Mem0's memory automatically updates based on the interactions, providing a continuously improving personalized learning experience. This setup ensures that the AI Tutor can offer contextually relevant and accurate responses, enhancing the overall educational process.