File: cosine-similarity.md | Updated: 11/15/2025
Menu
v5 (Latest)
AI SDK 5.x
Model Context Protocol (MCP) Tools
Experimental_StdioMCPTransport
Copy markdown
=========================================================================================================
When you want to compare the similarity of embeddings, standard vector similarity metrics like cosine similarity are often used.
cosineSimilarity calculates the cosine similarity between two vectors. A high value (close to 1) indicates that the vectors are very similar, while a low value (close to -1) indicates that they are different.
import { openai } from '@ai-sdk/openai';import { cosineSimilarity, embedMany } from 'ai';
const { embeddings } = await embedMany({ model: openai.textEmbeddingModel('text-embedding-3-small'), values: ['sunny day at the beach', 'rainy afternoon in the city'],});
console.log( `cosine similarity: ${cosineSimilarity(embeddings[0], embeddings[1])}`,);
import { cosineSimilarity } from "ai"
number[]
The first vector to compare
number[]
The second vector to compare
A number between -1 and 1 representing the cosine similarity between the two vectors.
On this page
Deploy and Scale AI Apps with Vercel.
Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.
Trusted by industry leaders: