Files
mem0/cookbooks/multion_travel_agent.ipynb
T

24 KiB

In [ ]:
!pip install mem0ai
In [ ]:
import os
from openai import OpenAI
from mem0 import MemoryClient
from multion.client import MultiOn

# Configuration
OPENAI_API_KEY = 'sk-xxx'  # Replace with your actual OpenAI API key
MULTION_API_KEY = 'xx'  # Replace with your actual MultiOn API key
MEM0_API_KEY = 'xx'  # Replace with your actual Mem0 API key
USER_ID = "test_travel_agent"

# Set up OpenAI API key
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY

# Initialize Mem0 and MultiOn
memory = MemoryClient(api_key=MEM0_API_KEY)
multion = MultiOn(api_key=MULTION_API_KEY)
In [ ]:
# Add conversation to Mem0
conversation = [
    {
        "role": "user",
        "content": "What are the best travel destinations in the world?"
    },
    {
        "role": "assistant",
        "content": "Could you please specify your interests or the type of travel information you are looking for? This will help me find the most relevant information for you."
    },
    {
        "role": "user",
        "content": "Sure, I want to travel to San Francisco."
    },
    {
        "role": "assistant",
        "content": """
        Based on the information gathered from TripAdvisor, here are some popular attractions, activities, and travel tips for San Francisco:

        1. **Golden Gate Bridge**: A must-see iconic landmark.
        2. **Alcatraz Island**: Famous former prison offering tours.
        3. **Fisherman's Wharf**: Popular tourist area with shops, restaurants, and sea lions.
        4. **Chinatown**: The largest Chinatown outside of Asia.
        5. **Golden Gate Park**: Large urban park with gardens, museums, and recreational activities.
        6. **Cable Cars**: Historic streetcars offering a unique way to see the city.
        7. **Exploratorium**: Interactive science museum.
        8. **San Francisco Museum of Modern Art (SFMOMA)**: Modern and contemporary art museum.
        9. **Lombard Street**: Known for its steep, one-block section with eight hairpin turns.
        10. **Union Square**: Major shopping and cultural hub.

        Travel Tips:
        - **Weather**: San Francisco has a mild climate, but it can be foggy and windy. Dress in layers.
        - **Transportation**: Use public transportation like BART, Muni, and cable cars to get around.
        - **Safety**: Be aware of your surroundings, especially in crowded tourist areas.
        - **Dining**: Try local specialties like sourdough bread, seafood, and Mission-style burritos.
        """
    },
    {
        "role": "user",
        "content": "Show me hotels around Golden Gate Bridge."
    },
    {
        "role": "assistant",
        "content": """
        The search results for hotels around Golden Gate Bridge in San Francisco include:

        1. Hilton Hotels In San Francisco - Hotel Near Fishermans Wharf (hilton.com)
        2. The 10 Closest Hotels to Golden Gate Bridge (tripadvisor.com)
        3. Hotels near Golden Gate Bridge (expedia.com)
        4. Hotels near Golden Gate Bridge (hotels.com)
        5. Holiday Inn Express & Suites San Francisco Fishermans Wharf, an IHG Hotel $146 (1.8K) 3-star hotel Golden Gate Bridge • 3.5 mi DEAL 19% less than usual
        6. Holiday Inn San Francisco-Golden Gateway, an IHG Hotel $151 (3.5K) 3-star hotel Golden Gate Bridge • 3.7 mi Casual hotel with dining, a bar & a pool
        7. Hotel Zephyr San Francisco $159 (3.8K) 4-star hotel Golden Gate Bridge • 3.7 mi Nautical-themed lodging with bay views
        8. Lodge at the Presidio
        9. The Inn Above Tide
        10. Cavallo Point
        11. Casa Madrona Hotel and Spa
        12. Cow Hollow Inn and Suites
        13. Samesun San Francisco
        14. Inn on Broadway
        15. Coventry Motor Inn
        16. HI San Francisco Fisherman's Wharf Hostel
        17. Loews Regency San Francisco Hotel
        18. Fairmont Heritage Place Ghirardelli Square
        19. Hotel Drisco Pacific Heights
        20. Travelodge by Wyndham Presidio San Francisco
        """
    }
]

memory.add(conversation, user_id=USER_ID)
{'message': 'Memory added successfully!'}
In [ ]:
def get_travel_info(question, use_memory=True):
    """
    Get travel information based on user's question and optionally their preferences from memory.

    """
    if use_memory:
        previous_memories = memory.search(question, user_id=USER_ID)
        relevant_memories_text = ""
        if previous_memories:
            print("Using previous memories to enhance the search...")
            relevant_memories_text = '\n'.join(mem["memory"] for mem in previous_memories)

        command = "Find travel information based on my interests:"
        prompt = f"{command}\n Question: {question} \n My preferences: {relevant_memories_text}"
    else:
        command = "Find travel information based on my interests:"
        prompt = f"{command}\n Question: {question}"


    print("Searching for travel information...")
    browse_result = multion.browse(cmd=prompt)
    return browse_result.message

Example 1

