This repository is deprecated and no longer actively maintained. It contains outdated code examples or practices that do not align with current MongoDB best practices. While the repository remains accessible for reference purposes, we strongly discourage its use in production environments. Users should be aware that this repository will not receive any further updates, bug fixes, or security patches. This code may expose you to security vulnerabilities, compatibility issues with current MongoDB versions, and potential performance problems. Any implementation based on this repository is at the user's own risk. For up-to-date resources, please refer to the MongoDB Developer Center.
Personalized Event Recommendation System
Jupyter Notebook Outline for Session Recommendation System
- Import Necessary Libraries
import pymongo
from pymongo import MongoClient
import numpy as np
from sentence_transformers import SentenceTransformer
import pandas as pd
- Connect to MongoDB Atlas
client = MongoClient("your_atlas_connection_string")
db = client.your_database
sessions_collection = db.sessions
users_collection = db.users
- Load and Preview Data
# Assuming sessions and users data are already in collections
# Fetch a sample to understand what the data looks like
sample_sessions = sessions_collection.find().limit(5)
print("Sample Sessions:")
for session in sample_sessions:
print(session)
sample_users = users_collection.find().limit(5)
print("\nSample Users:")
for user in sample_users:
print(user)
- Generate and Store Vector Embeddings for Sessions
model = SentenceTransformer('all-MiniLM-L6-v2')
# Function to generate embeddings
def generate_embeddings(text):
return model.encode(text)
# Update session documents with embeddings
for session in sessions_collection.find():
embedding = generate_embeddings(session['description'])
sessions_collection.update_one({'_id': session['_id']}, {'$set': {'embedding': embedding.tolist()}})
-
Setup Vector Search Index in MongoDB This step will be done through the MongoDB Atlas interface, so you can describe how to set up the index here.
-
Define a Function to Recommend Sessions
def recommend_sessions(user_id):
user = users_collection.find_one({'_id': user_id})
user_embedding = user['profile_embedding']
query = {
"$searchBeta": {
"vector": {
"path": "embedding",
"query": user_embedding,
"cosine": {
"radius": 0.5,
"origin": user_embedding
}
}
}
}
recommended_sessions = sessions_collection.find(query)
return pd.DataFrame(recommended_sessions)
# Example usage
user_id = some_user_id # Substitute with an actual user ID
recommendations = recommend_sessions(user_id)
print(recommendations)
- Display Recommendations for a Sample User
# Assuming you have user ID
sample_user_id = "user123"
recommended_sessions = recommend_sessions(sample_user_id)
print("Recommended Sessions for User ID -", sample_user_id)
print(recommended_sessions[['title', 'speaker', 'description']])