liveBook · Manning

Cosine similarity

As explained in Build AI-Enhanced Web Apps

Cosine similarity is a metric that measures the cosine of the angle between two vectors, with larger values indicating greater directional similarity.

Cosine similarity compares the direction of two vectors. A higher score, closer to 1, indicates greater similarity. In an application, it can rank stored embeddings against a user’s query embedding; in a test, it can provide a similarity score for generated and expected text instead of requiring an exact match. section 5.4.2 section 5.1.3

For retrieval, embed stored content and the user query with the same model. Compare the query embedding with each stored embedding, keep the highest-scoring match, and return its associated answer. The same loop could retain several close matches instead of only one. section 5.4.3 section 5.4.2

Listing 5.11 Performing similarity search on the embeddings
let maxSimilarity = -1;    #1
let mostRelevantQuestion = "";    #2

for (const [question, storedEmbedding] of Object.entries(embeddingDB)) {
  const similarity = 
cosineSimilarity(queryEmbedding, storedEmbedding);    #3
  if (similarity > maxSimilarity) {    #4
    maxSimilarity = similarity;
    mostRelevantQuestion = question;
  }
}

The JavaScript loop calls cosineSimilarity for each stored question embedding and tracks the highest score. The listing uses the function but does not define its implementation. section 5.4.3

Testing a generated response

A test uses stringComparison.cosine.similarity to compare a mocked AI response with expected text, then asserts that the score exceeds 0.7. The threshold is adjustable. Mocking makes the test faster and more controlled, but does not check how the live model will behave. section 5.1.3

Listing 5.3 Testing semantic similarity scoring
import { generateText } from "ai";
import stringComparison from 'string-comparison'   #1

function semanticSimilarity(text1, text2) {
  let cos = stringComparison.cosine
  return cos.similarity(text1, text2);
}

test("generated text is semantically similar to expected output", async () => {
  generateText.mockResolvedValue(
    "AI greatly influences modern life in many ways."
  );

  const expected = "AI has a significant impact on 
modern society.";   #2
  const generated = await generateText(
    "Describe the role of AI in today's world."
  );

  const similarity = 
semanticSimilarity(expected, generated);   #3
  expect(similarity).toBeGreaterThan(0.7);   #4
});

The listing shows the text-comparison call and its threshold assertion; it is a separate use from comparing stored embedding vectors for retrieval. section 5.1.3

How Build AI-Enhanced Web Apps covers it

How deep it goes
Defines the score’s directional meaning and walks through matching and threshold checks, but does not implement the embedding cosineSimilarity function.
Examples
Uses an IT support question-and-answer lookup and a mocked AI-response test with a 0.7 similarity threshold.
What it assumes
Introduces embeddings and their retrieval workflow while using JavaScript tests and calls to embedding and similarity functions.
What you can do after
Apply it via JavaScript examples to select a knowledge-base match or check whether generated text exceeds a chosen similarity threshold.
How it connects
Connects embeddings and similarity search with information retrieval, and links generated-text testing with semantic similarity scoring.

Also explained in

Compare how 14 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.