Notebooks
M
MongoDB
How To Build Ai Agent Claude 3 5 Sonnet Llamaindex Mongodb

How To Build Ai Agent Claude 3 5 Sonnet Llamaindex Mongodb

agentsartificial-intelligencellmsmongodb-genai-showcasenotebooksgenerative-airag

How To Build An AI Agent With Claude 3.5 Sonnet, LlamaIndex and MongoDB

Open In Colab

Set Up Libraries

[1]
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.4/15.4 MB 67.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 67.1 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.6/75.6 kB 8.8 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 130.8/130.8 kB 15.3 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 327.4/327.4 kB 31.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 64.8 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 290.4/290.4 kB 30.0 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 77.9/77.9 kB 9.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 7.1 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.2/49.2 kB 6.3 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 669.1/669.1 kB 5.1 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 307.7/307.7 kB 6.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 862.7/862.7 kB 5.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 318.9/318.9 kB 8.4 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.6/44.6 kB 1.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.9/43.9 kB 5.2 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.5/62.5 kB 7.8 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 853.2/853.2 kB 18.1 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.3/21.3 MB 61.9 MB/s eta 0:00:00
  Building wheel for nomic (pyproject.toml) ... done
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 547.8/547.8 kB 8.0 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 MB 13.0 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 116.3/116.3 kB 14.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.9/64.9 kB 7.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.1/194.1 kB 21.4 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 134.8/134.8 kB 16.3 MB/s eta 0:00:00
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
cudf-cu12 24.4.1 requires pyarrow<15.0.0a0,>=14.0.1, but you have pyarrow 16.1.0 which is incompatible.
google-colab 1.0.0 requires requests==2.31.0, but you have requests 2.32.3 which is incompatible.
ibis-framework 8.0.0 requires pyarrow<16,>=2, but you have pyarrow 16.1.0 which is incompatible.

Set Up Environment Variables

[2]

Configure LLMs and Embedding Models

[8]

Data Loading

[16]
[ ]

Data Processing

[19]

The LLM sees this: 
 Metadata: listing_url=>https://www.airbnb.com/rooms/10006546
