import { Isaacus } from 'isaacus';
// Define a function to compute the dot product of two vectors.
function dot(a, b) {
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += a[i] * b[i];
}
return sum;
}
// Create an Isaacus API client.
// NOTE see https://docs.isaacus.com/quickstart to learn how to get an API key.
const client = new Isaacus({ apiKey: "PASTE_YOUR_API_KEY_HERE" });
// Download the GitHub terms of service as an example.
const tos = await client.get("https://examples.isaacus.com/github-tos.md");
// Embed the terms of service.
const document_response = await client.embeddings.create({
model: "kanon-2-embedder",
texts: tos, // You can pass a single text or an array of texts here.
task: "retrieval/document",
// dimensions: 1792, // You may optionally wish to specify a lower dimension.
});
// Embed our search queries (relevant + irrelevant).
const query_responses = await client.embeddings.create({
model: "kanon-2-embedder",
texts: [
"What are GitHub's billing policies?", // This is a relevant query.
"What are Microsoft's billing policies?", // This is an irrelevant query.
],
task: "retrieval/query",
// dimensions: 1792, // You may optionally wish to specify a lower dimension.
});
// Unpack the embeddings.
const document_embedding = document_response.embeddings[0].embedding;
const query_embeddings = query_responses.embeddings;
const relevant_query_embedding = query_embeddings[0].embedding;
const irrelevant_query_embedding = query_embeddings[1].embedding;
// Compute the similarity between the queries and the document.
const relevant_similarity = dot(relevant_query_embedding, document_embedding);
const irrelevant_similarity = dot(irrelevant_query_embedding, document_embedding);
// Log the results.
console.log(`Similarity of relevant query to the document: ${(relevant_similarity * 100).toFixed(2)}`);
console.log(`Similarity of irrelevant query to the document: ${(irrelevant_similarity * 100).toFixed(2)}`);