In [ ]:
question = "Show me flight details for it."
answer_without_memory = get_travel_info(question, use_memory=False)
answer_with_memory = get_travel_info(question, use_memory=True)
Without Memory With Memory
I have performed a Google search for "flight details" and reviewed the search results. Here are some relevant links and information: Memorizing the following information: Flight details for San Francisco:
1. FlightStats Global Flight Tracker - Track the real-time flight status of your flight. See if your flight has been delayed or cancelled and track the live status.
Flight Tracker - FlightStats
1. Prices from $232. Depart Thursday, August 22. Return Thursday, August 29.
2. Prices from $216. Depart Friday, August 23. Return Friday, August 30.
3. Prices from $236. Depart Saturday, August 24. Return Saturday, August 31.
4. Prices from $215. Depart Sunday, August 25. Return Sunday, September 1.
2. FlightAware - Flight Tracker - Track live flights worldwide, see flight cancellations, and browse by airport.
FlightAware - Flight Tracker
5. Prices from $218. Depart Monday, August 26. Return Monday, September 2.
6. Prices from $211. Depart Tuesday, August 27. Return Tuesday, September 3.
7. Prices from $198. Depart Wednesday, August 28. Return Wednesday, September 4.
8. Prices from $218. Depart Thursday, August 29. Return Thursday, September 5.
3. Google Flights - Show flights based on your search.
Google Flights
9. Prices from $194. Depart Friday, August 30. Return Friday, September 6.
10. Prices from $218. Depart Saturday, August 31. Return Saturday, September 7.
11. Prices from $212. Depart Sunday, September 1. Return Sunday, September 8.
12. Prices from $247. Depart Monday, September 2. Return Monday, September 9.
13. Prices from $212. Depart Tuesday, September 3. Return Tuesday, September 10.
14. Prices from $203. Depart Wednesday, September 4. Return Wednesday, September 11.
15. Prices from $242. Depart Thursday, September 5. Return Thursday, September 12.
16. Prices from $191. Depart Friday, September 6. Return Friday, September 13.
17. Prices from $215. Depart Saturday, September 7. Return Saturday, September 14.
18. Prices from $229. Depart Sunday, September 8. Return Sunday, September 15.
19. Prices from $183. Depart Monday, September 9. Return Monday, September 16.
65. Prices from $194. Depart Friday, October 25. Return Friday, November 1.
66. Prices from $205. Depart Saturday, October 26. Return Saturday, November 2.
67. Prices from $241. Depart Sunday, October 27. Return Sunday, November 3.

Example 2

In [ ]:
question = "What places to visit there?"
answer_without_memory = get_travel_info(question, use_memory=False)
answer_with_memory = get_travel_info(question, use_memory=True)
Without Memory With Memory
Based on the information gathered, here are some top travel destinations to consider visiting: Based on the information gathered, here are some top places to visit in San Francisco:
1. Paris: Known for its iconic attractions like the Eiffel Tower and the Louvre, Paris offers quaint cafes, trendy shopping districts, and beautiful Haussmann architecture. It's a city where you can always discover something new with each visit. 1. Golden Gate Bridge - An iconic symbol of San Francisco, perfect for walking, biking, or simply enjoying the view.
2. Alcatraz Island - The historic former prison offers tours and insights into its storied past.
3. Fisherman's Wharf - A bustling waterfront area known for its seafood, shopping, and attractions like Pier 39.
4. Golden Gate Park - A large urban park with gardens, museums, and recreational activities.
5. Chinatown San Francisco - One of the oldest and most famous Chinatowns in North America, offering unique shops and delicious food.
6. Coit Tower - Offers panoramic views of the city and murals depicting San Francisco's history.
7. Lands End - A beautiful coastal trail with stunning views of the Pacific Ocean and the Golden Gate Bridge.
8. Palace of Fine Arts - A picturesque structure and park, perfect for a leisurely stroll or photo opportunities.
9. Crissy Field & The Presidio Tunnel Tops - Great for outdoor activities and scenic views of the bay.
2. Bora Bora: This small island in French Polynesia is famous for its stunning turquoise waters, luxurious overwater bungalows, and vibrant coral reefs. It's a popular destination for honeymooners and those seeking a tropical paradise.
3. Glacier National Park: Located in Montana, USA, this park is known for its breathtaking landscapes, including rugged mountains, pristine lakes, and diverse wildlife. It's a haven for outdoor enthusiasts and hikers.
4. Rome: The capital of Italy, Rome is rich in history and culture, featuring landmarks such as the Colosseum, the Vatican, and the Pantheon. It's a city where ancient history meets modern life.
5. Swiss Alps: Renowned for their stunning natural beauty, the Swiss Alps offer opportunities for skiing, hiking, and enjoying picturesque mountain villages.
6. Maui: One of Hawaii's most popular islands, Maui is known for its beautiful beaches, lush rainforests, and the scenic Hana Highway. It's a great destination for both relaxation and adventure.
7. London, England: A vibrant city with a mix of historical landmarks like the Tower of London and modern attractions such as the London Eye. London offers diverse cultural experiences, world-class museums, and a bustling nightlife.
8. Maldives: This tropical paradise in the Indian Ocean is famous for its crystal-clear waters, luxurious resorts, and abundant marine life. It's an ideal destination for snorkeling, diving, and relaxation.
9. Turks & Caicos: Known for its pristine beaches and turquoise waters, this Caribbean destination is perfect for water sports, beach lounging, and exploring coral reefs.
10. Tokyo: Japan's bustling capital offers a unique blend of traditional and modern attractions, from ancient temples to futuristic skyscrapers. Tokyo is also known for its vibrant food scene and shopping districts.

Example 3

In [ ]:
question = "What the weather there?"
answer_without_memory = get_travel_info(question, use_memory=False)
answer_with_memory = get_travel_info(question, use_memory=True)
Without Memory With Memory
The current weather in Paris is light rain with a temperature of 67°F. The precipitation is at 50%, humidity is 95%, and the wind speed is 5 mph. The current weather in San Francisco is as follows:
- Temperature: 59°F
- Condition: Clear with periodic clouds
- Precipitation: 3%
- Humidity: 87%
- Wind: 12 mph