name=>Ribeira Charming Duplex
summary=>Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character.
space=>Privileged views of the Douro River and Ribeira square, our apartment offers the perfect conditions to discover the history and the charm of Porto. Apartment comfortable, charming, romantic and cozy in the heart of Ribeira. Within walking distance of all the most emblematic places of the city of Porto. The apartment is fully equipped to host 8 people, with cooker, oven, washing machine, dishwasher, microwave, coffee machine (Nespresso) and kettle. The apartment is located in a very typical area of the city that allows to cross with the most picturesque population of the city, welcoming, genuine and happy people that fills the streets with his outspoken speech and contagious with your sincere generosity, wrapped in a only parochial spirit.
description=>Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character. Privileged views of the Douro River and Ribeira square, our apartment offers the perfect conditions to discover the history and the charm of Porto. Apartment comfortable, charming, romantic and cozy in the heart of Ribeira. Within walking distance of all the most emblematic places of the city of Porto. The apartment is fully equipped to host 8 people, with cooker, oven, washing machine, dishwasher, microwave, coffee machine (Nespresso) and kettle. The apartment is located in a very typical area of the city that allows to cross with the most picturesque population of the city, welcoming, genuine and happy people that fills the streets with his outspoken speech and contagious with your sincere generosity, wrapped in a only parochial spirit. We are always available to help guests
neighborhood_overview=>In the neighborhood of the river, you can find several restaurants as varied flavors, but without forgetting the so traditional northern food. You can also find several bars and pubs to unwind after a day's visit to the magnificent Port. To enjoy the Douro River can board the boats that daily make the ride of six bridges. You can also embark towards Régua, Barca d'Alva, Pinhão, etc and enjoy the Douro Wine Region, World Heritage of Humanity. The Infante's house is a few meters and no doubt it deserves a visit. They abound grocery stores, bakeries, etc. to make your meals. Souvenir shop, wine cellars, etc. to bring some souvenirs.
notes=>Lose yourself in the narrow streets and staircases zone, have lunch in pubs and typical restaurants, and find the renovated cafes and shops in town. If you like exercise, rent a bicycle in the area and ride along the river to the sea, where it will enter beautiful beaches and terraces for everyone. The area is safe, find the bus stops 1min and metro line 5min. The bustling nightlife is a 10 min walk, where the streets are filled with people and entertainment for all. But Porto is much more than the historical center, here is modern museums, concert halls, clean and cared for beaches and surf all year round. Walk through the Ponte D. Luis and visit the different Caves of Port wine, where you will enjoy the famous port wine. Porto is a spoken city everywhere in the world as the best to be visited and savored by all ... natural beauty, culture, tradition, river, sea, beach, single people, typical food, and we are among those who best receive tourists, confirm! Come visit us and feel at ho
access=>We are always available to help guests. The house is fully available to guests. We are always ready to assist guests. when possible we pick the guests at the airport.  This service transfer have a cost per person. We will also have service "meal at home" with a diverse menu and the taste of each. Enjoy the moment!
interaction=>Cot - 10 € / night Dog - € 7,5 / night
house_rules=>Make the house your home...
property_type=>House
room_type=>Entire home/apt
bed_type=>Real Bed
accommodates=>8
bedrooms=>3.0
beds=>5.0
number_of_reviews=>51
bathrooms=>1.0
amenities=>["TV", "Cable TV", "Wifi", "Kitchen", "Paid parking off premises", "Smoking allowed", "Pets allowed", "Buzzer/wireless intercom", "Heating", "Family/kid friendly", "Washer", "First aid kit", "Fire extinguisher", "Essentials", "Hangers", "Hair dryer", "Iron", "Pack \u2019n Play/travel crib", "Room-darkening shades", "Hot water", "Bed linens", "Extra pillows and blankets", "Microwave", "Coffee maker", "Refrigerator", "Dishwasher", "Dishes and silverware", "Cooking basics", "Oven", "Stove", "Cleaning before checkout", "Waterfront"]
price=>80
extra_people=>15
images=>{"thumbnail_url": "", "medium_url": "", "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large", "xl_picture_url": ""}
address=>{"street": "Porto, Porto, Portugal", "suburb": "", "government_area": "Cedofeita, Ildefonso, S\u00e9, Miragaia, Nicolau, Vit\u00f3ria", "market": "Porto", "country": "Portugal", "country_code": "PT", "location": {"type": "Point", "coordinates": [-8.61308, 41.1413], "is_location_exact": false}}
review_scores=>{"review_scores_accuracy": 9, "review_scores_cleanliness": 9, "review_scores_checkin": 10, "review_scores_communication": 10, "review_scores_location": 10, "review_scores_value": 9, "review_scores_rating": 89}
weekly_price=>None
monthly_price=>None
-----
Content: Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character. Privileged views of the Douro River and Ribeira square, our apartment offers the perfect conditions to discover the history and the charm of Porto. Apartment comfortable, charming, romantic and cozy in the heart of Ribeira. Within walking distance of all the most emblematic places of the city of Porto. The apartment is fully equipped to host 8 people, with cooker, oven, washing machine, dishwasher, microwave, coffee machine (Nespresso) and kettle. The apartment is located in a very typical area of the city that allows to cross with the most picturesque population of the city, welcoming, genuine and happy people that fills the streets with his outspoken speech and contagious with your sincere generosity, wrapped in a only parochial spirit. We are always available to help guests

The Embedding model sees this: 
 Metadata: listing_url=>https://www.airbnb.com/rooms/10006546
