personal-travel-assistant.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ---
  2. title: Personal AI Travel Assistant
  3. ---
  4. Create a personalized AI Travel Assistant using Mem0. This guide provides step-by-step instructions and the complete code to get you started.
  5. ## Overview
  6. The Personalized AI Travel Assistant uses Mem0 to store and retrieve information across interactions, enabling a tailored travel planning experience. It integrates with OpenAI's GPT-4 model to provide detailed and context-aware responses to user queries.
  7. ## Setup
  8. Install the required dependencies using pip:
  9. ```bash
  10. pip install openai mem0ai
  11. ```
  12. ## Full Code Example
  13. Here's the complete code to create and interact with a Personalized AI Travel Assistant using Mem0:
  14. ```python
  15. import os
  16. from openai import OpenAI
  17. from mem0 import Memory
  18. # Set the OpenAI API key
  19. os.environ['OPENAI_API_KEY'] = 'sk-xxx'
  20. class PersonalTravelAssistant:
  21. def __init__(self):
  22. self.client = OpenAI()
  23. self.memory = Memory()
  24. self.messages = [{"role": "system", "content": "You are a personal AI Assistant."}]
  25. def ask_question(self, question, user_id):
  26. # Fetch previous related memories
  27. previous_memories = self.search_memories(question, user_id=user_id)
  28. prompt = question
  29. if previous_memories:
  30. prompt = f"User input: {question}\n Previous memories: {previous_memories}"
  31. self.messages.append({"role": "user", "content": prompt})
  32. # Generate response using GPT-4o
  33. response = self.client.chat.completions.create(
  34. model="gpt-4o",
  35. messages=self.messages
  36. )
  37. answer = response.choices[0].message.content
  38. self.messages.append({"role": "assistant", "content": answer})
  39. # Store the question in memory
  40. self.memory.add(question, user_id=user_id)
  41. return answer
  42. def get_memories(self, user_id):
  43. memories = self.memory.get_all(user_id=user_id)
  44. return [m['text'] for m in memories]
  45. def search_memories(self, query, user_id):
  46. memories = self.memory.search(query, user_id=user_id)
  47. return [m['text'] for m in memories]
  48. # Usage example
  49. user_id = "traveler_123"
  50. ai_assistant = PersonalTravelAssistant()
  51. def main():
  52. while True:
  53. question = input("Question: ")
  54. if question.lower() in ['q', 'exit']:
  55. print("Exiting...")
  56. break
  57. answer = ai_assistant.ask_question(question, user_id=user_id)
  58. print(f"Answer: {answer}")
  59. memories = ai_assistant.get_memories(user_id=user_id)
  60. print("Memories:")
  61. for memory in memories:
  62. print(f"- {memory}")
  63. print("-----")
  64. if __name__ == "__main__":
  65. main()
  66. ```
  67. ## Key Components
  68. - **Initialization**: The `PersonalTravelAssistant` class is initialized with the OpenAI client and Mem0 memory setup.
  69. - **Asking Questions**: The `ask_question` method sends a question to the AI, incorporates previous memories, and stores new information.
  70. - **Memory Management**: The `get_memories` and search_memories methods handle retrieval and searching of stored memories.
  71. ## Usage
  72. 1. Set your OpenAI API key in the environment variable.
  73. 2. Instantiate the `PersonalTravelAssistant`.
  74. 3. Use the `main()` function to interact with the assistant in a loop.
  75. ## Conclusion
  76. This Personalized AI Travel Assistant leverages Mem0's memory capabilities to provide context-aware responses. As you interact with it, the assistant learns and improves, offering increasingly personalized travel advice and information.