liveBook · Manning

Cosine similarity

As explained in Build Python Web Apps with Streamlit

Cosine similarity is a metric that compares vectors by measuring the cosine of the angle between them.

Cosine similarity compares vectors by measuring the angle between them. In retrieval systems, it helps identify embeddings whose encoded meanings are similar, even when each vector contains hundreds or thousands of dimensions. section 10.5.1

Use cosine similarity to connect semantic representations with searchable knowledge:

  • Represent text as embeddings produced by a text embedding model. section 10.5.1
  • Store embeddings in a vector database that can compare vectors and find nearby entries. section 10.5.1
  • Choose cosine as the index metric when configuring a Pinecone vector store for retrieval-augmented generation. section 10.5.2

From movie attributes to support retrieval

A simple movie example places films in a space using features such as comedic value and explosions per hour. Real text embeddings use far more dimensions, whose meanings are not human-readable. A support chatbot can compare a question's embedding with stored support-article embeddings and retrieve the closest content. section 10.5.1 section 10.5.2

Figure 10.15 Converting movies into vectors and plotting them on a chart.

Inspect the vector-space illustration to see how items become points that can be compared for similarity. section 10.5.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines angle-based similarity and applies it to vector indexes, but does not derive the mathematical formula.
Examples
Uses plotted movie vectors and support-article embeddings as concrete similarity-search examples.
What you can do after
Operate/configure a Pinecone index using cosine as its vector similarity metric.
How it connects
Connects text embeddings, vector databases, Pinecone, and retrieval-augmented generation.

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 Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.