name=>Ribeira Charming Duplex
summary=>Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character.
space=>Privileged views of the Douro River and Ribeira square, our apartment offers the perfect conditions to discover the history and the charm of Porto. Apartment comfortable, charming, romantic and cozy in the heart of Ribeira. Within walking distance of all the most emblematic places of the city of Porto. The apartment is fully equipped to host 8 people, with cooker, oven, washing machine, dishwasher, microwave, coffee machine (Nespresso) and kettle. The apartment is located in a very typical area of the city that allows to cross with the most picturesque population of the city, welcoming, genuine and happy people that fills the streets with his outspoken speech and contagious with your sincere generosity, wrapped in a only parochial spirit.
description=>Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character. Privileged views of the Douro River and Ribeira square, our apartment offers the perfect conditions to discover the history and the charm of Porto. Apartment comfortable, charming, romantic and cozy in the heart of Ribeira. Within walking distance of all the most emblematic places of the city of Porto. The apartment is fully equipped to host 8 people, with cooker, oven, washing machine, dishwasher, microwave, coffee machine (Nespresso) and kettle. The apartment is located in a very typical area of the city that allows to cross with the most picturesque population of the city, welcoming, genuine and happy people that fills the streets with his outspoken speech and contagious with your sincere generosity, wrapped in a only parochial spirit. We are always available to help guests
neighborhood_overview=>In the neighborhood of the river, you can find several restaurants as varied flavors, but without forgetting the so traditional northern food. You can also find several bars and pubs to unwind after a day's visit to the magnificent Port. To enjoy the Douro River can board the boats that daily make the ride of six bridges. You can also embark towards Régua, Barca d'Alva, Pinhão, etc and enjoy the Douro Wine Region, World Heritage of Humanity. The Infante's house is a few meters and no doubt it deserves a visit. They abound grocery stores, bakeries, etc. to make your meals. Souvenir shop, wine cellars, etc. to bring some souvenirs.
notes=>Lose yourself in the narrow streets and staircases zone, have lunch in pubs and typical restaurants, and find the renovated cafes and shops in town. If you like exercise, rent a bicycle in the area and ride along the river to the sea, where it will enter beautiful beaches and terraces for everyone. The area is safe, find the bus stops 1min and metro line 5min. The bustling nightlife is a 10 min walk, where the streets are filled with people and entertainment for all. But Porto is much more than the historical center, here is modern museums, concert halls, clean and cared for beaches and surf all year round. Walk through the Ponte D. Luis and visit the different Caves of Port wine, where you will enjoy the famous port wine. Porto is a spoken city everywhere in the world as the best to be visited and savored by all ... natural beauty, culture, tradition, river, sea, beach, single people, typical food, and we are among those who best receive tourists, confirm! Come visit us and feel at ho
access=>We are always available to help guests. The house is fully available to guests. We are always ready to assist guests. when possible we pick the guests at the airport.  This service transfer have a cost per person. We will also have service "meal at home" with a diverse menu and the taste of each. Enjoy the moment!
interaction=>Cot - 10 € / night Dog - € 7,5 / night
house_rules=>Make the house your home...
property_type=>House
room_type=>Entire home/apt
bed_type=>Real Bed
accommodates=>8
bedrooms=>3.0
beds=>5.0
number_of_reviews=>51
bathrooms=>1.0
amenities=>["TV", "Cable TV", "Wifi", "Kitchen", "Paid parking off premises", "Smoking allowed", "Pets allowed", "Buzzer/wireless intercom", "Heating", "Family/kid friendly", "Washer", "First aid kit", "Fire extinguisher", "Essentials", "Hangers", "Hair dryer", "Iron", "Pack \u2019n Play/travel crib", "Room-darkening shades", "Hot water", "Bed linens", "Extra pillows and blankets", "Microwave", "Coffee maker", "Refrigerator", "Dishwasher", "Dishes and silverware", "Cooking basics", "Oven", "Stove", "Cleaning before checkout", "Waterfront"]
price=>80
extra_people=>15
images=>{"thumbnail_url": "", "medium_url": "", "picture_url": "https://a0.muscache.com/im/pictures/e83e702f-ef49-40fb-8fa0-6512d7e26e9b.jpg?aki_policy=large", "xl_picture_url": ""}
address=>{"street": "Porto, Porto, Portugal", "suburb": "", "government_area": "Cedofeita, Ildefonso, S\u00e9, Miragaia, Nicolau, Vit\u00f3ria", "market": "Porto", "country": "Portugal", "country_code": "PT", "location": {"type": "Point", "coordinates": [-8.61308, 41.1413], "is_location_exact": false}}
review_scores=>{"review_scores_accuracy": 9, "review_scores_cleanliness": 9, "review_scores_checkin": 10, "review_scores_communication": 10, "review_scores_location": 10, "review_scores_value": 9, "review_scores_rating": 89}
weekly_price=>None
monthly_price=>None
-----
Content: Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character. Privileged views of the Douro River and Ribeira square, our apartment offers the perfect conditions to discover the history and the charm of Porto. Apartment comfortable, charming, romantic and cozy in the heart of Ribeira. Within walking distance of all the most emblematic places of the city of Porto. The apartment is fully equipped to host 8 people, with cooker, oven, washing machine, dishwasher, microwave, coffee machine (Nespresso) and kettle. The apartment is located in a very typical area of the city that allows to cross with the most picturesque population of the city, welcoming, genuine and happy people that fills the streets with his outspoken speech and contagious with your sincere generosity, wrapped in a only parochial spirit. We are always available to help guests

