LangChain Cosine Similarity Guide - Calculate Vector Similarities
Posted: Nov 10, 2024.
When working with vector embeddings and semantic similarity in LangChain applications, the cosine similarity calculation is an essential tool. This guide will show you how to effectively use the cosine_similarity utility function from LangChain.
What is cosine_similarity?
The cosine_similarity function calculates the row-wise cosine similarity between two equal-width matrices. Cosine similarity is a measure that calculates the cosine of the angle between two vectors, providing a similarity score between -1 and 1, where:
- 1 means the vectors are identical
- 0 means they are orthogonal (perpendicular)
- -1 means they are opposite
This is particularly useful when working with text embeddings, semantic search, or routing between different components based on similarity.
Reference
Parameter | Type | Description |
---|---|---|
X | List[List[float]] | List[ndarray] | ndarray | First matrix of vectors |
Y | List[List[float]] | List[ndarray] | ndarray | Second matrix of vectors to compare against |
Returns | ndarray | Matrix of cosine similarity scores |
How to use cosine_similarity
Let's look at some practical examples of using the cosine_similarity function in LangChain.
Basic Usage with Numeric Vectors
Using with Text Embeddings
A common use case is calculating similarity between text embeddings:
Semantic Routing Example
Here's a practical example of using cosine similarity for routing queries to different templates based on their semantic similarity:
Batch Processing
You can also calculate similarities for multiple vectors at once:
This utility is particularly useful when building semantic search applications, recommendation systems, or implementing routing logic based on content similarity in your LangChain applications.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.
LangChain DocsJoin 10,000+ subscribers
Every 2 weeks, latest model releases and industry news.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.