Skip to content

Commit

Permalink
feat: Updated API/database.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Mar 13, 2024
1 parent 91e83d1 commit d6366eb
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions API/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,31 @@ def find_one_and_delete(self, collection, query):

def update_one(self, collection, query, update):
return self.db[collection].update_one(query, update)
def find_similar_vectors(self, collection, embedding_vector, n):
"""
Find the top n most similar vectors in the database to the given embedding_vector.
This method uses the Euclidean distance for similarity measure.
:param collection: The MongoDB collection to search within.
:param embedding_vector: The embedding vector to find similar vectors for.
:param n: The number of top similar vectors to return.
:return: The top n most similar vectors from the MongoDB database.
"""
pipeline = [
{
"$addFields": {
"distance": {
"$sqrt": {
"$reduce": {
"input": {"$zip": {"inputs": ["$vector", embedding_vector]}},
"initialValue": 0,
"in": {"$add": ["$$value", {"$pow": [{"$subtract": ["$$this.0", "$$this.1"]}, 2]}]}
}
}
}
}
},
{"$sort": {"distance": 1}},
{"$limit": n}
]
return list(self.db[collection].aggregate(pipeline))

0 comments on commit d6366eb

Please sign in to comment.