Embedding Generation

[24]
Embedding Progress: 100%|██████████| 4010/4010 [24:59<00:00,  2.67node/s]
Embedding process completed!

MongoDB Vector Database and Connection Setup

MongoDB acts as both an operational and a vector database for the RAG system. MongoDB Atlas specifically provides a database solution that efficiently stores, queries and retrieves vector embeddings.

Creating a database and collection within MongoDB is made simple with MongoDB Atlas.

  1. First, register for a MongoDB Atlas account. For existing users, sign into MongoDB Atlas.
  2. Follow the instructions. Select Atlas UI as the procedure to deploy your first cluster.
  3. Create the database: airbnb.
  4. Within the database airbnb, create the collection ‘listings_reviews’.
  5. Create a vector search index named vector_index for the ‘listings_reviews’ collection. This index enables the RAG application to retrieve records as additional context to supplement user queries via vector search. Below is the JSON definition of the data collection vector search index.

Your vector search index created on MongoDB Atlas should look like below:

	{
  "fields": [
    {
      "numDimensions": 256,
      "path": "embedding",
      "similarity": "cosine",
      "type": "vector"
    }
  ]
}


Follow MongoDB’s steps to get the connection string from the Atlas UI. After setting up the database and obtaining the Atlas cluster connection URI, securely store the URI within your development environment.

This guide uses Google Colab, which offers a feature for securely storing environment secrets. These secrets can then be accessed within the development environment. Specifically, the line mongo_uri = userdata.get('MONGO_URI') retrieves the URI from the secure storage.

[25]
[26]
Connection to MongoDB successful
[27]
DeleteResult({'n': 0, 'electionId': ObjectId('7fffffff0000000000000029'), 'opTime': {'ts': Timestamp(1719315234, 1), 't': 41}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1719315234, 1), 'signature': {'hash': b"\x11T\xcc'\xfd\xd5\x90@\x0f\xac%Z\x13\xc2\xf9t4B:h", 'keyId': 7320226449804230662}}, 'operationTime': Timestamp(1719315234, 1)}, acknowledged=True)

Data Ingestion

[ ]
[ ]

Creating Retriver Tool for Agent

[30]

AI Agent Creation

[31]
[32]
Added user message to memory: Tell me the best listing for a place in New York
=== LLM Response ===
Certainly! To provide you with the best listing for a place in New York, I'll need to use the knowledge base tool to gather information about Airbnb listings in New York. Let me do that for you.
=== Calling Function ===
Calling function: knowledge_base with args: {"input": "What is the best Airbnb listing in New York City? Please provide details about its location, amenities, price, and guest reviews."}
=== Function Output ===
While it's difficult to definitively say which is the "best" Airbnb listing in New York City, as preferences can vary, one standout option appears to be the newly renovated studio apartment in Midtown East Manhattan. 

