personal-ai-tutor.mdx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # Set the OpenAI API key
  18. os.environ['OPENAI_API_KEY'] = 'sk-xxx'
  19. # Initialize the OpenAI client
  20. client = OpenAI()
  21. class PersonalAITutor:
  22. def __init__(self):
  23. """
  24. Initialize the PersonalAITutor with memory configuration and OpenAI client.
  25. """
  26. config = {
  27. "vector_store": {
  28. "provider": "qdrant",
  29. "config": {
  30. "host": "localhost",
  31. "port": 6333,
  32. }
  33. },
  34. }
  35. self.memory = Memory.from_config(config)
  36. self.client = client
  37. self.app_id = "app-1"
  38. def ask(self, question, user_id=None):
  39. """
  40. Ask a question to the AI and store the relevant facts in memory
  41. :param question: The question to ask the AI.
  42. :param user_id: Optional user ID to associate with the memory.
  43. """
  44. # Start a streaming chat completion request to the AI
  45. stream = self.client.chat.completions.create(
  46. model="gpt-4",
  47. stream=True,
  48. messages=[
  49. {"role": "system", "content": "You are a personal AI Tutor."},
  50. {"role": "user", "content": question}
  51. ]
  52. )
  53. # Store the question in memory
  54. self.memory.add(question, user_id=user_id, metadata={"app_id": self.app_id})
  55. # Print the response from the AI in real-time
  56. for chunk in stream:
  57. if chunk.choices[0].delta.content is not None:
  58. print(chunk.choices[0].delta.content, end="")
  59. def get_memories(self, user_id=None):
  60. """
  61. Retrieve all memories associated with the given user ID.
  62. :param user_id: Optional user ID to filter memories.
  63. :return: List of memories.
  64. """
  65. return self.memory.get_all(user_id=user_id)
  66. # Instantiate the PersonalAITutor
  67. ai_tutor = PersonalAITutor()
  68. # Define a user ID
  69. user_id = "john_doe"
  70. # Ask a question
  71. ai_tutor.ask("I am learning introduction to CS. What is queue? Briefly explain.", user_id=user_id)
  72. ```
  73. ### Fetching Memories
  74. You can fetch all the memories at any point in time using the following code:
  75. ```python
  76. memories = ai_tutor.get_memories(user_id=user_id)
  77. for m in memories:
  78. print(m['text'])
  79. ```
  80. ### Key Points
  81. - **Initialization**: The PersonalAITutor class is initialized with the necessary memory configuration and OpenAI client setup.
  82. - **Asking Questions**: The ask method sends a question to the AI and stores the relevant information in memory.
  83. - **Retrieving Memories**: The get_memories method fetches all stored memories associated with a user.
  84. ### Conclusion
  85. 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.