liveBook · Manning

Cosine distance

As explained in Software Testing with Generative AI

A distance measure derived from the cosine of the angle between vectors, where a lower value indicates greater directional similarity.

Cosine distance ranks documents by directional similarity to a query. Lower scores indicate greater similarity, allowing a retrieval-augmented generation prototype to select context for a testing prompt. section 11.2.1

Selecting relevant testing guidance

A Java method applies CosineDistance to each document, tracks the lowest score, and returns that document. Given a query such as “I want test ideas,” the selected user story becomes relevant prompt context. section 11.2.1

How Software Testing with Generative AI covers it

How deep it goes
Demonstrates implementation and selection logic but defers the mathematical workings and production-level algorithm complexity.
Examples
Uses user stories, document corpora, the query “I want test ideas,” and a lowest-score Java matching method.
What it assumes
Assumes Java project and document-query inputs while teaching the basic retrieval loop from similarity scoring to context selection.
What you can do after
Apply it via Apache Commons Text to select the closest document for a generated prompt.
How it connects
Connects retrieval-augmented generation with similarity algorithms, Levenshtein distance, Jaccard similarity, and prompt engineering.

Also explained in

Compare how 6 books explain cosine distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Testing with Generative AI. The section links point to the parts of the book it draws on.