This luxurious studio is located in the heart of Manhattan, in a safe residential area that's very close to many attractions. It's just a 7-minute walk to the subway and 2 blocks from the United Nations.

The apartment was completely renovated in 2016 and features modern amenities including:
- A new kitchen with stainless steel appliances
- A new bathroom with a rain shower
- Hardwood floors
- A queen-size pillow top mattress
- Full cable TV and WiFi
- Air conditioning
- A HEPA air purifier for improved air quality

It can accommodate up to 4 guests with its queen bed and a double sofa bed. The price is $239 per night, with a $15 charge for each additional guest beyond the first two.

Guest reviews for this property are exceptional. It has received perfect 10/10 scores for accuracy, cleanliness, check-in, communication, location, and value. The overall rating is an impressive 98 out of 100, based on 119 reviews.

This apartment seems to offer a combination of prime location, modern amenities, and consistently positive guest experiences, making it a top contender for one of the best Airbnb listings in New York City.
=== LLM Response ===
Based on the information from our knowledge base, I can tell you about one of the best Airbnb listings in New York City. While "best" can be subjective depending on individual preferences, this particular listing stands out due to its location, amenities, price, and excellent guest reviews.

The standout option is a newly renovated studio apartment in Midtown East Manhattan. Here are the key details:

Location:
- Heart of Manhattan
- Safe residential area
- Close to many attractions
- 7-minute walk to the subway
- 2 blocks from the United Nations

Amenities:
- Completely renovated in 2016
- New kitchen with stainless steel appliances
- New bathroom with a rain shower
- Hardwood floors
- Queen-size pillow top mattress
- Full cable TV and WiFi
- Air conditioning
- HEPA air purifier

Capacity and Price:
- Accommodates up to 4 guests
- Queen bed and a double sofa bed
- $239 per night
- $15 charge for each additional guest beyond the first two

Guest Reviews:
- Perfect 10/10 scores for accuracy, cleanliness, check-in, communication, location, and value
- Overall rating of 98 out of 100 based on 119 reviews

This listing offers a great combination of a prime location in Manhattan, modern amenities due to its recent renovation, and consistently positive guest experiences. The perfect scores across various categories and the high overall rating suggest that guests have been very satisfied with their stays.

The price point of $239 per night is competitive for a well-appointed studio in such a central location in New York City, especially considering the high-quality amenities and positive reviews.

Would you like more information about this listing or are you interested in exploring other options in New York City?
Based on the information from our knowledge base, I can tell you about one of the best Airbnb listings in New York City. While "best" can be subjective depending on individual preferences, this particular listing stands out due to its location, amenities, price, and excellent guest reviews.

The standout option is a newly renovated studio apartment in Midtown East Manhattan. Here are the key details:

Location:
- Heart of Manhattan
- Safe residential area
- Close to many attractions
- 7-minute walk to the subway
- 2 blocks from the United Nations

Amenities:
- Completely renovated in 2016
- New kitchen with stainless steel appliances
- New bathroom with a rain shower
- Hardwood floors
- Queen-size pillow top mattress
- Full cable TV and WiFi
- Air conditioning
- HEPA air purifier

Capacity and Price:
- Accommodates up to 4 guests
- Queen bed and a double sofa bed
- $239 per night
- $15 charge for each additional guest beyond the first two

Guest Reviews:
- Perfect 10/10 scores for accuracy, cleanliness, check-in, communication, location, and value
- Overall rating of 98 out of 100 based on 119 reviews

This listing offers a great combination of a prime location in Manhattan, modern amenities due to its recent renovation, and consistently positive guest experiences. The perfect scores across various categories and the high overall rating suggest that guests have been very satisfied with their stays.

The price point of $239 per night is competitive for a well-appointed studio in such a central location in New York City, especially considering the high-quality amenities and positive reviews.

Would you like more information about this listing or are you interested in exploring other options in New York City?
[ ]