Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created April 17, 2024 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiangenco/3e23925885e3127f2c1775871b8f52f1 to your computer and use it in GitHub Desktop.
Save christiangenco/3e23925885e3127f2c1775871b8f52f1 to your computer and use it in GitHub Desktop.
Javascript + OpenAI Embedding Example
import "dotenv/config";
import { dot, norm, add, subtract } from "mathjs";
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_KEY });
function cosineSimilarity(array1, array2) {
const dotProduct = dot(array1, array2);
const normA = norm(array1);
const normB = norm(array2);
const cosineSimilarity = dotProduct / (normA * normB);
return cosineSimilarity;
}
function cosineDifference(array1, array2) {
return 1 - cosineSimilarity(array1, array2);
}
function sortBySimilarity(embedding, wordsWithEmbeddings) {
return wordsWithEmbeddings
.map((wordWithEmbedding) => {
const similarity = cosineSimilarity(
embedding,
wordWithEmbedding.embedding,
);
return { word: wordWithEmbedding.word, similarity };
})
.sort((a, b) => b.similarity - a.similarity);
}
async function main() {
const words = ["King", "Queen", "man", "woman", "dog", "wolf", "pet"];
// fetch embeddings for each word
const wordsWithEmbeddings = await Promise.all(
words.map(async (word) => {
const { data } = await openai.embeddings.create({
model: "text-embedding-3-large",
input: word,
encoding_format: "float",
});
return { word, embedding: data[0].embedding };
}),
);
const embeddingsByWord = wordsWithEmbeddings.reduce(
(acc, wordWithEmbedding) => {
acc[wordWithEmbedding.word] = wordWithEmbedding.embedding;
return acc;
},
{},
);
const kingMinusManPlusWoman = add(
subtract(embeddingsByWord["King"], embeddingsByWord["man"]),
embeddingsByWord["woman"],
);
console.log(
"king + man - woman",
sortBySimilarity(kingMinusManPlusWoman, wordsWithEmbeddings),
);
const wolfPlusPet = add(embeddingsByWord["wolf"], embeddingsByWord["pet"]);
console.log("wolf + pet", sortBySimilarity(wolfPlusPet, wordsWithEmbeddings));
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment