-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hookdeck/chore/refactor
Refactor to follow better practice
- Loading branch information
Showing
8 changed files
with
116 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from pymongo.mongo_client import MongoClient | ||
from pymongo.server_api import ServerApi | ||
|
||
from config import Config | ||
|
||
|
||
class Database: | ||
|
||
def __init__(self): | ||
MONGODB_CONNECTION_URI = Config.MONGODB_CONNECTION_URI | ||
|
||
self.client = MongoClient(MONGODB_CONNECTION_URI, server_api=ServerApi("1")) | ||
|
||
self.client.admin.command("ping") | ||
|
||
def get_client(self): | ||
return self.client | ||
|
||
def get_collection(self): | ||
return self.client.get_database(Config.DB_NAME).get_collection( | ||
Config.COLLECTION_NAME | ||
) |
File renamed without changes.
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Inspiration https://www.mongodb.com/developer/products/atlas/how-use-cohere-embeddings-rerank-modules-mongodb-atlas/#programmatically-create-vector-search-and-full-text-search-index | ||
|
||
from allthethings.mongo import Database | ||
from pymongo.operations import SearchIndexModel | ||
|
||
database = Database() | ||
collection = database.get_collection() | ||
|
||
|
||
def create_or_update_search_index(index_name, index_definition, index_type): | ||
indexes = list(collection.list_search_indexes(index_name)) | ||
if len(indexes) == 0: | ||
print(f'Creating search index: "{index_name}"') | ||
index_model = SearchIndexModel( | ||
definition=index_definition, | ||
name=index_name, | ||
type=index_type, | ||
) | ||
result = collection.create_search_index(model=index_model) | ||
|
||
else: | ||
print(f'Search index "{index_name}" already exists. Updating.') | ||
result = collection.update_search_index( | ||
name=index_name, definition=index_definition | ||
) | ||
|
||
return result | ||
|
||
|
||
vector_result = create_or_update_search_index( | ||
"vector_index", | ||
{ | ||
"fields": [ | ||
{ | ||
"type": "vector", | ||
"path": "embedding", | ||
"numDimensions": 768, | ||
"similarity": "euclidean", | ||
} | ||
] | ||
}, | ||
"vectorSearch", | ||
) | ||
print(vector_result) | ||
|
||
index_result = create_or_update_search_index( | ||
"replicate_by_embedding_id_index", | ||
{ | ||
"mappings": {"dynamic": True}, | ||
"fields": [ | ||
{ | ||
"type": "string", | ||
"path": "replicate_embedding_id", | ||
} | ||
], | ||
}, | ||
"search", | ||
) | ||
print(index_result) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.