-
Notifications
You must be signed in to change notification settings - Fork 15
Update mongodb_connector.py #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akgom
wants to merge
6
commits into
main
Choose a base branch
from
akgom-patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ec2c3e
Update mongodb_connector.py
akgom 3a49a0c
Update mongodb_connector.py
akgom 6ff5645
Update mongodb_connector.py
akgom d58b9f6
Update mongodb_connector.py
akgom dea0dd5
Update mongodb_connector.py
akgom 4573121
Update mongodb_connector.py
akgom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,86 @@ | ||
| import pymongo as pm | ||
| from nomic import AtlasProject | ||
| import nomic | ||
| from nomic import AtlasDataset | ||
| from sentence_transformers import SentenceTransformer | ||
| from pymongo.mongo_client import MongoClient | ||
| import numpy as np | ||
| import pandas as pd | ||
| from pathlib import Path | ||
| import nomic | ||
|
|
||
| # replace with your mongodb connect string / cert | ||
| client = pm.MongoClient('mongodb+srv://cluster0.l3jhqfs.mongodb.net/' | ||
| '?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority', | ||
| tls=True, | ||
| tlsCertificateKeyFile='mongocert.pem') | ||
| # MongoDB connection string | ||
| client = MongoClient('mongodb+srv://<USERNAME>:<PASSWORD>@<APPNAME>.1fy6rp1.mongodb.net/?appName=<APPNAME>', | ||
| tls=True) | ||
|
|
||
| collection = client.testdb.testcoll | ||
| # Replace with your actual API key | ||
| nomic.login('YOUR_KEY_HERE') | ||
|
|
||
| # MongoDB collection | ||
| collection = client.sample_mflix.comments | ||
|
|
||
| # Delete current content of collection | ||
| collection.delete_many({}) | ||
|
|
||
| # Load embedding data into mongodb | ||
| # Load embedding data into MongoDB from parquet file | ||
| mongo_so = pd.read_parquet(Path.cwd() / 'data' / 'mongo-so.parquet') | ||
|
|
||
| # Initialize SentenceTransformer model | ||
| model = SentenceTransformer('all-MiniLM-L6-v2') | ||
| title_embeds = model.encode(mongo_so['title']) | ||
| mso_te = mongo_so.assign(title_embedding=list(title_embeds)) | ||
|
|
||
| data = list(r._asdict() for r in mso_te.itertuples()) | ||
| for d in data: | ||
| del d['Index'] | ||
| d['title_embedding'] = d['title_embedding'].tolist() | ||
| data[0] | ||
| collection.insert_many(data) | ||
|
|
||
| # Read a mongodb collection with embeddings in it and map it: | ||
| project = AtlasProject( | ||
| name='MongoDB Stack Overflow Questions', | ||
| unique_id_field='mongo_id', | ||
| reset_project_if_exists=True, | ||
| is_public=True, | ||
| modality='embedding', | ||
| ) | ||
| # Encode titles using SentenceTransformer | ||
| title_embeds = model.encode(mongo_so['title'].tolist()) | ||
| mongo_so['title_embedding'] = list(title_embeds) | ||
|
|
||
| # Convert DataFrame to list of dictionaries for MongoDB insertion | ||
| data = mongo_so.to_dict(orient='records') | ||
|
|
||
| # Insert data into MongoDB collection | ||
| collection.insert_many(data) | ||
|
|
||
| # Fetch all items from MongoDB collection | ||
| all_items = list(collection.find()) | ||
|
|
||
| # Extract embeddings into numpy array | ||
| embs = np.array([d['title_embedding'] for d in all_items]) | ||
|
|
||
| # Remove 'title_embedding' field from each item, and convert '_id' to string | ||
| for d in all_items: | ||
| d['mongo_id'] = str(d['_id']) | ||
| d['_id'] = str(d['_id']) | ||
| del d['title_embedding'] | ||
| del d['_id'] | ||
|
|
||
| project.add_embeddings(all_items, embs) | ||
| # Create an AtlasDataset instance | ||
| dataset = AtlasDataset( | ||
| identifier='sample-mflix-comments', # Unique identifier for your dataset | ||
| description='MongoDB Movie Comments', | ||
| unique_id_field='_id', | ||
| is_public=True | ||
| ) | ||
|
|
||
| # Add data and embeddings to the AtlasDataset | ||
| dataset.add_data(data=all_items, embeddings=embs) | ||
|
|
||
| project.rebuild_maps() | ||
| project.create_index( | ||
| name='MongoDB Stack Overflow Questions', | ||
| topic_label_field='body', | ||
| build_topic_model=True, | ||
| # Create an index and map | ||
| dataset.create_index( | ||
| name='MongoDB Movie Comments', | ||
| indexed_field='body', # Replace with your topic label field | ||
| modality='embedding', | ||
| topic_model={ | ||
| 'build_topic_model': True, | ||
| 'topic_label_field': 'body' # Replace with the field used for topic labeling | ||
| }, | ||
| duplicate_detection={ | ||
| 'tag_duplicates': True, | ||
| 'duplicate_cutoff': 0.95 # Adjust as needed | ||
| }, | ||
| projection={ | ||
| 'n_neighbors': 15, # Example value, adjust as needed | ||
| 'n_epochs': 100, # Example value, adjust as needed | ||
| 'model': 'nomic-project-v2', | ||
| 'local_neighborhood_size': 30, | ||
| 'spread': 1.0, | ||
| 'rho': 0.5 | ||
| }, | ||
| embedding_model='NomicEmbed' # Specify the embedding model if needed | ||
| ) | ||
|
|
||
| print(project) | ||
| # Print the dataset to confirm | ||
| print(dataset) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.