This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
// Import openai and numpy-parser libraries
var openai = require('openai');
var np = require('numpy-parser');
// Export a function called endpoint
exports.endpoint = async function(request, response) {
// Get the input strings and OpenAI secret key from the payload
var string1 = request.body.string1;
var string2 = request.body.string2;
var openai_key = request.body.openai_key;
// Set the OpenAI secret key as an environment variable
process.env.OPENAI_KEY = openai_key;
// Create an embedding object with your input strings
var resp = await openai.Embedding.create(
input=[string1, string2],
engine="text-similarity-davinci-001");
// Extract the embeddings from the response
var embedding_a = resp['data'][0]['embedding'];
var embedding_b = resp['data'][1]['embedding'];
// Calculate the dot product of the embeddings
var similarity_score = np.dot(embedding_a, embedding_b);
// Return the similarity score as JSON
response.end(JSON.stringify({similarity: similarity_score}));
}