Hello I am working on a semantic search application using openai. When I am loading dataset …trigger does not take place …I mean a new field embedding does not generate in dataset which results an empty array when I trigger the search function. I will appriciate if anyone helps me. Here is my code -
Code to trigger:-
exports = async function(changeEvent) {
// Gets the full document that was changed
const changedDocument = changeEvent.fullDocument;
const url = 'https://api.openai.com/v1/embeddings';
console.log(`{"Processing documents with id" :${changedDocument._id}`})
// OpenAI API to change
const openai_key = context.values.get("openai_value");
try {
// HTTP call to OpenAI API
let response = await context.http.post({
url: url,
headers: {
'Authorization': [`Bearer ${openai_key}`],
'Content-Type': ['application/json']
},
body: JSON.stringify({
input: changedDocument.name,
model: "text-embedding-ada-002"
})
});
// Parse the JSON response
let responseData = EJSON.parse(response.body.text());
if(response.statusCode === 200) {
console.log("Successfully received embedding.");
const responseEmbedding = responseData.data[0].embedding;
// MongoDB Atlas Cluster / Database / Collection
const collection = context.services.get("Cluster0").db("myblog").collection("blogs");
// Update the document in MongoDB.
const result = await collection.updateOne(
{ _id: changedDocument._id },
// Adds the embedding field
{ $set: { name_embedding: responseEmbedding }}
);
if(result.modifiedCount === 1) {
console.log("Document successfully Updated.");
} else {
console.log("Failed to modify document.");
}
} else {
console.log(`Failed embedding with code: ${response.statusCode}`);
}
} catch(err) {
console.error(err);
}
};
to create search index:
{
"mappings": {
"dynamic": true,
"fields": {
"name_embedding": {
"dimensions": 1536,
"similarity": "cosine",
"type": "knnVector"
}
}
}
}
nodejs code:-
open ai embeding:-
async function openaiEmbedding(query) {
// OpenAI Embeddings
const url = 'https://api.openai.com/v1/embeddings';
const openai_key = process.env.OPENAI_KEY; // Replace with your OpenAI key.
// OpenAI embeddings APIs
let response = await axios.post(url, {
input: query,
model: "text-embedding-ada-002"
}, {
headers: {
'Authorization': `Bearer ${openai_key}`,
'Content-Type': 'application/json'
}
});
if(response.status === 200) {
// console.log(response.data.data[0].embedding)
return response.data.data[0].embedding;
} else {
throw new Error(`Failed to get embedding with code: ${response.status}`);
}
}
Get route:-
app.get("/vectorSearch/:query", async (req,res)=>{
try {
const embedding = await openaiEmbedding(req.params.query);
const uri = process.env.MONGODB_URI
const client = new MongoClient(uri);
const db = client.db("myblog");
const documents = await db.collection("blogs").aggregate([
{
"$search": {
"index": "productindex",
"knnBeta": {
"vector": embedding,
"path": "name_embedding",
"k": 5
}
}
},
{ $unset: "embedding" }
]).toArray();
console.log(documents);
res.send(documents);
} catch(err) {
console.error(err);
throw new Error(`Error`);
}
});
Error:
(InvalidResumeToken) Attempting to resume a change stream using ‘resumeAfter’ is not allowed from an invalidate notification
Logs:
[ "Processing documents with id :undefined" ]
Error:
TypeError: Cannot access member 'name' of undefined
dataset is as below:
[{
name:"Rome, Italy and beyond – from someone who actually lives here. You will find hundreds of personally vetted recommendations for what to do in Rome",
Location:"Rome",
Attractions:[
"Colosseum and Capitoline Museums",
" Vatican: Museums & Sistine Chapel Entrance Ticket",
" Rome:Appian Way E-bike Tour with Catacombs, Aqueducts & Food"
],
travelAgent:"David",
food:[
"BreakFast",
"Lunch",
"Dinner"
]
}]