-
Notifications
You must be signed in to change notification settings - Fork 0
/
change-trigger.js
33 lines (26 loc) · 1.2 KB
/
change-trigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#This trigger demonstrates how you can automatically update document embeddings
#whenever a new document is inserted or modified in a specific collection.
exports = function(changeEvent) {
const doc = changeEvent.fullDocument;
// Check if the document is newly inserted or modified
if (changeEvent.operationType === "insert" || changeEvent.operationType === "update") {
const transformedDoc = {
_id: doc._id,
content: doc.content,
embedding: computeEmbedding(doc.content)
};
// Update the document with the computed embedding
const updatedDoc = context.services.get("mongodb-atlas").db("yourDatabase").collection("yourCollection").updateOne(
{ _id: doc._id },
{ $set: transformedDoc }
);
console.log("Document embedding updated:", updatedDoc);
}
};
function computeEmbedding(content) {
// Use Sentence Transformers to compute the embedding for the content
// Replace this with the actual code for computing the sentence embedding using Sentence Transformers
// Make sure you have the necessary dependencies and libraries set up for Sentence Transformers
const sentenceEmbedding = ... // Compute the sentence embedding
return sentenceEmbedding;
}