const input = "Your message"; //the message sent
const response = await openai.createEmbedding({
model: "text-embedding-ada-002",
input: input,
});
const array = response.data.data[0].embedding;
const passage = "Avocado, also known as Persea americana, is a fruit that is highly valued for its nutritional and health benefits. Scientifically, it belongs to the Lauraceae family and is native to Mexico and Central America. The avocado tree can grow up to 20 meters tall, and its fruit is rich in monounsaturated fats, fiber, vitamins, and minerals.\n\nStudies have shown that consuming avocados can have positive effects on cardiovascular health by reducing cholesterol levels and inflammation. The fruit also contains antioxidants that may help prevent cancer and other chronic diseases.\n\nAvocado is also used in the cosmetic industry, as it is rich in essential fatty acids and vitamins that are beneficial for skin and hair. Additionally, avocado oil is used in cooking and as a salad dressing due to its high smoke point and mild flavor.\n\nOverall, the avocado is a versatile and nutritious fruit that has gained increasing popularity in recent years due to its health benefits and culinary uses.";
const sentenceTokenizer = new natural.SentenceTokenizer();
const sentences = sentenceTokenizer.tokenize(passage);
let sentenceInfo;
let sentenceArray = [];
let success = false;
for (let i = 0; i <= sentences.length - 1; i++){
while (!success){
try{
sentenceInfo = await openai.createEmbedding({
model: "text-embedding-ada-002",
input: sentences[i],
});
sentenceArray[i]= sentenceInfo.data.data[0].embedding;
success = true;
}catch(error){console.log(error);}
}
success = false;
}
let output = sentences[0];
let sim = similarity(array,sentenceArray[0]);
for (let i = 1; i <= sentences.length - 1; i++){
if (similarity(array,sentenceArray[i]) > sim){
sim = similarity(array,sentenceArray[i]);
output = sentences[i];
}
}
message.reply(`${output}`);
busy = false;
})();